[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"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
EndLine: 5,
StartPos: 17,
EndPos: 58,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -9510,7 +9501,6 @@ func TestStmtSwitch(t *testing.T) {
},
},
},
},
}
lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -9560,16 +9550,7 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
EndLine: 5,
StartPos: 17,
EndPos: 59,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -9639,7 +9620,6 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
},
},
},
},
}
lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -9670,7 +9650,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
},
},
Stmts: []ast.Vertex{
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
@ -9679,6 +9659,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
EndPos: 65,
},
},
Alt: true,
Cond: &ast.ScalarLnumber{
Node: ast.Node{
Position: &position.Position{
@ -9690,16 +9671,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: -1,
StartPos: 22,
EndPos: -1,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -9758,7 +9730,6 @@ func TestStmtSwitch_Alt(t *testing.T) {
},
},
},
},
}
lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -9788,7 +9759,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
},
},
Stmts: []ast.Vertex{
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
@ -9797,6 +9768,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
EndPos: 54,
},
},
Alt: true,
Cond: &ast.ScalarLnumber{
Node: ast.Node{
Position: &position.Position{
@ -9808,16 +9780,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: -1,
StartPos: 23,
EndPos: -1,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -9865,7 +9828,6 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
},
},
},
},
}
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
{
switch n := $3.(type) {
case *ast.StmtSwitch:
n.Cond = $2
case *ast.StmtAltSwitch:
n.Cond = $2
default:
panic("unexpected node type")
}
$3.(*ast.StmtSwitch).SwitchTkn = $1
$3.(*ast.StmtSwitch).OpenParenthesisTkn = $2.(*ast.ParserBrackets).OpenBracketTkn
$3.(*ast.StmtSwitch).Cond = $2.(*ast.ParserBrackets).Child
$3.(*ast.StmtSwitch).CloseParenthesisTkn = $2.(*ast.ParserBrackets).CloseBracketTkn
$3.(*ast.StmtSwitch).Node.Position = position.NewTokenNodePosition($1, $3)
$$ = $3
// save position
$$.GetNode().Position = position.NewTokenNodePosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
}
| T_BREAK ';'
{
@ -1790,62 +1781,53 @@ declare_list:
switch_case_list:
'{' case_list '}'
{
caseList := &ast.StmtCaseList{ast.Node{}, $2}
$$ = &ast.StmtSwitch{ast.Node{}, nil, caseList}
// save position
caseList.GetNode().Position = position.NewTokensPosition($1, $3)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating(caseList, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $3.SkippedTokens)
$$ = &ast.StmtSwitch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenCurlyBracketTkn: $1,
CaseList: $2,
CloseCurlyBracketTkn: $3,
}
}
| '{' ';' case_list '}'
{
caseList := &ast.StmtCaseList{ast.Node{}, $3}
$$ = &ast.StmtSwitch{ast.Node{}, nil, caseList}
// save position
caseList.GetNode().Position = position.NewTokensPosition($1, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating(caseList, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(caseList, token.CaseListStart, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $4.SkippedTokens)
$$ = &ast.StmtSwitch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
OpenCurlyBracketTkn: $1,
CaseSeparatorTkn: $2,
CaseList: $3,
CloseCurlyBracketTkn: $4,
}
}
| ':' case_list T_ENDSWITCH ';'
{
caseList := &ast.StmtCaseList{ast.Node{}, $2}
$$ = &ast.StmtAltSwitch{ast.Node{}, nil, caseList}
// save position
caseList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.AltEnd, $4.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $4.SkippedTokens)
$$ = &ast.StmtSwitch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
Alt: true,
ColonTkn: $1,
CaseList: $2,
EndSwitchTkn: $3,
SemiColonTkn: $4,
}
}
| ':' ';' case_list T_ENDSWITCH ';'
{
caseList := &ast.StmtCaseList{ast.Node{}, $3}
$$ = &ast.StmtAltSwitch{ast.Node{}, nil, caseList}
// save position
caseList.GetNode().Position = position.NewNodeListPosition($3)
$$.GetNode().Position = position.NewTokensPosition($1, $5)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens)
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)
$$ = &ast.StmtSwitch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $5),
},
Alt: true,
ColonTkn: $1,
CaseSeparatorTkn: $2,
CaseList: $3,
EndSwitchTkn: $4,
SemiColonTkn: $5,
}
}
;
@ -1853,33 +1835,30 @@ switch_case_list:
case_list:
/* empty */
{
$$ = []ast.Vertex{}
$$ = nil
}
| case_list T_CASE expr case_separator inner_statement_list
{
_case := &ast.StmtCase{ast.Node{}, $3, $5}
$$ = append($1, _case)
// save position
_case.GetNode().Position = position.NewTokenNodeListPosition($2, $5)
// save comments
yylex.(*Parser).setFreeFloating(_case, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(_case, token.Expr, $4.SkippedTokens)
yylex.(*Parser).setToken(_case, token.CaseSeparator, $4.SkippedTokens)
$$ = append($1, &ast.StmtCase{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $5),
},
CaseTkn: $2,
Cond: $3,
CaseSeparatorTkn: $4,
Stmts: $5,
})
}
| case_list T_DEFAULT case_separator inner_statement_list
{
_default := &ast.StmtDefault{ast.Node{}, $4}
$$ = append($1, _default)
// save position
_default.GetNode().Position = position.NewTokenNodeListPosition($2, $4)
// save comments
yylex.(*Parser).setFreeFloating(_default, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(_default, token.Default, $3.SkippedTokens)
yylex.(*Parser).setToken(_default, token.CaseSeparator, $3.SkippedTokens)
$$ = append($1, &ast.StmtDefault{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $4),
},
DefaultTkn: $2,
CaseSeparatorTkn: $3,
Stmts: $4,
})
}
;

View File

@ -6894,7 +6894,7 @@ func TestPhp5(t *testing.T) {
},
},
},
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
StartLine: 118,
@ -6903,6 +6903,7 @@ func TestPhp5(t *testing.T) {
EndPos: 2606,
},
},
Alt: true,
Cond: &ast.ScalarLnumber{
Node: ast.Node{
Position: &position.Position{
@ -6914,16 +6915,7 @@ func TestPhp5(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 119,
EndLine: -1,
StartPos: 2563,
EndPos: -1,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -6981,8 +6973,7 @@ func TestPhp5(t *testing.T) {
},
},
},
},
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
StartLine: 124,
@ -6991,6 +6982,7 @@ func TestPhp5(t *testing.T) {
EndPos: 2656,
},
},
Alt: true,
Cond: &ast.ScalarLnumber{
Node: ast.Node{
Position: &position.Position{
@ -7002,16 +6994,7 @@ func TestPhp5(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 125,
EndLine: -1,
StartPos: 2626,
EndPos: -1,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -7058,7 +7041,6 @@ func TestPhp5(t *testing.T) {
},
},
},
},
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
@ -7079,16 +7061,7 @@ func TestPhp5(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 129,
EndLine: 132,
StartPos: 2669,
EndPos: 2710,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -7157,7 +7130,6 @@ func TestPhp5(t *testing.T) {
},
},
},
},
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
@ -7178,16 +7150,7 @@ func TestPhp5(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 134,
EndLine: 137,
StartPos: 2723,
EndPos: 2765,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -7256,7 +7219,6 @@ func TestPhp5(t *testing.T) {
},
},
},
},
&ast.StmtThrow{
Node: ast.Node{
Position: &position.Position{

View File

@ -10226,16 +10226,7 @@ func TestStmtSwitch(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
EndLine: 5,
StartPos: 17,
EndPos: 58,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -10305,7 +10296,6 @@ func TestStmtSwitch(t *testing.T) {
},
},
},
},
}
lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -10355,16 +10345,7 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
EndLine: 5,
StartPos: 17,
EndPos: 59,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -10434,7 +10415,6 @@ func TestStmtSwitch_Semicolon(t *testing.T) {
},
},
},
},
}
lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -10465,7 +10445,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
},
},
Stmts: []ast.Vertex{
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
@ -10474,6 +10454,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
EndPos: 65,
},
},
Alt: true,
Cond: &ast.ScalarLnumber{
Node: ast.Node{
Position: &position.Position{
@ -10485,16 +10466,7 @@ func TestStmtSwitch_Alt(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: -1,
StartPos: 22,
EndPos: -1,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -10553,7 +10525,6 @@ func TestStmtSwitch_Alt(t *testing.T) {
},
},
},
},
}
lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -10583,7 +10554,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
},
},
Stmts: []ast.Vertex{
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
StartLine: 2,
@ -10592,6 +10563,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
EndPos: 54,
},
},
Alt: true,
Cond: &ast.ScalarLnumber{
Node: ast.Node{
Position: &position.Position{
@ -10603,16 +10575,7 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 3,
EndLine: -1,
StartPos: 23,
EndPos: -1,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -10660,7 +10623,6 @@ func TestStmtSwitch_AltSemicolon(t *testing.T) {
},
},
},
},
}
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
{
exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($2, $4),
},
OpenBracketTkn: $2,
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.(*ast.StmtSwitch).SwitchTkn = $1
$5.(*ast.StmtSwitch).OpenParenthesisTkn = $2
$5.(*ast.StmtSwitch).Cond = $3
$5.(*ast.StmtSwitch).CloseParenthesisTkn = $4
$5.(*ast.StmtSwitch).Node.Position = position.NewTokenNodePosition($1, $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 ';'
{
@ -1599,95 +1578,83 @@ declare_statement:
switch_case_list:
'{' case_list '}'
{
caseList := &ast.StmtCaseList{ast.Node{}, $2}
$$ = &ast.StmtSwitch{ast.Node{}, nil, caseList}
// save position
caseList.GetNode().Position = position.NewTokensPosition($1, $3)
$$.GetNode().Position = position.NewTokensPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating(caseList, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $3.SkippedTokens)
$$ = &ast.StmtSwitch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $3),
},
OpenCurlyBracketTkn: $1,
CaseList: $2,
CloseCurlyBracketTkn: $3,
}
}
| '{' ';' case_list '}'
{
caseList := &ast.StmtCaseList{ast.Node{}, $3}
$$ = &ast.StmtSwitch{ast.Node{}, nil, caseList}
// save position
caseList.GetNode().Position = position.NewTokensPosition($1, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating(caseList, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(caseList, token.CaseListStart, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $4.SkippedTokens)
$$ = &ast.StmtSwitch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
OpenCurlyBracketTkn: $1,
CaseSeparatorTkn: $2,
CaseList: $3,
CloseCurlyBracketTkn: $4,
}
}
| ':' case_list T_ENDSWITCH ';'
{
caseList := &ast.StmtCaseList{ast.Node{}, $2}
$$ = &ast.StmtAltSwitch{ast.Node{}, nil, caseList}
// save position
caseList.GetNode().Position = position.NewNodeListPosition($2)
$$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens)
yylex.(*Parser).setFreeFloating(caseList, token.CaseListEnd, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating($$, token.AltEnd, $4.SkippedTokens)
yylex.(*Parser).setToken($$, token.SemiColon, $4.SkippedTokens)
$$ = &ast.StmtSwitch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $4),
},
Alt: true,
ColonTkn: $1,
CaseList: $2,
EndSwitchTkn: $3,
SemiColonTkn: $4,
}
}
| ':' ';' case_list T_ENDSWITCH ';'
{
caseList := &ast.StmtCaseList{ast.Node{}, $3}
$$ = &ast.StmtAltSwitch{ast.Node{}, nil, caseList}
// save position
caseList.GetNode().Position = position.NewNodeListPosition($3)
$$.GetNode().Position = position.NewTokensPosition($1, $5)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Cond, $1.SkippedTokens)
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)
$$ = &ast.StmtSwitch{
Node: ast.Node{
Position: position.NewTokensPosition($1, $5),
},
Alt: true,
ColonTkn: $1,
CaseSeparatorTkn: $2,
CaseList: $3,
EndSwitchTkn: $4,
SemiColonTkn: $5,
}
}
;
case_list:
/* empty */
{
$$ = []ast.Vertex{}
$$ = nil
}
| case_list T_CASE expr case_separator inner_statement_list
{
_case := &ast.StmtCase{ast.Node{}, $3, $5}
$$ = append($1, _case)
// save position
_case.GetNode().Position = position.NewTokenNodeListPosition($2, $5)
// save comments
yylex.(*Parser).setFreeFloating(_case, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(_case, token.Expr, append($4.SkippedTokens))
yylex.(*Parser).setToken(_case, token.CaseSeparator, $4.SkippedTokens)
$$ = append($1, &ast.StmtCase{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $5),
},
CaseTkn: $2,
Cond: $3,
CaseSeparatorTkn: $4,
Stmts: $5,
})
}
| case_list T_DEFAULT case_separator inner_statement_list
{
_default := &ast.StmtDefault{ast.Node{}, $4}
$$ = append($1, _default)
// save position
_default.GetNode().Position = position.NewTokenNodeListPosition($2, $4)
// save comments
yylex.(*Parser).setFreeFloating(_default, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloating(_default, token.Default, $3.SkippedTokens)
yylex.(*Parser).setToken(_default, token.CaseSeparator, $3.SkippedTokens)
$$ = append($1, &ast.StmtDefault{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $4),
},
DefaultTkn: $2,
CaseSeparatorTkn: $3,
Stmts: $4,
})
}
;

View File

@ -7338,7 +7338,7 @@ func TestPhp7(t *testing.T) {
},
},
},
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
StartLine: 122,
@ -7347,6 +7347,7 @@ func TestPhp7(t *testing.T) {
EndPos: 2694,
},
},
Alt: true,
Cond: &ast.ScalarLnumber{
Node: ast.Node{
Position: &position.Position{
@ -7358,16 +7359,7 @@ func TestPhp7(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 123,
EndLine: -1,
StartPos: 2651,
EndPos: -1,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -7425,8 +7417,7 @@ func TestPhp7(t *testing.T) {
},
},
},
},
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
StartLine: 128,
@ -7435,6 +7426,7 @@ func TestPhp7(t *testing.T) {
EndPos: 2744,
},
},
Alt: true,
Cond: &ast.ScalarLnumber{
Node: ast.Node{
Position: &position.Position{
@ -7446,16 +7438,7 @@ func TestPhp7(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 129,
EndLine: -1,
StartPos: 2714,
EndPos: -1,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -7502,7 +7485,6 @@ func TestPhp7(t *testing.T) {
},
},
},
},
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
@ -7523,16 +7505,7 @@ func TestPhp7(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 133,
EndLine: 136,
StartPos: 2757,
EndPos: 2798,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -7601,7 +7574,6 @@ func TestPhp7(t *testing.T) {
},
},
},
},
&ast.StmtSwitch{
Node: ast.Node{
Position: &position.Position{
@ -7622,16 +7594,7 @@ func TestPhp7(t *testing.T) {
},
Value: []byte("1"),
},
CaseList: &ast.StmtCaseList{
Node: ast.Node{
Position: &position.Position{
StartLine: 138,
EndLine: 141,
StartPos: 2811,
EndPos: 2853,
},
},
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Node: ast.Node{
Position: &position.Position{
@ -7700,7 +7663,6 @@ func TestPhp7(t *testing.T) {
},
},
},
},
&ast.StmtThrow{
Node: ast.Node{
Position: &position.Position{

View File

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

View File

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

View File

@ -146,23 +146,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Stmt)
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:
if nn == nil {
return
@ -194,20 +177,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
}
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:
if nn == nil {
return
@ -869,9 +838,11 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.visitor.Leave("Cond", true)
}
if nn.CaseList != nil {
t.visitor.Enter("CaseList", true)
t.Traverse(nn.CaseList)
t.visitor.Leave("CaseList", true)
t.visitor.Enter("CaseList", false)
for _, c := range nn.CaseList {
t.Traverse(c)
}
t.visitor.Leave("CaseList", false)
}
case *ast.StmtThrow:
if nn == nil {

View File

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

View File

@ -13,26 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
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) {
for {
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.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
}
func (v *Null) StmtAltSwitch(_ *ast.StmtAltSwitch) {
// do nothing
}
func (v *Null) StmtBreak(_ *ast.StmtBreak) {
// do nothing
}
@ -70,10 +66,6 @@ func (v *Null) StmtCase(_ *ast.StmtCase) {
// do nothing
}
func (v *Null) StmtCaseList(_ *ast.StmtCaseList) {
// do nothing
}
func (v *Null) StmtCatch(_ *ast.StmtCatch) {
// do nothing
}

View File

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

View File

@ -2300,10 +2300,10 @@ func TestPrintStmtAltSwitch(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtNamespace{
Stmts: []ast.Vertex{
&ast.StmtAltSwitch{
&ast.StmtSwitch{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}},
CaseList: &ast.StmtCaseList{
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
@ -2319,7 +2319,6 @@ func TestPrintStmtAltSwitch(t *testing.T) {
},
},
},
},
})
expected := `namespace {
@ -3682,8 +3681,7 @@ func TestPrintStmtSwitch(t *testing.T) {
Stmts: []ast.Vertex{
&ast.StmtSwitch{
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}},
CaseList: &ast.StmtCaseList{
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
@ -3699,7 +3697,6 @@ func TestPrintStmtSwitch(t *testing.T) {
},
},
},
},
})
expected := `{

View File

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

View File

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

View File

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

View File

@ -2778,12 +2778,12 @@ func TestPrinterPrintStmtAltSwitch(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrinter(o)
p.Print(&ast.StmtAltSwitch{
p.Print(&ast.StmtSwitch{
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
},
CaseList: &ast.StmtCaseList{
Cases: []ast.Vertex{
Alt: true,
CaseList: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
@ -2801,7 +2801,6 @@ func TestPrinterPrintStmtAltSwitch(t *testing.T) {
},
},
},
},
})
expected := `switch($var):case 'a':$a;case 'b':$b;endswitch;`
@ -4103,8 +4102,7 @@ func TestPrinterPrintStmtSwitch(t *testing.T) {
Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$var")},
},
CaseList: &ast.StmtCaseList{
Cases: []ast.Vertex{
CaseList: []ast.Vertex{
&ast.StmtCase{
Cond: &ast.ScalarString{Value: []byte("'a'")},
Stmts: []ast.Vertex{
@ -4122,7 +4120,6 @@ func TestPrinterPrintStmtSwitch(t *testing.T) {
},
},
},
},
})
expected := `switch($var){case 'a':$a;case 'b':$b;}`