split Foreach and AltForeach nodes
This commit is contained in:
BIN
node/stmt/debug.test
Executable file
BIN
node/stmt/debug.test
Executable file
Binary file not shown.
63
node/stmt/n_alt_foreach.go
Normal file
63
node/stmt/n_alt_foreach.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package stmt
|
||||
|
||||
import (
|
||||
"github.com/z7zmey/php-parser/node"
|
||||
"github.com/z7zmey/php-parser/walker"
|
||||
)
|
||||
|
||||
// AltForeach node
|
||||
type AltForeach struct {
|
||||
ByRef bool
|
||||
Expr node.Node
|
||||
Key node.Node
|
||||
Variable node.Node
|
||||
Stmt node.Node
|
||||
}
|
||||
|
||||
// NewAltForeach node constuctor
|
||||
func NewAltForeach(Expr node.Node, Key node.Node, Variable node.Node, Stmt node.Node, ByRef bool) *AltForeach {
|
||||
return &AltForeach{
|
||||
ByRef,
|
||||
Expr,
|
||||
Key,
|
||||
Variable,
|
||||
Stmt,
|
||||
}
|
||||
}
|
||||
|
||||
// Attributes returns node attributes as map
|
||||
func (n *AltForeach) Attributes() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"ByRef": n.ByRef,
|
||||
}
|
||||
}
|
||||
|
||||
// Walk traverses nodes
|
||||
// Walk is invoked recursively until v.EnterNode returns true
|
||||
func (n *AltForeach) Walk(v walker.Visitor) {
|
||||
if v.EnterNode(n) == false {
|
||||
return
|
||||
}
|
||||
|
||||
if n.Expr != nil {
|
||||
vv := v.GetChildrenVisitor("Expr")
|
||||
n.Expr.Walk(vv)
|
||||
}
|
||||
|
||||
if n.Key != nil {
|
||||
vv := v.GetChildrenVisitor("Key")
|
||||
n.Key.Walk(vv)
|
||||
}
|
||||
|
||||
if n.Variable != nil {
|
||||
vv := v.GetChildrenVisitor("Variable")
|
||||
n.Variable.Walk(vv)
|
||||
}
|
||||
|
||||
if n.Stmt != nil {
|
||||
vv := v.GetChildrenVisitor("Stmt")
|
||||
n.Stmt.Walk(vv)
|
||||
}
|
||||
|
||||
v.LeaveNode(n)
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func TestAltForeach(t *testing.T) {
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Foreach{
|
||||
&stmt.AltForeach{
|
||||
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
|
||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
|
||||
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
|
||||
|
||||
@@ -231,6 +231,17 @@ var nodesToTest = []struct {
|
||||
[]string{"Expr", "Key", "Variable", "Stmt"},
|
||||
map[string]interface{}{"ByRef": true},
|
||||
},
|
||||
{
|
||||
&stmt.AltForeach{
|
||||
ByRef: true,
|
||||
Expr: &stmt.Expression{},
|
||||
Key: &expr.Variable{},
|
||||
Variable: &expr.Variable{},
|
||||
Stmt: &stmt.StmtList{},
|
||||
},
|
||||
[]string{"Expr", "Key", "Variable", "Stmt"},
|
||||
map[string]interface{}{"ByRef": true},
|
||||
},
|
||||
{
|
||||
&stmt.Function{
|
||||
ReturnsRef: true,
|
||||
|
||||
Reference in New Issue
Block a user