php2go/mutatingwalker.go

55 lines
1.0 KiB
Go

package main
import (
// "fmt"
//"io/ioutil"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
//"github.com/z7zmey/php-parser/walker"
)
func walkMutate(n_ *node.Node, cb func(*node.Node)) {
// Mutate in-place, non recursive...
cb(n_)
// Walk on the mutated version
// We must reimplement .Walk() in order to get pointer behaviour
switch n := (*n_).(type) {
case *stmt.StmtList:
for i, _ := range n.Stmts {
walkMutate(&n.Stmts[i], cb)
}
case *node.Root:
for i, _ := range n.Stmts {
walkMutate(&n.Stmts[i], cb)
}
case *stmt.If:
walkMutate(&n.Cond, cb)
walkMutate(&n.Else, cb)
for i, _ := range n.ElseIf {
walkMutate(&n.ElseIf[i], cb)
}
walkMutate(&n.Stmt, cb)
case *stmt.Else:
walkMutate(&n.Stmt, cb)
case *stmt.ElseIf:
walkMutate(&n.Cond, cb)
walkMutate(&n.Stmt, cb)
default:
// FIXME need to implement Walk() for all node types, to ensure that
// we can perform mutation transforms in nested contexts
// bail
//panic(fmt.Sprintf("walkMutate: unimplemented type '%t'", n))
}
}