5 // WalkFunc describes a function to be called for each node during a Walk. The
6 // returned node can be used to rewrite the AST. Walking stops the returned
8 type WalkFunc func(Node) (Node, bool)
10 // Walk traverses an AST in depth-first order: It starts by calling fn(node);
11 // node must not be nil. If fn returns true, Walk invokes fn recursively for
12 // each of the non-nil children of node, followed by a call of fn(nil). The
13 // returned node of fn can be used to rewrite the passed node to fn.
14 func Walk(node Node, fn WalkFunc) Node {
15 rewritten, ok := fn(node)
20 switch n := node.(type) {
22 n.Node = Walk(n.Node, fn)
24 for i, item := range n.Items {
25 n.Items[i] = Walk(item, fn).(*ObjectItem)
30 for i, k := range n.Keys {
31 n.Keys[i] = Walk(k, fn).(*ObjectKey)
35 n.Val = Walk(n.Val, fn)
40 for i, l := range n.List {
41 n.List[i] = Walk(l, fn)
44 n.List = Walk(n.List, fn).(*ObjectList)
46 // should we panic here?
47 fmt.Printf("unknown type: %T\n", n)