split For and AltFor nodes
This commit is contained in:
71
node/stmt/n_alt_for.go
Normal file
71
node/stmt/n_alt_for.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package stmt
|
||||
|
||||
import (
|
||||
"github.com/z7zmey/php-parser/node"
|
||||
"github.com/z7zmey/php-parser/walker"
|
||||
)
|
||||
|
||||
// AltFor node
|
||||
type AltFor struct {
|
||||
Init []node.Node
|
||||
Cond []node.Node
|
||||
Loop []node.Node
|
||||
Stmt node.Node
|
||||
}
|
||||
|
||||
// NewAltFor node constuctor
|
||||
func NewAltFor(Init []node.Node, Cond []node.Node, Loop []node.Node, Stmt node.Node) *AltFor {
|
||||
return &AltFor{
|
||||
Init,
|
||||
Cond,
|
||||
Loop,
|
||||
Stmt,
|
||||
}
|
||||
}
|
||||
|
||||
// Attributes returns node attributes as map
|
||||
func (n *AltFor) Attributes() map[string]interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Walk traverses nodes
|
||||
// Walk is invoked recursively until v.EnterNode returns true
|
||||
func (n *AltFor) Walk(v walker.Visitor) {
|
||||
if v.EnterNode(n) == false {
|
||||
return
|
||||
}
|
||||
|
||||
if n.Init != nil {
|
||||
vv := v.GetChildrenVisitor("Init")
|
||||
for _, nn := range n.Init {
|
||||
if nn != nil {
|
||||
nn.Walk(vv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if n.Cond != nil {
|
||||
vv := v.GetChildrenVisitor("Cond")
|
||||
for _, nn := range n.Cond {
|
||||
if nn != nil {
|
||||
nn.Walk(vv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if n.Loop != nil {
|
||||
vv := v.GetChildrenVisitor("Loop")
|
||||
for _, nn := range n.Loop {
|
||||
if nn != nil {
|
||||
nn.Walk(vv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if n.Stmt != nil {
|
||||
vv := v.GetChildrenVisitor("Stmt")
|
||||
n.Stmt.Walk(vv)
|
||||
}
|
||||
|
||||
v.LeaveNode(n)
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func TestAltFor(t *testing.T) {
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.For{
|
||||
&stmt.AltFor{
|
||||
Cond: []node.Node{
|
||||
&binary_op.Smaller{
|
||||
Left: &expr.Variable{VarName: &node.Identifier{Value: "$i"}},
|
||||
|
||||
@@ -210,6 +210,16 @@ var nodesToTest = []struct {
|
||||
[]string{"Init", "Cond", "Loop", "Stmt"},
|
||||
map[string]interface{}{},
|
||||
},
|
||||
{
|
||||
&stmt.AltFor{
|
||||
Init: []node.Node{&stmt.Expression{}},
|
||||
Cond: []node.Node{&stmt.Expression{}},
|
||||
Loop: []node.Node{&stmt.Expression{}},
|
||||
Stmt: &stmt.StmtList{},
|
||||
},
|
||||
[]string{"Init", "Cond", "Loop", "Stmt"},
|
||||
map[string]interface{}{},
|
||||
},
|
||||
{
|
||||
&stmt.Foreach{
|
||||
ByRef: true,
|
||||
|
||||
Reference in New Issue
Block a user