-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
2052 lines (1718 loc) · 46 KB
/
Copy pathparser.go
File metadata and controls
2052 lines (1718 loc) · 46 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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package solcparser
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/antlr/antlr4/runtime/Go/antlr"
solAntlr "github.com/umbracle/solidity-parser-go/antlr"
)
// exampleListener is an event-driven callback for the parser.
type exampleListener struct {
service reflect.Value
funcMap map[string]*funcData
}
type funcData struct {
inNum int
reqt []reflect.Type
fv reflect.Value
isDyn bool
}
func (f *funcData) numParams() int {
return f.inNum - 1
}
func (e *exampleListener) init() {
e.funcMap = map[string]*funcData{}
e.service = reflect.ValueOf(e)
st := reflect.TypeOf(e)
if st.Kind() == reflect.Struct {
panic("bad")
}
for i := 0; i < st.NumMethod(); i++ {
mv := st.Method(i)
if mv.PkgPath != "" {
// skip unexported methods
continue
}
name := mv.Name
if name == "Visit" {
continue
}
if !strings.HasPrefix(name, "Visit") {
continue
}
fd := &funcData{
fv: mv.Func,
}
var err error
if fd.inNum, fd.reqt, err = validateFunc(name, fd.fv, true); err != nil {
panic(fmt.Sprintf("jsonrpc: %s", err))
}
// check if last item is a pointer
if fd.numParams() != 0 {
last := fd.reqt[fd.numParams()]
if last.Kind() == reflect.Ptr {
fd.isDyn = true
}
}
e.funcMap[name] = fd
}
}
func validateFunc(funcName string, fv reflect.Value, isMethod bool) (inNum int, reqt []reflect.Type, err error) {
if funcName == "" {
err = fmt.Errorf("funcName cannot be empty")
return
}
ft := fv.Type()
if ft.Kind() != reflect.Func {
err = fmt.Errorf("function '%s' must be a function instead of %s", funcName, ft)
return
}
inNum = ft.NumIn()
outNum := ft.NumOut()
if outNum != 1 {
err = fmt.Errorf("unexpected number of output arguments in the function '%s': %d. Expected 1", funcName, outNum)
return
}
reqt = make([]reflect.Type, inNum)
for i := 0; i < inNum; i++ {
reqt[i] = ft.In(i)
}
return
}
func (e *exampleListener) Visit(i antlr.Tree) INode {
funcName := reflect.TypeOf(i).String()
if funcName == "*antlr.TerminalNodeImpl" {
return nil
}
funcName = strings.TrimPrefix(funcName, "*solcparser.")
xx := strings.TrimSuffix(funcName, "Context")
funcName = "Visit" + strings.TrimSuffix(funcName, "Context")
fd, ok := e.funcMap[funcName]
if !ok {
panic(fmt.Sprintf("BUG: visit not found %s", funcName))
}
inArgs := make([]reflect.Value, fd.inNum)
inArgs[0] = e.service
inArgs[1] = reflect.ValueOf(i)
output := fd.fv.Call(inArgs)[0]
if output.IsNil() {
return nil
}
ii := output.Interface().(INode)
if !skipNode(xx) {
ii.SetTypeName(xx)
}
return ii
}
func skipNode(i string) bool {
skip := []string{
"ContractPart",
"Statement",
"SimpleStatement",
"Expression",
"TypeName",
"MappingKey",
"PrimaryExpression",
"Parameter",
"FunctionTypeParameter",
}
for _, j := range skip {
if j == i {
return true
}
}
return false
}
type Node struct {
Type string `json:"type"`
}
func (n *Node) IsNode() {}
func (n *Node) SetTypeName(typ string) {
n.Type = typ
}
func (n *Node) GetType() string {
return n.Type
}
type INode interface {
GetType() string
IsNode()
SetTypeName(s string)
}
// SourceUnit
type SourceUnit struct {
Node
Children []interface{}
}
func (e *exampleListener) VisitSourceUnit(ctx *solAntlr.SourceUnitContext) INode {
decl := &SourceUnit{
Children: []interface{}{},
}
for _, p := range ctx.GetChildren() {
decl.Children = append(decl.Children, e.Visit(p))
}
return decl
}
type PragmaDirective struct {
Node
Name string
Value string
}
func (e *exampleListener) VisitPragmaDirective(ctx *solAntlr.PragmaDirectiveContext) INode {
decl := &PragmaDirective{
Name: toText(ctx.PragmaName()),
Value: toText(ctx.PragmaValue()),
}
return decl
}
// ContractDefinition
type ContractDefinition struct {
Node
Name string `json:"name"`
SubNodes []interface{}
BaseContracts []interface{}
Kind string
}
func (e *exampleListener) VisitContractDefinition(ctx *solAntlr.ContractDefinitionContext) INode {
decl := &ContractDefinition{
Name: toText(ctx.Identifier()),
Kind: toText(ctx.GetChild(0)),
SubNodes: []interface{}{},
BaseContracts: []interface{}{},
}
for _, i := range ctx.AllContractPart() {
decl.SubNodes = append(decl.SubNodes, e.Visit(i))
}
for _, i := range ctx.AllInheritanceSpecifier() {
decl.BaseContracts = append(decl.BaseContracts, e.Visit(i))
}
//addMeta(decl, ctx)
return decl
}
type InheritanceSpecifier struct {
Node
BaseName interface{}
Arguments []interface{}
}
func (e *exampleListener) VisitInheritanceSpecifier(ctx *solAntlr.InheritanceSpecifierContext) INode {
decl := &InheritanceSpecifier{
BaseName: e.Visit(ctx.UserDefinedTypeName()),
Arguments: []interface{}{},
}
if expr := ctx.ExpressionList(); expr != nil {
decl.Arguments = append(decl.Arguments, e.Visit(expr))
}
return decl
}
func (e *exampleListener) VisitContractPart(ctx *solAntlr.ContractPartContext) interface{} {
return e.Visit(ctx.GetChild(0))
}
// StructDefinition
type StructDefinition struct {
Node
Name string
Members []interface{}
}
func (e *exampleListener) VisitStructDefinition(ctx *solAntlr.StructDefinitionContext) INode {
members := []interface{}{}
for _, i := range ctx.AllVariableDeclaration() {
members = append(members, e.Visit(i))
}
decl := &StructDefinition{
Name: toText(ctx.Identifier()),
Members: members,
}
return decl
}
// VariableDeclaration
type VariableDeclaration struct {
Node
Name string
TypeName interface{}
Identifier interface{}
IsIndexed bool
IsStateVar bool
IsDeclaredConst bool
StorageLocation string
Expression interface{}
Visibility string
Override []interface{}
}
func (e *exampleListener) VisitVariableDeclaration(ctx *solAntlr.VariableDeclarationContext) INode {
decl := &VariableDeclaration{
Name: toText(ctx.Identifier()),
Identifier: e.Visit(ctx.Identifier()),
TypeName: e.Visit(ctx.TypeName()),
}
if ctx.StorageLocation() != nil {
decl.StorageLocation = toText(ctx.StorageLocation())
}
return decl
}
type Identifier struct {
Node
Name string
}
func (e *exampleListener) VisitIdentifier(ctx *solAntlr.IdentifierContext) INode {
decl := &Identifier{
Name: toText(ctx),
}
return decl
}
type StateVariableDeclaration struct {
Node
Variables []interface{}
InitialValue interface{}
}
type StateVariableDeclarationVariable struct {
VariableDeclaration
IsInmutable bool
}
func (e *exampleListener) VisitStateVariableDeclaration(ctx *solAntlr.StateVariableDeclarationContext) INode {
visibility := "default"
if hasElem(ctx.AllInternalKeyword()) {
visibility = "internal"
} else if hasElem(ctx.AllPublicKeyword()) {
visibility = "public"
} else if hasElem(ctx.AllPrivateKeyword()) {
visibility = "private"
}
var override []interface{}
overSpec := ctx.AllOverrideSpecifier()
if len(overSpec) != 0 {
for _, i := range overSpec[0].(*solAntlr.OverrideSpecifierContext).AllUserDefinedTypeName() {
override = append(override, e.Visit(i))
}
}
vv := &StateVariableDeclarationVariable{
VariableDeclaration: VariableDeclaration{
Node: Node{Type: "VariableDeclaration"},
Name: toText(ctx.Identifier()),
TypeName: e.Visit(ctx.TypeName()),
Identifier: e.Visit(ctx.Identifier()),
IsDeclaredConst: hasElem(ctx.AllConstantKeyword()),
Visibility: visibility,
IsStateVar: true,
Override: override,
},
IsInmutable: hasElem(ctx.AllImmutableKeyword()),
}
if ctx.Expression() != nil {
vv.Expression = e.Visit(ctx.Expression())
}
decl := &StateVariableDeclaration{
Variables: []interface{}{vv},
}
return decl
}
// FunctionDefinition
type FunctionDefinition struct {
Node
Name string
Parameters []interface{}
Modifiers []interface{}
ReturnParameters interface{}
Body interface{}
StateMutability string
Visibility string
IsConstructor bool
IsReceiveEther bool
IsFallback bool
IsVirtual bool
Override []interface{}
}
func hasElem(a []antlr.TerminalNode) bool {
return len(a) > 0
}
func (e *exampleListener) VisitFunctionDefinition(ctx *solAntlr.FunctionDefinitionContext) INode {
var isConstructor, isFallback, isVirtual, isReceiveEther bool
visibility := "default"
var block interface{}
if ctx.Block() != nil {
block = e.Visit(ctx.Block())
}
modifiers := []interface{}{}
for _, i := range ctx.ModifierList().(*solAntlr.ModifierListContext).AllModifierInvocation() {
modifiers = append(modifiers, e.Visit(i))
}
modList := ctx.ModifierList().(*solAntlr.ModifierListContext)
var parameters []interface{}
params := ctx.ParameterList().(*solAntlr.ParameterListContext).AllParameter()
for _, param := range params {
parameters = append(parameters, e.Visit(param))
}
var name string
var returnParameters interface{}
switch toText(ctx.FunctionDescriptor().(*solAntlr.FunctionDescriptorContext).GetChild(0)) {
case "constructor":
if hasElem(modList.AllInternalKeyword()) {
visibility = "internal"
} else if hasElem(modList.AllPublicKeyword()) {
visibility = "public"
} else {
visibility = "default"
}
isConstructor = true
case "fallback":
visibility = "external"
isFallback = true
case "receive":
visibility = "external"
isReceiveEther = true
case "function":
ident := ctx.FunctionDescriptor().(*solAntlr.FunctionDescriptorContext).Identifier()
if ident != nil {
name = toText(ident)
}
ctxRet := ctx.ReturnParameters()
if ctxRet != nil {
returnParameters = e.VisitReturnParameters(ctxRet.(*solAntlr.ReturnParametersContext))
}
if hasElem(modList.AllExternalKeyword()) {
visibility = "external"
} else if hasElem(modList.AllInternalKeyword()) {
visibility = "internal"
} else if hasElem(modList.AllPublicKeyword()) {
visibility = "public"
} else if hasElem(modList.AllPrivateKeyword()) {
visibility = "private"
}
isFallback = name == ""
}
if hasElem(modList.AllVirtualKeyword()) {
isVirtual = true
}
var override []interface{}
overrideSpec := modList.AllOverrideSpecifier()
if len(overrideSpec) != 0 {
for _, o := range overrideSpec[0].(*solAntlr.OverrideSpecifierContext).AllUserDefinedTypeName() {
override = append(override, e.Visit(o))
}
}
decl := &FunctionDefinition{
Name: name,
Body: block,
Parameters: parameters,
ReturnParameters: returnParameters,
IsConstructor: isConstructor,
IsFallback: isFallback,
IsVirtual: isVirtual,
IsReceiveEther: isReceiveEther,
Visibility: visibility,
Modifiers: modifiers,
Override: override,
}
return decl
}
func (e *exampleListener) VisitParameter(ctx *solAntlr.ParameterContext) INode {
decl := &VariableDeclaration{
Node: Node{Type: "VariableDeclaration"},
TypeName: e.Visit(ctx.TypeName()),
IsStateVar: false,
IsIndexed: false,
Expression: nil,
}
if ctx.StorageLocation() != nil {
decl.StorageLocation = toText(ctx.StorageLocation())
}
if iden := ctx.Identifier(); iden != nil {
decl.Name = toText(iden)
decl.Identifier = e.Visit(iden)
}
return decl
}
type ModifierInvocation struct {
Node
Name string
Arguments []interface{}
}
func (e *exampleListener) VisitModifierInvocation(ctx *solAntlr.ModifierInvocationContext) INode {
var args []interface{}
if expr := ctx.ExpressionList(); expr != nil {
for _, p := range expr.(*solAntlr.ExpressionListContext).AllExpression() {
args = append(args, e.Visit(p))
}
} else if child := ctx.GetChildren(); len(child) > 1 {
args = []interface{}{}
}
decl := &ModifierInvocation{
Name: toText(ctx.Identifier()),
Arguments: args,
}
return decl
}
// Block
type Block struct {
Node
Statements []interface{}
}
func (e *exampleListener) VisitBlock(ctx *solAntlr.BlockContext) INode {
decl := &Block{
Statements: []interface{}{},
}
for _, i := range ctx.AllStatement() {
decl.Statements = append(decl.Statements, e.Visit(i))
}
return decl
}
func (e *exampleListener) VisitStatement(ctx *solAntlr.StatementContext) INode {
return e.Visit(ctx.GetChild(0))
}
func (e *exampleListener) VisitSimpleStatement(ctx *solAntlr.SimpleStatementContext) INode {
return e.Visit(ctx.GetChild(0))
}
// ExpressionStatement
type ExpressionStatement struct {
Node
Expression interface{}
}
func (e *exampleListener) VisitExpressionStatement(ctx *solAntlr.ExpressionStatementContext) INode {
if ctx == nil {
return nil
}
decl := &ExpressionStatement{
Expression: e.Visit(ctx.Expression()),
}
return decl
}
// Expression
type NewExpression struct {
Node
TypeName interface{}
}
type UnaryOperation struct {
Node
Operator string
SubExpression interface{}
IsPrefix bool
}
type BinaryOperation struct {
Node
Operator string
Left interface{}
Right interface{}
}
type NumberLiteral struct {
Node
Number string
SubDenomination interface{}
}
type IndexRangeAccess struct {
Node
Base interface{}
IndexStart interface{}
IndexEnd interface{}
}
type Conditional struct {
Node
Condition interface{}
TrueExpression interface{}
FalseExpression interface{}
}
type MemberAccess struct {
Node
Expression interface{}
MemberName string
}
type TupleExpression struct {
Node
Components []interface{}
IsArray bool
}
type NameValueExpression struct {
Node
Expression interface{}
Arguments interface{}
}
type IndexAccess struct {
Node
Base interface{}
Index interface{}
}
type ArrayTypeName struct {
Node
BaseTypeName interface{}
Length interface{}
}
var (
unaryOps = []string{"-", "+", "--", "++", "~", "after", "delete", "!"}
postFixOps = []string{"++", "--"}
binaryOps = []string{"+", "-", "*", "/", "**", "%", "<<", ">>", "&&", "||", ",,", "&", ",", "^", "<", ">", "<=", ">=", "==", "!=", "=", ",=", "^=", "&=", "<<=", ">>=", "+=", "-=", "*=", "/=", "%=", "|", "|="}
)
func contains(list []string, s string) bool {
for _, op := range list {
if s == op {
return true
}
}
return false
}
func (e *exampleListener) VisitExpression(ctx *solAntlr.ExpressionContext) INode {
children := ctx.GetChildren()
switch len(children) {
case 1:
return e.Visit(children[0])
case 2:
op := toText(ctx.GetChild(0))
// new expression
if op == "new" {
decl := &NewExpression{
Node: Node{Type: "NewExpression"},
TypeName: e.Visit(ctx.TypeName()),
}
return decl
}
// prefix operators
if contains(unaryOps, op) {
decl := &UnaryOperation{
Node: Node{Type: "UnaryOperation"},
Operator: op,
SubExpression: e.Visit(ctx.Expression(0)),
IsPrefix: true,
}
return decl
}
op = toText(ctx.GetChild(1))
if contains(postFixOps, op) {
decl := &UnaryOperation{
Node: Node{Type: "UnaryOperation"},
Operator: op,
SubExpression: e.Visit(ctx.Expression(0)),
IsPrefix: false,
}
return decl
}
case 3:
if toText(ctx.GetChild(0)) == "(" && toText(ctx.GetChild(2)) == ")" {
decl := &TupleExpression{
Node: Node{Type: "TupleExpression"},
IsArray: false,
Components: []interface{}{
e.Visit(ctx.GetChild(1)),
},
}
return decl
}
op := toText(ctx.GetChild(1))
// member access
if op == "." {
decl := &MemberAccess{
Node: Node{Type: "MemberAccess"},
Expression: e.Visit(ctx.Expression(0)),
MemberName: toText(ctx.Identifier()),
}
return decl
}
if contains(binaryOps, op) {
decl := &BinaryOperation{
Node: Node{Type: "BinaryOperation"},
Operator: op,
Left: e.Visit(ctx.Expression(0)),
Right: e.Visit(ctx.Expression(1)),
}
return decl
}
case 4:
// function call
if toText(ctx.GetChild(1)) == "(" && toText(ctx.GetChild(3)) == ")" {
var names, args, identifiers []interface{}
ctxArgs := ctx.FunctionCallArguments().(*solAntlr.FunctionCallArgumentsContext)
if expr := ctxArgs.ExpressionList(); expr != nil {
for _, p := range expr.(*solAntlr.ExpressionListContext).AllExpression() {
args = append(args, e.Visit(p))
}
} else if expr := ctxArgs.NameValueList(); expr != nil {
for _, raw := range expr.(*solAntlr.NameValueListContext).AllNameValue() {
p := raw.(*solAntlr.NameValueContext)
args = append(args, e.Visit(p.Expression()))
names = append(names, toText(p.Identifier()))
identifiers = append(identifiers, e.Visit(p.Identifier()))
}
}
decl := &FunctionCall{
Node: Node{Type: "FunctionCall"},
Expression: e.Visit(ctx.Expression(0)),
Names: names,
Identifiers: identifiers,
Arguments: args,
}
return decl
}
// index access
if toText(ctx.GetChild(1)) == "[" && toText(ctx.GetChild(3)) == "]" {
if toText(ctx.GetChild(2)) == ":" {
decl := &IndexRangeAccess{
Node: Node{Type: "IndexRangeAccess"},
Base: e.Visit(ctx.Expression(0)),
}
return decl
}
decl := &IndexAccess{
Node: Node{Type: "IndexAccess"},
Base: e.Visit(ctx.Expression(0)),
Index: e.Visit(ctx.Expression(1)),
}
return decl
}
// expression with nameValueList
if toText(ctx.GetChild(1)) == "{" && toText(ctx.GetChild(3)) == "}" {
decl := &NameValueExpression{
Node: Node{Type: "NameValueExpression"},
Expression: e.Visit(ctx.Expression(0)),
Arguments: e.Visit(ctx.NameValueList()),
}
return decl
}
case 5:
// ternary operator
if toText(ctx.GetChild(1)) == "?" && toText(ctx.GetChild(3)) == ":" {
decl := &Conditional{
Node: Node{Type: "Conditional"},
Condition: e.Visit(ctx.Expression(0)),
TrueExpression: e.Visit(ctx.Expression(1)),
FalseExpression: e.Visit(ctx.Expression(2)),
}
return decl
}
// index range access
if toText(ctx.GetChild(1)) == "[" && toText(ctx.GetChild(2)) == ":" && toText(ctx.GetChild(4)) == "]" {
decl := &IndexRangeAccess{
Node: Node{Type: "IndexRangeAccess"},
Base: e.Visit(ctx.Expression(0)),
IndexEnd: e.Visit(ctx.Expression(1)),
}
return decl
} else if toText(ctx.GetChild(1)) == "[" && toText(ctx.GetChild(3)) == ":" && toText(ctx.GetChild(4)) == "]" {
decl := &IndexRangeAccess{
Node: Node{Type: "IndexRangeAccess"},
Base: e.Visit(ctx.Expression(0)),
IndexStart: e.Visit(ctx.Expression(1)),
}
return decl
}
case 6:
// index range access
if toText(ctx.GetChild(1)) == "[" && toText(ctx.GetChild(3)) == ":" && toText(ctx.GetChild(5)) == "]" {
decl := &IndexRangeAccess{
Node: Node{Type: "IndexRangeAccess"},
Base: e.Visit(ctx.Expression(0)),
IndexStart: e.Visit(ctx.Expression(1)),
IndexEnd: e.Visit(ctx.Expression(2)),
}
return decl
}
}
panic("TODO")
}
type Mapping struct {
Node
KeyType interface{}
ValueType interface{}
}
func (e *exampleListener) VisitMapping(ctx *solAntlr.MappingContext) INode {
decl := &Mapping{
KeyType: e.Visit(ctx.MappingKey()),
ValueType: e.Visit(ctx.TypeName()),
}
return decl
}
func (e *exampleListener) VisitMappingKey(ctx *solAntlr.MappingKeyContext) INode {
if elem := ctx.ElementaryTypeName(); elem != nil {
return e.Visit(elem)
}
if elem := ctx.UserDefinedTypeName(); elem != nil {
return e.Visit(elem)
}
panic("BUG")
}
type NameValueList struct {
Node
Names []string
Identifiers []interface{}
Args []interface{}
}
func (e *exampleListener) VisitNameValueList(ctx *solAntlr.NameValueListContext) INode {
decl := &NameValueList{
Names: []string{},
Identifiers: []interface{}{},
Args: []interface{}{},
}
for _, raw := range ctx.AllNameValue() {
p := raw.(*solAntlr.NameValueContext)
decl.Names = append(decl.Names, toText(p.Identifier()))
decl.Identifiers = append(decl.Identifiers, e.Visit(p.Identifier()))
decl.Args = append(decl.Args, e.Visit(p.Expression()))
}
return decl
}
func (e *exampleListener) VisitTupleExpression(ctx *solAntlr.TupleExpressionContext) INode {
count := ctx.GetChildCount()
if count < 2 {
panic("bad")
}
childs := ctx.GetChildren()[1 : count-1]
childs = mapCommasToNulls(childs)
components := []interface{}{}
for _, child := range childs {
if child == nil {
components = append(components, nil)
} else {
components = append(components, e.Visit(child))
}
}
decl := &TupleExpression{
Components: components,
IsArray: toText(ctx.GetChild(0)) == "[",
}
return decl
}
func mapCommasToNulls(tree []antlr.Tree) []antlr.Tree {
res := []antlr.Tree{}
if len(tree) == 0 {
return res
}
comma := true
for _, child := range tree {
if comma {
if toText(child) == "," {
res = append(res, nil)
} else {
res = append(res, child)
comma = false
}
} else {
if toText(child) != "," {
panic("Comma expected")
}
comma = true
}
}
if comma {
res = append(res, nil)
}
return res
}
type TypeNameExpression struct {
Node
TypeName interface{}
}
func (e *exampleListener) VisitTypeNameExpression(ctx *solAntlr.TypeNameExpressionContext) INode {
var typeName interface{}
if ctxElem := ctx.ElementaryTypeName(); ctxElem != nil {
typeName = e.Visit(ctxElem)
} else if ctxUser := ctx.UserDefinedTypeName(); ctxUser != nil {
typeName = e.Visit(ctxUser)
} else {
panic("BAD")
}
decl := &TypeNameExpression{
TypeName: typeName,
}
return decl
}
func (e *exampleListener) VisitNumberLiteral(ctx *solAntlr.NumberLiteralContext) INode {
decl := &NumberLiteral{
Number: toText(ctx.GetChild(0)),
SubDenomination: nil,
}
if ctx.GetChildCount() == 2 {
decl.SubDenomination = toText(ctx.GetChild(1))
}
return decl
}
type HexLiteral struct {
Node
Value string
Parts []string
}
func (e *exampleListener) VisitHexLiteral(ctx *solAntlr.HexLiteralContext) INode {
decl := &HexLiteral{
Parts: []string{},
}
for _, p := range ctx.AllHexLiteralFragment() {
hex := toText(p)
hex = strings.TrimPrefix(hex, "hex")
hex = strings.Trim(hex, "\"")
decl.Parts = append(decl.Parts, hex)
}
decl.Value = strings.Join(decl.Parts, "")
return decl
}
type BooleanLiteral struct {
Node
Value bool
}