parser.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (c) 2017 Ernest Micklei
  2. //
  3. // MIT License
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. package proto
  24. import (
  25. "bytes"
  26. "errors"
  27. "fmt"
  28. "io"
  29. "runtime"
  30. "strconv"
  31. "strings"
  32. "text/scanner"
  33. )
  34. // Parser represents a parser.
  35. type Parser struct {
  36. debug bool
  37. scanner *scanner.Scanner
  38. buf *nextValues
  39. scannerErrors []error
  40. }
  41. // nextValues is to capture the result of next()
  42. type nextValues struct {
  43. pos scanner.Position
  44. tok token
  45. lit string
  46. }
  47. // NewParser returns a new instance of Parser.
  48. func NewParser(r io.Reader) *Parser {
  49. s := new(scanner.Scanner)
  50. s.Init(r)
  51. s.Mode = scanner.ScanIdents | scanner.ScanFloats | scanner.ScanStrings | scanner.ScanRawStrings | scanner.ScanComments
  52. p := &Parser{scanner: s}
  53. s.Error = p.handleScanError
  54. return p
  55. }
  56. // handleScanError is called from the underlying Scanner
  57. func (p *Parser) handleScanError(s *scanner.Scanner, msg string) {
  58. p.scannerErrors = append(p.scannerErrors,
  59. fmt.Errorf("go scanner error at %v = %v", s.Position, msg))
  60. }
  61. // ignoreIllegalEscapesWhile is called for scanning constants of an option.
  62. // Such content can have a syntax that is not acceptable by the Go scanner.
  63. // This temporary installs a handler that ignores only one type of error: illegal char escape
  64. func (p *Parser) ignoreIllegalEscapesWhile(block func()) {
  65. // during block call change error handler
  66. p.scanner.Error = func(s *scanner.Scanner, msg string) {
  67. if strings.Contains(msg, "illegal char escape") { // too bad there is no constant for this in scanner pkg
  68. return
  69. }
  70. p.handleScanError(s, msg)
  71. }
  72. block()
  73. // restore
  74. p.scanner.Error = p.handleScanError
  75. }
  76. // Parse parses a proto definition. May return a parse or scanner error.
  77. func (p *Parser) Parse() (*Proto, error) {
  78. proto := new(Proto)
  79. if p.scanner.Filename != "" {
  80. proto.Filename = p.scanner.Filename
  81. }
  82. pro, parseError := proto.parse(p)
  83. // see if it was a scanner error
  84. if len(p.scannerErrors) > 0 {
  85. buf := new(bytes.Buffer)
  86. for _, each := range p.scannerErrors {
  87. fmt.Fprintln(buf, each)
  88. }
  89. return proto, errors.New(buf.String())
  90. }
  91. return pro, parseError
  92. }
  93. // Filename is for reporting. Optional.
  94. func (p *Parser) Filename(f string) {
  95. p.scanner.Filename = f
  96. }
  97. // next returns the next token using the scanner or drain the buffer.
  98. func (p *Parser) next() (pos scanner.Position, tok token, lit string) {
  99. if p.buf != nil {
  100. // consume buf
  101. vals := *p.buf
  102. p.buf = nil
  103. return vals.pos, vals.tok, vals.lit
  104. }
  105. ch := p.scanner.Scan()
  106. if ch == scanner.EOF {
  107. return p.scanner.Position, tEOF, ""
  108. }
  109. lit = p.scanner.TokenText()
  110. return p.scanner.Position, asToken(lit), lit
  111. }
  112. // nextPut sets the buffer
  113. func (p *Parser) nextPut(pos scanner.Position, tok token, lit string) {
  114. p.buf = &nextValues{pos, tok, lit}
  115. }
  116. func (p *Parser) unexpected(found, expected string, obj interface{}) error {
  117. debug := ""
  118. if p.debug {
  119. _, file, line, _ := runtime.Caller(1)
  120. debug = fmt.Sprintf(" at %s:%d (with %#v)", file, line, obj)
  121. }
  122. return fmt.Errorf("%v: found %q but expected [%s]%s", p.scanner.Position, found, expected, debug)
  123. }
  124. func (p *Parser) nextInteger() (i int, err error) {
  125. _, tok, lit := p.next()
  126. if "-" == lit {
  127. i, err = p.nextInteger()
  128. return i * -1, err
  129. }
  130. if tok != tIDENT {
  131. return 0, errors.New("non integer")
  132. }
  133. if strings.HasPrefix(lit, "0x") {
  134. // hex decode
  135. var i64 int64
  136. i64, err = strconv.ParseInt(lit, 0, 64)
  137. return int(i64), err
  138. }
  139. i, err = strconv.Atoi(lit)
  140. return
  141. }
  142. // nextIdentifier consumes tokens which may have one or more dot separators (namespaced idents).
  143. func (p *Parser) nextIdentifier() (pos scanner.Position, tok token, lit string) {
  144. return p.nextIdent(false)
  145. }
  146. // nextTypeName implements the Packages and Name Resolution for finding the name of the type.
  147. func (p *Parser) nextTypeName() (pos scanner.Position, tok token, lit string) {
  148. pos, tok, lit = p.nextIdent(false)
  149. if tDOT == tok {
  150. // leading dot allowed
  151. pos, tok, lit = p.nextIdent(false)
  152. lit = "." + lit
  153. }
  154. return
  155. }
  156. func (p *Parser) nextIdent(keywordStartAllowed bool) (pos scanner.Position, tok token, lit string) {
  157. pos, tok, lit = p.next()
  158. if tIDENT != tok {
  159. // can be keyword
  160. if !(isKeyword(tok) && keywordStartAllowed) {
  161. return
  162. }
  163. // proceed with keyword as first literal
  164. }
  165. startPos := pos
  166. fullLit := lit
  167. // see if identifier is namespaced
  168. for {
  169. r := p.scanner.Peek()
  170. if '.' != r {
  171. break
  172. }
  173. p.next() // consume dot
  174. pos, tok, lit := p.next()
  175. if tIDENT != tok && !isKeyword(tok) {
  176. p.nextPut(pos, tok, lit)
  177. break
  178. }
  179. fullLit = fmt.Sprintf("%s.%s", fullLit, lit)
  180. }
  181. return startPos, tIDENT, fullLit
  182. }
  183. func (p *Parser) peekNonWhitespace() rune {
  184. r := p.scanner.Peek()
  185. if r == scanner.EOF {
  186. return r
  187. }
  188. if isWhitespace(r) {
  189. // consume it
  190. p.scanner.Next()
  191. return p.peekNonWhitespace()
  192. }
  193. return r
  194. }