forked from jhump/protoreflect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
361 lines (336 loc) · 10.8 KB
/
options.go
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package ast
import "fmt"
// OptionDeclNode is a placeholder interface for AST nodes that represent
// options. This allows NoSourceNode to be used in place of *OptionNode
// for some usages.
type OptionDeclNode interface {
Node
GetName() Node
GetValue() ValueNode
}
var _ OptionDeclNode = (*OptionNode)(nil)
var _ OptionDeclNode = NoSourceNode{}
// OptionNode represents the declaration of a single option for an element.
// It is used both for normal option declarations (start with "option" keyword
// and end with semicolon) and for compact options found in fields, enum values,
// and extension ranges. Example:
//
// option (custom.option) = "foo";
type OptionNode struct {
compositeNode
Keyword *KeywordNode // absent for compact options
Name *OptionNameNode
Equals *RuneNode
Val ValueNode
Semicolon *RuneNode // absent for compact options
}
func (e *OptionNode) fileElement() {}
func (e *OptionNode) msgElement() {}
func (e *OptionNode) oneOfElement() {}
func (e *OptionNode) enumElement() {}
func (e *OptionNode) serviceElement() {}
func (e *OptionNode) methodElement() {}
// NewOptionNode creates a new *OptionNode for a full option declaration (as
// used in files, messages, oneofs, enums, services, and methods). All arguments
// must be non-nil. (Also see NewCompactOptionNode.)
// - keyword: The token corresponding to the "option" keyword.
// - name: The token corresponding to the name of the option.
// - equals: The token corresponding to the "=" rune after the name.
// - val: The token corresponding to the option value.
// - semicolon: The token corresponding to the ";" rune that ends the declaration.
func NewOptionNode(keyword *KeywordNode, name *OptionNameNode, equals *RuneNode, val ValueNode, semicolon *RuneNode) *OptionNode {
if keyword == nil {
panic("keyword is nil")
}
if name == nil {
panic("name is nil")
}
if equals == nil {
panic("equals is nil")
}
if val == nil {
panic("val is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
children := []Node{keyword, name, equals, val, semicolon}
return &OptionNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Name: name,
Equals: equals,
Val: val,
Semicolon: semicolon,
}
}
// NewCompactOptionNode creates a new *OptionNode for a full compact declaration
// (as used in fields, enum values, and extension ranges). All arguments must be
// non-nil.
// - name: The token corresponding to the name of the option.
// - equals: The token corresponding to the "=" rune after the name.
// - val: The token corresponding to the option value.
func NewCompactOptionNode(name *OptionNameNode, equals *RuneNode, val ValueNode) *OptionNode {
if name == nil {
panic("name is nil")
}
if equals == nil {
panic("equals is nil")
}
if val == nil {
panic("val is nil")
}
children := []Node{name, equals, val}
return &OptionNode{
compositeNode: compositeNode{
children: children,
},
Name: name,
Equals: equals,
Val: val,
}
}
func (n *OptionNode) GetName() Node {
return n.Name
}
func (n *OptionNode) GetValue() ValueNode {
return n.Val
}
// OptionNameNode represents an option name or even a traversal through message
// types to name a nested option field. Example:
//
// (foo.bar).baz.(bob)
type OptionNameNode struct {
compositeNode
Parts []*FieldReferenceNode
// Dots represent the separating '.' characters between name parts. The
// length of this slice must be exactly len(Parts)-1, each item in Parts
// having a corresponding item in this slice *except the last* (since a
// trailing dot is not allowed).
//
// These do *not* include dots that are inside of an extension name. For
// example: (foo.bar).baz.(bob) has three parts:
// 1. (foo.bar) - an extension name
// 2. baz - a regular field in foo.bar
// 3. (bob) - an extension field in baz
// Note that the dot in foo.bar will thus not be present in Dots but is
// instead in Parts[0].
Dots []*RuneNode
}
// NewOptionNameNode creates a new *OptionNameNode. The dots arg must have a
// length that is one less than the length of parts. The parts arg must not be
// empty.
func NewOptionNameNode(parts []*FieldReferenceNode, dots []*RuneNode) *OptionNameNode {
if len(parts) == 0 {
panic("must have at least one part")
}
if len(dots) != len(parts)-1 {
panic(fmt.Sprintf("%d parts requires %d dots, not %d", len(parts), len(parts)-1, len(dots)))
}
children := make([]Node, 0, len(parts)*2-1)
for i, part := range parts {
if part == nil {
panic(fmt.Sprintf("parts[%d] is nil", i))
}
if i > 0 {
if dots[i-1] == nil {
panic(fmt.Sprintf("dots[%d] is nil", i-1))
}
children = append(children, dots[i-1])
}
children = append(children, part)
}
return &OptionNameNode{
compositeNode: compositeNode{
children: children,
},
Parts: parts,
Dots: dots,
}
}
// FieldReferenceNode is a reference to a field name. It can indicate a regular
// field (simple unqualified name), an extension field (possibly-qualified name
// that is enclosed either in brackets or parentheses), or an "any" type
// reference (a type URL in the form "server.host/fully.qualified.Name" that is
// enclosed in brackets).
//
// Extension names are used in options to refer to custom options (which are
// actually extensions), in which case the name is enclosed in parentheses "("
// and ")". They can also be used to refer to extension fields of options.
//
// Extension names are also used in message literals to set extension fields,
// in which case the name is enclosed in square brackets "[" and "]".
//
// "Any" type references can only be used in message literals, and are not
// allowed in option names. They are always enclosed in square brackets. An
// "any" type reference is distinguished from an extension name by the presence
// of a slash, which must be present in an "any" type reference and must be
// absent in an extension name.
//
// Examples:
//
// foobar
// (foo.bar)
// [foo.bar]
// [type.googleapis.com/foo.bar]
type FieldReferenceNode struct {
compositeNode
Open *RuneNode // only present for extension names and "any" type references
// only present for "any" type references
UrlPrefix IdentValueNode
Slash *RuneNode
Name IdentValueNode
Close *RuneNode // only present for extension names and "any" type references
}
// NewFieldReferenceNode creates a new *FieldReferenceNode for a regular field.
// The name arg must not be nil.
func NewFieldReferenceNode(name *IdentNode) *FieldReferenceNode {
if name == nil {
panic("name is nil")
}
children := []Node{name}
return &FieldReferenceNode{
compositeNode: compositeNode{
children: children,
},
Name: name,
}
}
// NewExtensionFieldReferenceNode creates a new *FieldReferenceNode for an
// extension field. All args must be non-nil. The openSym and closeSym runes
// should be "(" and ")" or "[" and "]".
func NewExtensionFieldReferenceNode(openSym *RuneNode, name IdentValueNode, closeSym *RuneNode) *FieldReferenceNode {
if name == nil {
panic("name is nil")
}
if openSym == nil {
panic("openSym is nil")
}
if closeSym == nil {
panic("closeSym is nil")
}
children := []Node{openSym, name, closeSym}
return &FieldReferenceNode{
compositeNode: compositeNode{
children: children,
},
Open: openSym,
Name: name,
Close: closeSym,
}
}
// NewAnyTypeReferenceNode creates a new *FieldReferenceNode for an "any"
// type reference. All args must be non-nil. The openSym and closeSym runes
// should be "[" and "]". The slashSym run should be "/".
func NewAnyTypeReferenceNode(openSym *RuneNode, urlPrefix IdentValueNode, slashSym *RuneNode, name IdentValueNode, closeSym *RuneNode) *FieldReferenceNode {
if name == nil {
panic("name is nil")
}
if openSym == nil {
panic("openSym is nil")
}
if closeSym == nil {
panic("closeSym is nil")
}
if urlPrefix == nil {
panic("urlPrefix is nil")
}
if slashSym == nil {
panic("slashSym is nil")
}
children := []Node{openSym, urlPrefix, slashSym, name, closeSym}
return &FieldReferenceNode{
compositeNode: compositeNode{
children: children,
},
Open: openSym,
UrlPrefix: urlPrefix,
Slash: slashSym,
Name: name,
Close: closeSym,
}
}
// IsExtension reports if this is an extension name or not (e.g. enclosed in
// punctuation, such as parentheses or brackets).
func (a *FieldReferenceNode) IsExtension() bool {
return a.Open != nil && a.Slash == nil
}
// IsExtension reports if this is an extension name or not (e.g. enclosed in
// punctuation, such as parentheses or brackets).
func (a *FieldReferenceNode) IsAnyTypeReference() bool {
return a.Slash != nil
}
func (a *FieldReferenceNode) Value() string {
if a.Open != nil {
if a.Slash != nil {
return string(a.Open.Rune) + string(a.UrlPrefix.AsIdentifier()) + string(a.Slash.Rune) + string(a.Name.AsIdentifier()) + string(a.Close.Rune)
}
return string(a.Open.Rune) + string(a.Name.AsIdentifier()) + string(a.Close.Rune)
} else {
return string(a.Name.AsIdentifier())
}
}
// CompactOptionsNode represents a compact options declaration, as used with
// fields, enum values, and extension ranges. Example:
//
// [deprecated = true, json_name = "foo_bar"]
type CompactOptionsNode struct {
compositeNode
OpenBracket *RuneNode
Options []*OptionNode
// Commas represent the separating ',' characters between options. The
// length of this slice must be exactly len(Options)-1, with each item
// in Options having a corresponding item in this slice *except the last*
// (since a trailing comma is not allowed).
Commas []*RuneNode
CloseBracket *RuneNode
}
// NewCompactOptionsNode creates a *CompactOptionsNode. All args must be
// non-nil. The commas arg must have a length that is one less than the
// length of opts. The opts arg must not be empty.
func NewCompactOptionsNode(openBracket *RuneNode, opts []*OptionNode, commas []*RuneNode, closeBracket *RuneNode) *CompactOptionsNode {
if openBracket == nil {
panic("openBracket is nil")
}
if closeBracket == nil {
panic("closeBracket is nil")
}
if len(opts) == 0 {
panic("must have at least one part")
}
if len(commas) != len(opts)-1 {
panic(fmt.Sprintf("%d opts requires %d commas, not %d", len(opts), len(opts)-1, len(commas)))
}
children := make([]Node, 0, len(opts)*2+1)
children = append(children, openBracket)
for i, opt := range opts {
if i > 0 {
if commas[i-1] == nil {
panic(fmt.Sprintf("commas[%d] is nil", i-1))
}
children = append(children, commas[i-1])
}
if opt == nil {
panic(fmt.Sprintf("opts[%d] is nil", i))
}
children = append(children, opt)
}
children = append(children, closeBracket)
return &CompactOptionsNode{
compositeNode: compositeNode{
children: children,
},
OpenBracket: openBracket,
Options: opts,
Commas: commas,
CloseBracket: closeBracket,
}
}
func (e *CompactOptionsNode) GetElements() []*OptionNode {
if e == nil {
return nil
}
return e.Options
}