fileinfo.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright 2018 The Bazel Authors. All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package proto
  13. import (
  14. "bytes"
  15. "io/ioutil"
  16. "log"
  17. "path/filepath"
  18. "regexp"
  19. "sort"
  20. "strconv"
  21. "strings"
  22. )
  23. // FileInfo contains metadata extracted from a .proto file.
  24. type FileInfo struct {
  25. Path, Name string
  26. PackageName string
  27. Options []Option
  28. Imports []string
  29. HasServices bool
  30. }
  31. // Option represents a top-level option statement in a .proto file. Only
  32. // string options are supported for now.
  33. type Option struct {
  34. Key, Value string
  35. }
  36. var protoRe = buildProtoRegexp()
  37. func protoFileInfo(dir, name string) FileInfo {
  38. info := FileInfo{
  39. Path: filepath.Join(dir, name),
  40. Name: name,
  41. }
  42. content, err := ioutil.ReadFile(info.Path)
  43. if err != nil {
  44. log.Printf("%s: error reading proto file: %v", info.Path, err)
  45. return info
  46. }
  47. for _, match := range protoRe.FindAllSubmatch(content, -1) {
  48. switch {
  49. case match[importSubexpIndex] != nil:
  50. imp := unquoteProtoString(match[importSubexpIndex])
  51. info.Imports = append(info.Imports, imp)
  52. case match[packageSubexpIndex] != nil:
  53. pkg := string(match[packageSubexpIndex])
  54. if info.PackageName == "" {
  55. info.PackageName = pkg
  56. }
  57. case match[optkeySubexpIndex] != nil:
  58. key := string(match[optkeySubexpIndex])
  59. value := unquoteProtoString(match[optvalSubexpIndex])
  60. info.Options = append(info.Options, Option{key, value})
  61. case match[serviceSubexpIndex] != nil:
  62. info.HasServices = true
  63. default:
  64. // Comment matched. Nothing to extract.
  65. }
  66. }
  67. sort.Strings(info.Imports)
  68. return info
  69. }
  70. const (
  71. importSubexpIndex = 1
  72. packageSubexpIndex = 2
  73. optkeySubexpIndex = 3
  74. optvalSubexpIndex = 4
  75. serviceSubexpIndex = 5
  76. )
  77. // Based on https://developers.google.com/protocol-buffers/docs/reference/proto3-spec
  78. func buildProtoRegexp() *regexp.Regexp {
  79. hexEscape := `\\[xX][0-9a-fA-f]{2}`
  80. octEscape := `\\[0-7]{3}`
  81. charEscape := `\\[abfnrtv'"\\]`
  82. charValue := strings.Join([]string{hexEscape, octEscape, charEscape, "[^\x00\\'\\\"\\\\]"}, "|")
  83. strLit := `'(?:` + charValue + `|")*'|"(?:` + charValue + `|')*"`
  84. ident := `[A-Za-z][A-Za-z0-9_]*`
  85. fullIdent := ident + `(?:\.` + ident + `)*`
  86. importStmt := `\bimport\s*(?:public|weak)?\s*(?P<import>` + strLit + `)\s*;`
  87. packageStmt := `\bpackage\s*(?P<package>` + fullIdent + `)\s*;`
  88. optionStmt := `\boption\s*(?P<optkey>` + fullIdent + `)\s*=\s*(?P<optval>` + strLit + `)\s*;`
  89. serviceStmt := `(?P<service>service)`
  90. comment := `//[^\n]*`
  91. protoReSrc := strings.Join([]string{importStmt, packageStmt, optionStmt, serviceStmt, comment}, "|")
  92. return regexp.MustCompile(protoReSrc)
  93. }
  94. func unquoteProtoString(q []byte) string {
  95. // Adjust quotes so that Unquote is happy. We need a double quoted string
  96. // without unescaped double quote characters inside.
  97. noQuotes := bytes.Split(q[1:len(q)-1], []byte{'"'})
  98. if len(noQuotes) != 1 {
  99. for i := 0; i < len(noQuotes)-1; i++ {
  100. if len(noQuotes[i]) == 0 || noQuotes[i][len(noQuotes[i])-1] != '\\' {
  101. noQuotes[i] = append(noQuotes[i], '\\')
  102. }
  103. }
  104. q = append([]byte{'"'}, bytes.Join(noQuotes, []byte{'"'})...)
  105. q = append(q, '"')
  106. }
  107. if q[0] == '\'' {
  108. q[0] = '"'
  109. q[len(q)-1] = '"'
  110. }
  111. s, err := strconv.Unquote(string(q))
  112. if err != nil {
  113. log.Panicf("unquoting string literal %s from proto: %v", q, err)
  114. }
  115. return s
  116. }