diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go index 1af3152..11e39da 100644 --- a/internal/php5/parser_test.go +++ b/internal/php5/parser_test.go @@ -9431,78 +9431,68 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 22, + EndPos: 36, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, EndLine: 3, - StartPos: 22, - EndPos: 36, + StartPos: 27, + EndPos: 28, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("1"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, EndLine: 3, - StartPos: 27, - EndPos: 28, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 30, - EndPos: 36, - }, + 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{ Position: &position.Position{ StartLine: 4, EndLine: 4, - StartPos: 40, - EndPos: 54, + StartPos: 45, + EndPos: 46, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("2"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 4, EndLine: 4, - StartPos: 45, - EndPos: 46, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 48, - EndPos: 54, - }, + StartPos: 48, + EndPos: 54, }, }, }, @@ -9560,78 +9550,68 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 23, + EndPos: 37, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, EndLine: 3, - StartPos: 23, - EndPos: 37, + StartPos: 28, + EndPos: 29, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("1"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, EndLine: 3, - StartPos: 28, - EndPos: 29, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 31, - EndPos: 37, - }, + 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{ Position: &position.Position{ StartLine: 4, EndLine: 4, - StartPos: 41, - EndPos: 55, + StartPos: 46, + EndPos: 47, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("2"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 4, EndLine: 4, - StartPos: 46, - EndPos: 47, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 49, - EndPos: 55, - }, + StartPos: 49, + EndPos: 55, }, }, }, @@ -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,71 +9671,61 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: -1, + StartPos: 22, + EndPos: -1, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, - EndLine: -1, - StartPos: 22, - EndPos: -1, + EndLine: 3, + StartPos: 27, + EndPos: 28, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 27, - EndPos: 28, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("1"), }, - &ast.StmtDefault{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 4, - EndLine: -1, - StartPos: 33, - EndPos: -1, - }, + Stmts: []ast.Vertex{}, + }, + &ast.StmtDefault{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + 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{ Position: &position.Position{ StartLine: 5, - EndLine: -1, - StartPos: 45, - EndPos: -1, + EndLine: 5, + StartPos: 50, + EndPos: 51, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 50, - EndPos: 51, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("2"), }, + Stmts: []ast.Vertex{}, }, }, }, @@ -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,60 +9780,50 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: -1, + StartPos: 23, + EndPos: -1, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, - EndLine: -1, - StartPos: 23, - EndPos: -1, + EndLine: 3, + StartPos: 28, + EndPos: 29, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 28, - EndPos: 29, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("1"), }, - &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{ Position: &position.Position{ StartLine: 4, - EndLine: -1, - StartPos: 34, - EndPos: -1, + EndLine: 4, + StartPos: 39, + EndPos: 40, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 39, - EndPos: 40, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("2"), }, + Stmts: []ast.Vertex{}, }, }, }, diff --git a/internal/php5/php5.go b/internal/php5/php5.go index 90079ef..86cad0f 100644 Binary files a/internal/php5/php5.go and b/internal/php5/php5.go differ diff --git a/internal/php5/php5.y b/internal/php5/php5.y index 4bae606..cab691e 100644 --- a/internal/php5/php5.y +++ b/internal/php5/php5.y @@ -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, + }) } ; diff --git a/internal/php5/php5_test.go b/internal/php5/php5_test.go index 1fdd0e7..8537e6c 100644 --- a/internal/php5/php5_test.go +++ b/internal/php5/php5_test.go @@ -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,75 +6915,65 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 119, + EndLine: -1, + StartPos: 2563, + EndPos: -1, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 119, - EndLine: -1, - StartPos: 2563, - EndPos: -1, + EndLine: 119, + StartPos: 2568, + EndPos: 2569, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 119, - EndLine: 119, - StartPos: 2568, - EndPos: 2569, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("1"), }, - &ast.StmtDefault{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 120, - EndLine: -1, - StartPos: 2575, - EndPos: -1, - }, + Stmts: []ast.Vertex{}, + }, + &ast.StmtDefault{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 120, + 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{ Position: &position.Position{ StartLine: 121, - EndLine: -1, - StartPos: 2588, - EndPos: -1, + EndLine: 121, + StartPos: 2593, + EndPos: 2594, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 121, - EndLine: 121, - StartPos: 2593, - EndPos: 2594, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("2"), }, + Stmts: []ast.Vertex{}, }, }, }, - &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,60 +6994,50 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 125, + EndLine: -1, + StartPos: 2626, + EndPos: -1, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 125, - EndLine: -1, - StartPos: 2626, - EndPos: -1, + EndLine: 125, + StartPos: 2631, + EndPos: 2632, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 125, - EndLine: 125, - StartPos: 2631, - EndPos: 2632, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("1"), }, - &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{ Position: &position.Position{ StartLine: 126, - EndLine: -1, - StartPos: 2638, - EndPos: -1, + EndLine: 126, + StartPos: 2643, + EndPos: 2644, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 126, - EndLine: 126, - StartPos: 2643, - EndPos: 2644, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("2"), }, + Stmts: []ast.Vertex{}, }, }, }, @@ -7079,78 +7061,68 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 130, + EndLine: 130, + StartPos: 2675, + EndPos: 2689, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 130, EndLine: 130, - StartPos: 2675, - EndPos: 2689, + StartPos: 2680, + EndPos: 2681, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("1"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 130, EndLine: 130, - StartPos: 2680, - EndPos: 2681, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 130, - EndLine: 130, - StartPos: 2683, - EndPos: 2689, - }, + 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{ Position: &position.Position{ StartLine: 131, EndLine: 131, - StartPos: 2694, - EndPos: 2708, + StartPos: 2699, + EndPos: 2700, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("2"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 131, EndLine: 131, - StartPos: 2699, - EndPos: 2700, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 131, - EndLine: 131, - StartPos: 2702, - EndPos: 2708, - }, + StartPos: 2702, + EndPos: 2708, }, }, }, @@ -7178,78 +7150,68 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 135, + EndLine: 135, + StartPos: 2730, + EndPos: 2744, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 135, EndLine: 135, - StartPos: 2730, - EndPos: 2744, + StartPos: 2735, + EndPos: 2736, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("1"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 135, EndLine: 135, - StartPos: 2735, - EndPos: 2736, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 135, - EndLine: 135, - StartPos: 2738, - EndPos: 2744, - }, + 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{ Position: &position.Position{ StartLine: 136, EndLine: 136, - StartPos: 2749, - EndPos: 2763, + StartPos: 2754, + EndPos: 2755, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("2"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 136, EndLine: 136, - StartPos: 2754, - EndPos: 2755, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 136, - EndLine: 136, - StartPos: 2757, - EndPos: 2763, - }, + StartPos: 2757, + EndPos: 2763, }, }, }, diff --git a/internal/php7/parser_test.go b/internal/php7/parser_test.go index cfcf491..5efe233 100644 --- a/internal/php7/parser_test.go +++ b/internal/php7/parser_test.go @@ -10226,78 +10226,68 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 22, + EndPos: 36, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, EndLine: 3, - StartPos: 22, - EndPos: 36, + StartPos: 27, + EndPos: 28, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("1"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, EndLine: 3, - StartPos: 27, - EndPos: 28, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 30, - EndPos: 36, - }, + 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{ Position: &position.Position{ StartLine: 4, EndLine: 4, - StartPos: 40, - EndPos: 54, + StartPos: 45, + EndPos: 46, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("2"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 4, EndLine: 4, - StartPos: 45, - EndPos: 46, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 48, - EndPos: 54, - }, + StartPos: 48, + EndPos: 54, }, }, }, @@ -10355,78 +10345,68 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 23, + EndPos: 37, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, EndLine: 3, - StartPos: 23, - EndPos: 37, + StartPos: 28, + EndPos: 29, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("1"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, EndLine: 3, - StartPos: 28, - EndPos: 29, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 31, - EndPos: 37, - }, + 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{ Position: &position.Position{ StartLine: 4, EndLine: 4, - StartPos: 41, - EndPos: 55, + StartPos: 46, + EndPos: 47, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("2"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 4, EndLine: 4, - StartPos: 46, - EndPos: 47, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 49, - EndPos: 55, - }, + StartPos: 49, + EndPos: 55, }, }, }, @@ -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,71 +10466,61 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: -1, + StartPos: 22, + EndPos: -1, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, - EndLine: -1, - StartPos: 22, - EndPos: -1, + EndLine: 3, + StartPos: 27, + EndPos: 28, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 27, - EndPos: 28, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("1"), }, - &ast.StmtDefault{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 4, - EndLine: -1, - StartPos: 33, - EndPos: -1, - }, + Stmts: []ast.Vertex{}, + }, + &ast.StmtDefault{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + 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{ Position: &position.Position{ StartLine: 5, - EndLine: -1, - StartPos: 45, - EndPos: -1, + EndLine: 5, + StartPos: 50, + EndPos: 51, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 50, - EndPos: 51, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("2"), }, + Stmts: []ast.Vertex{}, }, }, }, @@ -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,60 +10575,50 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: -1, + StartPos: 23, + EndPos: -1, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 3, - EndLine: -1, - StartPos: 23, - EndPos: -1, + EndLine: 3, + StartPos: 28, + EndPos: 29, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 28, - EndPos: 29, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("1"), }, - &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{ Position: &position.Position{ StartLine: 4, - EndLine: -1, - StartPos: 34, - EndPos: -1, + EndLine: 4, + StartPos: 39, + EndPos: 40, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 39, - EndPos: 40, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("2"), }, + Stmts: []ast.Vertex{}, }, }, }, diff --git a/internal/php7/php7.go b/internal/php7/php7.go index 80adf9a..90b1972 100644 Binary files a/internal/php7/php7.go and b/internal/php7/php7.go differ diff --git a/internal/php7/php7.y b/internal/php7/php7.y index 0ecf1de..9cf8c08 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -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, + }) } ; diff --git a/internal/php7/php7_test.go b/internal/php7/php7_test.go index 24499be..06528b4 100644 --- a/internal/php7/php7_test.go +++ b/internal/php7/php7_test.go @@ -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,75 +7359,65 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 123, + EndLine: -1, + StartPos: 2651, + EndPos: -1, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 123, - EndLine: -1, - StartPos: 2651, - EndPos: -1, + EndLine: 123, + StartPos: 2656, + EndPos: 2657, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 123, - EndLine: 123, - StartPos: 2656, - EndPos: 2657, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("1"), }, - &ast.StmtDefault{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 124, - EndLine: -1, - StartPos: 2663, - EndPos: -1, - }, + Stmts: []ast.Vertex{}, + }, + &ast.StmtDefault{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 124, + 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{ Position: &position.Position{ StartLine: 125, - EndLine: -1, - StartPos: 2676, - EndPos: -1, + EndLine: 125, + StartPos: 2681, + EndPos: 2682, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 125, - EndLine: 125, - StartPos: 2681, - EndPos: 2682, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("2"), }, + Stmts: []ast.Vertex{}, }, }, }, - &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,60 +7438,50 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 129, + EndLine: -1, + StartPos: 2714, + EndPos: -1, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 129, - EndLine: -1, - StartPos: 2714, - EndPos: -1, + EndLine: 129, + StartPos: 2719, + EndPos: 2720, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 129, - EndLine: 129, - StartPos: 2719, - EndPos: 2720, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("1"), }, - &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{ Position: &position.Position{ StartLine: 130, - EndLine: -1, - StartPos: 2726, - EndPos: -1, + EndLine: 130, + StartPos: 2731, + EndPos: 2732, }, }, - Cond: &ast.ScalarLnumber{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 130, - EndLine: 130, - StartPos: 2731, - EndPos: 2732, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{}, + Value: []byte("2"), }, + Stmts: []ast.Vertex{}, }, }, }, @@ -7523,78 +7505,68 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 134, + EndLine: 134, + StartPos: 2763, + EndPos: 2777, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 134, EndLine: 134, - StartPos: 2763, - EndPos: 2777, + StartPos: 2768, + EndPos: 2769, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("1"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 134, EndLine: 134, - StartPos: 2768, - EndPos: 2769, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 134, - EndLine: 134, - StartPos: 2771, - EndPos: 2777, - }, + 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{ Position: &position.Position{ StartLine: 135, EndLine: 135, - StartPos: 2782, - EndPos: 2796, + StartPos: 2787, + EndPos: 2788, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("2"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 135, EndLine: 135, - StartPos: 2787, - EndPos: 2788, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 135, - EndLine: 135, - StartPos: 2790, - EndPos: 2796, - }, + StartPos: 2790, + EndPos: 2796, }, }, }, @@ -7622,78 +7594,68 @@ 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, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 139, + EndLine: 139, + StartPos: 2818, + EndPos: 2832, + }, }, - }, - Cases: []ast.Vertex{ - &ast.StmtCase{ + Cond: &ast.ScalarLnumber{ Node: ast.Node{ Position: &position.Position{ StartLine: 139, EndLine: 139, - StartPos: 2818, - EndPos: 2832, + StartPos: 2823, + EndPos: 2824, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("1"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 139, EndLine: 139, - StartPos: 2823, - EndPos: 2824, - }, - }, - Value: []byte("1"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 139, - EndLine: 139, - StartPos: 2826, - EndPos: 2832, - }, + 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{ Position: &position.Position{ StartLine: 140, EndLine: 140, - StartPos: 2837, - EndPos: 2851, + StartPos: 2842, + EndPos: 2843, }, }, - Cond: &ast.ScalarLnumber{ + Value: []byte("2"), + }, + Stmts: []ast.Vertex{ + &ast.StmtBreak{ Node: ast.Node{ Position: &position.Position{ StartLine: 140, EndLine: 140, - StartPos: 2842, - EndPos: 2843, - }, - }, - Value: []byte("2"), - }, - Stmts: []ast.Vertex{ - &ast.StmtBreak{ - Node: ast.Node{ - Position: &position.Position{ - StartLine: 140, - EndLine: 140, - StartPos: 2845, - EndPos: 2851, - }, + StartPos: 2845, + EndPos: 2851, }, }, }, diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index d86bf68..33713e7 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -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) diff --git a/pkg/ast/node.go b/pkg/ast/node.go index 80685cd..26850a4 100644 --- a/pkg/ast/node.go +++ b/pkg/ast/node.go @@ -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,24 +201,16 @@ func (n *StmtBreak) Accept(v NodeVisitor) { // StmtCase node type StmtCase struct { Node - Cond Vertex - Stmts []Vertex + CaseTkn *token.Token + Cond Vertex + CaseSeparatorTkn *token.Token + Stmts []Vertex } 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,7 +336,9 @@ func (n *StmtDeclare) Accept(v NodeVisitor) { // StmtDefault node type StmtDefault struct { Node - Stmts []Vertex + DefaultTkn *token.Token + CaseSeparatorTkn *token.Token + Stmts []Vertex } func (n *StmtDefault) Accept(v NodeVisitor) { @@ -674,8 +657,18 @@ func (n *StmtStmtList) Accept(v NodeVisitor) { // StmtSwitch node type StmtSwitch struct { Node - Cond Vertex - CaseList *StmtCaseList + Alt bool + 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) { diff --git a/pkg/ast/traverser/dfs.go b/pkg/ast/traverser/dfs.go index 629d6ec..cca893c 100644 --- a/pkg/ast/traverser/dfs.go +++ b/pkg/ast/traverser/dfs.go @@ -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 { diff --git a/pkg/ast/visitor/dump.go b/pkg/ast/visitor/dump.go index bd9cc4e..5569a60 100644 --- a/pkg/ast/visitor/dump.go +++ b/pkg/ast/visitor/dump.go @@ -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) { diff --git a/pkg/ast/visitor/filter_parser_nodes.go b/pkg/ast/visitor/filter_parser_nodes.go index 9160d72..0d7ce76 100644 --- a/pkg/ast/visitor/filter_parser_nodes.go +++ b/pkg/ast/visitor/filter_parser_nodes.go @@ -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 { diff --git a/pkg/ast/visitor/filter_tokens.go b/pkg/ast/visitor/filter_tokens.go index 9ec8a3d..b59f05b 100644 --- a/pkg/ast/visitor/filter_tokens.go +++ b/pkg/ast/visitor/filter_tokens.go @@ -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 +} diff --git a/pkg/ast/visitor/null.go b/pkg/ast/visitor/null.go index 7fd4992..225a604 100644 --- a/pkg/ast/visitor/null.go +++ b/pkg/ast/visitor/null.go @@ -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 } diff --git a/pkg/printer/pretty_printer.go b/pkg/printer/pretty_printer.go index 1053b76..987655d 100644 --- a/pkg/printer/pretty_printer.go +++ b/pkg/printer/pretty_printer.go @@ -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) diff --git a/pkg/printer/pretty_printer_test.go b/pkg/printer/pretty_printer_test.go index 0eec375..54fcdd0 100644 --- a/pkg/printer/pretty_printer_test.go +++ b/pkg/printer/pretty_printer_test.go @@ -2300,21 +2300,20 @@ 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{ - &ast.StmtCase{ - Cond: &ast.ScalarString{Value: []byte("'a'")}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Cond: &ast.ScalarString{Value: []byte("'a'")}, + Stmts: []ast.Vertex{ + &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, }, - &ast.StmtCase{ - Cond: &ast.ScalarString{Value: []byte("'b'")}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, + }, + &ast.StmtCase{ + Cond: &ast.ScalarString{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{ &ast.StmtSwitch{ Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("var")}}, - CaseList: &ast.StmtCaseList{ - Cases: []ast.Vertex{ - &ast.StmtCase{ - Cond: &ast.ScalarString{Value: []byte("'a'")}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, - }, + CaseList: []ast.Vertex{ + &ast.StmtCase{ + Cond: &ast.ScalarString{Value: []byte("'a'")}, + Stmts: []ast.Vertex{ + &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}}, }, - &ast.StmtCase{ - Cond: &ast.ScalarString{Value: []byte("'b'")}, - Stmts: []ast.Vertex{ - &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, - }, + }, + &ast.StmtCase{ + Cond: &ast.ScalarString{Value: []byte("'b'")}, + Stmts: []ast.Vertex{ + &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, }, }, }, diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index b7ded2f..5064d70 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -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) { diff --git a/pkg/printer/printer_parsed_php5_test.go b/pkg/printer/printer_parsed_php5_test.go index c607b65..2ea9ea4 100644 --- a/pkg/printer/printer_parsed_php5_test.go +++ b/pkg/printer/printer_parsed_php5_test.go @@ -791,7 +791,8 @@ func TestParseAndPrintPhp5AltForeach(t *testing.T) { } func TestParseAndPrintPhp5AltSwitch(t *testing.T) { - src := `