1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ast
import "fmt"
// ServiceNode represents a service declaration. Example:
//
// service Foo {
// rpc Bar (Baz) returns (Bob);
// rpc Frobnitz (stream Parts) returns (Gyzmeaux);
// }
type ServiceNode struct {
compositeNode
Keyword *KeywordNode
Name *IdentNode
OpenBrace *RuneNode
Decls []ServiceElement
CloseBrace *RuneNode
}
func (*ServiceNode) fileElement() {}
// NewServiceNode creates a new *ServiceNode. All arguments must be non-nil.
// - keyword: The token corresponding to the "service" keyword.
// - name: The token corresponding to the service's name.
// - openBrace: The token corresponding to the "{" rune that starts the body.
// - decls: All declarations inside the service body.
// - closeBrace: The token corresponding to the "}" rune that ends the body.
func NewServiceNode(keyword *KeywordNode, name *IdentNode, openBrace *RuneNode, decls []ServiceElement, closeBrace *RuneNode) *ServiceNode {
if keyword == nil {
panic("keyword is nil")
}
if name == nil {
panic("name is nil")
}
if openBrace == nil {
panic("openBrace is nil")
}
if closeBrace == nil {
panic("closeBrace is nil")
}
children := make([]Node, 0, 4+len(decls))
children = append(children, keyword, name, openBrace)
for _, decl := range decls {
switch decl := decl.(type) {
case *OptionNode, *RPCNode, *EmptyDeclNode:
default:
panic(fmt.Sprintf("invalid ServiceElement type: %T", decl))
}
children = append(children, decl)
}
children = append(children, closeBrace)
return &ServiceNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Name: name,
OpenBrace: openBrace,
Decls: decls,
CloseBrace: closeBrace,
}
}
func (n *ServiceNode) RangeOptions(fn func(*OptionNode) bool) {
for _, decl := range n.Decls {
if opt, ok := decl.(*OptionNode); ok {
if !fn(opt) {
return
}
}
}
}
// ServiceElement is an interface implemented by all AST nodes that can
// appear in the body of a service declaration.
type ServiceElement interface {
Node
serviceElement()
}
var _ ServiceElement = (*OptionNode)(nil)
var _ ServiceElement = (*RPCNode)(nil)
var _ ServiceElement = (*EmptyDeclNode)(nil)
// RPCDeclNode is a placeholder interface for AST nodes that represent RPC
// declarations. This allows NoSourceNode to be used in place of *RPCNode
// for some usages.
type RPCDeclNode interface {
NodeWithOptions
GetName() Node
GetInputType() Node
GetOutputType() Node
}
var _ RPCDeclNode = (*RPCNode)(nil)
var _ RPCDeclNode = (*NoSourceNode)(nil)
// RPCNode represents an RPC declaration. Example:
//
// rpc Foo (Bar) returns (Baz);
type RPCNode struct {
compositeNode
Keyword *KeywordNode
Name *IdentNode
Input *RPCTypeNode
Returns *KeywordNode
Output *RPCTypeNode
Semicolon *RuneNode
OpenBrace *RuneNode
Decls []RPCElement
CloseBrace *RuneNode
}
func (n *RPCNode) serviceElement() {}
// NewRPCNode creates a new *RPCNode with no body. All arguments must be non-nil.
// - keyword: The token corresponding to the "rpc" keyword.
// - name: The token corresponding to the RPC's name.
// - input: The token corresponding to the RPC input message type.
// - returns: The token corresponding to the "returns" keyword that precedes the output type.
// - output: The token corresponding to the RPC output message type.
// - semicolon: The token corresponding to the ";" rune that ends the declaration.
func NewRPCNode(keyword *KeywordNode, name *IdentNode, input *RPCTypeNode, returns *KeywordNode, output *RPCTypeNode, semicolon *RuneNode) *RPCNode {
if keyword == nil {
panic("keyword is nil")
}
if name == nil {
panic("name is nil")
}
if input == nil {
panic("input is nil")
}
if returns == nil {
panic("returns is nil")
}
if output == nil {
panic("output is nil")
}
var children []Node
if semicolon == nil {
children = []Node{keyword, name, input, returns, output}
} else {
children = []Node{keyword, name, input, returns, output, semicolon}
}
return &RPCNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Name: name,
Input: input,
Returns: returns,
Output: output,
Semicolon: semicolon,
}
}
// NewRPCNodeWithBody creates a new *RPCNode that includes a body (and possibly
// options). All arguments must be non-nil.
// - keyword: The token corresponding to the "rpc" keyword.
// - name: The token corresponding to the RPC's name.
// - input: The token corresponding to the RPC input message type.
// - returns: The token corresponding to the "returns" keyword that precedes the output type.
// - output: The token corresponding to the RPC output message type.
// - openBrace: The token corresponding to the "{" rune that starts the body.
// - decls: All declarations inside the RPC body.
// - closeBrace: The token corresponding to the "}" rune that ends the body.
func NewRPCNodeWithBody(keyword *KeywordNode, name *IdentNode, input *RPCTypeNode, returns *KeywordNode, output *RPCTypeNode, openBrace *RuneNode, decls []RPCElement, closeBrace *RuneNode) *RPCNode {
if keyword == nil {
panic("keyword is nil")
}
if name == nil {
panic("name is nil")
}
if input == nil {
panic("input is nil")
}
if returns == nil {
panic("returns is nil")
}
if output == nil {
panic("output is nil")
}
if openBrace == nil {
panic("openBrace is nil")
}
if closeBrace == nil {
panic("closeBrace is nil")
}
children := make([]Node, 0, 7+len(decls))
children = append(children, keyword, name, input, returns, output, openBrace)
for _, decl := range decls {
switch decl := decl.(type) {
case *OptionNode, *EmptyDeclNode:
default:
panic(fmt.Sprintf("invalid RPCElement type: %T", decl))
}
children = append(children, decl)
}
children = append(children, closeBrace)
return &RPCNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Name: name,
Input: input,
Returns: returns,
Output: output,
OpenBrace: openBrace,
Decls: decls,
CloseBrace: closeBrace,
}
}
func (n *RPCNode) GetName() Node {
return n.Name
}
func (n *RPCNode) GetInputType() Node {
return n.Input.MessageType
}
func (n *RPCNode) GetOutputType() Node {
return n.Output.MessageType
}
func (n *RPCNode) RangeOptions(fn func(*OptionNode) bool) {
for _, decl := range n.Decls {
if opt, ok := decl.(*OptionNode); ok {
if !fn(opt) {
return
}
}
}
}
// RPCElement is an interface implemented by all AST nodes that can
// appear in the body of an rpc declaration (aka method).
type RPCElement interface {
Node
methodElement()
}
var _ RPCElement = (*OptionNode)(nil)
var _ RPCElement = (*EmptyDeclNode)(nil)
// RPCTypeNode represents the declaration of a request or response type for an
// RPC. Example:
//
// (stream foo.Bar)
type RPCTypeNode struct {
compositeNode
OpenParen *RuneNode
Stream *KeywordNode
MessageType IdentValueNode
CloseParen *RuneNode
}
// NewRPCTypeNode creates a new *RPCTypeNode. All arguments must be non-nil
// except stream, which may be nil.
// - openParen: The token corresponding to the "(" rune that starts the declaration.
// - stream: The token corresponding to the "stream" keyword or nil if not present.
// - msgType: The token corresponding to the message type's name.
// - closeParen: The token corresponding to the ")" rune that ends the declaration.
func NewRPCTypeNode(openParen *RuneNode, stream *KeywordNode, msgType IdentValueNode, closeParen *RuneNode) *RPCTypeNode {
if openParen == nil {
panic("openParen is nil")
}
if msgType == nil {
panic("msgType is nil")
}
if closeParen == nil {
panic("closeParen is nil")
}
var children []Node
if stream != nil {
children = []Node{openParen, stream, msgType, closeParen}
} else {
children = []Node{openParen, msgType, closeParen}
}
return &RPCTypeNode{
compositeNode: compositeNode{
children: children,
},
OpenParen: openParen,
Stream: stream,
MessageType: msgType,
CloseParen: closeParen,
}
}
|