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

@ -8,14 +8,14 @@ import (
// AltSwitch node
type AltSwitch struct {
Cond node.Node
Cases []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

@ -8,14 +8,14 @@ import (
// Switch node
type Switch struct {
Cond node.Node
Cases []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,6 +25,7 @@ func TestAltSwitch(t *testing.T) {
Stmts: []node.Node{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -40,6 +41,7 @@ func TestAltSwitch(t *testing.T) {
},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -65,6 +67,7 @@ func TestAltSwitchSemicolon(t *testing.T) {
Stmts: []node.Node{
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -77,6 +80,7 @@ func TestAltSwitchSemicolon(t *testing.T) {
},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -102,6 +106,7 @@ func TestSwitch(t *testing.T) {
Stmts: []node.Node{
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -118,6 +123,7 @@ func TestSwitch(t *testing.T) {
},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
@ -143,6 +149,7 @@ func TestSwitchSemicolon(t *testing.T) {
Stmts: []node.Node{
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -159,6 +166,7 @@ func TestSwitchSemicolon(t *testing.T) {
},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")

View File

@ -361,17 +361,17 @@ var nodesToTest = []struct {
{
&stmt.Switch{
Cond: &expr.Variable{},
Cases: []node.Node{&stmt.Expression{}},
CaseList: &stmt.CaseList{},
},
[]string{"Cond", "Cases"},
[]string{"Cond", "CaseList"},
map[string]interface{}{},
},
{
&stmt.AltSwitch{
Cond: &expr.Variable{},
Cases: []node.Node{&stmt.Expression{}},
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{}},

File diff suppressed because it is too large Load Diff

View File

@ -244,6 +244,7 @@ import (
%type <node> static_scalar_value static_operation
%type <node> ctor_arguments function_call_parameter_list
%type <node> trait_adaptations
%type <node> switch_case_list
%type <list> top_statement_list namespace_name use_declarations use_function_declarations use_const_declarations
%type <list> inner_statement_list global_var_list static_var_list encaps_list isset_variables non_empty_array_pair_list
@ -260,7 +261,7 @@ import (
%type <simpleIndirectReference> simple_indirect_reference
%type <foreachVariable> foreach_variable foreach_optional_arg
%type <nodesWithEndToken> switch_case_list method_body
%type <nodesWithEndToken> method_body
%type <boolWithToken> is_reference is_variadic
%type <altSyntaxNode> while_statement for_statement foreach_statement
@ -684,12 +685,18 @@ unticked_statement:
}
| T_SWITCH parenthesis_expr switch_case_list
{
if ($3.endToken.Value == ";") {
$$ = stmt.NewAltSwitch($2, $3.nodes)
} else {
$$ = stmt.NewSwitch($2, $3.nodes)
switch n := $3.(type) {
case *stmt.Switch:
n.Cond = $2
case *stmt.AltSwitch:
n.Cond = $2
default:
panic("unexpected node type")
}
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3.endToken))
$$ = $3
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $3))
yylex.(*Parser).comments.AddComments($$, $1.Comments())
}
| T_BREAK ';'
@ -1153,13 +1160,38 @@ declare_list:
switch_case_list:
'{' case_list '}'
{ $$ = &nodesWithEndToken{$2, $3} }
{
caseList := stmt.NewCaseList($2)
$$ = stmt.NewSwitch(nil, caseList)
yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3))
}
| '{' ';' case_list '}'
{ $$ = &nodesWithEndToken{$3, $4} }
{
caseList := stmt.NewCaseList($3)
$$ = stmt.NewSwitch(nil, caseList)
yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4))
}
| ':' case_list T_ENDSWITCH ';'
{ $$ = &nodesWithEndToken{$2, $4} }
{
caseList := stmt.NewCaseList($2)
$$ = stmt.NewAltSwitch(nil, caseList)
yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewNodeListPosition($2))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4))
}
| ':' ';' case_list T_ENDSWITCH ';'
{ $$ = &nodesWithEndToken{$3, $5} }
{
caseList := stmt.NewCaseList($3)
$$ = stmt.NewAltSwitch(nil, caseList)
yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewNodeListPosition($3))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $5))
}
;

View File

@ -1354,6 +1354,7 @@ func TestPhp5(t *testing.T) {
},
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -1368,8 +1369,10 @@ func TestPhp5(t *testing.T) {
},
},
},
},
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -1381,8 +1384,10 @@ func TestPhp5(t *testing.T) {
},
},
},
},
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -1398,8 +1403,10 @@ func TestPhp5(t *testing.T) {
},
},
},
},
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -1415,6 +1422,7 @@ func TestPhp5(t *testing.T) {
},
},
},
},
&stmt.Throw{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
},

File diff suppressed because it is too large Load Diff

View File

@ -265,12 +265,13 @@ import (
%type <node> class_modifier
%type <node> argument_list ctor_arguments
%type <node> trait_adaptations
%type <node> switch_case_list
%type <node> member_modifier
%type <node> use_type
%type <foreachVariable> foreach_variable
%type <nodesWithEndToken> method_body switch_case_list
%type <nodesWithEndToken> method_body
%type <list> encaps_list backticks_expr namespace_name catch_name_list catch_list class_const_list
%type <list> const_list echo_expr_list for_exprs non_empty_for_exprs global_var_list
@ -823,14 +824,19 @@ statement:
}
| T_SWITCH '(' expr ')' switch_case_list
{
if ($5.endToken.Value == ";") {
$$ = stmt.NewAltSwitch($3, $5.nodes)
} else {
$$ = stmt.NewSwitch($3, $5.nodes)
switch n := $5.(type) {
case *stmt.Switch:
n.Cond = $3
case *stmt.AltSwitch:
n.Cond = $3
default:
panic("unexpected node type")
}
$$ = $5
// save position
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $5.endToken))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokenNodePosition($1, $5))
// save comments
yylex.(*Parser).comments.AddFromToken($$, $1, comment.SwitchToken)
@ -1399,10 +1405,63 @@ declare_statement:
;
switch_case_list:
'{' case_list '}' { $$ = &nodesWithEndToken{$2, $3} }
| '{' ';' case_list '}' { $$ = &nodesWithEndToken{$3, $4} }
| ':' case_list T_ENDSWITCH ';' { $$ = &nodesWithEndToken{$2, $4} }
| ':' ';' case_list T_ENDSWITCH ';' { $$ = &nodesWithEndToken{$3, $5} }
'{' case_list '}'
{
caseList := stmt.NewCaseList($2)
$$ = stmt.NewSwitch(nil, caseList)
// save position
yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $3))
// save comments
yylex.(*Parser).comments.AddFromToken(caseList, $1, comment.OpenCurlyBracesToken)
yylex.(*Parser).comments.AddFromToken(caseList, $3, comment.CloseCurlyBracesToken)
}
| '{' ';' case_list '}'
{
caseList := stmt.NewCaseList($3)
$$ = stmt.NewSwitch(nil, caseList)
// save position
yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4))
// save comments
yylex.(*Parser).comments.AddFromToken(caseList, $1, comment.OpenCurlyBracesToken)
yylex.(*Parser).comments.AddFromToken(caseList, $2, comment.SemiColonToken)
yylex.(*Parser).comments.AddFromToken(caseList, $4, comment.CloseCurlyBracesToken)
}
| ':' case_list T_ENDSWITCH ';'
{
caseList := stmt.NewCaseList($2)
$$ = stmt.NewAltSwitch(nil, caseList)
// save position
yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewNodeListPosition($2))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $4))
// save comments
yylex.(*Parser).comments.AddFromToken(caseList, $1, comment.ColonToken)
yylex.(*Parser).comments.AddFromToken(caseList, $3, comment.EndswitchToken)
yylex.(*Parser).comments.AddFromToken($$, $4, comment.SemiColonToken)
}
| ':' ';' case_list T_ENDSWITCH ';'
{
caseList := stmt.NewCaseList($3)
$$ = stmt.NewAltSwitch(nil, caseList)
// save position
yylex.(*Parser).positions.AddPosition(caseList, yylex.(*Parser).positionBuilder.NewNodeListPosition($3))
yylex.(*Parser).positions.AddPosition($$, yylex.(*Parser).positionBuilder.NewTokensPosition($1, $5))
// save comments
yylex.(*Parser).comments.AddFromToken(caseList, $1, comment.ColonToken)
yylex.(*Parser).comments.AddFromToken(caseList, $2, comment.SemiColonToken)
yylex.(*Parser).comments.AddFromToken(caseList, $4, comment.EndswitchToken)
yylex.(*Parser).comments.AddFromToken($$, $5, comment.SemiColonToken)
}
;
case_list:

View File

@ -1381,6 +1381,7 @@ func TestPhp7(t *testing.T) {
},
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -1395,8 +1396,10 @@ func TestPhp7(t *testing.T) {
},
},
},
},
&stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -1408,8 +1411,10 @@ func TestPhp7(t *testing.T) {
},
},
},
},
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -1425,8 +1430,10 @@ func TestPhp7(t *testing.T) {
},
},
},
},
&stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.Lnumber{Value: "1"},
@ -1442,6 +1449,7 @@ func TestPhp7(t *testing.T) {
},
},
},
},
&stmt.Throw{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "e"}},
},

View File

@ -1428,7 +1428,7 @@ func (p *Printer) printStmtAltSwitch(n node.Node) {
p.Print(nn.Cond)
io.WriteString(p.w, ") :\n")
s := nn.Cases
s := nn.CaseList.Cases
p.printNodes(s)
io.WriteString(p.w, "\n")
@ -2009,7 +2009,7 @@ func (p *Printer) printStmtSwitch(n node.Node) {
io.WriteString(p.w, ")")
io.WriteString(p.w, " {\n")
p.printNodes(nn.Cases)
p.printNodes(nn.CaseList.Cases)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "}")

View File

@ -2284,6 +2284,7 @@ func TestPrintStmtAltSwitch(t *testing.T) {
Stmts: []node.Node{
&stmt.AltSwitch{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.String{Value: "'a'"},
@ -2300,6 +2301,7 @@ func TestPrintStmtAltSwitch(t *testing.T) {
},
},
},
},
})
expected := `namespace {
@ -3649,6 +3651,7 @@ func TestPrintStmtSwitch(t *testing.T) {
Stmts: []node.Node{
&stmt.Switch{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
CaseList: &stmt.CaseList{
Cases: []node.Node{
&stmt.Case{
Cond: &scalar.String{Value: "'a'"},
@ -3665,6 +3668,7 @@ func TestPrintStmtSwitch(t *testing.T) {
},
},
},
},
})
expected := `{