-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathvisitor.go
78 lines (74 loc) · 1.34 KB
/
visitor.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
package ast
import "fmt"
type Visitor interface {
Visit(node *Node)
}
func Walk(node *Node, v Visitor) {
if *node == nil {
return
}
switch n := (*node).(type) {
case *NilNode:
case *IdentifierNode:
case *IntegerNode:
case *FloatNode:
case *BoolNode:
case *StringNode:
case *ConstantNode:
case *UnaryNode:
Walk(&n.Node, v)
case *BinaryNode:
Walk(&n.Left, v)
Walk(&n.Right, v)
case *ChainNode:
Walk(&n.Node, v)
case *MemberNode:
Walk(&n.Node, v)
Walk(&n.Property, v)
case *SliceNode:
Walk(&n.Node, v)
if n.From != nil {
Walk(&n.From, v)
}
if n.To != nil {
Walk(&n.To, v)
}
case *CallNode:
Walk(&n.Callee, v)
for i := range n.Arguments {
Walk(&n.Arguments[i], v)
}
case *BuiltinNode:
for i := range n.Arguments {
Walk(&n.Arguments[i], v)
}
case *PredicateNode:
Walk(&n.Node, v)
case *PointerNode:
case *VariableDeclaratorNode:
Walk(&n.Value, v)
Walk(&n.Expr, v)
case *SequenceNode:
for i := range n.Nodes {
Walk(&n.Nodes[i], v)
}
case *ConditionalNode:
Walk(&n.Cond, v)
Walk(&n.Exp1, v)
Walk(&n.Exp2, v)
case *ArrayNode:
for i := range n.Nodes {
Walk(&n.Nodes[i], v)
}
case *MapNode:
for i := range n.Pairs {
Walk(&n.Pairs[i], v)
}
case *PairNode:
Walk(&n.Key, v)
Walk(&n.Value, v)
default:
panic(fmt.Sprintf("undefined node type (%T)", node))
}
v.Visit(node)
}