forked from alecthomas/participle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.go
383 lines (328 loc) · 8.51 KB
/
nodes.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package participle
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/alecthomas/participle/lexer"
)
var (
positionType = reflect.TypeOf(lexer.Position{})
captureType = reflect.TypeOf((*Capture)(nil)).Elem()
parseableType = reflect.TypeOf((*Parseable)(nil)).Elem()
// NextMatch should be returned by Parseable.Parse() method implementations to indicate
// that the node did not match and that other matches should be attempted, if appropriate.
NextMatch = errors.New("no match") // nolint: golint
)
// A node in the grammar.
type node interface {
// Parse from scanner into value.
// Nodes should panic if parsing fails.
Parse(lex lexer.Lexer, parent reflect.Value) []reflect.Value
String() string
}
func decorate(name string) {
if msg := recover(); msg != nil {
panic(name + ": " + msg.(string))
}
}
// A node that proxies to an implementation that implements the Parseable interface.
type parseable struct {
t reflect.Type
}
func (p *parseable) String() string {
return p.t.String()
}
func (p *parseable) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
rv := reflect.New(p.t.Elem())
v := rv.Interface().(Parseable)
err := v.Parse(lex)
if err != nil {
if err == NextMatch {
return nil
}
panic(err)
}
return []reflect.Value{rv.Elem()}
}
type strct struct {
typ reflect.Type
expr node
}
func (s *strct) String() string {
return s.expr.String()
}
func (s *strct) maybeInjectPos(pos lexer.Position, v reflect.Value) {
// Fast path
if f := v.FieldByName("Pos"); f.IsValid() && f.Type() == positionType {
f.Set(reflect.ValueOf(pos))
return
}
// Iterate over fields.
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if f.Type() == positionType {
f.Set(reflect.ValueOf(pos))
break
}
}
}
func (s *strct) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
sv := reflect.New(s.typ).Elem()
s.maybeInjectPos(lex.Peek().Pos, sv)
if s.expr.Parse(lex, sv) == nil {
return nil
}
return []reflect.Value{sv}
}
// <expr> {"|" <expr>}
type disjunction []node
func (e disjunction) String() string {
out := []string{}
for _, n := range e {
out = append(out, n.String())
}
return strings.Join(out, " | ")
}
func (e disjunction) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
for _, a := range e {
if value := a.Parse(lex, parent); value != nil {
return value
}
}
return nil
}
// <node> ...
type sequence []node
func (a sequence) String() string {
return a[0].String()
}
func (a sequence) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
for i, n := range a {
// If first value doesn't match, we early exit, otherwise all values must match.
child := n.Parse(lex, parent)
if child == nil {
if i == 0 {
return nil
}
lexer.Panicf(lex.Peek().Pos, "expected ( %s ) not %q", n, lex.Peek())
}
if len(child) == 0 && out == nil {
out = []reflect.Value{}
} else {
out = append(out, child...)
}
}
return out
}
// @<expr>
type reference struct {
field reflect.StructField
node node
}
func (r *reference) String() string {
return r.field.Name + ":" + r.node.String()
}
func (r *reference) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
pos := lex.Peek().Pos
v := r.node.Parse(lex, parent)
if v == nil {
return nil
}
setField(pos, parent, r.field, v)
return []reflect.Value{parent}
}
type tokenReference struct {
typ rune
identifier string
}
func (t *tokenReference) String() string {
return t.identifier
}
func (t *tokenReference) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
token := lex.Peek()
if token.Type != t.typ {
return nil
}
lex.Next()
return []reflect.Value{reflect.ValueOf(token.Value)}
}
// [ <expr> ]
type optional struct {
node node
}
func (o *optional) String() string {
return o.node.String()
}
func (o *optional) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
v := o.node.Parse(lex, parent)
if v == nil {
return []reflect.Value{}
}
return v
}
// { <expr> }
type repetition struct {
node node
}
func (r *repetition) String() string {
return r.node.String()
}
// Parse a repetition. Once a repetition is encountered it will always match, so grammars
// should ensure that branches are differentiated prior to the repetition.
func (r *repetition) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
out = []reflect.Value{}
for {
v := r.node.Parse(lex, parent)
if v == nil {
break
}
out = append(out, v...)
}
return out
}
// Match a token literal exactly "...".
type literal struct {
s string
t rune
}
func (s *literal) String() string {
if s.t != -1 {
return fmt.Sprintf("%q:%d", s.s, s.t)
}
return fmt.Sprintf("%q", s.s)
}
func (s *literal) Parse(lex lexer.Lexer, parent reflect.Value) (out []reflect.Value) {
token := lex.Peek()
if token.Value == s.s && (s.t == -1 || s.t == token.Type) {
return []reflect.Value{reflect.ValueOf(lex.Next().Value)}
}
return nil
}
// Attempt to transform values to given type.
//
// This will dereference pointers, and attempt to parse strings into integer values, floats, etc.
func conform(t reflect.Type, values []reflect.Value) (out []reflect.Value) {
for _, v := range values {
for t != v.Type() && t.Kind() == reflect.Ptr && v.Kind() != reflect.Ptr {
v = v.Addr()
}
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(v.String(), 0, 64)
if err == nil {
v = reflect.New(t).Elem()
v.SetInt(n)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, err := strconv.ParseUint(v.String(), 0, 64)
if err == nil {
v = reflect.New(t).Elem()
v.SetUint(n)
}
case reflect.Bool:
v = reflect.ValueOf(true)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(v.String(), 64)
if err == nil {
v = reflect.New(t).Elem()
v.SetFloat(n)
}
}
out = append(out, v)
}
return out
}
// Set field.
//
// If field is a pointer the pointer will be set to the value. If field is a string, value will be
// appended. If field is a slice, value will be appended to slice.
//
// For all other types, an attempt will be made to convert the string to the corresponding
// type (int, float32, etc.).
func setField(pos lexer.Position, strct reflect.Value, field reflect.StructField, fieldValue []reflect.Value) { // nolint: gocyclo
defer decorate(strct.Type().String() + "." + field.Name)
f := strct.FieldByIndex(field.Index)
switch f.Kind() {
case reflect.Slice:
fieldValue = conform(f.Type().Elem(), fieldValue)
f.Set(reflect.Append(f, fieldValue...))
return
case reflect.Ptr:
if f.IsNil() {
fv := reflect.New(f.Type().Elem()).Elem()
f.Set(fv.Addr())
f = fv
} else {
f = f.Elem()
}
}
if f.CanAddr() {
if d, ok := f.Addr().Interface().(Capture); ok {
ifv := []string{}
for _, v := range fieldValue {
ifv = append(ifv, v.Interface().(string))
}
err := d.Capture(ifv)
if err != nil {
lexer.Panic(pos, err.Error())
}
return
}
}
fieldValue = conform(f.Type(), fieldValue)
// Strings concatenate all captured tokens.
if f.Kind() == reflect.String {
for _, v := range fieldValue {
f.Set(reflect.ValueOf(f.String() + v.String()))
}
return
}
// All other types are treated as scalar.
if len(fieldValue) != 1 {
values := []interface{}{}
for _, v := range fieldValue {
values = append(values, v.Interface())
}
panicf("a single value must be assigned to a field of type %s but have %#v", f.Type(), values)
}
fv := fieldValue[0]
switch f.Kind() {
// Numeric types will increment if the token can not be coerced.
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if fv.Type() != f.Type() {
f.SetInt(f.Int() + 1)
} else {
f.Set(fv)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if fv.Type() != f.Type() {
f.SetUint(f.Uint() + 1)
} else {
f.Set(fv)
}
case reflect.Float32, reflect.Float64:
if fv.Type() != f.Type() {
f.SetFloat(f.Float() + 1)
} else {
f.Set(fv)
}
case reflect.Bool, reflect.Struct:
if fv.Type() != f.Type() {
panicf("value %q is not correct type %s", fv, f.Type())
}
f.Set(fv)
default:
panicf("unsupported field type %s for field %s", f.Type(), field.Name)
}
}
func indirectType(t reflect.Type) reflect.Type {
if t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice {
return indirectType(t.Elem())
}
return t
}
func panicf(f string, args ...interface{}) {
panic(fmt.Sprintf(f, args...))
}