...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package proto
25
26 import (
27 "text/scanner"
28 )
29
30
31 type Oneof struct {
32 Position scanner.Position
33 Comment *Comment
34 Name string
35 Elements []Visitee
36 Parent Visitee
37 }
38
39
40 func (o *Oneof) addElement(v Visitee) {
41 v.parent(o)
42 o.Elements = append(o.Elements, v)
43 }
44
45
46 func (o *Oneof) elements() []Visitee {
47 return o.Elements
48 }
49
50
51
52 func (o *Oneof) takeLastComment(expectedOnLine int) (last *Comment) {
53 last, o.Elements = takeLastCommentIfEndsOnLine(o.Elements, expectedOnLine)
54 return last
55 }
56
57
58
59 func (o *Oneof) parse(p *Parser) error {
60 pos, tok, lit := p.next()
61 if tok != tIDENT {
62 if !isKeyword(tok) {
63 return p.unexpected(lit, "oneof identifier", o)
64 }
65 }
66 o.Name = lit
67 consumeCommentFor(p, o)
68 pos, tok, lit = p.next()
69 if tok != tLEFTCURLY {
70 return p.unexpected(lit, "oneof opening {", o)
71 }
72 for {
73 pos, tok, lit = p.nextTypeName()
74 switch tok {
75 case tCOMMENT:
76 if com := mergeOrReturnComment(o.elements(), lit, pos); com != nil {
77 o.addElement(com)
78 }
79 case tIDENT:
80 f := newOneOfField()
81 f.Position = pos
82 f.Comment, o.Elements = takeLastCommentIfEndsOnLine(o.elements(), pos.Line-1)
83 f.Type = lit
84 if err := parseFieldAfterType(f.Field, p, f); err != nil {
85 return err
86 }
87 o.addElement(f)
88 case tGROUP:
89 g := new(Group)
90 g.Position = pos
91 g.Comment, o.Elements = takeLastCommentIfEndsOnLine(o.elements(), pos.Line-1)
92 if err := g.parse(p); err != nil {
93 return err
94 }
95 o.addElement(g)
96 case tOPTION:
97 opt := new(Option)
98 opt.Position = pos
99 opt.Comment, o.Elements = takeLastCommentIfEndsOnLine(o.elements(), pos.Line-1)
100 if err := opt.parse(p); err != nil {
101 return err
102 }
103 o.addElement(opt)
104 case tSEMICOLON:
105 maybeScanInlineComment(p, o)
106
107 default:
108 goto done
109 }
110 }
111 done:
112 if tok != tRIGHTCURLY {
113 return p.unexpected(lit, "oneof closing }", o)
114 }
115 return nil
116 }
117
118
119 func (o *Oneof) Accept(v Visitor) {
120 v.VisitOneof(o)
121 }
122
123
124 func (o *Oneof) Doc() *Comment {
125 return o.Comment
126 }
127
128
129 type OneOfField struct {
130 *Field
131 }
132
133 func newOneOfField() *OneOfField { return &OneOfField{Field: new(Field)} }
134
135
136 func (o *OneOfField) Accept(v Visitor) {
137 v.VisitOneofField(o)
138 }
139
140
141
142 func (o *OneOfField) Doc() *Comment {
143 return o.Comment
144 }
145
146 func (o *Oneof) parent(v Visitee) { o.Parent = v }
147
View as plain text