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

File diff suppressed because it is too large Load Diff

View File

@ -629,7 +629,11 @@ unticked_statement:
}
| T_SWITCH parenthesis_expr switch_case_list
{
$$ = stmt.NewSwitch($2, $3.nodes)
if ($3.endToken.Value == ";") {
$$ = stmt.NewAltSwitch($2, $3.nodes)
} else {
$$ = stmt.NewSwitch($2, $3.nodes)
}
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $3.endToken))
comments.AddComments($$, $1.Comments())
}

View File

@ -1400,7 +1400,7 @@ CAD;
},
},
},
&stmt.Switch{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
@ -1416,7 +1416,7 @@ CAD;
},
},
},
&stmt.Switch{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{

File diff suppressed because it is too large Load Diff

View File

@ -552,7 +552,11 @@ statement:
}
| T_SWITCH '(' expr ')' switch_case_list
{
$$ = stmt.NewSwitch($3, $5.nodes)
if ($5.endToken.Value == ";") {
$$ = stmt.NewAltSwitch($3, $5.nodes)
} else {
$$ = stmt.NewSwitch($3, $5.nodes)
}
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $5.endToken))
comments.AddComments($$, $1.Comments())
}

View File

@ -1423,7 +1423,7 @@ CAD;
},
},
},
&stmt.Switch{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
@ -1439,7 +1439,7 @@ CAD;
},
},
},
&stmt.Switch{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{