From 2fdd3a919c4ee11182d0831b84456668cdeb7850 Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 15 Apr 2020 19:05:55 +1200 Subject: [PATCH] move miniwalker to parseutil package --- miniwalker.go | 44 --------------------------------------- parseutil/SimpleWalker.go | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 44 deletions(-) delete mode 100644 miniwalker.go create mode 100644 parseutil/SimpleWalker.go diff --git a/miniwalker.go b/miniwalker.go deleted file mode 100644 index 71b5414..0000000 --- a/miniwalker.go +++ /dev/null @@ -1,44 +0,0 @@ -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 -} diff --git a/parseutil/SimpleWalker.go b/parseutil/SimpleWalker.go new file mode 100644 index 0000000..80d1d5c --- /dev/null +++ b/parseutil/SimpleWalker.go @@ -0,0 +1,44 @@ +package parseutil + +import ( + "fmt" + + "github.com/z7zmey/php-parser/node" + "github.com/z7zmey/php-parser/walker" +) + +type simpleWalker struct { + cb func(node.Node) error + lastErr error +} + +func (mw *simpleWalker) 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 *simpleWalker) LeaveNode(w walker.Walkable) {} +func (mw *simpleWalker) EnterChildNode(key string, w walker.Walkable) {} +func (mw *simpleWalker) LeaveChildNode(key string, w walker.Walkable) {} +func (mw *simpleWalker) EnterChildList(key string, w walker.Walkable) {} +func (mw *simpleWalker) LeaveChildList(key string, w walker.Walkable) {} + +var _ walker.Visitor = &simpleWalker{} // interface assertion + +func SimpleWalk(n node.Node, cb func(node.Node) error) error { + mw := simpleWalker{cb: cb} + n.Walk(&mw) + + return mw.lastErr +}