split Switch and AltSwitch nodes
This commit is contained in:
49
node/stmt/n_alt_switch.go
Normal file
49
node/stmt/n_alt_switch.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package stmt
|
||||
|
||||
import (
|
||||
"github.com/z7zmey/php-parser/node"
|
||||
"github.com/z7zmey/php-parser/walker"
|
||||
)
|
||||
|
||||
// AltSwitch node
|
||||
type AltSwitch struct {
|
||||
Cond node.Node
|
||||
Cases []node.Node
|
||||
}
|
||||
|
||||
// NewAltSwitch node constuctor
|
||||
func NewAltSwitch(Cond node.Node, Cases []node.Node) *AltSwitch {
|
||||
return &AltSwitch{
|
||||
Cond,
|
||||
Cases,
|
||||
}
|
||||
}
|
||||
|
||||
// Attributes returns node attributes as map
|
||||
func (n *AltSwitch) Attributes() map[string]interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Walk traverses nodes
|
||||
// Walk is invoked recursively until v.EnterNode returns true
|
||||
func (n *AltSwitch) Walk(v walker.Visitor) {
|
||||
if v.EnterNode(n) == false {
|
||||
return
|
||||
}
|
||||
|
||||
if n.Cond != nil {
|
||||
vv := v.GetChildrenVisitor("Cond")
|
||||
n.Cond.Walk(vv)
|
||||
}
|
||||
|
||||
if n.Cases != nil {
|
||||
vv := v.GetChildrenVisitor("Cases")
|
||||
for _, nn := range n.Cases {
|
||||
if nn != nil {
|
||||
nn.Walk(vv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
v.LeaveNode(n)
|
||||
}
|
||||
@@ -23,7 +23,7 @@ func TestAltSwitch(t *testing.T) {
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Switch{
|
||||
&stmt.AltSwitch{
|
||||
Cond: &scalar.Lnumber{Value: "1"},
|
||||
Cases: []node.Node{
|
||||
&stmt.Case{
|
||||
@@ -59,7 +59,7 @@ func TestAltSwitchSemicolon(t *testing.T) {
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Switch{
|
||||
&stmt.AltSwitch{
|
||||
Cond: &scalar.Lnumber{Value: "1"},
|
||||
Cases: []node.Node{
|
||||
&stmt.Case{
|
||||
|
||||
@@ -366,6 +366,14 @@ var nodesToTest = []struct {
|
||||
[]string{"Cond", "Cases"},
|
||||
map[string]interface{}{},
|
||||
},
|
||||
{
|
||||
&stmt.AltSwitch{
|
||||
Cond: &expr.Variable{},
|
||||
Cases: []node.Node{&stmt.Expression{}},
|
||||
},
|
||||
[]string{"Cond", "Cases"},
|
||||
map[string]interface{}{},
|
||||
},
|
||||
{
|
||||
&stmt.Throw{
|
||||
Expr: &stmt.Expression{},
|
||||
|
||||
Reference in New Issue
Block a user