Debian Codegraph @

proto.go

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