enum.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // Enum definition consists of a name and an enum body.
  28. type Enum struct {
  29. Position scanner.Position
  30. Comment *Comment
  31. Name string
  32. Elements []Visitee
  33. Parent Visitee
  34. }
  35. // Accept dispatches the call to the visitor.
  36. func (e *Enum) Accept(v Visitor) {
  37. v.VisitEnum(e)
  38. }
  39. // Doc is part of Documented
  40. func (e *Enum) Doc() *Comment {
  41. return e.Comment
  42. }
  43. // addElement is part of elementContainer
  44. func (e *Enum) addElement(v Visitee) {
  45. v.parent(e)
  46. e.Elements = append(e.Elements, v)
  47. }
  48. // elements is part of elementContainer
  49. func (e *Enum) elements() []Visitee {
  50. return e.Elements
  51. }
  52. // takeLastComment is part of elementContainer
  53. // removes and returns the last element of the list if it is a Comment.
  54. func (e *Enum) takeLastComment(expectedOnLine int) (last *Comment) {
  55. last, e.Elements = takeLastCommentIfEndsOnLine(e.Elements, expectedOnLine)
  56. return
  57. }
  58. func (e *Enum) parse(p *Parser) error {
  59. pos, tok, lit := p.next()
  60. if tok != tIDENT {
  61. if !isKeyword(tok) {
  62. return p.unexpected(lit, "enum identifier", e)
  63. }
  64. }
  65. e.Name = lit
  66. _, tok, lit = p.next()
  67. if tok != tLEFTCURLY {
  68. return p.unexpected(lit, "enum opening {", e)
  69. }
  70. for {
  71. pos, tok, lit = p.next()
  72. switch tok {
  73. case tCOMMENT:
  74. if com := mergeOrReturnComment(e.elements(), lit, pos); com != nil { // not merged?
  75. e.addElement(com)
  76. }
  77. case tOPTION:
  78. v := new(Option)
  79. v.Position = pos
  80. v.Comment = e.takeLastComment(pos.Line)
  81. err := v.parse(p)
  82. if err != nil {
  83. return err
  84. }
  85. e.addElement(v)
  86. case tRIGHTCURLY, tEOF:
  87. goto done
  88. case tSEMICOLON:
  89. maybeScanInlineComment(p, e)
  90. case tRESERVED:
  91. r := new(Reserved)
  92. r.Position = pos
  93. r.Comment = e.takeLastComment(pos.Line - 1)
  94. if err := r.parse(p); err != nil {
  95. return err
  96. }
  97. e.addElement(r)
  98. default:
  99. p.nextPut(pos, tok, lit)
  100. f := new(EnumField)
  101. f.Position = pos
  102. f.Comment = e.takeLastComment(pos.Line - 1)
  103. if err := f.parse(p); err != nil {
  104. return err
  105. }
  106. e.addElement(f)
  107. }
  108. }
  109. done:
  110. if tok != tRIGHTCURLY {
  111. return p.unexpected(lit, "enum closing }", e)
  112. }
  113. return nil
  114. }
  115. // parent is part of elementContainer
  116. func (e *Enum) parent(p Visitee) { e.Parent = p }
  117. // EnumField is part of the body of an Enum.
  118. type EnumField struct {
  119. Position scanner.Position
  120. Comment *Comment
  121. Name string
  122. Integer int
  123. // ValueOption is deprecated, use Elements instead
  124. ValueOption *Option
  125. Elements []Visitee // such as Option and Comment
  126. InlineComment *Comment
  127. Parent Visitee
  128. }
  129. // Accept dispatches the call to the visitor.
  130. func (f *EnumField) Accept(v Visitor) {
  131. v.VisitEnumField(f)
  132. }
  133. // inlineComment is part of commentInliner.
  134. func (f *EnumField) inlineComment(c *Comment) {
  135. f.InlineComment = c
  136. }
  137. // Doc is part of Documented
  138. func (f *EnumField) Doc() *Comment {
  139. return f.Comment
  140. }
  141. func (f *EnumField) parse(p *Parser) (err error) {
  142. _, tok, lit := p.nextIdentifier()
  143. if tok != tIDENT {
  144. if !isKeyword(tok) {
  145. return p.unexpected(lit, "enum field identifier", f)
  146. }
  147. }
  148. f.Name = lit
  149. pos, tok, lit := p.next()
  150. if tok != tEQUALS {
  151. return p.unexpected(lit, "enum field =", f)
  152. }
  153. var i int
  154. if i, err = p.nextInteger(); err != nil {
  155. return p.unexpected(err.Error(), "enum field integer", f)
  156. }
  157. f.Integer = i
  158. pos, tok, lit = p.next()
  159. if tok == tLEFTSQUARE {
  160. for {
  161. o := new(Option)
  162. o.Position = pos
  163. o.IsEmbedded = true
  164. err := o.parse(p)
  165. if err != nil {
  166. return err
  167. }
  168. // update deprecated field with the last option found
  169. f.ValueOption = o
  170. f.addElement(o)
  171. pos, tok, lit = p.next()
  172. if tok == tCOMMA {
  173. continue
  174. }
  175. if tok == tRIGHTSQUARE {
  176. break
  177. }
  178. }
  179. }
  180. if tSEMICOLON == tok {
  181. p.nextPut(pos, tok, lit) // put back this token for scanning inline comment
  182. }
  183. return
  184. }
  185. // addElement is part of elementContainer
  186. func (f *EnumField) addElement(v Visitee) {
  187. v.parent(f)
  188. f.Elements = append(f.Elements, v)
  189. }
  190. func (f *EnumField) parent(v Visitee) { f.Parent = v }