Skip to content

Commit 70ceebb

Browse files
committed
Simplify ast.Visitor interface
1 parent 345c047 commit 70ceebb

File tree

10 files changed

+40
-69
lines changed

10 files changed

+40
-69
lines changed

ast/visitor.go

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,10 @@ package ast
33
import "fmt"
44

55
type Visitor interface {
6-
Enter(node *Node)
7-
Exit(node *Node)
6+
Visit(node *Node)
87
}
98

10-
type walker struct {
11-
visitor Visitor
12-
}
13-
14-
func Walk(node *Node, visitor Visitor) {
15-
w := walker{
16-
visitor: visitor,
17-
}
18-
w.walk(node)
19-
}
20-
21-
func (w *walker) walk(node *Node) {
22-
w.visitor.Enter(node)
23-
9+
func Walk(node *Node, v Visitor) {
2410
switch n := (*node).(type) {
2511
case *NilNode:
2612
case *IdentifierNode:
@@ -30,56 +16,56 @@ func (w *walker) walk(node *Node) {
3016
case *StringNode:
3117
case *ConstantNode:
3218
case *UnaryNode:
33-
w.walk(&n.Node)
19+
Walk(&n.Node, v)
3420
case *BinaryNode:
35-
w.walk(&n.Left)
36-
w.walk(&n.Right)
21+
Walk(&n.Left, v)
22+
Walk(&n.Right, v)
3723
case *MatchesNode:
38-
w.walk(&n.Left)
39-
w.walk(&n.Right)
24+
Walk(&n.Left, v)
25+
Walk(&n.Right, v)
4026
case *ChainNode:
41-
w.walk(&n.Node)
27+
Walk(&n.Node, v)
4228
case *MemberNode:
43-
w.walk(&n.Node)
44-
w.walk(&n.Property)
29+
Walk(&n.Node, v)
30+
Walk(&n.Property, v)
4531
case *SliceNode:
46-
w.walk(&n.Node)
32+
Walk(&n.Node, v)
4733
if n.From != nil {
48-
w.walk(&n.From)
34+
Walk(&n.From, v)
4935
}
5036
if n.To != nil {
51-
w.walk(&n.To)
37+
Walk(&n.To, v)
5238
}
5339
case *CallNode:
54-
w.walk(&n.Callee)
40+
Walk(&n.Callee, v)
5541
for i := range n.Arguments {
56-
w.walk(&n.Arguments[i])
42+
Walk(&n.Arguments[i], v)
5743
}
5844
case *BuiltinNode:
5945
for i := range n.Arguments {
60-
w.walk(&n.Arguments[i])
46+
Walk(&n.Arguments[i], v)
6147
}
6248
case *ClosureNode:
63-
w.walk(&n.Node)
49+
Walk(&n.Node, v)
6450
case *PointerNode:
6551
case *ConditionalNode:
66-
w.walk(&n.Cond)
67-
w.walk(&n.Exp1)
68-
w.walk(&n.Exp2)
52+
Walk(&n.Cond, v)
53+
Walk(&n.Exp1, v)
54+
Walk(&n.Exp2, v)
6955
case *ArrayNode:
7056
for i := range n.Nodes {
71-
w.walk(&n.Nodes[i])
57+
Walk(&n.Nodes[i], v)
7258
}
7359
case *MapNode:
7460
for i := range n.Pairs {
75-
w.walk(&n.Pairs[i])
61+
Walk(&n.Pairs[i], v)
7662
}
7763
case *PairNode:
78-
w.walk(&n.Key)
79-
w.walk(&n.Value)
64+
Walk(&n.Key, v)
65+
Walk(&n.Value, v)
8066
default:
8167
panic(fmt.Sprintf("undefined node type (%T)", node))
8268
}
8369

84-
w.visitor.Exit(node)
70+
v.Visit(node)
8571
}

ast/visitor_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ type visitor struct {
1111
identifiers []string
1212
}
1313

14-
func (v *visitor) Enter(node *ast.Node) {}
15-
func (v *visitor) Exit(node *ast.Node) {
14+
func (v *visitor) Visit(node *ast.Node) {
1615
if n, ok := (*node).(*ast.IdentifierNode); ok {
1716
v.identifiers = append(v.identifiers, n.Value)
1817
}
@@ -33,12 +32,11 @@ func TestWalk(t *testing.T) {
3332

3433
type patcher struct{}
3534

36-
func (p *patcher) Enter(node *ast.Node) {
35+
func (p *patcher) Visit(node *ast.Node) {
3736
if _, ok := (*node).(*ast.IdentifierNode); ok {
3837
*node = &ast.NilNode{}
3938
}
4039
}
41-
func (p *patcher) Exit(node *ast.Node) {}
4240

4341
func TestWalk_patch(t *testing.T) {
4442
var node ast.Node

conf/operators.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ type OperatorPatcher struct {
3434
Types TypesTable
3535
}
3636

37-
func (p *OperatorPatcher) Enter(_ *ast.Node) {}
38-
func (p *OperatorPatcher) Exit(node *ast.Node) {
37+
func (p *OperatorPatcher) Visit(node *ast.Node) {
3938
binaryNode, ok := (*node).(*ast.BinaryNode)
4039
if !ok {
4140
return

docs/Visitor-and-Patch.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ type visitor struct {
2020
identifiers []string
2121
}
2222

23-
func (v *visitor) Enter(node *ast.Node) {}
24-
func (v *visitor) Exit(node *ast.Node) {
23+
func (v *visitor) Visit(node *ast.Node) {
2524
if n, ok := (*node).(*ast.IdentifierNode); ok {
2625
v.identifiers = append(v.identifiers, n.Value)
2726
}
@@ -82,8 +81,7 @@ func main() {
8281

8382
type patcher struct{}
8483

85-
func (p *patcher) Enter(_ *ast.Node) {}
86-
func (p *patcher) Exit(node *ast.Node) {
84+
func (p *patcher) Visit(node *ast.Node) {
8785
n, ok := (*node).(*ast.IndexNode)
8886
if !ok {
8987
return
@@ -148,8 +146,7 @@ var stringer = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
148146

149147
type stringerPatcher struct{}
150148

151-
func (p *stringerPatcher) Enter(_ *ast.Node) {}
152-
func (p *stringerPatcher) Exit(node *ast.Node) {
149+
func (p *stringerPatcher) Visit(node *ast.Node) {
153150
t := (*node).Type()
154151
if t == nil {
155152
return

expr_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,7 @@ func ExamplePatch() {
402402
/*
403403
type patcher struct{}
404404
405-
func (p *patcher) Enter(_ *ast.Node) {}
406-
func (p *patcher) Exit(node *ast.Node) {
405+
func (p *patcher) Visit(node *ast.Node) {
407406
switch n := (*node).(type) {
408407
case *ast.MemberNode:
409408
ast.Patch(node, &ast.CallNode{
@@ -1200,8 +1199,7 @@ var stringer = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
12001199

12011200
type stringerPatcher struct{}
12021201

1203-
func (p *stringerPatcher) Enter(_ *ast.Node) {}
1204-
func (p *stringerPatcher) Exit(node *ast.Node) {
1202+
func (p *stringerPatcher) Visit(node *ast.Node) {
12051203
t := (*node).Type()
12061204
if t == nil {
12071205
return
@@ -1234,8 +1232,7 @@ func TestPatch(t *testing.T) {
12341232

12351233
type lengthPatcher struct{}
12361234

1237-
func (p *lengthPatcher) Enter(_ *ast.Node) {}
1238-
func (p *lengthPatcher) Exit(node *ast.Node) {
1235+
func (p *lengthPatcher) Visit(node *ast.Node) {
12391236
switch n := (*node).(type) {
12401237
case *ast.MemberNode:
12411238
if prop, ok := n.Property.(*ast.StringNode); ok && prop.Value == "length" {
@@ -1601,8 +1598,7 @@ func (is) Nil(a interface{}) bool {
16011598

16021599
type patcher struct{}
16031600

1604-
func (p *patcher) Enter(_ *ast.Node) {}
1605-
func (p *patcher) Exit(node *ast.Node) {
1601+
func (p *patcher) Visit(node *ast.Node) {
16061602
switch n := (*node).(type) {
16071603
case *ast.MemberNode:
16081604
ast.Patch(node, &ast.CallNode{

optimizer/const_expr.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ type constExpr struct {
1717
fns map[string]reflect.Value
1818
}
1919

20-
func (*constExpr) Enter(*Node) {}
21-
func (c *constExpr) Exit(node *Node) {
20+
func (c *constExpr) Visit(node *Node) {
2221
defer func() {
2322
if r := recover(); r != nil {
2423
msg := fmt.Sprintf("%v", r)

optimizer/const_range.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66

77
type constRange struct{}
88

9-
func (*constRange) Enter(*Node) {}
10-
func (*constRange) Exit(node *Node) {
9+
func (*constRange) Visit(node *Node) {
1110
switch n := (*node).(type) {
1211
case *BinaryNode:
1312
if n.Operator == ".." {

optimizer/fold.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ type fold struct {
1313
err *file.Error
1414
}
1515

16-
func (*fold) Enter(*Node) {}
17-
func (fold *fold) Exit(node *Node) {
16+
func (fold *fold) Visit(node *Node) {
1817
patch := func(newNode Node) {
1918
fold.applied = true
2019
Patch(node, newNode)

optimizer/in_array.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import (
88

99
type inArray struct{}
1010

11-
func (*inArray) Enter(*Node) {}
12-
func (*inArray) Exit(node *Node) {
11+
func (*inArray) Visit(node *Node) {
1312
switch n := (*node).(type) {
1413
case *BinaryNode:
1514
if n.Operator == "in" || n.Operator == "not in" {

optimizer/in_range.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66

77
type inRange struct{}
88

9-
func (*inRange) Enter(*Node) {}
10-
func (*inRange) Exit(node *Node) {
9+
func (*inRange) Visit(node *Node) {
1110
switch n := (*node).(type) {
1211
case *BinaryNode:
1312
if n.Operator == "in" || n.Operator == "not in" {

0 commit comments

Comments
 (0)