-
-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathparser.go
More file actions
714 lines (664 loc) · 20 KB
/
parser.go
File metadata and controls
714 lines (664 loc) · 20 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package pattern
import (
"errors"
"fmt"
"go/ast"
"go/token"
"iter"
"reflect"
"strings"
)
type Pattern struct {
Root Node
// EntryNodes contains instances of ast.Node that could potentially
// initiate a successful match of the pattern.
EntryNodes []ast.Node
// SymbolsPattern is a pattern consisting or Any, Or, And, and IndexSymbol,
// that can be used to implement fast rejection of whole packages using
// typeindex.
SymbolsPattern Node
// If non-empty, all possible candidate nodes for this pattern can be found
// by finding all call expressions for this list of symbols.
RootCallSymbols []IndexSymbol
// Mapping from binding index to binding name
Bindings []string
}
func MustParse(s string) Pattern {
p := &Parser{AllowTypeInfo: true}
pat, err := p.Parse(s)
if err != nil {
panic(err)
}
return pat
}
func symbolToIndexSymbol(name string) IndexSymbol {
if len(name) == 0 {
return IndexSymbol{}
}
if name[0] == '(' {
end := strings.IndexAny(name, ")")
// Ensure there's a ), and also that there are at least two more
// characters after it, for a dot and an identifier.
if end == -1 || end > len(name)-2 {
return IndexSymbol{}
}
pathAndType := strings.TrimPrefix(name[1:end], "*")
dot := strings.LastIndex(pathAndType, ".")
if dot == -1 {
return IndexSymbol{}
}
path := pathAndType[:dot]
typ := pathAndType[dot+1:]
ident := name[end+2:]
return IndexSymbol{path, typ, ident}
} else {
dot := strings.LastIndex(name, ".")
if dot == -1 {
return IndexSymbol{"", "", name}
}
path := name[:dot]
ident := name[dot+1:]
return IndexSymbol{path, "", ident}
}
}
func collectSymbols(node Node, inSymbol bool) Node {
and := func(c Node, out *And) {
switch cc := c.(type) {
case And:
out.Nodes = append(out.Nodes, cc.Nodes...)
case Any:
case nil:
default:
out.Nodes = append(out.Nodes, c)
}
}
switch node := node.(type) {
case Or:
s := Or{}
for _, el := range node.Nodes {
c := collectSymbols(el, inSymbol)
switch cc := c.(type) {
case Or:
s.Nodes = append(s.Nodes, cc.Nodes...)
case Any:
return Any{}
case nil:
default:
s.Nodes = append(s.Nodes, c)
}
}
switch len(s.Nodes) {
case 0:
return nil
case 1:
return s.Nodes[0]
default:
return s
}
case Not, Token, nil:
return Any{}
case Symbol:
return collectSymbols(node.Name, true)
case String:
if !inSymbol {
return Any{}
}
// In logically correct patterns, all Strings that are children of
// Symbols describe the names of symbols.
return symbolToIndexSymbol(string(node))
case Binding:
return collectSymbols(node.Node, inSymbol)
case Any:
return Any{}
case List:
var out And
and(collectSymbols(node.Head, inSymbol), &out)
and(collectSymbols(node.Tail, inSymbol), &out)
switch len(out.Nodes) {
case 0:
return Any{}
case 1:
return out.Nodes[0]
default:
return out
}
default:
var out And
rv := reflect.ValueOf(node)
for i := range rv.NumField() {
c := collectSymbols(rv.Field(i).Interface().(Node), inSymbol)
and(c, &out)
}
switch len(out.Nodes) {
case 0:
return Any{}
case 1:
return out.Nodes[0]
default:
return out
}
}
}
func collectRootCallSymbols(node Node) []IndexSymbol {
root, ok := node.(CallExpr)
if !ok {
return nil
}
var names []String
var handleSymName func(name Node) bool
handleSymName = func(name Node) bool {
switch name := name.(type) {
case String:
names = append(names, name)
case Or:
for _, node := range name.Nodes {
if name, ok := node.(String); ok {
names = append(names, name)
} else {
return false
}
}
case Binding:
return handleSymName(name.Node)
default:
return false
}
return true
}
var handleRootFun func(node Node) bool
handleRootFun = func(node Node) bool {
switch fun := node.(type) {
case Binding:
return handleRootFun(fun.Node)
case Symbol:
return handleSymName(fun.Name)
case Or:
for _, node := range fun.Nodes {
if sym, ok := node.(Symbol); !ok || !handleSymName(sym.Name) {
return false
}
}
return true
default:
return false
}
}
if !handleRootFun(root.Fun) {
return nil
}
out := make([]IndexSymbol, len(names))
for i, name := range names {
out[i] = symbolToIndexSymbol(string(name))
}
return out
}
func collectEntryNodes(node Node, m map[reflect.Type]struct{}) {
switch node := node.(type) {
case Or:
for _, el := range node.Nodes {
collectEntryNodes(el, m)
}
case Not:
collectEntryNodes(node.Node, m)
case Binding:
collectEntryNodes(node.Node, m)
case Nil, nil:
// this branch is reached via bindings
for _, T := range allTypes {
m[T] = struct{}{}
}
default:
Ts, ok := nodeToASTTypes[reflect.TypeOf(node)]
if !ok {
panic(fmt.Sprintf("internal error: unhandled type %T", node))
}
for _, T := range Ts {
m[T] = struct{}{}
}
}
}
var allTypes = []reflect.Type{
reflect.TypeFor[*ast.RangeStmt](),
reflect.TypeFor[*ast.AssignStmt](),
reflect.TypeFor[*ast.IndexExpr](),
reflect.TypeFor[*ast.Ident](),
reflect.TypeFor[*ast.ValueSpec](),
reflect.TypeFor[*ast.GenDecl](),
reflect.TypeFor[*ast.BinaryExpr](),
reflect.TypeFor[*ast.ForStmt](),
reflect.TypeFor[*ast.ArrayType](),
reflect.TypeFor[*ast.DeferStmt](),
reflect.TypeFor[*ast.MapType](),
reflect.TypeFor[*ast.ReturnStmt](),
reflect.TypeFor[*ast.SliceExpr](),
reflect.TypeFor[*ast.StarExpr](),
reflect.TypeFor[*ast.UnaryExpr](),
reflect.TypeFor[*ast.SendStmt](),
reflect.TypeFor[*ast.SelectStmt](),
reflect.TypeFor[*ast.ImportSpec](),
reflect.TypeFor[*ast.IfStmt](),
reflect.TypeFor[*ast.GoStmt](),
reflect.TypeFor[*ast.Field](),
reflect.TypeFor[*ast.SelectorExpr](),
reflect.TypeFor[*ast.StructType](),
reflect.TypeFor[*ast.KeyValueExpr](),
reflect.TypeFor[*ast.FuncType](),
reflect.TypeFor[*ast.FuncLit](),
reflect.TypeFor[*ast.FuncDecl](),
reflect.TypeFor[*ast.ChanType](),
reflect.TypeFor[*ast.CallExpr](),
reflect.TypeFor[*ast.CaseClause](),
reflect.TypeFor[*ast.CommClause](),
reflect.TypeFor[*ast.CompositeLit](),
reflect.TypeFor[*ast.EmptyStmt](),
reflect.TypeFor[*ast.SwitchStmt](),
reflect.TypeFor[*ast.TypeSwitchStmt](),
reflect.TypeFor[*ast.TypeAssertExpr](),
reflect.TypeFor[*ast.TypeSpec](),
reflect.TypeFor[*ast.InterfaceType](),
reflect.TypeFor[*ast.BranchStmt](),
reflect.TypeFor[*ast.IncDecStmt](),
reflect.TypeFor[*ast.BasicLit](),
}
var nodeToASTTypes = map[reflect.Type][]reflect.Type{
reflect.TypeFor[String](): nil,
reflect.TypeFor[Token](): nil,
reflect.TypeFor[List](): {reflect.TypeFor[*ast.BlockStmt](), reflect.TypeFor[*ast.FieldList]()},
reflect.TypeFor[Builtin](): {reflect.TypeFor[*ast.Ident]()},
reflect.TypeFor[Object](): {reflect.TypeFor[*ast.Ident]()},
reflect.TypeFor[Symbol](): {reflect.TypeFor[*ast.Ident](), reflect.TypeFor[*ast.SelectorExpr]()},
reflect.TypeFor[Any](): allTypes,
reflect.TypeFor[RangeStmt](): {reflect.TypeFor[*ast.RangeStmt]()},
reflect.TypeFor[AssignStmt](): {reflect.TypeFor[*ast.AssignStmt]()},
reflect.TypeFor[IndexExpr](): {reflect.TypeFor[*ast.IndexExpr]()},
reflect.TypeFor[Ident](): {reflect.TypeFor[*ast.Ident]()},
reflect.TypeFor[ValueSpec](): {reflect.TypeFor[*ast.ValueSpec]()},
reflect.TypeFor[GenDecl](): {reflect.TypeFor[*ast.GenDecl]()},
reflect.TypeFor[BinaryExpr](): {reflect.TypeFor[*ast.BinaryExpr]()},
reflect.TypeFor[ForStmt](): {reflect.TypeFor[*ast.ForStmt]()},
reflect.TypeFor[ArrayType](): {reflect.TypeFor[*ast.ArrayType]()},
reflect.TypeFor[DeferStmt](): {reflect.TypeFor[*ast.DeferStmt]()},
reflect.TypeFor[MapType](): {reflect.TypeFor[*ast.MapType]()},
reflect.TypeFor[ReturnStmt](): {reflect.TypeFor[*ast.ReturnStmt]()},
reflect.TypeFor[SliceExpr](): {reflect.TypeFor[*ast.SliceExpr]()},
reflect.TypeFor[StarExpr](): {reflect.TypeFor[*ast.StarExpr]()},
reflect.TypeFor[UnaryExpr](): {reflect.TypeFor[*ast.UnaryExpr]()},
reflect.TypeFor[SendStmt](): {reflect.TypeFor[*ast.SendStmt]()},
reflect.TypeFor[SelectStmt](): {reflect.TypeFor[*ast.SelectStmt]()},
reflect.TypeFor[ImportSpec](): {reflect.TypeFor[*ast.ImportSpec]()},
reflect.TypeFor[IfStmt](): {reflect.TypeFor[*ast.IfStmt]()},
reflect.TypeFor[GoStmt](): {reflect.TypeFor[*ast.GoStmt]()},
reflect.TypeFor[Field](): {reflect.TypeFor[*ast.Field]()},
reflect.TypeFor[SelectorExpr](): {reflect.TypeFor[*ast.SelectorExpr]()},
reflect.TypeFor[StructType](): {reflect.TypeFor[*ast.StructType]()},
reflect.TypeFor[KeyValueExpr](): {reflect.TypeFor[*ast.KeyValueExpr]()},
reflect.TypeFor[FuncType](): {reflect.TypeFor[*ast.FuncType]()},
reflect.TypeFor[FuncLit](): {reflect.TypeFor[*ast.FuncLit]()},
reflect.TypeFor[FuncDecl](): {reflect.TypeFor[*ast.FuncDecl]()},
reflect.TypeFor[ChanType](): {reflect.TypeFor[*ast.ChanType]()},
reflect.TypeFor[CallExpr](): {reflect.TypeFor[*ast.CallExpr]()},
reflect.TypeFor[CaseClause](): {reflect.TypeFor[*ast.CaseClause]()},
reflect.TypeFor[CommClause](): {reflect.TypeFor[*ast.CommClause]()},
reflect.TypeFor[CompositeLit](): {reflect.TypeFor[*ast.CompositeLit]()},
reflect.TypeFor[EmptyStmt](): {reflect.TypeFor[*ast.EmptyStmt]()},
reflect.TypeFor[SwitchStmt](): {reflect.TypeFor[*ast.SwitchStmt]()},
reflect.TypeFor[TypeSwitchStmt](): {reflect.TypeFor[*ast.TypeSwitchStmt]()},
reflect.TypeFor[TypeAssertExpr](): {reflect.TypeFor[*ast.TypeAssertExpr]()},
reflect.TypeFor[TypeSpec](): {reflect.TypeFor[*ast.TypeSpec]()},
reflect.TypeFor[InterfaceType](): {reflect.TypeFor[*ast.InterfaceType]()},
reflect.TypeFor[BranchStmt](): {reflect.TypeFor[*ast.BranchStmt]()},
reflect.TypeFor[IncDecStmt](): {reflect.TypeFor[*ast.IncDecStmt]()},
reflect.TypeFor[BasicLit](): {reflect.TypeFor[*ast.BasicLit]()},
reflect.TypeFor[IntegerLiteral](): {reflect.TypeFor[*ast.BasicLit](), reflect.TypeFor[*ast.UnaryExpr]()},
reflect.TypeFor[TrulyConstantExpression](): allTypes, // this is an over-approximation, which is fine
}
var requiresTypeInfo = map[string]bool{
"Symbol": true,
"Builtin": true,
"Object": true,
"IntegerLiteral": true,
"TrulyConstantExpression": true,
}
type Parser struct {
// Allow nodes that rely on type information
AllowTypeInfo bool
f *token.File
cur item
last *item
nextItem func() (item, bool)
bindings map[string]int
}
func (p *Parser) bindingIndex(name string) int {
if p.bindings == nil {
p.bindings = map[string]int{}
}
if idx, ok := p.bindings[name]; ok {
return idx
}
idx := len(p.bindings)
p.bindings[name] = idx
return idx
}
func (p *Parser) Parse(s string) (Pattern, error) {
f := token.NewFileSet().AddFile("<input>", -1, len(s))
// Run the lexer iterator as a coroutine.
// The parser will call 'next' to consume each item.
// After the parser returns, we must call 'stop' to
// terminate the coroutine.
next, stop := iter.Pull(lex(f, s))
defer stop()
p.cur = item{}
p.last = nil
p.f = f
p.nextItem = next
// Parse.
root, err := p.node()
if err != nil {
return Pattern{}, err
}
// Consume final EOF token.
if item, ok := next(); !ok || item.typ != itemEOF {
return Pattern{}, fmt.Errorf("unexpected token %s after end of pattern", item.typ)
}
if len(p.bindings) > 64 {
return Pattern{}, errors.New("encountered more than 64 bindings")
}
bindings := make([]string, len(p.bindings))
for name, idx := range p.bindings {
bindings[idx] = name
}
_, isSymbol := root.(Symbol)
sym := collectSymbols(root, isSymbol)
rootSyms := collectRootCallSymbols(root)
relevantMap := map[reflect.Type]struct{}{}
collectEntryNodes(root, relevantMap)
relevantNodes := make([]ast.Node, 0, len(relevantMap))
for k := range relevantMap {
relevantNodes = append(relevantNodes, reflect.Zero(k).Interface().(ast.Node))
}
return Pattern{
Root: root,
EntryNodes: relevantNodes,
SymbolsPattern: sym,
RootCallSymbols: rootSyms,
Bindings: bindings,
}, nil
}
func (p *Parser) next() item {
if p.last != nil {
n := *p.last
p.last = nil
return n
}
var ok bool
p.cur, ok = p.nextItem()
if !ok {
p.cur = item{typ: eof}
}
return p.cur
}
func (p *Parser) rewind() {
p.last = &p.cur
}
func (p *Parser) peek() item {
n := p.next()
p.rewind()
return n
}
func (p *Parser) accept(typ itemType) (item, bool) {
n := p.next()
if n.typ == typ {
return n, true
}
p.rewind()
return item{}, false
}
func (p *Parser) unexpectedToken(valid string) error {
if p.cur.typ == itemError {
return fmt.Errorf("error lexing input: %s", p.cur.val)
}
var got string
switch p.cur.typ {
case itemTypeName, itemVariable, itemString:
got = p.cur.val
default:
got = "'" + p.cur.typ.String() + "'"
}
pos := p.f.Position(token.Pos(p.cur.pos))
return fmt.Errorf("%s: expected %s, found %s", pos, valid, got)
}
func (p *Parser) node() (Node, error) {
if _, ok := p.accept(itemLeftParen); !ok {
return nil, p.unexpectedToken("'('")
}
typ, ok := p.accept(itemTypeName)
if !ok {
return nil, p.unexpectedToken("Node type")
}
var objs []Node
for {
if _, ok := p.accept(itemRightParen); ok {
break
} else {
p.rewind()
obj, err := p.object()
if err != nil {
return nil, err
}
objs = append(objs, obj)
}
}
node, err := p.populateNode(typ.val, objs)
if err != nil {
return nil, err
}
if node, ok := node.(Binding); ok {
node.idx = p.bindingIndex(node.Name)
}
return node, nil
}
func populateNode(typ string, objs []Node, allowTypeInfo bool) (Node, error) {
T, ok := structNodes[typ]
if !ok {
return nil, fmt.Errorf("unknown node %s", typ)
}
if !allowTypeInfo && requiresTypeInfo[typ] {
return nil, fmt.Errorf("Node %s requires type information", typ)
}
pv := reflect.New(T)
v := pv.Elem()
if v.NumField() == 1 {
f := v.Field(0)
if f.Type().Kind() == reflect.Slice {
// Variadic node
f.Set(reflect.AppendSlice(f, reflect.ValueOf(objs)))
return v.Interface().(Node), nil
}
}
n := -1
for i := 0; i < T.NumField(); i++ {
if !T.Field(i).IsExported() {
break
}
n = i
}
if len(objs) != n+1 {
return nil, fmt.Errorf("tried to initialize node %s with %d values, expected %d", typ, len(objs), n+1)
}
for i := 0; i < v.NumField(); i++ {
if !T.Field(i).IsExported() {
break
}
f := v.Field(i)
if f.Kind() == reflect.String {
if obj, ok := objs[i].(String); ok {
f.Set(reflect.ValueOf(string(obj)))
} else {
return nil, fmt.Errorf("first argument of (Binding name node) must be string, but got %s", objs[i])
}
} else {
f.Set(reflect.ValueOf(objs[i]))
}
}
return v.Interface().(Node), nil
}
func (p *Parser) populateNode(typ string, objs []Node) (Node, error) {
return populateNode(typ, objs, p.AllowTypeInfo)
}
var structNodes = map[string]reflect.Type{
"Any": reflect.TypeFor[Any](),
"Ellipsis": reflect.TypeFor[Ellipsis](),
"List": reflect.TypeFor[List](),
"Binding": reflect.TypeFor[Binding](),
"RangeStmt": reflect.TypeFor[RangeStmt](),
"AssignStmt": reflect.TypeFor[AssignStmt](),
"IndexExpr": reflect.TypeFor[IndexExpr](),
"Ident": reflect.TypeFor[Ident](),
"Builtin": reflect.TypeFor[Builtin](),
"ValueSpec": reflect.TypeFor[ValueSpec](),
"GenDecl": reflect.TypeFor[GenDecl](),
"BinaryExpr": reflect.TypeFor[BinaryExpr](),
"ForStmt": reflect.TypeFor[ForStmt](),
"ArrayType": reflect.TypeFor[ArrayType](),
"DeferStmt": reflect.TypeFor[DeferStmt](),
"MapType": reflect.TypeFor[MapType](),
"ReturnStmt": reflect.TypeFor[ReturnStmt](),
"SliceExpr": reflect.TypeFor[SliceExpr](),
"StarExpr": reflect.TypeFor[StarExpr](),
"UnaryExpr": reflect.TypeFor[UnaryExpr](),
"SendStmt": reflect.TypeFor[SendStmt](),
"SelectStmt": reflect.TypeFor[SelectStmt](),
"ImportSpec": reflect.TypeFor[ImportSpec](),
"IfStmt": reflect.TypeFor[IfStmt](),
"GoStmt": reflect.TypeFor[GoStmt](),
"Field": reflect.TypeFor[Field](),
"SelectorExpr": reflect.TypeFor[SelectorExpr](),
"StructType": reflect.TypeFor[StructType](),
"KeyValueExpr": reflect.TypeFor[KeyValueExpr](),
"FuncType": reflect.TypeFor[FuncType](),
"FuncLit": reflect.TypeFor[FuncLit](),
"FuncDecl": reflect.TypeFor[FuncDecl](),
"ChanType": reflect.TypeFor[ChanType](),
"CallExpr": reflect.TypeFor[CallExpr](),
"CaseClause": reflect.TypeFor[CaseClause](),
"CommClause": reflect.TypeFor[CommClause](),
"CompositeLit": reflect.TypeFor[CompositeLit](),
"EmptyStmt": reflect.TypeFor[EmptyStmt](),
"SwitchStmt": reflect.TypeFor[SwitchStmt](),
"TypeSwitchStmt": reflect.TypeFor[TypeSwitchStmt](),
"TypeAssertExpr": reflect.TypeFor[TypeAssertExpr](),
"TypeSpec": reflect.TypeFor[TypeSpec](),
"InterfaceType": reflect.TypeFor[InterfaceType](),
"BranchStmt": reflect.TypeFor[BranchStmt](),
"IncDecStmt": reflect.TypeFor[IncDecStmt](),
"BasicLit": reflect.TypeFor[BasicLit](),
"Object": reflect.TypeFor[Object](),
"Symbol": reflect.TypeFor[Symbol](),
"Or": reflect.TypeFor[Or](),
"Not": reflect.TypeFor[Not](),
"IntegerLiteral": reflect.TypeFor[IntegerLiteral](),
"TrulyConstantExpression": reflect.TypeFor[TrulyConstantExpression](),
}
func (p *Parser) object() (Node, error) {
n := p.next()
switch n.typ {
case itemLeftParen:
p.rewind()
node, err := p.node()
if err != nil {
return node, err
}
if p.peek().typ == itemColon {
p.next()
tail, err := p.object()
if err != nil {
return node, err
}
return List{Head: node, Tail: tail}, nil
}
return node, nil
case itemLeftBracket:
p.rewind()
return p.array()
case itemVariable:
v := n
if v.val == "nil" {
return Nil{}, nil
}
var b Binding
if _, ok := p.accept(itemAt); ok {
o, err := p.node()
if err != nil {
return nil, err
}
b = Binding{
Name: v.val,
Node: o,
idx: p.bindingIndex(v.val),
}
} else {
p.rewind()
b = Binding{
Name: v.val,
idx: p.bindingIndex(v.val),
}
}
if p.peek().typ == itemColon {
p.next()
tail, err := p.object()
if err != nil {
return b, err
}
return List{Head: b, Tail: tail}, nil
}
return b, nil
case itemBlank:
if p.peek().typ == itemColon {
p.next()
tail, err := p.object()
if err != nil {
return Any{}, err
}
return List{Head: Any{}, Tail: tail}, nil
}
return Any{}, nil
case itemString:
return String(n.val), nil
default:
return nil, p.unexpectedToken("object")
}
}
func (p *Parser) array() (Node, error) {
if _, ok := p.accept(itemLeftBracket); !ok {
return nil, p.unexpectedToken("'['")
}
var objs []Node
for {
if _, ok := p.accept(itemRightBracket); ok {
break
} else {
p.rewind()
obj, err := p.object()
if err != nil {
return nil, err
}
objs = append(objs, obj)
}
}
tail := List{}
for i := len(objs) - 1; i >= 0; i-- {
l := List{
Head: objs[i],
Tail: tail,
}
tail = l
}
return tail, nil
}
/*
Node ::= itemLeftParen itemTypeName Object* itemRightParen
Object ::= Node | Array | Binding | itemVariable | itemBlank | itemString
Array := itemLeftBracket Object* itemRightBracket
Array := Object itemColon Object
Binding ::= itemVariable itemAt Node
*/