hcl/hclnew/ast/walk.go
Mitchell Hashimoto a1fbdda609 Add 'hclnew/' from commit '2e450506d2b521ae348f9bb97d1b07800177b83f'
git-subtree-dir: hclnew
git-subtree-mainline: 4de51957ef
git-subtree-split: 2e450506d2
2015-11-06 16:00:53 -08:00

43 lines
820 B
Go

package ast
import "fmt"
// Walk traverses an AST in depth-first order: It starts by calling fn(node);
// node must not be nil. If f returns true, Walk invokes f recursively for
// each of the non-nil children of node, followed by a call of f(nil).
func Walk(node Node, fn func(Node) bool) {
if !fn(node) {
return
}
switch n := node.(type) {
case *File:
Walk(n.Node, fn)
case *ObjectList:
for _, item := range n.Items {
Walk(item, fn)
}
case *ObjectKey:
// nothing to do
case *ObjectItem:
for _, k := range n.Keys {
Walk(k, fn)
}
Walk(n.Val, fn)
case *LiteralType:
// nothing to do
case *ListType:
for _, l := range n.List {
Walk(l, fn)
}
case *ObjectType:
for _, l := range n.List.Items {
Walk(l, fn)
}
default:
fmt.Printf(" unknown type: %T\n", n)
}
fn(nil)
}