mirror of
https://github.com/golang/go
synced 2024-11-22 04:24:39 -07:00
go/ast: add Inspect function for easy AST inspection w/o a visitor
R=rsc CC=golang-dev https://golang.org/cl/2770044
This commit is contained in:
parent
035696c59a
commit
9384fdc96a
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user