[refactoring] update ast structure of "Switch", "Case", "Default" nodes; remove "CaseList" node

This commit is contained in:
Vadym Slizov 2020-09-06 12:45:08 +03:00
parent 0e73cd8852
commit f6cb2bff4d
21 changed files with 883 additions and 1201 deletions

View File

@ -9431,78 +9431,68 @@ func TestStmtSwitch(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 2, Position: &position.Position{
EndLine: 5, StartLine: 3,
StartPos: 17, EndLine: 3,
EndPos: 58, StartPos: 22,
EndPos: 36,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: 3, EndLine: 3,
StartPos: 22, StartPos: 27,
EndPos: 36, EndPos: 28,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: 3, EndLine: 3,
StartPos: 27, StartPos: 30,
EndPos: 28, EndPos: 36,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 30,
EndPos: 36,
},
}, },
}, },
}, },
}, },
&ast.StmtCase{ },
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 40,
EndPos: 54,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: 4, EndLine: 4,
StartPos: 40, StartPos: 45,
EndPos: 54, EndPos: 46,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: 4, EndLine: 4,
StartPos: 45, StartPos: 48,
EndPos: 46, EndPos: 54,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 48,
EndPos: 54,
},
}, },
}, },
}, },
@ -9560,78 +9550,68 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 2, Position: &position.Position{
EndLine: 5, StartLine: 3,
StartPos: 17, EndLine: 3,
EndPos: 59, StartPos: 23,
EndPos: 37,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: 3, EndLine: 3,
StartPos: 23, StartPos: 28,
EndPos: 37, EndPos: 29,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: 3, EndLine: 3,
StartPos: 28, StartPos: 31,
EndPos: 29, EndPos: 37,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 31,
EndPos: 37,
},
}, },
}, },
}, },
}, },
&ast.StmtCase{ },
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 41,
EndPos: 55,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: 4, EndLine: 4,
StartPos: 41, StartPos: 46,
EndPos: 55, EndPos: 47,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: 4, EndLine: 4,
StartPos: 46, StartPos: 49,
EndPos: 47, EndPos: 55,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 49,
EndPos: 55,
},
}, },
}, },
}, },
@ -9670,7 +9650,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -9679,6 +9659,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
EndPos: 65, EndPos: 65,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -9690,71 +9671,61 @@ func TestStmtSwitch_Alt(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 3, Position: &position.Position{
EndLine: -1, StartLine: 3,
StartPos: 22, EndLine: -1,
EndPos: -1, StartPos: 22,
EndPos: -1,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: -1, EndLine: 3,
StartPos: 22, StartPos: 27,
EndPos: -1, EndPos: 28,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 27,
EndPos: 28,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{},
}, },
&ast.StmtDefault{ Stmts: []ast.Vertex{},
Node: ast.Node{ },
Position: &position.Position{ &ast.StmtDefault{
StartLine: 4, Node: ast.Node{
EndLine: -1, Position: &position.Position{
StartPos: 33, StartLine: 4,
EndPos: -1, EndLine: -1,
}, StartPos: 33,
EndPos: -1,
}, },
Stmts: []ast.Vertex{},
}, },
&ast.StmtCase{ Stmts: []ast.Vertex{},
},
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 5,
EndLine: -1,
StartPos: 45,
EndPos: -1,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 5, StartLine: 5,
EndLine: -1, EndLine: 5,
StartPos: 45, StartPos: 50,
EndPos: -1, EndPos: 51,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
Node: ast.Node{
Position: &position.Position{
StartLine: 5,
EndLine: 5,
StartPos: 50,
EndPos: 51,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{},
}, },
Stmts: []ast.Vertex{},
}, },
}, },
}, },
@ -9788,7 +9759,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -9797,6 +9768,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
EndPos: 54, EndPos: 54,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -9808,60 +9780,50 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 3, Position: &position.Position{
EndLine: -1, StartLine: 3,
StartPos: 23, EndLine: -1,
EndPos: -1, StartPos: 23,
EndPos: -1,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: -1, EndLine: 3,
StartPos: 23, StartPos: 28,
EndPos: -1, EndPos: 29,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 28,
EndPos: 29,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{},
}, },
&ast.StmtCase{ Stmts: []ast.Vertex{},
},
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: -1,
StartPos: 34,
EndPos: -1,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: -1, EndLine: 4,
StartPos: 34, StartPos: 39,
EndPos: -1, EndPos: 40,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 39,
EndPos: 40,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{},
}, },
Stmts: []ast.Vertex{},
}, },
}, },
}, },

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -940,22 +940,13 @@ unticked_statement:
} }
| T_SWITCH parenthesis_expr switch_case_list | T_SWITCH parenthesis_expr switch_case_list
{ {
switch n := $3.(type) { $3.(*ast.StmtSwitch).SwitchTkn = $1
case *ast.StmtSwitch: $3.(*ast.StmtSwitch).OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn
n.Cond = $2 $3.(*ast.StmtSwitch).Cond = $2.(*ast.ParserBrackets).Child
case *ast.StmtAltSwitch: $3.(*ast.StmtSwitch).CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn
n.Cond = $2 $3.(*ast.StmtSwitch).Node.Position = position.NewTokenNodePosition($1, $3)
default:
panic("unexpected node type")
}
$$ = $3 $$ = $3
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
} }
| T_BREAK ';' | T_BREAK ';'
{ {
@ -1790,62 +1781,53 @@ declare_list:
switch_case_list: switch_case_list:
'{' case_list '}' '{' case_list '}'
{ {
caseList := &ast.StmtCaseList{ast.Node{}, $2} $$ = &ast.StmtSwitch{
$$ = &ast.StmtSwitch{ast.Node{}, nil, caseList} Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
// save position },
caseList.GetNode().Position = position.NewTokensPosition($1, $3) OpenCurlyBracketTkn: $1,
$$.GetNode().Position = position.NewTokensPosition($1, $3) CaseList: $2,
CloseCurlyBracketTkn: $3,
// save comments }
yylex.(*Parser).setFreeFloating(caseList, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $3.SkippedTokens)
} }
| '{' ';' case_list '}' | '{' ';' case_list '}'
{ {
caseList := &ast.StmtCaseList{ast.Node{}, $3} $$ = &ast.StmtSwitch{
$$ = &ast.StmtSwitch{ast.Node{}, nil, caseList} Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
// save position },
caseList.GetNode().Position = position.NewTokensPosition($1, $4) OpenCurlyBracketTkn: $1,
$$.GetNode().Position = position.NewTokensPosition($1, $4) CaseSeparatorTkn: $2,
CaseList: $3,
// save comments CloseCurlyBracketTkn: $4,
yylex.(*Parser).setFreeFloating(caseList, token.Start, $1.SkippedTokens) }
yylex.(*Parser).setFreeFloatingTokens(caseList, token.CaseListStart, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $4.SkippedTokens)
} }
| ':' case_list T_ENDSWITCH ';' | ':' case_list T_ENDSWITCH ';'
{ {
caseList := &ast.StmtCaseList{ast.Node{}, $2} $$ = &ast.StmtSwitch{
$$ = &ast.StmtAltSwitch{ast.Node{}, nil, caseList} Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
// save position },
caseList.GetNode().Position = position.NewNodeListPosition($2) Alt: true,
$$.GetNode().Position = position.NewTokensPosition($1, $4) ColonTkn: $1,
CaseList: $2,
// save comments EndSwitchTkn: $3,
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens) SemiColonTkn: $4,
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $3.SkippedTokens) }
yylex.(*Parser).setFreeFloating($$, token.AltEnd, $4.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $4.SkippedTokens)
} }
| ':' ';' case_list T_ENDSWITCH ';' | ':' ';' case_list T_ENDSWITCH ';'
{ {
$$ = &ast.StmtSwitch{
caseList := &ast.StmtCaseList{ast.Node{}, $3} Node: ast.Node{
$$ = &ast.StmtAltSwitch{ast.Node{}, nil, caseList} Position: position.NewTokensPosition($1, $5),
},
// save position Alt: true,
caseList.GetNode().Position = position.NewNodeListPosition($3) ColonTkn: $1,
$$.GetNode().Position = position.NewTokensPosition($1, $5) CaseSeparatorTkn: $2,
CaseList: $3,
// save comments EndSwitchTkn: $4,
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens) SemiColonTkn: $5,
yylex.(*Parser).setFreeFloatingTokens(caseList, token.CaseListStart, $2.SkippedTokens) }
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $4.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.AltEnd, $5.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $5.SkippedTokens)
} }
; ;
@ -1853,33 +1835,30 @@ switch_case_list:
case_list: case_list:
/* empty */ /* empty */
{ {
$$ = []ast.Vertex{} $$ = nil
} }
| case_list T_CASE expr case_separator inner_statement_list | case_list T_CASE expr case_separator inner_statement_list
{ {
_case := &ast.StmtCase{ast.Node{}, $3, $5} $$ = append($1, &ast.StmtCase{
$$ = append($1, _case) Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $5),
// save position },
_case.GetNode().Position = position.NewTokenNodeListPosition($2, $5) CaseTkn: $2,
Cond: $3,
// save comments CaseSeparatorTkn: $4,
yylex.(*Parser).setFreeFloating(_case, token.Start, $2.SkippedTokens) Stmts: $5,
yylex.(*Parser).setFreeFloating(_case, token.Expr, $4.SkippedTokens) })
yylex.(*Parser).setToken(_case, token.CaseSeparator, $4.SkippedTokens)
} }
| case_list T_DEFAULT case_separator inner_statement_list | case_list T_DEFAULT case_separator inner_statement_list
{ {
_default := &ast.StmtDefault{ast.Node{}, $4} $$ = append($1, &ast.StmtDefault{
$$ = append($1, _default) Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $4),
// save position },
_default.GetNode().Position = position.NewTokenNodeListPosition($2, $4) DefaultTkn: $2,
CaseSeparatorTkn: $3,
// save comments Stmts: $4,
yylex.(*Parser).setFreeFloating(_default, token.Start, $2.SkippedTokens) })
yylex.(*Parser).setFreeFloating(_default, token.Default, $3.SkippedTokens)
yylex.(*Parser).setToken(_default, token.CaseSeparator, $3.SkippedTokens)
} }
; ;

View File

@ -6894,7 +6894,7 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 118, StartLine: 118,
@ -6903,6 +6903,7 @@ func TestPhp5(t *testing.T) {
EndPos: 2606, EndPos: 2606,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -6914,75 +6915,65 @@ func TestPhp5(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 119, Position: &position.Position{
EndLine: -1, StartLine: 119,
StartPos: 2563, EndLine: -1,
EndPos: -1, StartPos: 2563,
EndPos: -1,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 119, StartLine: 119,
EndLine: -1, EndLine: 119,
StartPos: 2563, StartPos: 2568,
EndPos: -1, EndPos: 2569,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
Node: ast.Node{
Position: &position.Position{
StartLine: 119,
EndLine: 119,
StartPos: 2568,
EndPos: 2569,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{},
}, },
&ast.StmtDefault{ Stmts: []ast.Vertex{},
Node: ast.Node{ },
Position: &position.Position{ &ast.StmtDefault{
StartLine: 120, Node: ast.Node{
EndLine: -1, Position: &position.Position{
StartPos: 2575, StartLine: 120,
EndPos: -1, EndLine: -1,
}, StartPos: 2575,
EndPos: -1,
}, },
Stmts: []ast.Vertex{},
}, },
&ast.StmtCase{ Stmts: []ast.Vertex{},
},
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 121,
EndLine: -1,
StartPos: 2588,
EndPos: -1,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 121, StartLine: 121,
EndLine: -1, EndLine: 121,
StartPos: 2588, StartPos: 2593,
EndPos: -1, EndPos: 2594,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
Node: ast.Node{
Position: &position.Position{
StartLine: 121,
EndLine: 121,
StartPos: 2593,
EndPos: 2594,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{},
}, },
Stmts: []ast.Vertex{},
}, },
}, },
}, },
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 124, StartLine: 124,
@ -6991,6 +6982,7 @@ func TestPhp5(t *testing.T) {
EndPos: 2656, EndPos: 2656,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7002,60 +6994,50 @@ func TestPhp5(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 125, Position: &position.Position{
EndLine: -1, StartLine: 125,
StartPos: 2626, EndLine: -1,
EndPos: -1, StartPos: 2626,
EndPos: -1,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 125, StartLine: 125,
EndLine: -1, EndLine: 125,
StartPos: 2626, StartPos: 2631,
EndPos: -1, EndPos: 2632,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
Node: ast.Node{
Position: &position.Position{
StartLine: 125,
EndLine: 125,
StartPos: 2631,
EndPos: 2632,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{},
}, },
&ast.StmtCase{ Stmts: []ast.Vertex{},
},
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 126,
EndLine: -1,
StartPos: 2638,
EndPos: -1,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 126, StartLine: 126,
EndLine: -1, EndLine: 126,
StartPos: 2638, StartPos: 2643,
EndPos: -1, EndPos: 2644,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
Node: ast.Node{
Position: &position.Position{
StartLine: 126,
EndLine: 126,
StartPos: 2643,
EndPos: 2644,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{},
}, },
Stmts: []ast.Vertex{},
}, },
}, },
}, },
@ -7079,78 +7061,68 @@ func TestPhp5(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 129, Position: &position.Position{
EndLine: 132, StartLine: 130,
StartPos: 2669, EndLine: 130,
EndPos: 2710, StartPos: 2675,
EndPos: 2689,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 130, StartLine: 130,
EndLine: 130, EndLine: 130,
StartPos: 2675, StartPos: 2680,
EndPos: 2689, EndPos: 2681,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 130, StartLine: 130,
EndLine: 130, EndLine: 130,
StartPos: 2680, StartPos: 2683,
EndPos: 2681, EndPos: 2689,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 130,
EndLine: 130,
StartPos: 2683,
EndPos: 2689,
},
}, },
}, },
}, },
}, },
&ast.StmtCase{ },
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 131,
EndLine: 131,
StartPos: 2694,
EndPos: 2708,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 131, StartLine: 131,
EndLine: 131, EndLine: 131,
StartPos: 2694, StartPos: 2699,
EndPos: 2708, EndPos: 2700,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 131, StartLine: 131,
EndLine: 131, EndLine: 131,
StartPos: 2699, StartPos: 2702,
EndPos: 2700, EndPos: 2708,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 131,
EndLine: 131,
StartPos: 2702,
EndPos: 2708,
},
}, },
}, },
}, },
@ -7178,78 +7150,68 @@ func TestPhp5(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 134, Position: &position.Position{
EndLine: 137, StartLine: 135,
StartPos: 2723, EndLine: 135,
EndPos: 2765, StartPos: 2730,
EndPos: 2744,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 135, StartLine: 135,
EndLine: 135, EndLine: 135,
StartPos: 2730, StartPos: 2735,
EndPos: 2744, EndPos: 2736,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 135, StartLine: 135,
EndLine: 135, EndLine: 135,
StartPos: 2735, StartPos: 2738,
EndPos: 2736, EndPos: 2744,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 135,
EndLine: 135,
StartPos: 2738,
EndPos: 2744,
},
}, },
}, },
}, },
}, },
&ast.StmtCase{ },
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 136,
EndLine: 136,
StartPos: 2749,
EndPos: 2763,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 136, StartLine: 136,
EndLine: 136, EndLine: 136,
StartPos: 2749, StartPos: 2754,
EndPos: 2763, EndPos: 2755,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 136, StartLine: 136,
EndLine: 136, EndLine: 136,
StartPos: 2754, StartPos: 2757,
EndPos: 2755, EndPos: 2763,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 136,
EndLine: 136,
StartPos: 2757,
EndPos: 2763,
},
}, },
}, },
}, },

View File

@ -10226,78 +10226,68 @@ func TestStmtSwitch(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 2, Position: &position.Position{
EndLine: 5, StartLine: 3,
StartPos: 17, EndLine: 3,
EndPos: 58, StartPos: 22,
EndPos: 36,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: 3, EndLine: 3,
StartPos: 22, StartPos: 27,
EndPos: 36, EndPos: 28,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: 3, EndLine: 3,
StartPos: 27, StartPos: 30,
EndPos: 28, EndPos: 36,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 30,
EndPos: 36,
},
}, },
}, },
}, },
}, },
&ast.StmtCase{ },
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 40,
EndPos: 54,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: 4, EndLine: 4,
StartPos: 40, StartPos: 45,
EndPos: 54, EndPos: 46,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: 4, EndLine: 4,
StartPos: 45, StartPos: 48,
EndPos: 46, EndPos: 54,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 48,
EndPos: 54,
},
}, },
}, },
}, },
@ -10355,78 +10345,68 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 2, Position: &position.Position{
EndLine: 5, StartLine: 3,
StartPos: 17, EndLine: 3,
EndPos: 59, StartPos: 23,
EndPos: 37,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: 3, EndLine: 3,
StartPos: 23, StartPos: 28,
EndPos: 37, EndPos: 29,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: 3, EndLine: 3,
StartPos: 28, StartPos: 31,
EndPos: 29, EndPos: 37,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 31,
EndPos: 37,
},
}, },
}, },
}, },
}, },
&ast.StmtCase{ },
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 41,
EndPos: 55,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: 4, EndLine: 4,
StartPos: 41, StartPos: 46,
EndPos: 55, EndPos: 47,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: 4, EndLine: 4,
StartPos: 46, StartPos: 49,
EndPos: 47, EndPos: 55,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 49,
EndPos: 55,
},
}, },
}, },
}, },
@ -10465,7 +10445,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -10474,6 +10454,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
EndPos: 65, EndPos: 65,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -10485,71 +10466,61 @@ func TestStmtSwitch_Alt(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 3, Position: &position.Position{
EndLine: -1, StartLine: 3,
StartPos: 22, EndLine: -1,
EndPos: -1, StartPos: 22,
EndPos: -1,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: -1, EndLine: 3,
StartPos: 22, StartPos: 27,
EndPos: -1, EndPos: 28,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 27,
EndPos: 28,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{},
}, },
&ast.StmtDefault{ Stmts: []ast.Vertex{},
Node: ast.Node{ },
Position: &position.Position{ &ast.StmtDefault{
StartLine: 4, Node: ast.Node{
EndLine: -1, Position: &position.Position{
StartPos: 33, StartLine: 4,
EndPos: -1, EndLine: -1,
}, StartPos: 33,
EndPos: -1,
}, },
Stmts: []ast.Vertex{},
}, },
&ast.StmtCase{ Stmts: []ast.Vertex{},
},
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 5,
EndLine: -1,
StartPos: 45,
EndPos: -1,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 5, StartLine: 5,
EndLine: -1, EndLine: 5,
StartPos: 45, StartPos: 50,
EndPos: -1, EndPos: 51,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
Node: ast.Node{
Position: &position.Position{
StartLine: 5,
EndLine: 5,
StartPos: 50,
EndPos: 51,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{},
}, },
Stmts: []ast.Vertex{},
}, },
}, },
}, },
@ -10583,7 +10554,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -10592,6 +10563,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
EndPos: 54, EndPos: 54,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -10603,60 +10575,50 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 3, Position: &position.Position{
EndLine: -1, StartLine: 3,
StartPos: 23, EndLine: -1,
EndPos: -1, StartPos: 23,
EndPos: -1,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
EndLine: -1, EndLine: 3,
StartPos: 23, StartPos: 28,
EndPos: -1, EndPos: 29,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 28,
EndPos: 29,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{},
}, },
&ast.StmtCase{ Stmts: []ast.Vertex{},
},
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: -1,
StartPos: 34,
EndPos: -1,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
EndLine: -1, EndLine: 4,
StartPos: 34, StartPos: 39,
EndPos: -1, EndPos: 40,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
Node: ast.Node{
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 39,
EndPos: 40,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{},
}, },
Stmts: []ast.Vertex{},
}, },
}, },
}, },

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -909,34 +909,13 @@ statement:
} }
| T_SWITCH '(' expr ')' switch_case_list | T_SWITCH '(' expr ')' switch_case_list
{ {
exprBrackets := &ast.ParserBrackets{ $5.(*ast.StmtSwitch).SwitchTkn = $1
Node: ast.Node{ $5.(*ast.StmtSwitch).OpenParenthesisTkn = $2
Position: position.NewTokensPosition($2, $4), $5.(*ast.StmtSwitch).Cond = $3
}, $5.(*ast.StmtSwitch).CloseParenthesisTkn = $4
OpenBracketTkn: $2, $5.(*ast.StmtSwitch).Node.Position = position.NewTokenNodePosition($1, $5)
Child: $3,
CloseBracketTkn: $4,
}
switch n := $5.(type) {
case *ast.StmtSwitch:
n.Cond = exprBrackets
case *ast.StmtAltSwitch:
n.Cond = exprBrackets
default:
panic("unexpected node type")
}
$$ = $5 $$ = $5
// save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
$$.GetNode().Position = position.NewTokenNodePosition($1, $5)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.SkippedTokens)
} }
| T_BREAK optional_expr ';' | T_BREAK optional_expr ';'
{ {
@ -1599,95 +1578,83 @@ declare_statement:
switch_case_list: switch_case_list:
'{' case_list '}' '{' case_list '}'
{ {
caseList := &ast.StmtCaseList{ast.Node{}, $2} $$ = &ast.StmtSwitch{
$$ = &ast.StmtSwitch{ast.Node{}, nil, caseList} Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
// save position },
caseList.GetNode().Position = position.NewTokensPosition($1, $3) OpenCurlyBracketTkn: $1,
$$.GetNode().Position = position.NewTokensPosition($1, $3) CaseList: $2,
CloseCurlyBracketTkn: $3,
// save comments }
yylex.(*Parser).setFreeFloating(caseList, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $3.SkippedTokens)
} }
| '{' ';' case_list '}' | '{' ';' case_list '}'
{ {
caseList := &ast.StmtCaseList{ast.Node{}, $3} $$ = &ast.StmtSwitch{
$$ = &ast.StmtSwitch{ast.Node{}, nil, caseList} Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
// save position },
caseList.GetNode().Position = position.NewTokensPosition($1, $4) OpenCurlyBracketTkn: $1,
$$.GetNode().Position = position.NewTokensPosition($1, $4) CaseSeparatorTkn: $2,
CaseList: $3,
// save comments CloseCurlyBracketTkn: $4,
yylex.(*Parser).setFreeFloating(caseList, token.Start, $1.SkippedTokens) }
yylex.(*Parser).setFreeFloatingTokens(caseList, token.CaseListStart, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $4.SkippedTokens)
} }
| ':' case_list T_ENDSWITCH ';' | ':' case_list T_ENDSWITCH ';'
{ {
caseList := &ast.StmtCaseList{ast.Node{}, $2} $$ = &ast.StmtSwitch{
$$ = &ast.StmtAltSwitch{ast.Node{}, nil, caseList} Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
// save position },
caseList.GetNode().Position = position.NewNodeListPosition($2) Alt: true,
$$.GetNode().Position = position.NewTokensPosition($1, $4) ColonTkn: $1,
CaseList: $2,
// save comments EndSwitchTkn: $3,
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens) SemiColonTkn: $4,
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $3.SkippedTokens) }
yylex.(*Parser).setFreeFloating($$, token.AltEnd, $4.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $4.SkippedTokens)
} }
| ':' ';' case_list T_ENDSWITCH ';' | ':' ';' case_list T_ENDSWITCH ';'
{ {
$$ = &ast.StmtSwitch{
caseList := &ast.StmtCaseList{ast.Node{}, $3} Node: ast.Node{
$$ = &ast.StmtAltSwitch{ast.Node{}, nil, caseList} Position: position.NewTokensPosition($1, $5),
},
// save position Alt: true,
caseList.GetNode().Position = position.NewNodeListPosition($3) ColonTkn: $1,
$$.GetNode().Position = position.NewTokensPosition($1, $5) CaseSeparatorTkn: $2,
CaseList: $3,
// save comments EndSwitchTkn: $4,
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens) SemiColonTkn: $5,
yylex.(*Parser).setFreeFloatingTokens(caseList, token.CaseListStart, $2.SkippedTokens) }
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $4.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.AltEnd, $5.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $5.SkippedTokens)
} }
; ;
case_list: case_list:
/* empty */ /* empty */
{ {
$$ = []ast.Vertex{} $$ = nil
} }
| case_list T_CASE expr case_separator inner_statement_list | case_list T_CASE expr case_separator inner_statement_list
{ {
_case := &ast.StmtCase{ast.Node{}, $3, $5} $$ = append($1, &ast.StmtCase{
$$ = append($1, _case) Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $5),
// save position },
_case.GetNode().Position = position.NewTokenNodeListPosition($2, $5) CaseTkn: $2,
Cond: $3,
// save comments CaseSeparatorTkn: $4,
yylex.(*Parser).setFreeFloating(_case, token.Start, $2.SkippedTokens) Stmts: $5,
yylex.(*Parser).setFreeFloating(_case, token.Expr, append($4.SkippedTokens)) })
yylex.(*Parser).setToken(_case, token.CaseSeparator, $4.SkippedTokens)
} }
| case_list T_DEFAULT case_separator inner_statement_list | case_list T_DEFAULT case_separator inner_statement_list
{ {
_default := &ast.StmtDefault{ast.Node{}, $4} $$ = append($1, &ast.StmtDefault{
$$ = append($1, _default) Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $4),
// save position },
_default.GetNode().Position = position.NewTokenNodeListPosition($2, $4) DefaultTkn: $2,
CaseSeparatorTkn: $3,
// save comments Stmts: $4,
yylex.(*Parser).setFreeFloating(_default, token.Start, $2.SkippedTokens) })
yylex.(*Parser).setFreeFloating(_default, token.Default, $3.SkippedTokens)
yylex.(*Parser).setToken(_default, token.CaseSeparator, $3.SkippedTokens)
} }
; ;

View File

@ -7338,7 +7338,7 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 122, StartLine: 122,
@ -7347,6 +7347,7 @@ func TestPhp7(t *testing.T) {
EndPos: 2694, EndPos: 2694,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7358,75 +7359,65 @@ func TestPhp7(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 123, Position: &position.Position{
EndLine: -1, StartLine: 123,
StartPos: 2651, EndLine: -1,
EndPos: -1, StartPos: 2651,
EndPos: -1,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 123, StartLine: 123,
EndLine: -1, EndLine: 123,
StartPos: 2651, StartPos: 2656,
EndPos: -1, EndPos: 2657,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
Node: ast.Node{
Position: &position.Position{
StartLine: 123,
EndLine: 123,
StartPos: 2656,
EndPos: 2657,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{},
}, },
&ast.StmtDefault{ Stmts: []ast.Vertex{},
Node: ast.Node{ },
Position: &position.Position{ &ast.StmtDefault{
StartLine: 124, Node: ast.Node{
EndLine: -1, Position: &position.Position{
StartPos: 2663, StartLine: 124,
EndPos: -1, EndLine: -1,
}, StartPos: 2663,
EndPos: -1,
}, },
Stmts: []ast.Vertex{},
}, },
&ast.StmtCase{ Stmts: []ast.Vertex{},
},
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 125,
EndLine: -1,
StartPos: 2676,
EndPos: -1,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 125, StartLine: 125,
EndLine: -1, EndLine: 125,
StartPos: 2676, StartPos: 2681,
EndPos: -1, EndPos: 2682,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
Node: ast.Node{
Position: &position.Position{
StartLine: 125,
EndLine: 125,
StartPos: 2681,
EndPos: 2682,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{},
}, },
Stmts: []ast.Vertex{},
}, },
}, },
}, },
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 128, StartLine: 128,
@ -7435,6 +7426,7 @@ func TestPhp7(t *testing.T) {
EndPos: 2744, EndPos: 2744,
}, },
}, },
Alt: true,
Cond: &ast.ScalarLnumber{ Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7446,60 +7438,50 @@ func TestPhp7(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 129, Position: &position.Position{
EndLine: -1, StartLine: 129,
StartPos: 2714, EndLine: -1,
EndPos: -1, StartPos: 2714,
EndPos: -1,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 129, StartLine: 129,
EndLine: -1, EndLine: 129,
StartPos: 2714, StartPos: 2719,
EndPos: -1, EndPos: 2720,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
Node: ast.Node{
Position: &position.Position{
StartLine: 129,
EndLine: 129,
StartPos: 2719,
EndPos: 2720,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{},
}, },
&ast.StmtCase{ Stmts: []ast.Vertex{},
},
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 130,
EndLine: -1,
StartPos: 2726,
EndPos: -1,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 130, StartLine: 130,
EndLine: -1, EndLine: 130,
StartPos: 2726, StartPos: 2731,
EndPos: -1, EndPos: 2732,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
Node: ast.Node{
Position: &position.Position{
StartLine: 130,
EndLine: 130,
StartPos: 2731,
EndPos: 2732,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{},
}, },
Stmts: []ast.Vertex{},
}, },
}, },
}, },
@ -7523,78 +7505,68 @@ func TestPhp7(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 133, Position: &position.Position{
EndLine: 136, StartLine: 134,
StartPos: 2757, EndLine: 134,
EndPos: 2798, StartPos: 2763,
EndPos: 2777,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 134, StartLine: 134,
EndLine: 134, EndLine: 134,
StartPos: 2763, StartPos: 2768,
EndPos: 2777, EndPos: 2769,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 134, StartLine: 134,
EndLine: 134, EndLine: 134,
StartPos: 2768, StartPos: 2771,
EndPos: 2769, EndPos: 2777,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 134,
EndLine: 134,
StartPos: 2771,
EndPos: 2777,
},
}, },
}, },
}, },
}, },
&ast.StmtCase{ },
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 135,
EndLine: 135,
StartPos: 2782,
EndPos: 2796,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 135, StartLine: 135,
EndLine: 135, EndLine: 135,
StartPos: 2782, StartPos: 2787,
EndPos: 2796, EndPos: 2788,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 135, StartLine: 135,
EndLine: 135, EndLine: 135,
StartPos: 2787, StartPos: 2790,
EndPos: 2788, EndPos: 2796,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 135,
EndLine: 135,
StartPos: 2790,
EndPos: 2796,
},
}, },
}, },
}, },
@ -7622,78 +7594,68 @@ func TestPhp7(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{ &ast.StmtCase{
Position: &position.Position{ Node: ast.Node{
StartLine: 138, Position: &position.Position{
EndLine: 141, StartLine: 139,
StartPos: 2811, EndLine: 139,
EndPos: 2853, StartPos: 2818,
EndPos: 2832,
},
}, },
}, Cond: &ast.ScalarLnumber{
Cases: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 139, StartLine: 139,
EndLine: 139, EndLine: 139,
StartPos: 2818, StartPos: 2823,
EndPos: 2832, EndPos: 2824,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 139, StartLine: 139,
EndLine: 139, EndLine: 139,
StartPos: 2823, StartPos: 2826,
EndPos: 2824, EndPos: 2832,
},
},
Value: []byte("1"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 139,
EndLine: 139,
StartPos: 2826,
EndPos: 2832,
},
}, },
}, },
}, },
}, },
&ast.StmtCase{ },
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
StartLine: 140,
EndLine: 140,
StartPos: 2837,
EndPos: 2851,
},
},
Cond: &ast.ScalarLnumber{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 140, StartLine: 140,
EndLine: 140, EndLine: 140,
StartPos: 2837, StartPos: 2842,
EndPos: 2851, EndPos: 2843,
}, },
}, },
Cond: &ast.ScalarLnumber{ Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 140, StartLine: 140,
EndLine: 140, EndLine: 140,
StartPos: 2842, StartPos: 2845,
EndPos: 2843, EndPos: 2851,
},
},
Value: []byte("2"),
},
Stmts: []ast.Vertex{
&ast.StmtBreak{
Node: ast.Node{
Position: &position.Position{
StartLine: 140,
EndLine: 140,
StartPos: 2845,
EndPos: 2851,
},
}, },
}, },
}, },

View File

@ -28,10 +28,8 @@ type NodeVisitor interface {
Argument(n *Argument) Argument(n *Argument)
StmtAltForeach(n *StmtAltForeach) StmtAltForeach(n *StmtAltForeach)
StmtAltSwitch(n *StmtAltSwitch)
StmtBreak(n *StmtBreak) StmtBreak(n *StmtBreak)
StmtCase(n *StmtCase) StmtCase(n *StmtCase)
StmtCaseList(n *StmtCaseList)
StmtCatch(n *StmtCatch) StmtCatch(n *StmtCatch)
StmtClass(n *StmtClass) StmtClass(n *StmtClass)
StmtClassConstList(n *StmtClassConstList) StmtClassConstList(n *StmtClassConstList)

View File

@ -188,17 +188,6 @@ func (n *StmtAltForeach) Accept(v NodeVisitor) {
v.StmtAltForeach(n) v.StmtAltForeach(n)
} }
// StmtAltSwitch node
type StmtAltSwitch struct {
Node
Cond Vertex
CaseList *StmtCaseList
}
func (n *StmtAltSwitch) Accept(v NodeVisitor) {
v.StmtAltSwitch(n)
}
// StmtBreak node // StmtBreak node
type StmtBreak struct { type StmtBreak struct {
Node Node
@ -212,24 +201,16 @@ func (n *StmtBreak) Accept(v NodeVisitor) {
// StmtCase node // StmtCase node
type StmtCase struct { type StmtCase struct {
Node Node
Cond Vertex CaseTkn *token.Token
Stmts []Vertex Cond Vertex
CaseSeparatorTkn *token.Token
Stmts []Vertex
} }
func (n *StmtCase) Accept(v NodeVisitor) { func (n *StmtCase) Accept(v NodeVisitor) {
v.StmtCase(n) v.StmtCase(n)
} }
// StmtCaseList node
type StmtCaseList struct {
Node
Cases []Vertex
}
func (n *StmtCaseList) Accept(v NodeVisitor) {
v.StmtCaseList(n)
}
// StmtCatch node // StmtCatch node
type StmtCatch struct { type StmtCatch struct {
Node Node
@ -355,7 +336,9 @@ func (n *StmtDeclare) Accept(v NodeVisitor) {
// StmtDefault node // StmtDefault node
type StmtDefault struct { type StmtDefault struct {
Node Node
Stmts []Vertex DefaultTkn *token.Token
CaseSeparatorTkn *token.Token
Stmts []Vertex
} }
func (n *StmtDefault) Accept(v NodeVisitor) { func (n *StmtDefault) Accept(v NodeVisitor) {
@ -674,8 +657,18 @@ func (n *StmtStmtList) Accept(v NodeVisitor) {
// StmtSwitch node // StmtSwitch node
type StmtSwitch struct { type StmtSwitch struct {
Node Node
Cond Vertex Alt bool
CaseList *StmtCaseList SwitchTkn *token.Token
OpenParenthesisTkn *token.Token
Cond Vertex
CloseParenthesisTkn *token.Token
ColonTkn *token.Token
OpenCurlyBracketTkn *token.Token
CaseSeparatorTkn *token.Token
CaseList []Vertex
CloseCurlyBracketTkn *token.Token
EndSwitchTkn *token.Token
SemiColonTkn *token.Token
} }
func (n *StmtSwitch) Accept(v NodeVisitor) { func (n *StmtSwitch) Accept(v NodeVisitor) {

View File

@ -146,23 +146,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Stmt) t.Traverse(nn.Stmt)
t.visitor.Leave("Stmt", true) t.visitor.Leave("Stmt", true)
} }
case *ast.StmtAltSwitch:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Cond != nil {
t.visitor.Enter("Cond", true)
t.Traverse(nn.Cond)
t.visitor.Leave("Cond", true)
}
if nn.CaseList != nil {
t.visitor.Enter("CaseList", true)
t.Traverse(nn.CaseList)
t.visitor.Leave("CaseList", true)
}
case *ast.StmtBreak: case *ast.StmtBreak:
if nn == nil { if nn == nil {
return return
@ -194,20 +177,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
} }
t.visitor.Leave("Stmts", false) t.visitor.Leave("Stmts", false)
} }
case *ast.StmtCaseList:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Cases != nil {
t.visitor.Enter("Cases", false)
for _, c := range nn.Cases {
t.Traverse(c)
}
t.visitor.Leave("Cases", false)
}
case *ast.StmtCatch: case *ast.StmtCatch:
if nn == nil { if nn == nil {
return return
@ -869,9 +838,11 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.visitor.Leave("Cond", true) t.visitor.Leave("Cond", true)
} }
if nn.CaseList != nil { if nn.CaseList != nil {
t.visitor.Enter("CaseList", true) t.visitor.Enter("CaseList", false)
t.Traverse(nn.CaseList) for _, c := range nn.CaseList {
t.visitor.Leave("CaseList", true) t.Traverse(c)
}
t.visitor.Leave("CaseList", false)
} }
case *ast.StmtThrow: case *ast.StmtThrow:
if nn == nil { if nn == nil {

View File

@ -258,12 +258,6 @@ func (v *Dump) StmtAltForeach(n *ast.StmtAltForeach) {
v.printNode(n.GetNode()) v.printNode(n.GetNode())
} }
func (v *Dump) StmtAltSwitch(n *ast.StmtAltSwitch) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtAltSwitch{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtBreak(n *ast.StmtBreak) { func (v *Dump) StmtBreak(n *ast.StmtBreak) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtBreak{\n") v.print("&ast.StmtBreak{\n")
@ -276,12 +270,6 @@ func (v *Dump) StmtCase(n *ast.StmtCase) {
v.printNode(n.GetNode()) v.printNode(n.GetNode())
} }
func (v *Dump) StmtCaseList(n *ast.StmtCaseList) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtCaseList{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtCatch(n *ast.StmtCatch) { func (v *Dump) StmtCatch(n *ast.StmtCatch) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtCatch{\n") v.print("&ast.StmtCatch{\n")
@ -540,6 +528,11 @@ func (v *Dump) StmtSwitch(n *ast.StmtSwitch) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtSwitch{\n") v.print("&ast.StmtSwitch{\n")
v.printNode(n.GetNode()) v.printNode(n.GetNode())
if n.Alt {
v.printIndent(v.indent)
v.print("Alt: true,\n")
}
} }
func (v *Dump) StmtThrow(n *ast.StmtThrow) { func (v *Dump) StmtThrow(n *ast.StmtThrow) {

View File

@ -13,26 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
return true return true
} }
func (v *FilterParserNodes) StmtSwitch(n *ast.StmtSwitch) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
}
func (v *FilterParserNodes) StmtAltSwitch(n *ast.StmtAltSwitch) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
}
func (v *FilterParserNodes) ExprExit(n *ast.ExprExit) { func (v *FilterParserNodes) ExprExit(n *ast.ExprExit) {
for { for {
if nn, ok := n.Expr.(*ast.ParserBrackets); ok { if nn, ok := n.Expr.(*ast.ParserBrackets); ok {

View File

@ -141,3 +141,25 @@ func (v *FilterTokens) StmtFor(n *ast.StmtFor) {
n.EndForTkn = nil n.EndForTkn = nil
n.SemiColonTkn = nil n.SemiColonTkn = nil
} }
func (v *FilterTokens) StmtSwitch(n *ast.StmtSwitch) {
n.SwitchTkn = nil
n.OpenParenthesisTkn = nil
n.CloseParenthesisTkn = nil
n.OpenCurlyBracketTkn = nil
n.CaseSeparatorTkn = nil
n.ColonTkn = nil
n.CloseCurlyBracketTkn = nil
n.EndSwitchTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtCase(n *ast.StmtCase) {
n.CaseTkn = nil
n.CaseSeparatorTkn = nil
}
func (v *FilterTokens) StmtDefault(n *ast.StmtDefault) {
n.DefaultTkn = nil
n.CaseSeparatorTkn = nil
}

View File

@ -58,10 +58,6 @@ func (v *Null) StmtAltForeach(_ *ast.StmtAltForeach) {
// do nothing // do nothing
} }
func (v *Null) StmtAltSwitch(_ *ast.StmtAltSwitch) {
// do nothing
}
func (v *Null) StmtBreak(_ *ast.StmtBreak) { func (v *Null) StmtBreak(_ *ast.StmtBreak) {
// do nothing // do nothing
} }
@ -70,10 +66,6 @@ func (v *Null) StmtCase(_ *ast.StmtCase) {
// do nothing // do nothing
} }
func (v *Null) StmtCaseList(_ *ast.StmtCaseList) {
// do nothing
}
func (v *Null) StmtCatch(_ *ast.StmtCatch) { func (v *Null) StmtCatch(_ *ast.StmtCatch) {
// do nothing // do nothing
} }

View File

@ -299,8 +299,6 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
case *ast.StmtAltForeach: case *ast.StmtAltForeach:
p.printStmtAltForeach(n) p.printStmtAltForeach(n)
case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n)
case *ast.StmtBreak: case *ast.StmtBreak:
p.printStmtBreak(n) p.printStmtBreak(n)
case *ast.StmtCase: case *ast.StmtCase:
@ -1412,21 +1410,6 @@ func (p *PrettyPrinter) printStmtAltIf(n ast.Vertex) {
io.WriteString(p.w, "endif;") io.WriteString(p.w, "endif;")
} }
func (p *PrettyPrinter) printStmtAltSwitch(n ast.Vertex) {
nn := n.(*ast.StmtAltSwitch)
io.WriteString(p.w, "switch (")
p.Print(nn.Cond)
io.WriteString(p.w, ") :\n")
s := nn.CaseList.Cases
p.printNodes(s)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endswitch;")
}
func (p *PrettyPrinter) printStmtBreak(n ast.Vertex) { func (p *PrettyPrinter) printStmtBreak(n ast.Vertex) {
nn := n.(*ast.StmtBreak) nn := n.(*ast.StmtBreak)
@ -2005,17 +1988,37 @@ func (p *PrettyPrinter) printStmtStmtList(n ast.Vertex) {
func (p *PrettyPrinter) printStmtSwitch(n ast.Vertex) { func (p *PrettyPrinter) printStmtSwitch(n ast.Vertex) {
nn := n.(*ast.StmtSwitch) nn := n.(*ast.StmtSwitch)
if nn.Alt {
p.printStmtAltSwitch(n)
return
}
io.WriteString(p.w, "switch (") io.WriteString(p.w, "switch (")
p.Print(nn.Cond) p.Print(nn.Cond)
io.WriteString(p.w, ")") io.WriteString(p.w, ")")
io.WriteString(p.w, " {\n") io.WriteString(p.w, " {\n")
p.printNodes(nn.CaseList.Cases) p.printNodes(nn.CaseList)
io.WriteString(p.w, "\n") io.WriteString(p.w, "\n")
p.printIndent() p.printIndent()
io.WriteString(p.w, "}") io.WriteString(p.w, "}")
} }
func (p *PrettyPrinter) printStmtAltSwitch(n ast.Vertex) {
nn := n.(*ast.StmtSwitch)
io.WriteString(p.w, "switch (")
p.Print(nn.Cond)
io.WriteString(p.w, ") :\n")
s := nn.CaseList
p.printNodes(s)
io.WriteString(p.w, "\n")
p.printIndent()
io.WriteString(p.w, "endswitch;")
}
func (p *PrettyPrinter) printStmtThrow(n ast.Vertex) { func (p *PrettyPrinter) printStmtThrow(n ast.Vertex) {
nn := n.(*ast.StmtThrow) nn := n.(*ast.StmtThrow)

View File

@ -2300,21 +2300,20 @@ func TestPrintStmtAltSwitch(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ") p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtNamespace{ p.Print(&ast.StmtNamespace{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltSwitch{ &ast.StmtSwitch{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}},
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Cases: []ast.Vertex{ &ast.StmtCase{
&ast.StmtCase{ Cond: &ast.ScalarString{Value: []byte("'a'")},
Cond: &ast.ScalarString{Value: []byte("'a'")}, Stmts: []ast.Vertex{
Stmts: []ast.Vertex{ &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
},
}, },
&ast.StmtCase{ },
Cond: &ast.ScalarString{Value: []byte("'b'")}, &ast.StmtCase{
Stmts: []ast.Vertex{ Cond: &ast.ScalarString{Value: []byte("'b'")},
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, Stmts: []ast.Vertex{
}, &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},
}, },
}, },
}, },
@ -3682,19 +3681,17 @@ func TestPrintStmtSwitch(t *testing.T) {
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtSwitch{ &ast.StmtSwitch{
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}},
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Cases: []ast.Vertex{ &ast.StmtCase{
&ast.StmtCase{ Cond: &ast.ScalarString{Value: []byte("'a'")},
Cond: &ast.ScalarString{Value: []byte("'a'")}, Stmts: []ast.Vertex{
Stmts: []ast.Vertex{ &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}},
},
}, },
&ast.StmtCase{ },
Cond: &ast.ScalarString{Value: []byte("'b'")}, &ast.StmtCase{
Stmts: []ast.Vertex{ Cond: &ast.ScalarString{Value: []byte("'b'")},
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, Stmts: []ast.Vertex{
}, &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},
}, },
}, },
}, },

View File

@ -361,8 +361,6 @@ func (p *Printer) printNode(n ast.Vertex) {
case *ast.StmtAltForeach: case *ast.StmtAltForeach:
p.printStmtAltForeach(n) p.printStmtAltForeach(n)
case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n)
case *ast.StmtBreak: case *ast.StmtBreak:
p.printStmtBreak(n) p.printStmtBreak(n)
case *ast.StmtCase: case *ast.StmtCase:
@ -2019,41 +2017,6 @@ func (p *Printer) printStmtAltForeach(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtAltSwitch(n ast.Vertex) {
nn := n.(*ast.StmtAltSwitch)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "switch")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
}
p.Print(nn.Cond)
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, ")")
}
p.printFreeFloating(nn, token.Cond)
io.WriteString(p.w, ":")
p.printFreeFloating(nn.CaseList, token.Start)
p.printFreeFloating(nn.CaseList, token.CaseListStart)
p.printNodes(nn.CaseList.Cases)
p.printFreeFloating(nn.CaseList, token.CaseListEnd)
p.printFreeFloating(nn.CaseList, token.End)
io.WriteString(p.w, "endswitch")
p.printFreeFloating(nn, token.AltEnd)
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtBreak(n ast.Vertex) { func (p *Printer) printStmtBreak(n ast.Vertex) {
nn := n.(*ast.StmtBreak) nn := n.(*ast.StmtBreak)
p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Start)
@ -2075,26 +2038,12 @@ func (p *Printer) printStmtBreak(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtCase(n ast.Vertex) { func (p *Printer) printStmtCase(n *ast.StmtCase) {
nn := n.(*ast.StmtCase) p.printToken(n.CaseTkn, "case")
p.printFreeFloating(nn, token.Start) p.bufStart = " "
p.Print(n.Cond)
io.WriteString(p.w, "case") p.printToken(n.CaseSeparatorTkn, ":")
if nn.Cond.GetNode().Tokens.IsEmpty() { p.printNodes(n.Stmts)
io.WriteString(p.w, " ")
}
p.Print(nn.Cond)
p.printFreeFloating(nn, token.Expr)
p.printFreeFloating(nn, token.CaseSeparator)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ":")
}
if len(nn.Stmts) > 0 {
p.printNodes(nn.Stmts)
}
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtCatch(n ast.Vertex) { func (p *Printer) printStmtCatch(n ast.Vertex) {
@ -2308,22 +2257,10 @@ func (p *Printer) printStmtDeclare(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtDefault(n ast.Vertex) { func (p *Printer) printStmtDefault(n *ast.StmtDefault) {
nn := n.(*ast.StmtDefault) p.printToken(n.DefaultTkn, "default")
p.printFreeFloating(nn, token.Start) p.printToken(n.CaseSeparatorTkn, ":")
p.printNodes(n.Stmts)
io.WriteString(p.w, "default")
p.printFreeFloating(nn, token.Default)
p.printFreeFloating(nn, token.CaseSeparator)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ":")
}
if len(nn.Stmts) > 0 {
p.printNodes(nn.Stmts)
}
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtDo(n *ast.StmtDo) { func (p *Printer) printStmtDo(n *ast.StmtDo) {
@ -2828,31 +2765,32 @@ func (p *Printer) printStmtStmtList(n *ast.StmtStmtList) {
p.printToken(n.CloseCurlyBracket, "}") p.printToken(n.CloseCurlyBracket, "}")
} }
func (p *Printer) printStmtSwitch(n ast.Vertex) { func (p *Printer) printStmtSwitch(n *ast.StmtSwitch) {
nn := n.(*ast.StmtSwitch) if n.Alt {
p.printFreeFloating(nn, token.Start) p.printStmtAltSwitch(n)
return
io.WriteString(p.w, "switch")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
} }
p.Print(nn.Cond) p.printToken(n.SwitchTkn, "switch")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.OpenCurlyBracketTkn, "{")
p.printToken(n.CaseSeparatorTkn, "")
p.printNodes(n.CaseList)
p.printToken(n.CloseCurlyBracketTkn, "}")
}
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { func (p *Printer) printStmtAltSwitch(n *ast.StmtSwitch) {
io.WriteString(p.w, ")") p.printToken(n.SwitchTkn, "switch")
} p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printFreeFloating(nn.CaseList, token.Start) p.printToken(n.CloseParenthesisTkn, ")")
io.WriteString(p.w, "{") p.printToken(n.ColonTkn, ":")
p.printFreeFloating(nn.CaseList, token.CaseListStart) p.printToken(n.CaseSeparatorTkn, "")
p.printNodes(nn.CaseList.Cases) p.printNodes(n.CaseList)
p.printFreeFloating(nn.CaseList, token.CaseListEnd) p.printToken(n.EndSwitchTkn, "endswitch")
io.WriteString(p.w, "}") p.printToken(n.SemiColonTkn, ";")
p.printFreeFloating(nn.CaseList, token.End)
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtThrow(n ast.Vertex) { func (p *Printer) printStmtThrow(n ast.Vertex) {

View File

@ -791,7 +791,8 @@ func TestParseAndPrintPhp5AltForeach(t *testing.T) {
} }
func TestParseAndPrintPhp5AltSwitch(t *testing.T) { func TestParseAndPrintPhp5AltSwitch(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
switch ( $a ) : switch ( $a ) :
case 1 : case 1 :
@ -1232,7 +1233,8 @@ func TestParseAndPrintPhp5StmtList(t *testing.T) {
} }
func TestParseAndPrintPhp5Switch(t *testing.T) { func TestParseAndPrintPhp5Switch(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
switch ( $a ) { switch ( $a ) {
case 1 : ; case 1 : ;

View File

@ -904,7 +904,8 @@ func TestParseAndPrintAltForeach(t *testing.T) {
} }
func TestParseAndPrintAltSwitch(t *testing.T) { func TestParseAndPrintAltSwitch(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
switch ( $a ) : switch ( $a ) :
case 1 : case 1 :
@ -1365,7 +1366,8 @@ func TestParseAndPrintStmtList(t *testing.T) {
} }
func TestParseAndPrintSwitch(t *testing.T) { func TestParseAndPrintSwitch(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
switch ( $a ) { switch ( $a ) {
case 1 : ; case 1 : ;

View File

@ -2778,27 +2778,26 @@ func TestPrinterPrintStmtAltSwitch(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtAltSwitch{ p.Print(&ast.StmtSwitch{
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")}, VarName: &ast.Identifier{Value: []byte("$var")},
}, },
CaseList: &ast.StmtCaseList{ Alt: true,
Cases: []ast.Vertex{ CaseList: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")}, Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{ &ast.StmtExpression{Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")}, VarName: &ast.Identifier{Value: []byte("$a")},
}}, }},
},
}, },
&ast.StmtCase{ },
Cond: &ast.ScalarString{Value: []byte("'b'")}, &ast.StmtCase{
Stmts: []ast.Vertex{ Cond: &ast.ScalarString{Value: []byte("'b'")},
&ast.StmtExpression{Expr: &ast.ExprVariable{ Stmts: []ast.Vertex{
VarName: &ast.Identifier{Value: []byte("$b")}, &ast.StmtExpression{Expr: &ast.ExprVariable{
}}, VarName: &ast.Identifier{Value: []byte("$b")},
}, }},
}, },
}, },
}, },
@ -4103,23 +4102,21 @@ func TestPrinterPrintStmtSwitch(t *testing.T) {
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")}, VarName: &ast.Identifier{Value: []byte("$var")},
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Cases: []ast.Vertex{ &ast.StmtCase{
&ast.StmtCase{ Cond: &ast.ScalarString{Value: []byte("'a'")},
Cond: &ast.ScalarString{Value: []byte("'a'")}, Stmts: []ast.Vertex{
Stmts: []ast.Vertex{ &ast.StmtExpression{Expr: &ast.ExprVariable{
&ast.StmtExpression{Expr: &ast.ExprVariable{ VarName: &ast.Identifier{Value: []byte("$a")},
VarName: &ast.Identifier{Value: []byte("$a")}, }},
}},
},
}, },
&ast.StmtCase{ },
Cond: &ast.ScalarString{Value: []byte("'b'")}, &ast.StmtCase{
Stmts: []ast.Vertex{ Cond: &ast.ScalarString{Value: []byte("'b'")},
&ast.StmtExpression{Expr: &ast.ExprVariable{ Stmts: []ast.Vertex{
VarName: &ast.Identifier{Value: []byte("$b")}, &ast.StmtExpression{Expr: &ast.ExprVariable{
}}, VarName: &ast.Identifier{Value: []byte("$b")},
}, }},
}, },
}, },
}, },