field.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "text/scanner"
  26. )
  27. // Field is an abstract message field.
  28. type Field struct {
  29. Position scanner.Position
  30. Comment *Comment
  31. Name string
  32. Type string
  33. Sequence int
  34. Options []*Option
  35. InlineComment *Comment
  36. Parent Visitee
  37. }
  38. // inlineComment is part of commentInliner.
  39. func (f *Field) inlineComment(c *Comment) {
  40. f.InlineComment = c
  41. }
  42. // NormalField represents a field in a Message.
  43. type NormalField struct {
  44. *Field
  45. Repeated bool
  46. Optional bool // proto2
  47. Required bool // proto2
  48. }
  49. func newNormalField() *NormalField { return &NormalField{Field: new(Field)} }
  50. // Accept dispatches the call to the visitor.
  51. func (f *NormalField) Accept(v Visitor) {
  52. v.VisitNormalField(f)
  53. }
  54. // Doc is part of Documented
  55. func (f *NormalField) Doc() *Comment {
  56. return f.Comment
  57. }
  58. // parse expects:
  59. // [ "repeated" | "optional" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
  60. func (f *NormalField) parse(p *Parser) error {
  61. for {
  62. _, tok, lit := p.nextTypeName()
  63. switch tok {
  64. case tREPEATED:
  65. f.Repeated = true
  66. return f.parse(p)
  67. case tOPTIONAL: // proto2
  68. f.Optional = true
  69. return f.parse(p)
  70. case tIDENT:
  71. f.Type = lit
  72. return parseFieldAfterType(f.Field, p)
  73. default:
  74. goto done
  75. }
  76. }
  77. done:
  78. return nil
  79. }
  80. // parseFieldAfterType expects:
  81. // fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";
  82. func parseFieldAfterType(f *Field, p *Parser) error {
  83. pos, tok, lit := p.next()
  84. if tok != tIDENT {
  85. if !isKeyword(tok) {
  86. return p.unexpected(lit, "field identifier", f)
  87. }
  88. }
  89. f.Name = lit
  90. pos, tok, lit = p.next()
  91. if tok != tEQUALS {
  92. return p.unexpected(lit, "field =", f)
  93. }
  94. i, err := p.nextInteger()
  95. if err != nil {
  96. return p.unexpected(lit, "field sequence number", f)
  97. }
  98. f.Sequence = i
  99. // see if there are options
  100. pos, tok, _ = p.next()
  101. if tLEFTSQUARE != tok {
  102. p.nextPut(pos, tok, lit)
  103. return nil
  104. }
  105. // consume options
  106. for {
  107. o := new(Option)
  108. o.Position = pos
  109. o.IsEmbedded = true
  110. err := o.parse(p)
  111. if err != nil {
  112. return err
  113. }
  114. f.Options = append(f.Options, o)
  115. pos, tok, lit = p.next()
  116. if tRIGHTSQUARE == tok {
  117. break
  118. }
  119. if tCOMMA != tok {
  120. return p.unexpected(lit, "option ,", o)
  121. }
  122. }
  123. return nil
  124. }
  125. // MapField represents a map entry in a message.
  126. type MapField struct {
  127. *Field
  128. KeyType string
  129. }
  130. func newMapField() *MapField { return &MapField{Field: new(Field)} }
  131. // Accept dispatches the call to the visitor.
  132. func (f *MapField) Accept(v Visitor) {
  133. v.VisitMapField(f)
  134. }
  135. // parse expects:
  136. // mapField = "map" "<" keyType "," type ">" mapName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
  137. // keyType = "int32" | "int64" | "uint32" | "uint64" | "sint32" | "sint64" |
  138. // "fixed32" | "fixed64" | "sfixed32" | "sfixed64" | "bool" | "string"
  139. func (f *MapField) parse(p *Parser) error {
  140. _, tok, lit := p.next()
  141. if tLESS != tok {
  142. return p.unexpected(lit, "map keyType <", f)
  143. }
  144. _, tok, lit = p.nextTypeName()
  145. if tIDENT != tok {
  146. return p.unexpected(lit, "map identifier", f)
  147. }
  148. f.KeyType = lit
  149. _, tok, lit = p.next()
  150. if tCOMMA != tok {
  151. return p.unexpected(lit, "map type separator ,", f)
  152. }
  153. _, tok, lit = p.nextTypeName()
  154. if tIDENT != tok {
  155. return p.unexpected(lit, "map valueType identifier", f)
  156. }
  157. f.Type = lit
  158. _, tok, lit = p.next()
  159. if tGREATER != tok {
  160. return p.unexpected(lit, "map valueType >", f)
  161. }
  162. return parseFieldAfterType(f.Field, p)
  163. }
  164. func (f *Field) parent(v Visitee) { f.Parent = v }