[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,16 +9431,7 @@ func TestStmtSwitch(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
EndLine: 5,
StartPos: 17,
EndPos: 58,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -9510,7 +9501,6 @@ func TestStmtSwitch(t *testing.T) {
}, },
}, },
}, },
},
} }
lexer := scanner.NewLexer([]byte(src), "5.6", nil) lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -9560,16 +9550,7 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
EndLine: 5,
StartPos: 17,
EndPos: 59,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -9639,7 +9620,6 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
}, },
}, },
}, },
},
} }
lexer := scanner.NewLexer([]byte(src), "5.6", nil) lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -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,16 +9671,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: -1,
StartPos: 22,
EndPos: -1,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -9758,7 +9730,6 @@ func TestStmtSwitch_Alt(t *testing.T) {
}, },
}, },
}, },
},
} }
lexer := scanner.NewLexer([]byte(src), "5.6", nil) lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -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,16 +9780,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: -1,
StartPos: 23,
EndPos: -1,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -9865,7 +9828,6 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
}, },
}, },
}, },
},
} }
lexer := scanner.NewLexer([]byte(src), "5.6", nil) lexer := scanner.NewLexer([]byte(src), "5.6", nil)

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,16 +6915,7 @@ func TestPhp5(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 119,
EndLine: -1,
StartPos: 2563,
EndPos: -1,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -6981,8 +6973,7 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
}, &ast.StmtSwitch{
&ast.StmtAltSwitch{
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,16 +6994,7 @@ func TestPhp5(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 125,
EndLine: -1,
StartPos: 2626,
EndPos: -1,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7058,7 +7041,6 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
},
&ast.StmtSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7079,16 +7061,7 @@ func TestPhp5(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 129,
EndLine: 132,
StartPos: 2669,
EndPos: 2710,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7157,7 +7130,6 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
},
&ast.StmtSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7178,16 +7150,7 @@ func TestPhp5(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 134,
EndLine: 137,
StartPos: 2723,
EndPos: 2765,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7256,7 +7219,6 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
},
&ast.StmtThrow{ &ast.StmtThrow{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

View File

@ -10226,16 +10226,7 @@ func TestStmtSwitch(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
EndLine: 5,
StartPos: 17,
EndPos: 58,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -10305,7 +10296,6 @@ func TestStmtSwitch(t *testing.T) {
}, },
}, },
}, },
},
} }
lexer := scanner.NewLexer([]byte(src), "7.4", nil) lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -10355,16 +10345,7 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
EndLine: 5,
StartPos: 17,
EndPos: 59,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -10434,7 +10415,6 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
}, },
}, },
}, },
},
} }
lexer := scanner.NewLexer([]byte(src), "7.4", nil) lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -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,16 +10466,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: -1,
StartPos: 22,
EndPos: -1,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -10553,7 +10525,6 @@ func TestStmtSwitch_Alt(t *testing.T) {
}, },
}, },
}, },
},
} }
lexer := scanner.NewLexer([]byte(src), "7.4", nil) lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -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,16 +10575,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: -1,
StartPos: 23,
EndPos: -1,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -10660,7 +10623,6 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
}, },
}, },
}, },
},
} }
lexer := scanner.NewLexer([]byte(src), "7.4", nil) lexer := scanner.NewLexer([]byte(src), "7.4", nil)

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,16 +7359,7 @@ func TestPhp7(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 123,
EndLine: -1,
StartPos: 2651,
EndPos: -1,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7425,8 +7417,7 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
}, &ast.StmtSwitch{
&ast.StmtAltSwitch{
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,16 +7438,7 @@ func TestPhp7(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 129,
EndLine: -1,
StartPos: 2714,
EndPos: -1,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7502,7 +7485,6 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
},
&ast.StmtSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7523,16 +7505,7 @@ func TestPhp7(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 133,
EndLine: 136,
StartPos: 2757,
EndPos: 2798,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7601,7 +7574,6 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
},
&ast.StmtSwitch{ &ast.StmtSwitch{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7622,16 +7594,7 @@ func TestPhp7(t *testing.T) {
}, },
Value: []byte("1"), Value: []byte("1"),
}, },
CaseList: &ast.StmtCaseList{ CaseList: []ast.Vertex{
Node: ast.Node{
Position: &position.Position{
StartLine: 138,
EndLine: 141,
StartPos: 2811,
EndPos: 2853,
},
},
Cases: []ast.Vertex{
&ast.StmtCase{ &ast.StmtCase{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -7700,7 +7663,6 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
},
&ast.StmtThrow{ &ast.StmtThrow{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

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,7 +201,9 @@ func (n *StmtBreak) Accept(v NodeVisitor) {
// StmtCase node // StmtCase node
type StmtCase struct { type StmtCase struct {
Node Node
CaseTkn *token.Token
Cond Vertex Cond Vertex
CaseSeparatorTkn *token.Token
Stmts []Vertex Stmts []Vertex
} }
@ -220,16 +211,6 @@ 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,6 +336,8 @@ func (n *StmtDeclare) Accept(v NodeVisitor) {
// StmtDefault node // StmtDefault node
type StmtDefault struct { type StmtDefault struct {
Node Node
DefaultTkn *token.Token
CaseSeparatorTkn *token.Token
Stmts []Vertex Stmts []Vertex
} }
@ -674,8 +657,18 @@ func (n *StmtStmtList) Accept(v NodeVisitor) {
// StmtSwitch node // StmtSwitch node
type StmtSwitch struct { type StmtSwitch struct {
Node Node
Alt bool
SwitchTkn *token.Token
OpenParenthesisTkn *token.Token
Cond Vertex Cond Vertex
CaseList *StmtCaseList 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,10 +2300,10 @@ 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{
@ -2319,7 +2319,6 @@ func TestPrintStmtAltSwitch(t *testing.T) {
}, },
}, },
}, },
},
}) })
expected := `namespace { expected := `namespace {
@ -3682,8 +3681,7 @@ 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{
@ -3699,7 +3697,6 @@ func TestPrintStmtSwitch(t *testing.T) {
}, },
}, },
}, },
},
}) })
expected := `{ expected := `{

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, "(")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { p.Print(n.Cond)
io.WriteString(p.w, ")") p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.OpenCurlyBracketTkn, "{")
p.printToken(n.CaseSeparatorTkn, "")
p.printNodes(n.CaseList)
p.printToken(n.CloseCurlyBracketTkn, "}")
} }
p.printFreeFloating(nn.CaseList, token.Start) func (p *Printer) printStmtAltSwitch(n *ast.StmtSwitch) {
io.WriteString(p.w, "{") p.printToken(n.SwitchTkn, "switch")
p.printFreeFloating(nn.CaseList, token.CaseListStart) p.printToken(n.OpenParenthesisTkn, "(")
p.printNodes(nn.CaseList.Cases) p.Print(n.Cond)
p.printFreeFloating(nn.CaseList, token.CaseListEnd) p.printToken(n.CloseParenthesisTkn, ")")
io.WriteString(p.w, "}") p.printToken(n.ColonTkn, ":")
p.printFreeFloating(nn.CaseList, token.End) p.printToken(n.CaseSeparatorTkn, "")
p.printNodes(n.CaseList)
p.printFreeFloating(nn, token.End) p.printToken(n.EndSwitchTkn, "endswitch")
p.printToken(n.SemiColonTkn, ";")
} }
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,12 +2778,12 @@ 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{
@ -2801,7 +2801,6 @@ func TestPrinterPrintStmtAltSwitch(t *testing.T) {
}, },
}, },
}, },
},
}) })
expected := `switch($var):case 'a':$a;case 'b':$b;endswitch;` expected := `switch($var):case 'a':$a;case 'b':$b;endswitch;`
@ -4103,8 +4102,7 @@ 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{
@ -4122,7 +4120,6 @@ func TestPrinterPrintStmtSwitch(t *testing.T) {
}, },
}, },
}, },
},
}) })
expected := `switch($var){case 'a':$a;case 'b':$b;}` expected := `switch($var){case 'a':$a;case 'b':$b;}`