package main import ( "fmt" "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/walker" ) type miniWalker struct { cb func(node.Node) error lastErr error } func (mw *miniWalker) EnterNode(w walker.Walkable) bool { n, ok := w.(node.Node) if !ok { mw.lastErr = fmt.Errorf("Tried to walk non-node '%t'", w) return false } err := mw.cb(n) if err != nil { mw.lastErr = err return false } return true } func (mw *miniWalker) LeaveNode(w walker.Walkable) {} func (mw *miniWalker) EnterChildNode(key string, w walker.Walkable) {} func (mw *miniWalker) LeaveChildNode(key string, w walker.Walkable) {} func (mw *miniWalker) EnterChildList(key string, w walker.Walkable) {} func (mw *miniWalker) LeaveChildList(key string, w walker.Walkable) {} var _ walker.Visitor = &miniWalker{} // interface assertion func walk(n node.Node, cb func(node.Node) error) error { mw := miniWalker{cb: cb} n.Walk(&mw) return mw.lastErr }