proto.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // Proto represents a .proto definition
  25. type Proto struct {
  26. Filename string
  27. Elements []Visitee
  28. Imports []Import
  29. Enums []Enum
  30. Package []Package
  31. Options []Option
  32. Messages []Message
  33. Services []Service
  34. Extends []Extensions
  35. }
  36. // Accept dispatches the call to the visitor.
  37. func (proto *Proto) Accept(v Visitor) {
  38. // As Proto is not (yet) a Visitee, we enumerate its elements instead
  39. //v.VisitProto(proto)
  40. for _, each := range proto.Elements {
  41. each.Accept(v)
  42. }
  43. }
  44. // addElement is part of elementContainer
  45. func (proto *Proto) addElement(v Visitee) {
  46. v.parent(proto)
  47. proto.Elements = append(proto.Elements, v)
  48. }
  49. // elements is part of elementContainer
  50. func (proto *Proto) elements() []Visitee {
  51. return proto.Elements
  52. }
  53. // takeLastComment is part of elementContainer
  54. // removes and returns the last element of the list if it is a Comment.
  55. func (proto *Proto) takeLastComment(expectedOnLine int) (last *Comment) {
  56. last, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, expectedOnLine)
  57. return
  58. }
  59. // parse parsers a complete .proto definition source.
  60. func (proto *Proto) parse(p *Parser) (*Proto, error) {
  61. pro := new(Proto)
  62. for {
  63. pos, tok, lit := p.next()
  64. switch {
  65. case isComment(lit):
  66. if com := mergeOrReturnComment(proto.Elements, lit, pos); com != nil { // not merged?
  67. proto.Elements = append(proto.Elements, com)
  68. }
  69. case tOPTION == tok:
  70. o := new(Option)
  71. o.Position = pos
  72. o.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
  73. if err := o.parse(p); err != nil {
  74. return pro, err
  75. }
  76. pro.Options = append(pro.Options, *o)
  77. proto.addElement(o)
  78. case tSYNTAX == tok:
  79. s := new(Syntax)
  80. s.Position = pos
  81. s.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
  82. if err := s.parse(p); err != nil {
  83. return pro, err
  84. }
  85. proto.addElement(s)
  86. case tIMPORT == tok:
  87. im := new(Import)
  88. im.Position = pos
  89. im.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
  90. if err := im.parse(p); err != nil {
  91. return pro, err
  92. }
  93. pro.Imports = append(pro.Imports, *im)
  94. proto.addElement(im)
  95. case tENUM == tok:
  96. enum := new(Enum)
  97. enum.Position = pos
  98. enum.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
  99. if err := enum.parse(p); err != nil {
  100. return pro, err
  101. }
  102. pro.Enums = append(pro.Enums, *enum)
  103. proto.addElement(enum)
  104. case tSERVICE == tok:
  105. service := new(Service)
  106. service.Position = pos
  107. service.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
  108. err := service.parse(p)
  109. if err != nil {
  110. return pro, err
  111. }
  112. pro.Services = append(pro.Services, *service)
  113. proto.addElement(service)
  114. case tPACKAGE == tok:
  115. pkg := new(Package)
  116. pkg.Position = pos
  117. pkg.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
  118. if err := pkg.parse(p); err != nil {
  119. return pro, err
  120. }
  121. pro.Package = append(pro.Package, *pkg)
  122. proto.addElement(pkg)
  123. case tMESSAGE == tok:
  124. msg := new(Message)
  125. msg.Position = pos
  126. msg.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
  127. if err := msg.parse(p); err != nil {
  128. return pro, err
  129. }
  130. pro.Messages = append(pro.Messages, *msg)
  131. proto.addElement(msg)
  132. // BEGIN proto2
  133. case tEXTEND == tok:
  134. msg := new(Message)
  135. msg.Position = pos
  136. msg.Comment, proto.Elements = takeLastCommentIfEndsOnLine(proto.Elements, pos.Line-1)
  137. msg.IsExtend = true
  138. if err := msg.parse(p); err != nil {
  139. return pro, err
  140. }
  141. proto.addElement(msg)
  142. // END proto2
  143. case tSEMICOLON == tok:
  144. maybeScanInlineComment(p, proto)
  145. // continue
  146. case tEOF == tok:
  147. goto done
  148. default:
  149. return pro, p.unexpected(lit, ".proto element {comment|option|import|syntax|enum|service|package|message}", p)
  150. }
  151. }
  152. done:
  153. return pro, nil
  154. }
  155. func (proto *Proto) parent(v Visitee) {}
  156. // elementContainer unifies types that have elements.
  157. type elementContainer interface {
  158. addElement(v Visitee)
  159. elements() []Visitee
  160. takeLastComment(expectedOnLine int) *Comment
  161. }