]> Cypherpunks repositories - gostls13.git/commitdiff
go/ast: add Inspect function for easy AST inspection w/o a visitor
authorRobert Griesemer <gri@golang.org>
Mon, 1 Nov 2010 22:06:34 +0000 (15:06 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 1 Nov 2010 22:06:34 +0000 (15:06 -0700)
R=rsc
CC=golang-dev
https://golang.org/cl/2770044

src/pkg/go/ast/walk.go

index 6c9837a01d546e243b65a117d7a3a4530db6819e..296da5652de700eb8726853de37fc3535188adfb 100644 (file)
@@ -321,3 +321,22 @@ func Walk(v Visitor, node interface{}) {
 
        v.Visit(nil)
 }
+
+
+type inspector func(node interface{}) bool
+
+func (f inspector) Visit(node interface{}) Visitor {
+       if node != nil && f(node) {
+               return f
+       }
+       return nil
+}
+
+
+// Inspect traverses an AST in depth-first order: If node != nil, it
+// invokes f(node). If f returns true, inspect invokes f for all the
+// non-nil children of node, recursively.
+//
+func Inspect(ast interface{}, f func(node interface{}) bool) {
+       Walk(inspector(f), ast)
+}