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

View File

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

View File

@ -360,18 +360,18 @@ var nodesToTest = []struct {
}, },
{ {
&stmt.Switch{ &stmt.Switch{
Cond: &expr.Variable{}, Cond: &expr.Variable{},
Cases: []node.Node{&stmt.Expression{}}, CaseList: &stmt.CaseList{},
}, },
[]string{"Cond", "Cases"}, []string{"Cond", "CaseList"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{ {
&stmt.AltSwitch{ &stmt.AltSwitch{
Cond: &expr.Variable{}, Cond: &expr.Variable{},
Cases: []node.Node{&stmt.Expression{}}, CaseList: &stmt.CaseList{Cases: []node.Node{}},
}, },
[]string{"Cond", "Cases"}, []string{"Cond", "CaseList"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{ {
@ -479,6 +479,13 @@ var nodesToTest = []struct {
[]string{"Stmts"}, []string{"Stmts"},
map[string]interface{}{}, map[string]interface{}{},
}, },
{
&stmt.CaseList{
Cases: []node.Node{&stmt.Expression{}},
},
[]string{"Cases"},
map[string]interface{}{},
},
{ {
&stmt.TraitAdaptationList{ &stmt.TraitAdaptationList{
Adaptations: []node.Node{&stmt.TraitUsePrecedence{}}, 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> static_scalar_value static_operation
%type <node> ctor_arguments function_call_parameter_list %type <node> ctor_arguments function_call_parameter_list
%type <node> trait_adaptations %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> 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 %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 <simpleIndirectReference> simple_indirect_reference
%type <foreachVariable> foreach_variable foreach_optional_arg %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 <boolWithToken> is_reference is_variadic
%type <altSyntaxNode> while_statement for_statement foreach_statement %type <altSyntaxNode> while_statement for_statement foreach_statement
@ -684,12 +685,18 @@ unticked_statement:
} }
| T_SWITCH parenthesis_expr switch_case_list | T_SWITCH parenthesis_expr switch_case_list
{ {
if ($3.endToken.Value == ";") { switch n := $3.(type) {
$$ = stmt.NewAltSwitch($2, $3.nodes) case *stmt.Switch:
} else { n.Cond = $2
$$ = stmt.NewSwitch($2, $3.nodes) 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()) yylex.(*Parser).comments.AddComments($$, $1.Comments())
} }
| T_BREAK ';' | T_BREAK ';'
@ -1153,13 +1160,38 @@ declare_list:
switch_case_list: switch_case_list:
'{' 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 '}' | '{' ';' 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 ';' | ':' 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 ';' | ':' ';' 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,63 +1354,71 @@ func TestPhp5(t *testing.T) {
}, },
&stmt.AltSwitch{ &stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{ CaseList: &stmt.CaseList{
&stmt.Case{ Cases: []node.Node{
Cond: &scalar.Lnumber{Value: "1"}, &stmt.Case{
Stmts: []node.Node{}, Cond: &scalar.Lnumber{Value: "1"},
}, Stmts: []node.Node{},
&stmt.Default{ },
Stmts: []node.Node{}, &stmt.Default{
}, Stmts: []node.Node{},
&stmt.Case{ },
Cond: &scalar.Lnumber{Value: "2"}, &stmt.Case{
Stmts: []node.Node{}, Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
},
}, },
}, },
}, },
&stmt.AltSwitch{ &stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{ CaseList: &stmt.CaseList{
&stmt.Case{ Cases: []node.Node{
Cond: &scalar.Lnumber{Value: "1"}, &stmt.Case{
Stmts: []node.Node{}, Cond: &scalar.Lnumber{Value: "1"},
}, Stmts: []node.Node{},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
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{},
}, },
}, &stmt.Case{
&stmt.Case{ Cond: &scalar.Lnumber{Value: "2"},
Cond: &scalar.Lnumber{Value: "2"}, Stmts: []node.Node{},
Stmts: []node.Node{
&stmt.Break{},
}, },
}, },
}, },
}, },
&stmt.Switch{ &stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{ CaseList: &stmt.CaseList{
&stmt.Case{ Cases: []node.Node{
Cond: &scalar.Lnumber{Value: "1"}, &stmt.Case{
Stmts: []node.Node{ Cond: &scalar.Lnumber{Value: "1"},
&stmt.Break{}, 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.Switch{
&stmt.Break{}, Cond: &scalar.Lnumber{Value: "1"},
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{},
},
}, },
}, },
}, },

File diff suppressed because it is too large Load Diff

View File

@ -265,12 +265,13 @@ import (
%type <node> class_modifier %type <node> class_modifier
%type <node> argument_list ctor_arguments %type <node> argument_list ctor_arguments
%type <node> trait_adaptations %type <node> trait_adaptations
%type <node> switch_case_list
%type <node> member_modifier %type <node> member_modifier
%type <node> use_type %type <node> use_type
%type <foreachVariable> foreach_variable %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> 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 %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 | T_SWITCH '(' expr ')' switch_case_list
{ {
if ($5.endToken.Value == ";") { switch n := $5.(type) {
$$ = stmt.NewAltSwitch($3, $5.nodes) case *stmt.Switch:
} else { n.Cond = $3
$$ = stmt.NewSwitch($3, $5.nodes) case *stmt.AltSwitch:
n.Cond = $3
default:
panic("unexpected node type")
} }
$$ = $5
// save position // 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 // save comments
yylex.(*Parser).comments.AddFromToken($$, $1, comment.SwitchToken) yylex.(*Parser).comments.AddFromToken($$, $1, comment.SwitchToken)
@ -1399,10 +1405,63 @@ declare_statement:
; ;
switch_case_list: switch_case_list:
'{' case_list '}' { $$ = &nodesWithEndToken{$2, $3} } '{' case_list '}'
| '{' ';' case_list '}' { $$ = &nodesWithEndToken{$3, $4} } {
| ':' case_list T_ENDSWITCH ';' { $$ = &nodesWithEndToken{$2, $4} } caseList := stmt.NewCaseList($2)
| ':' ';' case_list T_ENDSWITCH ';' { $$ = &nodesWithEndToken{$3, $5} } $$ = 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: case_list:

View File

@ -1381,63 +1381,71 @@ func TestPhp7(t *testing.T) {
}, },
&stmt.AltSwitch{ &stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{ CaseList: &stmt.CaseList{
&stmt.Case{ Cases: []node.Node{
Cond: &scalar.Lnumber{Value: "1"}, &stmt.Case{
Stmts: []node.Node{}, Cond: &scalar.Lnumber{Value: "1"},
}, Stmts: []node.Node{},
&stmt.Default{ },
Stmts: []node.Node{}, &stmt.Default{
}, Stmts: []node.Node{},
&stmt.Case{ },
Cond: &scalar.Lnumber{Value: "2"}, &stmt.Case{
Stmts: []node.Node{}, Cond: &scalar.Lnumber{Value: "2"},
Stmts: []node.Node{},
},
}, },
}, },
}, },
&stmt.AltSwitch{ &stmt.AltSwitch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{ CaseList: &stmt.CaseList{
&stmt.Case{ Cases: []node.Node{
Cond: &scalar.Lnumber{Value: "1"}, &stmt.Case{
Stmts: []node.Node{}, Cond: &scalar.Lnumber{Value: "1"},
}, Stmts: []node.Node{},
&stmt.Case{
Cond: &scalar.Lnumber{Value: "2"},
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{},
}, },
}, &stmt.Case{
&stmt.Case{ Cond: &scalar.Lnumber{Value: "2"},
Cond: &scalar.Lnumber{Value: "2"}, Stmts: []node.Node{},
Stmts: []node.Node{
&stmt.Break{},
}, },
}, },
}, },
}, },
&stmt.Switch{ &stmt.Switch{
Cond: &scalar.Lnumber{Value: "1"}, Cond: &scalar.Lnumber{Value: "1"},
Cases: []node.Node{ CaseList: &stmt.CaseList{
&stmt.Case{ Cases: []node.Node{
Cond: &scalar.Lnumber{Value: "1"}, &stmt.Case{
Stmts: []node.Node{ Cond: &scalar.Lnumber{Value: "1"},
&stmt.Break{}, 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.Switch{
&stmt.Break{}, Cond: &scalar.Lnumber{Value: "1"},
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{},
},
}, },
}, },
}, },

View File

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

View File

@ -2284,17 +2284,19 @@ func TestPrintStmtAltSwitch(t *testing.T) {
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.AltSwitch{ &stmt.AltSwitch{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
Cases: []node.Node{ CaseList: &stmt.CaseList{
&stmt.Case{ Cases: []node.Node{
Cond: &scalar.String{Value: "'a'"}, &stmt.Case{
Stmts: []node.Node{ Cond: &scalar.String{Value: "'a'"},
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
}, },
}, &stmt.Case{
&stmt.Case{ Cond: &scalar.String{Value: "'b'"},
Cond: &scalar.String{Value: "'b'"}, Stmts: []node.Node{
Stmts: []node.Node{ &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, },
}, },
}, },
}, },
@ -3649,17 +3651,19 @@ func TestPrintStmtSwitch(t *testing.T) {
Stmts: []node.Node{ Stmts: []node.Node{
&stmt.Switch{ &stmt.Switch{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}}, Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}},
Cases: []node.Node{ CaseList: &stmt.CaseList{
&stmt.Case{ Cases: []node.Node{
Cond: &scalar.String{Value: "'a'"}, &stmt.Case{
Stmts: []node.Node{ Cond: &scalar.String{Value: "'a'"},
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, Stmts: []node.Node{
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
},
}, },
}, &stmt.Case{
&stmt.Case{ Cond: &scalar.String{Value: "'b'"},
Cond: &scalar.String{Value: "'b'"}, Stmts: []node.Node{
Stmts: []node.Node{ &stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, },
}, },
}, },
}, },