split Switch and AltSwitch nodes

This commit is contained in:
z7zmey
2018-02-18 20:39:41 +02:00
parent d0296f78e3
commit e75a625528
9 changed files with 897 additions and 824 deletions

49
node/stmt/n_alt_switch.go Normal file
View 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)
}

View File

@@ -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{

View File

@@ -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{},