split Switch and AltSwitch nodes
This commit is contained in:
parent
d0296f78e3
commit
e75a625528
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{
|
expected := &stmt.StmtList{
|
||||||
Stmts: []node.Node{
|
Stmts: []node.Node{
|
||||||
&stmt.Switch{
|
&stmt.AltSwitch{
|
||||||
Cond: &scalar.Lnumber{Value: "1"},
|
Cond: &scalar.Lnumber{Value: "1"},
|
||||||
Cases: []node.Node{
|
Cases: []node.Node{
|
||||||
&stmt.Case{
|
&stmt.Case{
|
||||||
@ -59,7 +59,7 @@ func TestAltSwitchSemicolon(t *testing.T) {
|
|||||||
|
|
||||||
expected := &stmt.StmtList{
|
expected := &stmt.StmtList{
|
||||||
Stmts: []node.Node{
|
Stmts: []node.Node{
|
||||||
&stmt.Switch{
|
&stmt.AltSwitch{
|
||||||
Cond: &scalar.Lnumber{Value: "1"},
|
Cond: &scalar.Lnumber{Value: "1"},
|
||||||
Cases: []node.Node{
|
Cases: []node.Node{
|
||||||
&stmt.Case{
|
&stmt.Case{
|
||||||
|
@ -366,6 +366,14 @@ var nodesToTest = []struct {
|
|||||||
[]string{"Cond", "Cases"},
|
[]string{"Cond", "Cases"},
|
||||||
map[string]interface{}{},
|
map[string]interface{}{},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
&stmt.AltSwitch{
|
||||||
|
Cond: &expr.Variable{},
|
||||||
|
Cases: []node.Node{&stmt.Expression{}},
|
||||||
|
},
|
||||||
|
[]string{"Cond", "Cases"},
|
||||||
|
map[string]interface{}{},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
&stmt.Throw{
|
&stmt.Throw{
|
||||||
Expr: &stmt.Expression{},
|
Expr: &stmt.Expression{},
|
||||||
|
930
php5/php5.go
930
php5/php5.go
File diff suppressed because it is too large
Load Diff
@ -629,7 +629,11 @@ unticked_statement:
|
|||||||
}
|
}
|
||||||
| T_SWITCH parenthesis_expr switch_case_list
|
| T_SWITCH parenthesis_expr switch_case_list
|
||||||
{
|
{
|
||||||
|
if ($3.endToken.Value == ";") {
|
||||||
|
$$ = stmt.NewAltSwitch($2, $3.nodes)
|
||||||
|
} else {
|
||||||
$$ = stmt.NewSwitch($2, $3.nodes)
|
$$ = stmt.NewSwitch($2, $3.nodes)
|
||||||
|
}
|
||||||
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $3.endToken))
|
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $3.endToken))
|
||||||
comments.AddComments($$, $1.Comments())
|
comments.AddComments($$, $1.Comments())
|
||||||
}
|
}
|
||||||
|
@ -1400,7 +1400,7 @@ CAD;
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&stmt.Switch{
|
&stmt.AltSwitch{
|
||||||
Cond: &scalar.Lnumber{Value: "1"},
|
Cond: &scalar.Lnumber{Value: "1"},
|
||||||
Cases: []node.Node{
|
Cases: []node.Node{
|
||||||
&stmt.Case{
|
&stmt.Case{
|
||||||
@ -1416,7 +1416,7 @@ CAD;
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&stmt.Switch{
|
&stmt.AltSwitch{
|
||||||
Cond: &scalar.Lnumber{Value: "1"},
|
Cond: &scalar.Lnumber{Value: "1"},
|
||||||
Cases: []node.Node{
|
Cases: []node.Node{
|
||||||
&stmt.Case{
|
&stmt.Case{
|
||||||
|
706
php7/php7.go
706
php7/php7.go
File diff suppressed because it is too large
Load Diff
@ -552,7 +552,11 @@ statement:
|
|||||||
}
|
}
|
||||||
| T_SWITCH '(' expr ')' switch_case_list
|
| T_SWITCH '(' expr ')' switch_case_list
|
||||||
{
|
{
|
||||||
|
if ($5.endToken.Value == ";") {
|
||||||
|
$$ = stmt.NewAltSwitch($3, $5.nodes)
|
||||||
|
} else {
|
||||||
$$ = stmt.NewSwitch($3, $5.nodes)
|
$$ = stmt.NewSwitch($3, $5.nodes)
|
||||||
|
}
|
||||||
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $5.endToken))
|
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $5.endToken))
|
||||||
comments.AddComments($$, $1.Comments())
|
comments.AddComments($$, $1.Comments())
|
||||||
}
|
}
|
||||||
|
@ -1423,7 +1423,7 @@ CAD;
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&stmt.Switch{
|
&stmt.AltSwitch{
|
||||||
Cond: &scalar.Lnumber{Value: "1"},
|
Cond: &scalar.Lnumber{Value: "1"},
|
||||||
Cases: []node.Node{
|
Cases: []node.Node{
|
||||||
&stmt.Case{
|
&stmt.Case{
|
||||||
@ -1439,7 +1439,7 @@ CAD;
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&stmt.Switch{
|
&stmt.AltSwitch{
|
||||||
Cond: &scalar.Lnumber{Value: "1"},
|
Cond: &scalar.Lnumber{Value: "1"},
|
||||||
Cases: []node.Node{
|
Cases: []node.Node{
|
||||||
&stmt.Case{
|
&stmt.Case{
|
||||||
|
Loading…
Reference in New Issue
Block a user