Skip to content

Commit

Permalink
Remove the index from the walk function, make it properly recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
bandesz committed Oct 24, 2018
1 parent 9460b96 commit db83f30
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
6 changes: 3 additions & 3 deletions ast/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ func AppendNode(n1, n2 parsley.Node) parsley.Node {
// Walkable is a generic interface to allow to apply a function on the node
// The Walk function should return true if the walk should be interrupted
type Walkable interface {
Walk(f func(i int, n parsley.Node) bool) bool
Walk(f func(n parsley.Node) bool) bool
}

// WalkNode applies the given function to the node
func WalkNode(node parsley.Node, f func(i int, n parsley.Node) bool) bool {
func WalkNode(node parsley.Node, f func(n parsley.Node) bool) bool {
switch n := node.(type) {
case Walkable:
return n.Walk(f)
default:
return f(0, node)
return f(node)
}
}

Expand Down
6 changes: 2 additions & 4 deletions ast/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ var _ = Describe("WalkNode", func() {

nl := ast.NodeList([]parsley.Node{n1, n2})
called := []parsley.Node{}
f := func(i int, n parsley.Node) bool {
Expect(i).To(Equal(len(called)))
f := func(n parsley.Node) bool {
called = append(called, n)
return false
}
Expand All @@ -80,8 +79,7 @@ var _ = Describe("WalkNode", func() {
node := &parsleyfakes.FakeNode{}
node.TokenReturns("TEST")
called := []parsley.Node{}
f := func(i int, n parsley.Node) bool {
Expect(i).To(Equal(len(called)))
f := func(n parsley.Node) bool {
called = append(called, n)
return false
}
Expand Down
12 changes: 6 additions & 6 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ func (n *NonTerminalNode) String() string {
}

// Walk runs the given function on all child nodes
func (n *NonTerminalNode) Walk(f func(i int, n parsley.Node) bool) bool {
for i, node := range n.children {
if f(i, node) {
func (n *NonTerminalNode) Walk(f func(n parsley.Node) bool) bool {
for _, n := range n.children {
if WalkNode(n, f) {
return true
}
}
Expand Down Expand Up @@ -231,9 +231,9 @@ func (nl *NodeList) Append(node parsley.Node) {
}

// Walk runs the given function on all nodes
func (nl NodeList) Walk(f func(i int, n parsley.Node) bool) bool {
for i, node := range nl {
if f(i, node) {
func (nl NodeList) Walk(f func(n parsley.Node) bool) bool {
for _, n := range nl {
if WalkNode(n, f) {
return true
}
}
Expand Down
18 changes: 7 additions & 11 deletions ast/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ var _ = Describe("NonTerminalNode", func() {
Describe("Walk", func() {
It("should call the function with all children", func() {
called := []parsley.Node{}
f := func(i int, n parsley.Node) bool {
Expect(i).To(Equal(len(called)))
f := func(n parsley.Node) bool {
called = append(called, n)
return false
}
Expand All @@ -219,10 +218,9 @@ var _ = Describe("NonTerminalNode", func() {

It("should stop if the function returns with true", func() {
called := []parsley.Node{}
f := func(i int, n parsley.Node) bool {
Expect(i).To(Equal(len(called)))
f := func(n parsley.Node) bool {
called = append(called, n)
return i == 0
return true
}

res := node.Walk(f)
Expand Down Expand Up @@ -417,8 +415,7 @@ var _ = Describe("NodeList", func() {
It("should call the function with all children", func() {
nl = ast.NodeList([]parsley.Node{n1, n2})
called := []parsley.Node{}
f := func(i int, n parsley.Node) bool {
Expect(i).To(Equal(len(called)))
f := func(n parsley.Node) bool {
called = append(called, n)
return false
}
Expand All @@ -432,16 +429,15 @@ var _ = Describe("NodeList", func() {
It("should stop if the function returns with true", func() {
nl = ast.NodeList([]parsley.Node{n1, n2, n3})
called := []parsley.Node{}
f := func(i int, n parsley.Node) bool {
Expect(i).To(Equal(len(called)))
f := func(n parsley.Node) bool {
called = append(called, n)
return i > 0
return true
}

res := nl.Walk(f)
Expect(res).To(BeTrue())

Expect(called).To(Equal([]parsley.Node{n1, n2}))
Expect(called).To(Equal([]parsley.Node{n1}))
})
})
})

0 comments on commit db83f30

Please sign in to comment.