-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
visit.go
55 lines (53 loc) · 1.15 KB
/
visit.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
package participle
import "fmt"
// Visit all nodes.
//
// Cycles are deliberately not detected, it is up to the visitor function to handle this.
func visit(n node, visitor func(n node, next func() error) error) error {
return visitor(n, func() error {
switch n := n.(type) {
case *disjunction:
for _, child := range n.nodes {
if err := visit(child, visitor); err != nil {
return err
}
}
return nil
case *strct:
return visit(n.expr, visitor)
case *custom:
return nil
case *union:
for _, member := range n.disjunction.nodes {
if err := visit(member, visitor); err != nil {
return err
}
}
return nil
case *sequence:
if err := visit(n.node, visitor); err != nil {
return err
}
if n.next != nil {
return visit(n.next, visitor)
}
return nil
case *parseable:
return nil
case *capture:
return visit(n.node, visitor)
case *reference:
return nil
case *negation:
return visit(n.node, visitor)
case *literal:
return nil
case *group:
return visit(n.expr, visitor)
case *lookaheadGroup:
return visit(n.expr, visitor)
default:
panic(fmt.Sprintf("%T", n))
}
})
}