create CaseList node

This commit is contained in:
z7zmey
2018-04-29 23:10:56 +03:00
parent f8d9d6d7c2
commit d1b0cebf9a
13 changed files with 1578 additions and 1349 deletions

View File

@@ -7,15 +7,15 @@ import (
// AltSwitch node
type AltSwitch struct {
Cond node.Node
Cases []node.Node
Cond node.Node
CaseList *CaseList
}
// NewAltSwitch node constructor
func NewAltSwitch(Cond node.Node, Cases []node.Node) *AltSwitch {
func NewAltSwitch(Cond node.Node, CaseList *CaseList) *AltSwitch {
return &AltSwitch{
Cond,
Cases,
CaseList,
}
}
@@ -36,13 +36,9 @@ func (n *AltSwitch) Walk(v walker.Visitor) {
n.Cond.Walk(vv)
}
if n.Cases != nil {
vv := v.GetChildrenVisitor("Cases")
for _, nn := range n.Cases {
if nn != nil {
nn.Walk(vv)
}
}
if n.CaseList != nil {
vv := v.GetChildrenVisitor("CaseList")
n.CaseList.Walk(vv)
}
v.LeaveNode(n)

42
node/stmt/n_case_list.go Normal file
View File

@@ -0,0 +1,42 @@
package stmt
import (
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/walker"
)
// CaseList node
type CaseList struct {
Cases []node.Node
}
// NewCaseList node constructor
func NewCaseList(Cases []node.Node) *CaseList {
return &CaseList{
Cases,
}
}
// Attributes returns node attributes as map
func (n *CaseList) Attributes() map[string]interface{} {
return nil
}
// Walk traverses nodes
// Walk is invoked recursively until v.EnterNode returns true
func (n *CaseList) Walk(v walker.Visitor) {
if v.EnterNode(n) == false {
return
}
if n.Cases != nil {
vv := v.GetChildrenVisitor("Cases")
for _, nn := range n.Cases {
if nn != nil {
nn.Walk(vv)
}
}
}
v.LeaveNode(n)
}

View File

@@ -7,15 +7,15 @@ import (
// Switch node
type Switch struct {
Cond node.Node
Cases []node.Node
Cond node.Node
CaseList *CaseList
}
// NewSwitch node constructor
func NewSwitch(Cond node.Node, Cases []node.Node) *Switch {
func NewSwitch(Cond node.Node, CaseList *CaseList) *Switch {
return &Switch{
Cond,
Cases,
CaseList,
}
}
@@ -36,13 +36,9 @@ func (n *Switch) Walk(v walker.Visitor) {
n.Cond.Walk(vv)
}
if n.Cases != nil {
vv := v.GetChildrenVisitor("Cases")
for _, nn := range n.Cases {
if nn != nil {
nn.Walk(vv)
}
}
if n.CaseList != nil {
vv := v.GetChildrenVisitor("CaseList")
n.CaseList.Walk(vv)
}
v.LeaveNode(n)

View File

@@ -25,17 +25,19 @@ func TestAltSwitch(t *testing.T) {
Stmts: []node.Node{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{},
},
&stmt.Default{
Stmts: []node.Node{},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{},
},
&stmt.Default{
Stmts: []node.Node{},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
},
},
},
},
@@ -65,14 +67,16 @@ func TestAltSwitchSemicolon(t *testing.T) {
Stmts: []node.Node{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
},
},
},
},
@@ -102,17 +106,19 @@ func TestSwitch(t *testing.T) {
Stmts: []node.Node{
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{
&stmt.Break{},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{
&stmt.Break{},
},
},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{
&stmt.Break{},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{
&stmt.Break{},
},
},
},
},
@@ -143,17 +149,19 @@ func TestSwitchSemicolon(t *testing.T) {
Stmts: []node.Node{
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{
&stmt.Break{},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
Stmts: []node.Node{
&stmt.Break{},
},
},
},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{
&stmt.Break{},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{
&stmt.Break{},
},
},
},
},

View File

@@ -360,18 +360,18 @@ var nodesToTest = []struct {
},
{
&stmt.Switch{
Cond: &expr.Variable{},
Cases: []node.Node{&stmt.Expression{}},
Cond: &expr.Variable{},
CaseList: &stmt.CaseList{},
},
[]string{"Cond", "Cases"},
[]string{"Cond", "CaseList"},
map[string]interface{}{},
},
{
&stmt.AltSwitch{
Cond: &expr.Variable{},
Cases: []node.Node{&stmt.Expression{}},
Cond: &expr.Variable{},
CaseList: &stmt.CaseList{Cases: []node.Node{}},
},
[]string{"Cond", "Cases"},
[]string{"Cond", "CaseList"},
map[string]interface{}{},
},
{
@@ -479,6 +479,13 @@ var nodesToTest = []struct {
[]string{"Stmts"},
map[string]interface{}{},
},
{
&stmt.CaseList{
Cases: []node.Node{&stmt.Expression{}},
},
[]string{"Cases"},
map[string]interface{}{},
},
{
&stmt.TraitAdaptationList{
Adaptations: []node.Node{&stmt.TraitUsePrecedence{}},