forked from jhump/protoreflect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranges.go
305 lines (287 loc) · 8.71 KB
/
ranges.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
package ast
import "fmt"
// ExtensionRangeNode represents an extension range declaration in an extendable
// message. Example:
//
// extensions 100 to max;
type ExtensionRangeNode struct {
compositeNode
Keyword *KeywordNode
Ranges []*RangeNode
// Commas represent the separating ',' characters between ranges. The
// length of this slice must be exactly len(Ranges)-1, each item in Ranges
// having a corresponding item in this slice *except the last* (since a
// trailing comma is not allowed).
Commas []*RuneNode
Options *CompactOptionsNode
Semicolon *RuneNode
}
func (e *ExtensionRangeNode) msgElement() {}
// NewExtensionRangeNode creates a new *ExtensionRangeNode. All args must be
// non-nil except opts, which may be nil.
// - keyword: The token corresponding to the "extends" keyword.
// - ranges: One or more range expressions.
// - commas: Tokens that represent the "," runes that delimit the range expressions.
// The length of commas must be one less than the length of ranges.
// - opts: The node corresponding to options that apply to each of the ranges.
// - semicolon The token corresponding to the ";" rune that ends the declaration.
func NewExtensionRangeNode(keyword *KeywordNode, ranges []*RangeNode, commas []*RuneNode, opts *CompactOptionsNode, semicolon *RuneNode) *ExtensionRangeNode {
if keyword == nil {
panic("keyword is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
if len(ranges) == 0 {
panic("must have at least one range")
}
if len(commas) != len(ranges)-1 {
panic(fmt.Sprintf("%d ranges requires %d commas, not %d", len(ranges), len(ranges)-1, len(commas)))
}
numChildren := len(ranges)*2 + 1
if opts != nil {
numChildren++
}
children := make([]Node, 0, numChildren)
children = append(children, keyword)
for i, rng := range ranges {
if i > 0 {
if commas[i-1] == nil {
panic(fmt.Sprintf("commas[%d] is nil", i-1))
}
children = append(children, commas[i-1])
}
if rng == nil {
panic(fmt.Sprintf("ranges[%d] is nil", i))
}
children = append(children, rng)
}
if opts != nil {
children = append(children, opts)
}
children = append(children, semicolon)
return &ExtensionRangeNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Ranges: ranges,
Commas: commas,
Options: opts,
Semicolon: semicolon,
}
}
// RangeDeclNode is a placeholder interface for AST nodes that represent
// numeric values. This allows NoSourceNode to be used in place of *RangeNode
// for some usages.
type RangeDeclNode interface {
Node
RangeStart() Node
RangeEnd() Node
}
var _ RangeDeclNode = (*RangeNode)(nil)
var _ RangeDeclNode = NoSourceNode{}
// RangeNode represents a range expression, used in both extension ranges and
// reserved ranges. Example:
//
// 1000 to max
type RangeNode struct {
compositeNode
StartVal IntValueNode
// if To is non-nil, then exactly one of EndVal or Max must also be non-nil
To *KeywordNode
// EndVal and Max are mutually exclusive
EndVal IntValueNode
Max *KeywordNode
}
// NewRangeNode creates a new *RangeNode. The start argument must be non-nil.
// The to argument represents the "to" keyword. If present (i.e. if it is non-nil),
// then so must be exactly one of end or max. If max is non-nil, it indicates a
// "100 to max" style range. But if end is non-nil, the end of the range is a
// literal, such as "100 to 200".
func NewRangeNode(start IntValueNode, to *KeywordNode, end IntValueNode, max *KeywordNode) *RangeNode {
if start == nil {
panic("start is nil")
}
numChildren := 1
if to != nil {
if end == nil && max == nil {
panic("to is not nil, but end and max both are")
}
if end != nil && max != nil {
panic("end and max cannot be both non-nil")
}
numChildren = 3
} else {
if end != nil {
panic("to is nil, but end is not")
}
if max != nil {
panic("to is nil, but max is not")
}
}
children := make([]Node, 0, numChildren)
children = append(children, start)
if to != nil {
children = append(children, to)
if end != nil {
children = append(children, end)
} else {
children = append(children, max)
}
}
return &RangeNode{
compositeNode: compositeNode{
children: children,
},
StartVal: start,
To: to,
EndVal: end,
Max: max,
}
}
func (n *RangeNode) RangeStart() Node {
return n.StartVal
}
func (n *RangeNode) RangeEnd() Node {
if n.Max != nil {
return n.Max
}
if n.EndVal != nil {
return n.EndVal
}
return n.StartVal
}
func (n *RangeNode) StartValue() interface{} {
return n.StartVal.Value()
}
func (n *RangeNode) StartValueAsInt32(min, max int32) (int32, bool) {
return AsInt32(n.StartVal, min, max)
}
func (n *RangeNode) EndValue() interface{} {
if n.EndVal == nil {
return nil
}
return n.EndVal.Value()
}
func (n *RangeNode) EndValueAsInt32(min, max int32) (int32, bool) {
if n.Max != nil {
return max, true
}
if n.EndVal == nil {
return n.StartValueAsInt32(min, max)
}
return AsInt32(n.EndVal, min, max)
}
// ReservedNode represents reserved declaration, which can be used to reserve
// either names or numbers. Examples:
//
// reserved 1, 10-12, 15;
// reserved "foo", "bar", "baz";
type ReservedNode struct {
compositeNode
Keyword *KeywordNode
// If non-empty, this node represents reserved ranges and Names will be empty.
Ranges []*RangeNode
// If non-empty, this node represents reserved names and Ranges will be empty.
Names []StringValueNode
// Commas represent the separating ',' characters between options. The
// length of this slice must be exactly len(Ranges)-1 or len(Names)-1, depending
// on whether this node represents reserved ranges or reserved names. Each item
// in Ranges or Names has a corresponding item in this slice *except the last*
// (since a trailing comma is not allowed).
Commas []*RuneNode
Semicolon *RuneNode
}
func (*ReservedNode) msgElement() {}
func (*ReservedNode) enumElement() {}
// NewReservedRangesNode creates a new *ReservedNode that represents reserved
// numeric ranges. All args must be non-nil.
// - keyword: The token corresponding to the "reserved" keyword.
// - ranges: One or more range expressions.
// - commas: Tokens that represent the "," runes that delimit the range expressions.
// The length of commas must be one less than the length of ranges.
// - semicolon The token corresponding to the ";" rune that ends the declaration.
func NewReservedRangesNode(keyword *KeywordNode, ranges []*RangeNode, commas []*RuneNode, semicolon *RuneNode) *ReservedNode {
if keyword == nil {
panic("keyword is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
if len(ranges) == 0 {
panic("must have at least one range")
}
if len(commas) != len(ranges)-1 {
panic(fmt.Sprintf("%d ranges requires %d commas, not %d", len(ranges), len(ranges)-1, len(commas)))
}
children := make([]Node, 0, len(ranges)*2+1)
children = append(children, keyword)
for i, rng := range ranges {
if i > 0 {
if commas[i-1] == nil {
panic(fmt.Sprintf("commas[%d] is nil", i-1))
}
children = append(children, commas[i-1])
}
if rng == nil {
panic(fmt.Sprintf("ranges[%d] is nil", i))
}
children = append(children, rng)
}
children = append(children, semicolon)
return &ReservedNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Ranges: ranges,
Commas: commas,
Semicolon: semicolon,
}
}
// NewReservedNamesNode creates a new *ReservedNode that represents reserved
// names. All args must be non-nil.
// - keyword: The token corresponding to the "reserved" keyword.
// - names: One or more names.
// - commas: Tokens that represent the "," runes that delimit the names.
// The length of commas must be one less than the length of names.
// - semicolon The token corresponding to the ";" rune that ends the declaration.
func NewReservedNamesNode(keyword *KeywordNode, names []StringValueNode, commas []*RuneNode, semicolon *RuneNode) *ReservedNode {
if keyword == nil {
panic("keyword is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
if len(names) == 0 {
panic("must have at least one name")
}
if len(commas) != len(names)-1 {
panic(fmt.Sprintf("%d names requires %d commas, not %d", len(names), len(names)-1, len(commas)))
}
children := make([]Node, 0, len(names)*2+1)
children = append(children, keyword)
for i, name := range names {
if i > 0 {
if commas[i-1] == nil {
panic(fmt.Sprintf("commas[%d] is nil", i-1))
}
children = append(children, commas[i-1])
}
if name == nil {
panic(fmt.Sprintf("names[%d] is nil", i))
}
children = append(children, name)
}
children = append(children, semicolon)
return &ReservedNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Names: names,
Commas: commas,
Semicolon: semicolon,
}
}