service.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // Service defines a set of RPC calls.
  28. type Service struct {
  29. Position scanner.Position
  30. Comment *Comment
  31. Name string
  32. Elements []Visitee
  33. Parent Visitee
  34. RPCElements []RPC
  35. }
  36. // Accept dispatches the call to the visitor.
  37. func (s *Service) Accept(v Visitor) {
  38. v.VisitService(s)
  39. }
  40. // Doc is part of Documented
  41. func (s *Service) Doc() *Comment {
  42. return s.Comment
  43. }
  44. // addElement is part of elementContainer
  45. func (s *Service) addElement(v Visitee) {
  46. v.parent(s)
  47. s.Elements = append(s.Elements, v)
  48. }
  49. // elements is part of elementContainer
  50. func (s *Service) elements() []Visitee {
  51. return s.Elements
  52. }
  53. // takeLastComment is part of elementContainer
  54. // removes and returns the last elements of the list if it is a Comment.
  55. func (s *Service) takeLastComment(expectedOnLine int) (last *Comment) {
  56. last, s.Elements = takeLastCommentIfEndsOnLine(s.Elements, expectedOnLine)
  57. return
  58. }
  59. // parse continues after reading "service"
  60. func (s *Service) parse(p *Parser) error {
  61. pos, tok, lit := p.nextIdentifier()
  62. if tok != tIDENT {
  63. if !isKeyword(tok) {
  64. return p.unexpected(lit, "service identifier", s)
  65. }
  66. }
  67. s.Name = lit
  68. pos, tok, lit = p.next()
  69. if tok != tLEFTCURLY {
  70. return p.unexpected(lit, "service opening {", s)
  71. }
  72. for {
  73. pos, tok, lit = p.next()
  74. switch tok {
  75. case tCOMMENT:
  76. if com := mergeOrReturnComment(s.Elements, lit, pos); com != nil { // not merged?
  77. s.addElement(com)
  78. }
  79. case tOPTION:
  80. opt := new(Option)
  81. opt.Position = pos
  82. opt.Comment, s.Elements = takeLastCommentIfEndsOnLine(s.elements(), pos.Line-1)
  83. if err := opt.parse(p); err != nil {
  84. return err
  85. }
  86. s.addElement(opt)
  87. case tRPC:
  88. rpc := new(RPC)
  89. rpc.Position = pos
  90. rpc.Comment, s.Elements = takeLastCommentIfEndsOnLine(s.Elements, pos.Line-1)
  91. err := rpc.parse(p)
  92. if err != nil {
  93. return err
  94. }
  95. s.RPCElements = append(s.RPCElements, *rpc)
  96. s.addElement(rpc)
  97. maybeScanInlineComment(p, s)
  98. case tSEMICOLON:
  99. maybeScanInlineComment(p, s)
  100. case tRIGHTCURLY:
  101. goto done
  102. default:
  103. return p.unexpected(lit, "service comment|rpc", s)
  104. }
  105. }
  106. done:
  107. return nil
  108. }
  109. func (s *Service) parent(v Visitee) { s.Parent = v }
  110. // RPC represents an rpc entry in a message.
  111. type RPC struct {
  112. Position scanner.Position
  113. Comment *Comment
  114. Name string
  115. RequestType string
  116. StreamsRequest bool
  117. ReturnsType string
  118. StreamsReturns bool
  119. Elements []Visitee
  120. InlineComment *Comment
  121. Parent Visitee
  122. // Options field is DEPRECATED, use Elements instead.
  123. Options []*Option
  124. }
  125. // Accept dispatches the call to the visitor.
  126. func (r *RPC) Accept(v Visitor) {
  127. v.VisitRPC(r)
  128. }
  129. // Doc is part of Documented
  130. func (r *RPC) Doc() *Comment {
  131. return r.Comment
  132. }
  133. // inlineComment is part of commentInliner.
  134. func (r *RPC) inlineComment(c *Comment) {
  135. r.InlineComment = c
  136. }
  137. // parse continues after reading "rpc"
  138. func (r *RPC) parse(p *Parser) error {
  139. pos, tok, lit := p.next()
  140. if tok != tIDENT {
  141. return p.unexpected(lit, "rpc method", r)
  142. }
  143. r.Name = lit
  144. pos, tok, lit = p.next()
  145. if tok != tLEFTPAREN {
  146. return p.unexpected(lit, "rpc type opening (", r)
  147. }
  148. pos, tok, lit = p.nextIdentifier()
  149. if tSTREAM == tok {
  150. r.StreamsRequest = true
  151. pos, tok, lit = p.nextIdentifier()
  152. }
  153. if tok != tIDENT {
  154. return p.unexpected(lit, "rpc stream | request type", r)
  155. }
  156. r.RequestType = lit
  157. pos, tok, lit = p.next()
  158. if tok != tRIGHTPAREN {
  159. return p.unexpected(lit, "rpc type closing )", r)
  160. }
  161. pos, tok, lit = p.next()
  162. if tok != tRETURNS {
  163. return p.unexpected(lit, "rpc returns", r)
  164. }
  165. pos, tok, lit = p.next()
  166. if tok != tLEFTPAREN {
  167. return p.unexpected(lit, "rpc type opening (", r)
  168. }
  169. pos, tok, lit = p.nextIdentifier()
  170. if tSTREAM == tok {
  171. r.StreamsReturns = true
  172. pos, tok, lit = p.nextIdentifier()
  173. }
  174. if tok == tDOT {
  175. pos, tok, lit = p.nextIdentifier()
  176. }
  177. if tok != tIDENT {
  178. return p.unexpected(lit, "rpc stream | returns type", r)
  179. }
  180. r.ReturnsType = lit
  181. pos, tok, lit = p.next()
  182. if tok != tRIGHTPAREN {
  183. return p.unexpected(lit, "rpc type closing )", r)
  184. }
  185. pos, tok, lit = p.next()
  186. if tSEMICOLON == tok {
  187. p.nextPut(pos, tok, lit) // allow for inline comment parsing
  188. return nil
  189. }
  190. if tLEFTCURLY == tok {
  191. // parse options
  192. for {
  193. pos, tok, lit = p.next()
  194. if tRIGHTCURLY == tok {
  195. break
  196. }
  197. if isComment(lit) {
  198. if com := mergeOrReturnComment(r.elements(), lit, pos); com != nil { // not merged?
  199. r.addElement(com)
  200. continue
  201. }
  202. }
  203. if tSEMICOLON == tok {
  204. maybeScanInlineComment(p, r)
  205. continue
  206. }
  207. if tOPTION == tok {
  208. o := new(Option)
  209. o.Position = pos
  210. if err := o.parse(p); err != nil {
  211. return err
  212. }
  213. r.addElement(o)
  214. }
  215. }
  216. }
  217. return nil
  218. }
  219. // addElement is part of elementContainer
  220. func (r *RPC) addElement(v Visitee) {
  221. v.parent(r)
  222. r.Elements = append(r.Elements, v)
  223. // handle deprecated field
  224. if option, ok := v.(*Option); ok {
  225. r.Options = append(r.Options, option)
  226. }
  227. }
  228. // elements is part of elementContainer
  229. func (r *RPC) elements() []Visitee {
  230. return r.Elements
  231. }
  232. func (r *RPC) takeLastComment(expectedOnLine int) (last *Comment) {
  233. last, r.Elements = takeLastCommentIfEndsOnLine(r.Elements, expectedOnLine)
  234. return
  235. }
  236. func (r *RPC) parent(v Visitee) { r.Parent = v }