[refactoring] update ast structure of "If", "ElseIf", "Else" nodes

This commit is contained in:
Vadym Slizov 2020-09-03 22:18:06 +03:00
parent 954208510e
commit 6976388a82
21 changed files with 537 additions and 636 deletions

View File

@ -3660,7 +3660,7 @@ func TestStmtAltIf_AltIf(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -3669,6 +3669,7 @@ func TestStmtAltIf_AltIf(t *testing.T) {
EndPos: 23, EndPos: 23,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3731,7 +3732,7 @@ func TestStmtAltIf_AltElseIf(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -3740,6 +3741,7 @@ func TestStmtAltIf_AltElseIf(t *testing.T) {
EndPos: 38, EndPos: 38,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3773,7 +3775,7 @@ func TestStmtAltIf_AltElseIf(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
@ -3782,6 +3784,7 @@ func TestStmtAltIf_AltElseIf(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3846,7 +3849,7 @@ func TestStmtAltIf_AltElse(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -3855,6 +3858,7 @@ func TestStmtAltIf_AltElse(t *testing.T) {
EndPos: 31, EndPos: 31,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3887,7 +3891,7 @@ func TestStmtAltIf_AltElse(t *testing.T) {
}, },
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
@ -3896,6 +3900,7 @@ func TestStmtAltIf_AltElse(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3940,7 +3945,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -3949,6 +3954,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
EndPos: 61, EndPos: 61,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3982,7 +3988,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
@ -3991,6 +3997,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -4024,7 +4031,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
}, },
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
@ -4033,6 +4040,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -4067,7 +4075,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
}, },
}, },
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 5, StartLine: 5,
@ -4076,6 +4084,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -855,46 +855,49 @@ unticked_statement:
} }
| T_IF parenthesis_expr statement elseif_list else_single | T_IF parenthesis_expr statement elseif_list else_single
{ {
$$ = &ast.StmtIf{ast.Node{}, $2, $3, $4, $5} pos := position.NewTokenNodePosition($1, $3)
// save position
if $5 != nil { if $5 != nil {
$$.GetNode().Position = position.NewTokenNodePosition($1, $5) pos = position.NewTokenNodePosition($1, $5)
} else if len($4) > 0 { } else if len($4) > 0 {
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $4) pos = position.NewTokenNodeListPosition($1, $4)
} else {
$$.GetNode().Position = position.NewTokenNodePosition($1, $3)
} }
// save comments $$ = &ast.StmtIf{
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) Node: ast.Node{
Position: pos,
},
IfTkn: $1,
OpenParenthesisTkn: $2.(*ast.ParserBrackets).OpenBracketTkn,
Cond: $2.(*ast.ParserBrackets).Child,
CloseParenthesisTkn: $2.(*ast.ParserBrackets).CloseBracketTkn,
Stmt: $3,
ElseIf: $4,
Else: $5,
}
} }
| T_IF parenthesis_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';' | T_IF parenthesis_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
{ {
stmts := &ast.StmtStmtList{ $$ = &ast.StmtIf{
Node: ast.Node{
Position: position.NewTokensPosition($1, $8),
},
Alt: true,
IfTkn: $1,
OpenParenthesisTkn: $2.(*ast.ParserBrackets).OpenBracketTkn,
Cond: $2.(*ast.ParserBrackets).Child,
CloseParenthesisTkn: $2.(*ast.ParserBrackets).CloseBracketTkn,
ColonTkn: $3,
Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($4), Position: position.NewNodeListPosition($4),
}, },
Stmts: $4, Stmts: $4,
},
ElseIf: $5,
Else: $6,
EndIfTkn: $7,
SemiColonTkn: $8,
} }
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
$$ = &ast.StmtAltIf{ast.Node{}, $2, stmtsBrackets, $5, $6}
// save position
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($3, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $8)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(stmtsBrackets, token.Start, $3.SkippedTokens)
if $6 != nil {
yylex.(*Parser).setFreeFloating($6.(*ast.StmtAltElse).Stmt, token.End, append($7.SkippedTokens, $8.SkippedTokens...))
} else if len($5) > 0 {
yylex.(*Parser).setFreeFloating($5[len($5)-1].(*ast.StmtAltElseIf).Stmt, token.End, append($7.SkippedTokens, $8.SkippedTokens...))
} else {
yylex.(*Parser).setFreeFloating(stmtsBrackets, token.End, append($7.SkippedTokens, $8.SkippedTokens...))
}
yylex.(*Parser).setToken($$, token.SemiColon, $8.SkippedTokens)
} }
| T_WHILE parenthesis_expr while_statement | T_WHILE parenthesis_expr while_statement
{ {
@ -1946,14 +1949,16 @@ elseif_list:
} }
| elseif_list T_ELSEIF parenthesis_expr statement | elseif_list T_ELSEIF parenthesis_expr statement
{ {
_elseIf := &ast.StmtElseIf{ast.Node{}, $3, $4} $$ = append($1, &ast.StmtElseIf{
$$ = append($1, _elseIf) Node: ast.Node{
Position: position.NewTokenNodePosition($2, $4),
// save position },
_elseIf.GetNode().Position = position.NewTokenNodePosition($2, $4) ElseIfTkn: $2,
OpenParenthesisTkn: $3.(*ast.ParserBrackets).OpenBracketTkn,
// save comments Cond: $3.(*ast.ParserBrackets).Child,
yylex.(*Parser).setFreeFloating(_elseIf, token.Start, $2.SkippedTokens) CloseParenthesisTkn: $3.(*ast.ParserBrackets).CloseBracketTkn,
Stmt: $4,
})
} }
; ;
@ -1965,23 +1970,23 @@ new_elseif_list:
} }
| new_elseif_list T_ELSEIF parenthesis_expr ':' inner_statement_list | new_elseif_list T_ELSEIF parenthesis_expr ':' inner_statement_list
{ {
stmts := &ast.StmtStmtList{ $$ = append($1, &ast.StmtElseIf{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $5),
},
Alt: true,
ElseIfTkn: $2,
OpenParenthesisTkn: $3.(*ast.ParserBrackets).OpenBracketTkn,
Cond: $3.(*ast.ParserBrackets).Child,
CloseParenthesisTkn: $3.(*ast.ParserBrackets).CloseBracketTkn,
ColonTkn: $4,
Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($5), Position: position.NewNodeListPosition($5),
}, },
Stmts: $5, Stmts: $5,
} },
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} })
_elseIf := &ast.StmtAltElseIf{ast.Node{}, $3, stmtsBrackets}
$$ = append($1, _elseIf)
// save position
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($4, $5)
_elseIf.GetNode().Position = position.NewTokenNodeListPosition($2, $5)
// save comments
yylex.(*Parser).setFreeFloating(_elseIf, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(stmtsBrackets, token.Start, $4.SkippedTokens)
} }
; ;
@ -1993,13 +1998,13 @@ else_single:
} }
| T_ELSE statement | T_ELSE statement
{ {
$$ = &ast.StmtElse{ast.Node{}, $2} $$ = &ast.StmtElse{
Node: ast.Node{
// save position Position: position.NewTokenNodePosition($1, $2),
$$.GetNode().Position = position.NewTokenNodePosition($1, $2) },
ElseTkn: $1,
// save comments Stmt: $2,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) }
} }
; ;
@ -2011,22 +2016,20 @@ new_else_single:
} }
| T_ELSE ':' inner_statement_list | T_ELSE ':' inner_statement_list
{ {
stmts := &ast.StmtStmtList{ $$ = &ast.StmtElse{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($1, $3),
},
Alt: true,
ElseTkn: $1,
ColonTkn: $2,
Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($3), Position: position.NewNodeListPosition($3),
}, },
Stmts: $3, Stmts: $3,
},
} }
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
$$ = &ast.StmtAltElse{ast.Node{}, stmtsBrackets}
// save position
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($2, $3)
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(stmtsBrackets, token.Start, $2.SkippedTokens)
} }
; ;
@ -4318,10 +4321,13 @@ exit_expr:
} }
| '(' ')' | '(' ')'
{ {
$$ = &ast.ParserBrackets{ast.Node{}, nil} $$ = &ast.ParserBrackets{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $2),
$$.GetNode().Position = position.NewTokensPosition($1, $2) },
OpenBracketTkn: $1,
CloseBracketTkn: $2,
}
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens) yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
@ -5166,10 +5172,14 @@ expr:
parenthesis_expr: parenthesis_expr:
'(' expr ')' '(' expr ')'
{ {
$$ = &ast.ParserBrackets{ast.Node{}, $2} $$ = &ast.ParserBrackets{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenBracketTkn: $1,
Child: $2,
CloseBracketTkn: $3,
}
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens) yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
@ -5177,10 +5187,14 @@ parenthesis_expr:
} }
| '(' yield_expr ')' | '(' yield_expr ')'
{ {
$$ = &ast.ParserBrackets{ast.Node{}, $2} $$ = &ast.ParserBrackets{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenBracketTkn: $1,
Child: $2,
CloseBracketTkn: $3,
}
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens) yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
@ -6008,11 +6022,17 @@ internal_functions_in_yacc:
} }
| T_EMPTY '(' variable ')' | T_EMPTY '(' variable ')'
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($2, $4),
},
OpenBracketTkn: $2,
Child: $3,
CloseBracketTkn: $4,
}
$$ = &ast.ExprEmpty{ast.Node{}, exprBrackets} $$ = &ast.ExprEmpty{ast.Node{}, exprBrackets}
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -6022,11 +6042,17 @@ internal_functions_in_yacc:
} }
| T_EMPTY '(' expr ')' | T_EMPTY '(' expr ')'
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($2, $4),
},
OpenBracketTkn: $2,
Child: $3,
CloseBracketTkn: $4,
}
$$ = &ast.ExprEmpty{ast.Node{}, exprBrackets} $$ = &ast.ExprEmpty{ast.Node{}, exprBrackets}
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -6056,11 +6082,17 @@ internal_functions_in_yacc:
} }
| T_EVAL '(' expr ')' | T_EVAL '(' expr ')'
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($2, $4),
},
OpenBracketTkn: $2,
Child: $3,
CloseBracketTkn: $4,
}
$$ = &ast.ExprEval{ast.Node{}, exprBrackets} $$ = &ast.ExprEval{ast.Node{}, exprBrackets}
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments

View File

@ -2603,7 +2603,7 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 41, StartLine: 41,
@ -2612,6 +2612,7 @@ func TestPhp5(t *testing.T) {
EndPos: 867, EndPos: 867,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -2645,7 +2646,7 @@ func TestPhp5(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
}, },
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 43, StartLine: 43,
@ -2654,6 +2655,7 @@ func TestPhp5(t *testing.T) {
EndPos: 897, EndPos: 897,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -2687,7 +2689,7 @@ func TestPhp5(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 44, StartLine: 44,
@ -2696,6 +2698,7 @@ func TestPhp5(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -2731,7 +2734,7 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 46, StartLine: 46,
@ -2740,6 +2743,7 @@ func TestPhp5(t *testing.T) {
EndPos: 920, EndPos: 920,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -2772,7 +2776,7 @@ func TestPhp5(t *testing.T) {
}, },
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 47, StartLine: 47,
@ -2781,6 +2785,7 @@ func TestPhp5(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -2794,7 +2799,7 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 49, StartLine: 49,
@ -2803,6 +2808,7 @@ func TestPhp5(t *testing.T) {
EndPos: 969, EndPos: 969,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -2836,7 +2842,7 @@ func TestPhp5(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 50, StartLine: 50,
@ -2845,6 +2851,7 @@ func TestPhp5(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -2878,7 +2885,7 @@ func TestPhp5(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
}, },
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 51, StartLine: 51,
@ -2887,6 +2894,7 @@ func TestPhp5(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -2921,7 +2929,7 @@ func TestPhp5(t *testing.T) {
}, },
}, },
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 52, StartLine: 52,
@ -2930,6 +2938,7 @@ func TestPhp5(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

View File

@ -3811,7 +3811,7 @@ func TestStmtAltIf_AltIf(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -3820,6 +3820,7 @@ func TestStmtAltIf_AltIf(t *testing.T) {
EndPos: 23, EndPos: 23,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3882,7 +3883,7 @@ func TestStmtAltIf_AltElseIf(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -3891,6 +3892,7 @@ func TestStmtAltIf_AltElseIf(t *testing.T) {
EndPos: 38, EndPos: 38,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3924,7 +3926,7 @@ func TestStmtAltIf_AltElseIf(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
@ -3933,6 +3935,7 @@ func TestStmtAltIf_AltElseIf(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3997,7 +4000,7 @@ func TestStmtAltIf_AltElse(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -4006,6 +4009,7 @@ func TestStmtAltIf_AltElse(t *testing.T) {
EndPos: 31, EndPos: 31,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -4038,7 +4042,7 @@ func TestStmtAltIf_AltElse(t *testing.T) {
}, },
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
@ -4047,6 +4051,7 @@ func TestStmtAltIf_AltElse(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -4091,7 +4096,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
}, },
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 2, StartLine: 2,
@ -4100,6 +4105,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
EndPos: 61, EndPos: 61,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -4133,7 +4139,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 3, StartLine: 3,
@ -4142,6 +4148,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -4175,7 +4182,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
}, },
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 4, StartLine: 4,
@ -4184,6 +4191,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -4218,7 +4226,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
}, },
}, },
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 5, StartLine: 5,
@ -4227,6 +4235,7 @@ func TestStmtAltIf_AltElseElseIf(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -870,7 +870,14 @@ statement:
} }
| T_WHILE '(' expr ')' while_statement | T_WHILE '(' expr ')' while_statement
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($2, $4),
},
OpenBracketTkn: $2,
Child: $3,
CloseBracketTkn: $4,
}
switch n := $5.(type) { switch n := $5.(type) {
case *ast.StmtWhile : case *ast.StmtWhile :
@ -882,7 +889,6 @@ statement:
$$ = $5 $$ = $5
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
$$.GetNode().Position = position.NewTokenNodePosition($1, $5) $$.GetNode().Position = position.NewTokenNodePosition($1, $5)
// save comments // save comments
@ -892,11 +898,17 @@ statement:
} }
| T_DO statement T_WHILE '(' expr ')' ';' | T_DO statement T_WHILE '(' expr ')' ';'
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $5} exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($4, $6),
},
OpenBracketTkn: $4,
Child: $5,
CloseBracketTkn: $6,
}
$$ = &ast.StmtDo{ast.Node{}, $2, exprBrackets} $$ = &ast.StmtDo{ast.Node{}, $2, exprBrackets}
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($4, $6)
$$.GetNode().Position = position.NewTokensPosition($1, $7) $$.GetNode().Position = position.NewTokensPosition($1, $7)
// save comments // save comments
@ -933,7 +945,14 @@ statement:
} }
| T_SWITCH '(' expr ')' switch_case_list | T_SWITCH '(' expr ')' switch_case_list
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($2, $4),
},
OpenBracketTkn: $2,
Child: $3,
CloseBracketTkn: $4,
}
switch n := $5.(type) { switch n := $5.(type) {
case *ast.StmtSwitch: case *ast.StmtSwitch:
@ -1750,35 +1769,33 @@ while_statement:
if_stmt_without_else: if_stmt_without_else:
T_IF '(' expr ')' statement T_IF '(' expr ')' statement
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} $$ = &ast.StmtIf{
$$ = &ast.StmtIf{ast.Node{}, exprBrackets, $5, nil, nil} Node: ast.Node{
Position: position.NewTokenNodePosition($1, $5),
// save position },
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) IfTkn: $1,
$$.GetNode().Position = position.NewTokenNodePosition($1, $5) OpenParenthesisTkn: $2,
Cond: $3,
// save comments CloseParenthesisTkn: $4,
yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) Stmt: $5,
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.SkippedTokens) }
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.SkippedTokens)
} }
| if_stmt_without_else T_ELSEIF '(' expr ')' statement | if_stmt_without_else T_ELSEIF '(' expr ')' statement
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $4} $1.(*ast.StmtIf).ElseIf = append($1.(*ast.StmtIf).ElseIf, &ast.StmtElseIf{
_elseIf := &ast.StmtElseIf{ast.Node{}, exprBrackets, $6} Node: ast.Node{
$1.(*ast.StmtIf).ElseIf = append($1.(*ast.StmtIf).ElseIf, _elseIf) Position: position.NewTokenNodePosition($2, $6),
},
ElseIfTkn: $2,
OpenParenthesisTkn: $3,
Cond: $4,
CloseParenthesisTkn: $5,
Stmt: $6,
})
$1.(*ast.StmtIf).Node.Position = position.NewNodesPosition($1, $6)
$$ = $1 $$ = $1
// save position
exprBrackets.GetNode().Position = position.NewTokensPosition($3, $5)
_elseIf.GetNode().Position = position.NewTokenNodePosition($2, $6)
$$.GetNode().Position = position.NewNodesPosition($1, $6)
// save comments
yylex.(*Parser).setFreeFloating(_elseIf, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $3.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $5.SkippedTokens)
} }
; ;
@ -1789,113 +1806,95 @@ if_stmt:
} }
| if_stmt_without_else T_ELSE statement | if_stmt_without_else T_ELSE statement
{ {
_else := &ast.StmtElse{ast.Node{}, $3} $1.(*ast.StmtIf).Else = &ast.StmtElse{
$1.(*ast.StmtIf).Else = _else Node: ast.Node{
Position: position.NewTokenNodePosition($2, $3),
},
ElseTkn: $2,
Stmt: $3,
}
$1.(*ast.StmtIf).Node.Position = position.NewNodesPosition($1, $3)
$$ = $1 $$ = $1
// save position
_else.GetNode().Position = position.NewTokenNodePosition($2, $3)
$$.GetNode().Position = position.NewNodesPosition($1, $3)
// save comments
yylex.(*Parser).setFreeFloating(_else, token.Start, $2.SkippedTokens)
} }
; ;
alt_if_stmt_without_else: alt_if_stmt_without_else:
T_IF '(' expr ')' ':' inner_statement_list T_IF '(' expr ')' ':' inner_statement_list
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} $$ = &ast.StmtIf{
stmts := &ast.StmtStmtList{ Node: ast.Node{
Position: position.NewTokenNodeListPosition($1, $6),
},
Alt: true,
IfTkn: $1,
OpenParenthesisTkn: $2,
Cond: $3,
CloseParenthesisTkn: $4,
ColonTkn: $5,
Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($6), Position: position.NewNodeListPosition($6),
}, },
Stmts: $6, Stmts: $6,
},
} }
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts}
$$ = &ast.StmtAltIf{ast.Node{}, exprBrackets, stmtsBrackets, nil, nil}
// save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($5, $6)
$$.GetNode().Position = position.NewTokenNodeListPosition($1, $6)
// 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)
yylex.(*Parser).setFreeFloatingTokens(stmtsBrackets, token.Start, $5.SkippedTokens)
} }
| alt_if_stmt_without_else T_ELSEIF '(' expr ')' ':' inner_statement_list | alt_if_stmt_without_else T_ELSEIF '(' expr ')' ':' inner_statement_list
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $4} $1.(*ast.StmtIf).ElseIf = append($1.(*ast.StmtIf).ElseIf, &ast.StmtElseIf{
stmts := &ast.StmtStmtList{ Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $7),
},
Alt: true,
ElseIfTkn: $2,
OpenParenthesisTkn: $3,
Cond: $4,
CloseParenthesisTkn: $5,
ColonTkn: $6,
Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($7), Position: position.NewNodeListPosition($7),
}, },
Stmts: $7, Stmts: $7,
} },
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} })
_elseIf := &ast.StmtAltElseIf{ast.Node{}, exprBrackets, stmtsBrackets}
$1.(*ast.StmtAltIf).ElseIf = append($1.(*ast.StmtAltIf).ElseIf, _elseIf)
$$ = $1 $$ = $1
// save position
exprBrackets.GetNode().Position = position.NewTokensPosition($3, $5)
stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($6, $7)
_elseIf.GetNode().Position = position.NewTokenNodeListPosition($2, $7)
// save comments
yylex.(*Parser).setFreeFloating(_elseIf, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $3.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $5.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(stmtsBrackets, token.Start, $6.SkippedTokens)
} }
; ;
alt_if_stmt: alt_if_stmt:
alt_if_stmt_without_else T_ENDIF ';' alt_if_stmt_without_else T_ENDIF ';'
{ {
$1.(*ast.StmtIf).EndIfTkn = $2
$1.(*ast.StmtIf).SemiColonTkn = $3
$1.(*ast.StmtIf).Node.Position = position.NewNodeTokenPosition($1, $3)
$$ = $1 $$ = $1
// save position
$$.GetNode().Position = position.NewNodeTokenPosition($1, $3)
// save comments
altif := $$.(*ast.StmtAltIf)
if len(altif.ElseIf) > 0 {
yylex.(*Parser).setFreeFloating(altif.ElseIf[len(altif.ElseIf)-1], token.End, append($2.SkippedTokens, $3.SkippedTokens...))
} else {
yylex.(*Parser).setFreeFloating(altif.Stmt, token.End, append($2.SkippedTokens, $3.SkippedTokens...))
}
yylex.(*Parser).setToken($$, token.SemiColon, $3.SkippedTokens)
} }
| alt_if_stmt_without_else T_ELSE ':' inner_statement_list T_ENDIF ';' | alt_if_stmt_without_else T_ELSE ':' inner_statement_list T_ENDIF ';'
{ {
stmts := &ast.StmtStmtList{ $1.(*ast.StmtIf).Else = &ast.StmtElse{
Node: ast.Node{
Position: position.NewTokenNodeListPosition($2, $4),
},
Alt: true,
ElseTkn: $2,
ColonTkn: $3,
Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: position.NewNodeListPosition($4), Position: position.NewNodeListPosition($4),
}, },
Stmts: $4, Stmts: $4,
},
} }
stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} $1.(*ast.StmtIf).EndIfTkn = $5
_else := &ast.StmtAltElse{ast.Node{}, stmtsBrackets} $1.(*ast.StmtIf).SemiColonTkn = $6
$1.(*ast.StmtAltIf).Else = _else $1.(*ast.StmtIf).Node.Position = position.NewNodeTokenPosition($1, $6)
$$ = $1 $$ = $1
// save position
stmtsBrackets.GetNode().Position = position.NewTokensPosition($3, $5)
_else.GetNode().Position = position.NewTokenNodeListPosition($2, $4)
$$.GetNode().Position = position.NewNodeTokenPosition($1, $6)
// save comments
yylex.(*Parser).setFreeFloating(_else, token.Start, $2.SkippedTokens)
yylex.(*Parser).setFreeFloatingTokens(stmtsBrackets, token.Start, $3.SkippedTokens)
yylex.(*Parser).setFreeFloating(stmtsBrackets, token.End, append($5.SkippedTokens, $6.SkippedTokens...))
yylex.(*Parser).setToken($$, token.SemiColon, $6.SkippedTokens)
} }
; ;
@ -3368,10 +3367,14 @@ expr_without_variable:
} }
| '(' expr ')' | '(' expr ')'
{ {
$$ = &ast.ParserBrackets{ast.Node{}, $2} $$ = &ast.ParserBrackets{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenBracketTkn: $1,
Child: $2,
CloseBracketTkn: $3,
}
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens) yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
@ -3825,10 +3828,14 @@ exit_expr:
} }
| '(' optional_expr ')' | '(' optional_expr ')'
{ {
$$ = &ast.ParserBrackets{ast.Node{}, $2} $$ = &ast.ParserBrackets{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenBracketTkn: $1,
Child: $2,
CloseBracketTkn: $3,
}
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens) yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
@ -4132,10 +4139,14 @@ dereferencable:
} }
| '(' expr ')' | '(' expr ')'
{ {
$$ = &ast.ParserBrackets{ast.Node{}, $2} $$ = &ast.ParserBrackets{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenBracketTkn: $1,
Child: $2,
CloseBracketTkn: $3,
}
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens) yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
@ -4154,10 +4165,14 @@ callable_expr:
} }
| '(' expr ')' | '(' expr ')'
{ {
$$ = &ast.ParserBrackets{ast.Node{}, $2} $$ = &ast.ParserBrackets{
Node: ast.Node{
// save position Position: position.NewTokensPosition($1, $3),
$$.GetNode().Position = position.NewTokensPosition($1, $3) },
OpenBracketTkn: $1,
Child: $2,
CloseBracketTkn: $3,
}
// save comments // save comments
yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens) yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.SkippedTokens)
@ -4768,11 +4783,17 @@ internal_functions_in_yacc:
} }
| T_EMPTY '(' expr ')' | T_EMPTY '(' expr ')'
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($2, $4),
},
OpenBracketTkn: $2,
Child: $3,
CloseBracketTkn: $4,
}
$$ = &ast.ExprEmpty{ast.Node{}, exprBrackets} $$ = &ast.ExprEmpty{ast.Node{}, exprBrackets}
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments
@ -4802,11 +4823,17 @@ internal_functions_in_yacc:
} }
| T_EVAL '(' expr ')' | T_EVAL '(' expr ')'
{ {
exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} exprBrackets := &ast.ParserBrackets{
Node: ast.Node{
Position: position.NewTokensPosition($2, $4),
},
OpenBracketTkn: $2,
Child: $3,
CloseBracketTkn: $4,
}
$$ = &ast.ExprEval{ast.Node{}, exprBrackets} $$ = &ast.ExprEval{ast.Node{}, exprBrackets}
// save position // save position
exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4)
$$.GetNode().Position = position.NewTokensPosition($1, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4)
// save comments // save comments

View File

@ -3375,7 +3375,7 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 53, StartLine: 53,
@ -3384,6 +3384,7 @@ func TestPhp7(t *testing.T) {
EndPos: 1111, EndPos: 1111,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3417,7 +3418,7 @@ func TestPhp7(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
}, },
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 55, StartLine: 55,
@ -3426,6 +3427,7 @@ func TestPhp7(t *testing.T) {
EndPos: 1141, EndPos: 1141,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3459,7 +3461,7 @@ func TestPhp7(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 56, StartLine: 56,
@ -3468,6 +3470,7 @@ func TestPhp7(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3503,7 +3506,7 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 58, StartLine: 58,
@ -3512,6 +3515,7 @@ func TestPhp7(t *testing.T) {
EndPos: 1164, EndPos: 1164,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3544,7 +3548,7 @@ func TestPhp7(t *testing.T) {
}, },
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 59, StartLine: 59,
@ -3553,6 +3557,7 @@ func TestPhp7(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3566,7 +3571,7 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltIf{ &ast.StmtIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 61, StartLine: 61,
@ -3575,6 +3580,7 @@ func TestPhp7(t *testing.T) {
EndPos: 1213, EndPos: 1213,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3608,7 +3614,7 @@ func TestPhp7(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 62, StartLine: 62,
@ -3617,6 +3623,7 @@ func TestPhp7(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3650,7 +3657,7 @@ func TestPhp7(t *testing.T) {
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
}, },
}, },
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 63, StartLine: 63,
@ -3659,6 +3666,7 @@ func TestPhp7(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
@ -3693,7 +3701,7 @@ func TestPhp7(t *testing.T) {
}, },
}, },
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{
StartLine: 64, StartLine: 64,
@ -3702,6 +3710,7 @@ func TestPhp7(t *testing.T) {
EndPos: -1, EndPos: -1,
}, },
}, },
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Node: ast.Node{ Node: ast.Node{
Position: &position.Position{ Position: &position.Position{

View File

@ -27,11 +27,8 @@ type NodeVisitor interface {
ArgumentList(n *ArgumentList) ArgumentList(n *ArgumentList)
Argument(n *Argument) Argument(n *Argument)
StmtAltElse(n *StmtAltElse)
StmtAltElseIf(n *StmtAltElseIf)
StmtAltFor(n *StmtAltFor) StmtAltFor(n *StmtAltFor)
StmtAltForeach(n *StmtAltForeach) StmtAltForeach(n *StmtAltForeach)
StmtAltIf(n *StmtAltIf)
StmtAltSwitch(n *StmtAltSwitch) StmtAltSwitch(n *StmtAltSwitch)
StmtAltWhile(n *StmtAltWhile) StmtAltWhile(n *StmtAltWhile)
StmtBreak(n *StmtBreak) StmtBreak(n *StmtBreak)

View File

@ -175,27 +175,6 @@ func (n *ScalarString) Accept(v NodeVisitor) {
v.ScalarString(n) v.ScalarString(n)
} }
// StmtAltElse node
type StmtAltElse struct {
Node
Stmt Vertex
}
func (n *StmtAltElse) Accept(v NodeVisitor) {
v.StmtAltElse(n)
}
// StmtAltElseIf node
type StmtAltElseIf struct {
Node
Cond Vertex
Stmt Vertex
}
func (n *StmtAltElseIf) Accept(v NodeVisitor) {
v.StmtAltElseIf(n)
}
// StmtAltFor node // StmtAltFor node
type StmtAltFor struct { type StmtAltFor struct {
Node Node
@ -222,19 +201,6 @@ func (n *StmtAltForeach) Accept(v NodeVisitor) {
v.StmtAltForeach(n) v.StmtAltForeach(n)
} }
// StmtAltIf node
type StmtAltIf struct {
Node
Cond Vertex
Stmt Vertex
ElseIf []Vertex
Else Vertex
}
func (n *StmtAltIf) Accept(v NodeVisitor) {
v.StmtAltIf(n)
}
// StmtAltSwitch node // StmtAltSwitch node
type StmtAltSwitch struct { type StmtAltSwitch struct {
Node Node
@ -444,6 +410,9 @@ func (n *StmtEcho) Accept(v NodeVisitor) {
// StmtElse node // StmtElse node
type StmtElse struct { type StmtElse struct {
Node Node
Alt bool
ElseTkn *token.Token
ColonTkn *token.Token
Stmt Vertex Stmt Vertex
} }
@ -454,7 +423,12 @@ func (n *StmtElse) Accept(v NodeVisitor) {
// StmtElseIf node // StmtElseIf node
type StmtElseIf struct { type StmtElseIf struct {
Node Node
Alt bool
ElseIfTkn *token.Token
OpenParenthesisTkn *token.Token
Cond Vertex Cond Vertex
CloseParenthesisTkn *token.Token
ColonTkn *token.Token
Stmt Vertex Stmt Vertex
} }
@ -558,10 +532,17 @@ func (n *StmtHaltCompiler) Accept(v NodeVisitor) {
// StmtIf node // StmtIf node
type StmtIf struct { type StmtIf struct {
Node Node
Alt bool
IfTkn *token.Token
OpenParenthesisTkn *token.Token
Cond Vertex Cond Vertex
CloseParenthesisTkn *token.Token
ColonTkn *token.Token
Stmt Vertex Stmt Vertex
ElseIf []Vertex ElseIf []Vertex
Else Vertex Else Vertex
EndIfTkn *token.Token
SemiColonTkn *token.Token
} }
func (n *StmtIf) Accept(v NodeVisitor) { func (n *StmtIf) Accept(v NodeVisitor) {
@ -1918,7 +1899,9 @@ func (n *ParserNsSeparator) Accept(v NodeVisitor) {
type ParserBrackets struct { type ParserBrackets struct {
Node Node
OpenBracketTkn *token.Token
Child Vertex Child Vertex
CloseBracketTkn *token.Token
} }
func (n *ParserBrackets) Accept(v NodeVisitor) { func (n *ParserBrackets) Accept(v NodeVisitor) {

View File

@ -119,35 +119,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Expr) t.Traverse(nn.Expr)
t.visitor.Leave("Expr", true) t.visitor.Leave("Expr", true)
} }
case *ast.StmtAltElse:
if nn == nil {
return
}
if !t.visitor.EnterNode(nn) {
return
}
if nn.Stmt != nil {
t.visitor.Enter("Stmt", true)
t.Traverse(nn.Stmt)
t.visitor.Leave("Stmt", true)
}
case *ast.StmtAltElseIf:
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.Stmt != nil {
t.visitor.Enter("Stmt", true)
t.Traverse(nn.Stmt)
t.visitor.Leave("Stmt", true)
}
case *ast.StmtAltFor: case *ast.StmtAltFor:
if nn == nil { if nn == nil {
return return
@ -208,35 +179,6 @@ func (t *DFS) Traverse(n ast.Vertex) {
t.Traverse(nn.Stmt) t.Traverse(nn.Stmt)
t.visitor.Leave("Stmt", true) t.visitor.Leave("Stmt", true)
} }
case *ast.StmtAltIf:
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.Stmt != nil {
t.visitor.Enter("Stmt", true)
t.Traverse(nn.Stmt)
t.visitor.Leave("Stmt", true)
}
if nn.ElseIf != nil {
t.visitor.Enter("ElseIf", false)
for _, c := range nn.ElseIf {
t.Traverse(c)
}
t.visitor.Leave("ElseIf", false)
}
if nn.Else != nil {
t.visitor.Enter("Else", true)
t.Traverse(nn.Else)
t.visitor.Leave("Else", true)
}
case *ast.StmtAltSwitch: case *ast.StmtAltSwitch:
if nn == nil { if nn == nil {
return return

View File

@ -252,18 +252,6 @@ func (v *Dump) Argument(n *ast.Argument) {
} }
} }
func (v *Dump) StmtAltElse(n *ast.StmtAltElse) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtAltElse{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtAltElseIf(n *ast.StmtAltElseIf) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtAltElseIf{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtAltFor(n *ast.StmtAltFor) { func (v *Dump) StmtAltFor(n *ast.StmtAltFor) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtAltFor{\n") v.print("&ast.StmtAltFor{\n")
@ -276,12 +264,6 @@ func (v *Dump) StmtAltForeach(n *ast.StmtAltForeach) {
v.printNode(n.GetNode()) v.printNode(n.GetNode())
} }
func (v *Dump) StmtAltIf(n *ast.StmtAltIf) {
v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtAltIf{\n")
v.printNode(n.GetNode())
}
func (v *Dump) StmtAltSwitch(n *ast.StmtAltSwitch) { func (v *Dump) StmtAltSwitch(n *ast.StmtAltSwitch) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtAltSwitch{\n") v.print("&ast.StmtAltSwitch{\n")
@ -404,12 +386,22 @@ func (v *Dump) StmtElse(n *ast.StmtElse) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtElse{\n") v.print("&ast.StmtElse{\n")
v.printNode(n.GetNode()) v.printNode(n.GetNode())
if n.Alt {
v.printIndent(v.indent)
v.print("Alt: true,\n")
}
} }
func (v *Dump) StmtElseIf(n *ast.StmtElseIf) { func (v *Dump) StmtElseIf(n *ast.StmtElseIf) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtElseIf{\n") v.print("&ast.StmtElseIf{\n")
v.printNode(n.GetNode()) v.printNode(n.GetNode())
if n.Alt {
v.printIndent(v.indent)
v.print("Alt: true,\n")
}
} }
func (v *Dump) StmtExpression(n *ast.StmtExpression) { func (v *Dump) StmtExpression(n *ast.StmtExpression) {
@ -469,6 +461,11 @@ func (v *Dump) StmtIf(n *ast.StmtIf) {
v.printIndentIfNotSingle(v.indent - 1) v.printIndentIfNotSingle(v.indent - 1)
v.print("&ast.StmtIf{\n") v.print("&ast.StmtIf{\n")
v.printNode(n.GetNode()) v.printNode(n.GetNode())
if n.Alt {
v.printIndent(v.indent)
v.print("Alt: true,\n")
}
} }
func (v *Dump) StmtInlineHtml(n *ast.StmtInlineHtml) { func (v *Dump) StmtInlineHtml(n *ast.StmtInlineHtml) {

View File

@ -13,60 +13,6 @@ func (v *FilterParserNodes) EnterNode(n ast.Vertex) bool {
return true return true
} }
func (v *FilterParserNodes) StmtAltIf(n *ast.StmtAltIf) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
if nn, ok := n.Stmt.(*ast.ParserBrackets); ok {
n.Stmt = nn.Child
}
}
func (v *FilterParserNodes) StmtAltElseIf(n *ast.StmtAltElseIf) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
if nn, ok := n.Stmt.(*ast.ParserBrackets); ok {
n.Stmt = nn.Child
}
}
func (v *FilterParserNodes) StmtAltElse(n *ast.StmtAltElse) {
if nn, ok := n.Stmt.(*ast.ParserBrackets); ok {
n.Stmt = nn.Child
}
}
func (v *FilterParserNodes) StmtIf(n *ast.StmtIf) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
}
func (v *FilterParserNodes) StmtElseIf(n *ast.StmtElseIf) {
for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok {
n.Cond = nn.Child
} else {
break
}
}
}
func (v *FilterParserNodes) StmtWhile(n *ast.StmtWhile) { func (v *FilterParserNodes) StmtWhile(n *ast.StmtWhile) {
for { for {
if nn, ok := n.Cond.(*ast.ParserBrackets); ok { if nn, ok := n.Cond.(*ast.ParserBrackets); ok {

View File

@ -87,3 +87,29 @@ func (v *FilterTokens) StmtStmtList(n *ast.StmtStmtList) {
n.OpenCurlyBracket = nil n.OpenCurlyBracket = nil
n.CloseCurlyBracket = nil n.CloseCurlyBracket = nil
} }
func (v *FilterTokens) StmtIf(n *ast.StmtIf) {
n.IfTkn = nil
n.OpenParenthesisTkn = nil
n.CloseParenthesisTkn = nil
n.ColonTkn = nil
n.EndIfTkn = nil
n.SemiColonTkn = nil
}
func (v *FilterTokens) StmtElseIf(n *ast.StmtElseIf) {
n.ElseIfTkn = nil
n.OpenParenthesisTkn = nil
n.CloseParenthesisTkn = nil
n.ColonTkn = nil
}
func (v *FilterTokens) StmtElse(n *ast.StmtElse) {
n.ElseTkn = nil
n.ColonTkn = nil
}
func (v *FilterTokens) ParserBrackets(n *ast.ParserBrackets) {
n.OpenBracketTkn = nil
n.CloseBracketTkn = nil
}

View File

@ -54,14 +54,6 @@ func (v *Null) Argument(_ *ast.Argument) {
// do nothing // do nothing
} }
func (v *Null) StmtAltElse(_ *ast.StmtAltElse) {
// do nothing
}
func (v *Null) StmtAltElseIf(_ *ast.StmtAltElseIf) {
// do nothing
}
func (v *Null) StmtAltFor(_ *ast.StmtAltFor) { func (v *Null) StmtAltFor(_ *ast.StmtAltFor) {
// do nothing // do nothing
} }
@ -70,10 +62,6 @@ func (v *Null) StmtAltForeach(_ *ast.StmtAltForeach) {
// do nothing // do nothing
} }
func (v *Null) StmtAltIf(_ *ast.StmtAltIf) {
// do nothing
}
func (v *Null) StmtAltSwitch(_ *ast.StmtAltSwitch) { func (v *Null) StmtAltSwitch(_ *ast.StmtAltSwitch) {
// do nothing // do nothing
} }

View File

@ -297,16 +297,10 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
// stmt // stmt
case *ast.StmtAltElseIf:
p.printStmtAltElseIf(n)
case *ast.StmtAltElse:
p.printStmtAltElse(n)
case *ast.StmtAltFor: case *ast.StmtAltFor:
p.printStmtAltFor(n) p.printStmtAltFor(n)
case *ast.StmtAltForeach: case *ast.StmtAltForeach:
p.printStmtAltForeach(n) p.printStmtAltForeach(n)
case *ast.StmtAltIf:
p.printStmtAltIf(n)
case *ast.StmtAltSwitch: case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n) p.printStmtAltSwitch(n)
case *ast.StmtAltWhile: case *ast.StmtAltWhile:
@ -1348,7 +1342,7 @@ func (p *PrettyPrinter) printExprYield(n ast.Vertex) {
// smtm // smtm
func (p *PrettyPrinter) printStmtAltElseIf(n ast.Vertex) { func (p *PrettyPrinter) printStmtAltElseIf(n ast.Vertex) {
nn := n.(*ast.StmtAltElseIf) nn := n.(*ast.StmtElseIf)
io.WriteString(p.w, "elseif (") io.WriteString(p.w, "elseif (")
p.Print(nn.Cond) p.Print(nn.Cond)
@ -1361,7 +1355,7 @@ func (p *PrettyPrinter) printStmtAltElseIf(n ast.Vertex) {
} }
func (p *PrettyPrinter) printStmtAltElse(n ast.Vertex) { func (p *PrettyPrinter) printStmtAltElse(n ast.Vertex) {
nn := n.(*ast.StmtAltElse) nn := n.(*ast.StmtElse)
io.WriteString(p.w, "else :") io.WriteString(p.w, "else :")
@ -1415,7 +1409,7 @@ func (p *PrettyPrinter) printStmtAltForeach(n ast.Vertex) {
} }
func (p *PrettyPrinter) printStmtAltIf(n ast.Vertex) { func (p *PrettyPrinter) printStmtAltIf(n ast.Vertex) {
nn := n.(*ast.StmtAltIf) nn := n.(*ast.StmtIf)
io.WriteString(p.w, "if (") io.WriteString(p.w, "if (")
p.Print(nn.Cond) p.Print(nn.Cond)
@ -1687,6 +1681,11 @@ func (p *PrettyPrinter) printStmtEcho(n ast.Vertex) {
func (p *PrettyPrinter) printStmtElseif(n ast.Vertex) { func (p *PrettyPrinter) printStmtElseif(n ast.Vertex) {
nn := n.(*ast.StmtElseIf) nn := n.(*ast.StmtElseIf)
if nn.Alt {
p.printStmtAltElseIf(nn)
return
}
io.WriteString(p.w, "elseif (") io.WriteString(p.w, "elseif (")
p.Print(nn.Cond) p.Print(nn.Cond)
io.WriteString(p.w, ")") io.WriteString(p.w, ")")
@ -1710,6 +1709,11 @@ func (p *PrettyPrinter) printStmtElseif(n ast.Vertex) {
func (p *PrettyPrinter) printStmtElse(n ast.Vertex) { func (p *PrettyPrinter) printStmtElse(n ast.Vertex) {
nn := n.(*ast.StmtElse) nn := n.(*ast.StmtElse)
if nn.Alt {
p.printStmtAltElse(nn)
return
}
io.WriteString(p.w, "else") io.WriteString(p.w, "else")
switch s := nn.Stmt.(type) { switch s := nn.Stmt.(type) {
@ -1854,6 +1858,11 @@ func (p *PrettyPrinter) printStmtHaltCompiler(n ast.Vertex) {
func (p *PrettyPrinter) printStmtIf(n ast.Vertex) { func (p *PrettyPrinter) printStmtIf(n ast.Vertex) {
nn := n.(*ast.StmtIf) nn := n.(*ast.StmtIf)
if nn.Alt {
p.printStmtAltIf(nn)
return
}
io.WriteString(p.w, "if (") io.WriteString(p.w, "if (")
p.Print(nn.Cond) p.Print(nn.Cond)
io.WriteString(p.w, ")") io.WriteString(p.w, ")")

View File

@ -2090,7 +2090,8 @@ func TestPrintAltElseIf(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ") p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtAltElseIf{ p.Print(&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
@ -2112,7 +2113,8 @@ func TestPrintAltElseIfEmpty(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ") p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtAltElseIf{ p.Print(&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
Stmt: &ast.StmtStmtList{}, Stmt: &ast.StmtStmtList{},
}) })
@ -2129,7 +2131,8 @@ func TestPrintAltElse(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ") p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtAltElse{ p.Print(&ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},
@ -2150,7 +2153,8 @@ func TestPrintAltElseEmpty(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, " ") p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtAltElse{ p.Print(&ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{}, Stmt: &ast.StmtStmtList{},
}) })
@ -2236,7 +2240,8 @@ func TestPrintAltIf(t *testing.T) {
p := printer.NewPrettyPrinter(o, " ") p := printer.NewPrettyPrinter(o, " ")
p.Print(&ast.StmtNamespace{ p.Print(&ast.StmtNamespace{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtAltIf{ &ast.StmtIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}}, Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("a")}},
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
@ -2244,7 +2249,8 @@ func TestPrintAltIf(t *testing.T) {
}, },
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}, Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}},
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
@ -2252,12 +2258,14 @@ func TestPrintAltIf(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}}, Cond: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("c")}},
Stmt: &ast.StmtStmtList{}, Stmt: &ast.StmtStmtList{},
}, },
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}}, &ast.StmtExpression{Expr: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("b")}}},

View File

@ -359,16 +359,10 @@ func (p *Printer) printNode(n ast.Vertex) {
// stmt // stmt
case *ast.StmtAltElseIf:
p.printStmtAltElseIf(n)
case *ast.StmtAltElse:
p.printStmtAltElse(n)
case *ast.StmtAltFor: case *ast.StmtAltFor:
p.printStmtAltFor(n) p.printStmtAltFor(n)
case *ast.StmtAltForeach: case *ast.StmtAltForeach:
p.printStmtAltForeach(n) p.printStmtAltForeach(n)
case *ast.StmtAltIf:
p.printStmtAltIf(n)
case *ast.StmtAltSwitch: case *ast.StmtAltSwitch:
p.printStmtAltSwitch(n) p.printStmtAltSwitch(n)
case *ast.StmtAltWhile: case *ast.StmtAltWhile:
@ -1981,68 +1975,6 @@ func (p *Printer) printExprYield(n ast.Vertex) {
// smtm // smtm
func (p *Printer) printStmtAltElseIf(n ast.Vertex) {
nn := n.(*ast.StmtAltElseIf)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "elseif")
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, ")")
}
stmtList, _ := nn.Stmt.(*ast.StmtStmtList)
brackets, ok := nn.Stmt.(*ast.ParserBrackets)
if ok {
p.printFreeFloating(brackets, token.Start)
stmtList = brackets.Child.(*ast.StmtStmtList)
} else {
io.WriteString(p.w, ":")
}
p.printFreeFloating(stmtList, token.Stmts)
p.printNodes(stmtList.Stmts)
p.printFreeFloating(stmtList, token.End)
if ok {
p.printFreeFloating(brackets, token.End)
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtAltElse(n ast.Vertex) {
nn := n.(*ast.StmtAltElse)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "else")
stmtList, _ := nn.Stmt.(*ast.StmtStmtList)
brackets, ok := nn.Stmt.(*ast.ParserBrackets)
if ok {
p.printFreeFloating(brackets, token.Start)
stmtList = brackets.Child.(*ast.StmtStmtList)
} else {
io.WriteString(p.w, ":")
}
p.printFreeFloating(stmtList, token.Stmts)
p.printNodes(stmtList.Stmts)
p.printFreeFloating(stmtList, token.End)
if ok {
p.printFreeFloating(brackets, token.End)
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtAltFor(n ast.Vertex) { func (p *Printer) printStmtAltFor(n ast.Vertex) {
nn := n.(*ast.StmtAltFor) nn := n.(*ast.StmtAltFor)
p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Start)
@ -2124,59 +2056,6 @@ func (p *Printer) printStmtAltForeach(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtAltIf(n ast.Vertex) {
nn := n.(*ast.StmtAltIf)
p.printFreeFloating(nn, token.Start)
io.WriteString(p.w, "if")
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, ")")
}
stmtList, _ := nn.Stmt.(*ast.StmtStmtList)
brackets, ok := nn.Stmt.(*ast.ParserBrackets)
if ok {
p.printFreeFloating(brackets, token.Start)
stmtList = brackets.Child.(*ast.StmtStmtList)
} else {
io.WriteString(p.w, ":")
}
p.printFreeFloating(stmtList, token.Stmts)
p.printNodes(stmtList.Stmts)
p.printFreeFloating(stmtList, token.End)
if ok {
p.printFreeFloating(brackets, token.End)
}
for _, elseif := range nn.ElseIf {
p.Print(elseif)
}
if nn.Else != nil {
p.Print(nn.Else)
}
if !ok {
io.WriteString(p.w, "endif")
}
p.printFreeFloating(nn, token.SemiColon)
if nn.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";")
}
p.printFreeFloating(nn, token.End)
}
func (p *Printer) printStmtAltSwitch(n ast.Vertex) { func (p *Printer) printStmtAltSwitch(n ast.Vertex) {
nn := n.(*ast.StmtAltSwitch) nn := n.(*ast.StmtAltSwitch)
p.printFreeFloating(nn, token.Start) p.printFreeFloating(nn, token.Start)
@ -2579,42 +2458,54 @@ func (p *Printer) printStmtEcho(n ast.Vertex) {
p.printFreeFloating(nn, token.End) p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtElseif(n ast.Vertex) { func (p *Printer) printStmtElseif(n *ast.StmtElseIf) {
nn := n.(*ast.StmtElseIf) if n.Alt {
p.printFreeFloating(nn, token.Start) p.printStmtAltElseIf(n)
return
io.WriteString(p.w, "elseif")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
} }
p.Print(nn.Cond) p.printToken(n.ElseIfTkn, "elseif")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { p.Print(n.Stmt)
io.WriteString(p.w, ")")
}
p.Print(nn.Stmt)
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtElse(n ast.Vertex) { func (p *Printer) printStmtAltElseIf(n *ast.StmtElseIf) {
nn := n.(*ast.StmtElse) p.printToken(n.ElseIfTkn, "elseif")
p.printFreeFloating(nn, token.Start) p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.ColonTkn, ":")
io.WriteString(p.w, "else") if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
p.printNodes(stmtList.Stmts)
if _, ok := nn.Stmt.(*ast.StmtStmtList); !ok { } else {
if nn.Stmt.GetNode().Tokens.IsEmpty() { p.Print(n.Stmt)
io.WriteString(p.w, " ")
} }
}
func (p *Printer) printStmtElse(n *ast.StmtElse) {
if n.Alt {
p.printStmtAltElse(n)
return
} }
p.Print(nn.Stmt) p.printToken(n.ElseTkn, "else")
p.bufStart = " "
p.Print(n.Stmt)
}
p.printFreeFloating(nn, token.End) func (p *Printer) printStmtAltElse(n *ast.StmtElse) {
p.printToken(n.ElseTkn, "else")
p.printToken(n.ColonTkn, ":")
if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
p.printNodes(stmtList.Stmts)
} else {
p.Print(n.Stmt)
}
} }
func (p *Printer) printStmtExpression(n ast.Vertex) { func (p *Printer) printStmtExpression(n ast.Vertex) {
@ -2789,33 +2680,40 @@ func (p *Printer) printStmtHaltCompiler(n *ast.StmtHaltCompiler) {
p.printToken(n.SemiColonTkn, ";") p.printToken(n.SemiColonTkn, ";")
} }
func (p *Printer) printStmtIf(n ast.Vertex) { func (p *Printer) printStmtIf(n *ast.StmtIf) {
nn := n.(*ast.StmtIf) if n.Alt {
p.printFreeFloating(nn, token.Start) p.printStmtAltIf(n)
return
io.WriteString(p.w, "if")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok {
io.WriteString(p.w, "(")
} }
p.Print(nn.Cond) p.printToken(n.IfTkn, "if")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { p.Print(n.Stmt)
io.WriteString(p.w, ")") p.printNodes(n.ElseIf)
p.Print(n.Else)
}
func (p *Printer) printStmtAltIf(n *ast.StmtIf) {
p.printToken(n.IfTkn, "if")
p.printToken(n.OpenParenthesisTkn, "(")
p.Print(n.Cond)
p.printToken(n.CloseParenthesisTkn, ")")
p.printToken(n.ColonTkn, ":")
if stmtList, ok := n.Stmt.(*ast.StmtStmtList); ok {
p.printNodes(stmtList.Stmts)
} else {
p.Print(n.Stmt)
} }
p.Print(nn.Stmt) p.printNodes(n.ElseIf)
p.Print(n.Else)
if nn.ElseIf != nil { p.printToken(n.EndIfTkn, "endif")
p.printNodes(nn.ElseIf) p.printToken(n.SemiColonTkn, ";")
}
if nn.Else != nil {
p.Print(nn.Else)
}
p.printFreeFloating(nn, token.End)
} }
func (p *Printer) printStmtInlineHTML(n ast.Vertex) { func (p *Printer) printStmtInlineHTML(n ast.Vertex) {
@ -2900,7 +2798,7 @@ func (p *Printer) printStmtNamespace(n *ast.StmtNamespace) {
} }
func (p *Printer) printStmtNop(n ast.Vertex) { func (p *Printer) printStmtNop(n ast.Vertex) {
p.printFreeFloating(n, token.Start) p.printFreeFloatingOrDefault(n, token.Start, p.bufStart)
p.printFreeFloating(n, token.SemiColon) p.printFreeFloating(n, token.SemiColon)
if n.GetNode().Tokens.IsEmpty() { if n.GetNode().Tokens.IsEmpty() {
io.WriteString(p.w, ";") io.WriteString(p.w, ";")

View File

@ -743,7 +743,8 @@ func TestParseAndPrintPhp5Yield(t *testing.T) {
// test stmt // test stmt
func TestParseAndPrintPhp5AltIf(t *testing.T) { func TestParseAndPrintPhp5AltIf(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
if ( 1 ) : if ( 1 ) :
// do nothing // do nothing
elseif ( 2 ) : elseif ( 2 ) :
@ -1064,7 +1065,8 @@ func TestParseAndPrintPhp5HaltCompiler(t *testing.T) {
} }
func TestParseAndPrintPhp5IfElseIfElse(t *testing.T) { func TestParseAndPrintPhp5IfElseIfElse(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
if ( 1 ) ; if ( 1 ) ;
elseif ( 2 ) { elseif ( 2 ) {
; ;

View File

@ -856,7 +856,8 @@ func TestParseAndPrintYield(t *testing.T) {
// test stmt // test stmt
func TestParseAndPrintAltIf(t *testing.T) { func TestParseAndPrintAltIf(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
if ( 1 ) : if ( 1 ) :
// do nothing // do nothing
elseif ( 2 ) : elseif ( 2 ) :
@ -1197,7 +1198,8 @@ func TestParseAndPrintHaltCompiler(t *testing.T) {
} }
func TestParseAndPrintIfElseIfElse(t *testing.T) { func TestParseAndPrintIfElseIfElse(t *testing.T) {
src := `<?php // TODO: remove ; after <?php
src := `<?php ;
if ( 1 ) ; if ( 1 ) ;
elseif ( 2 ) { elseif ( 2 ) {
; ;

View File

@ -2565,7 +2565,8 @@ func TestPrinterPrintAltElseIf(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtAltElseIf{ p.Print(&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")}, VarName: &ast.Identifier{Value: []byte("$a")},
}, },
@ -2590,7 +2591,8 @@ func TestPrinterPrintAltElseIfEmpty(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtAltElseIf{ p.Print(&ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")}, VarName: &ast.Identifier{Value: []byte("$a")},
}, },
@ -2609,7 +2611,8 @@ func TestPrinterPrintAltElse(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtAltElse{ p.Print(&ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{ &ast.StmtExpression{Expr: &ast.ExprVariable{
@ -2631,7 +2634,8 @@ func TestPrinterPrintAltElseEmpty(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtAltElse{ p.Print(&ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{}, Stmt: &ast.StmtStmtList{},
}) })
@ -2715,7 +2719,8 @@ func TestPrinterPrintAltIf(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")
p := printer.NewPrinter(o) p := printer.NewPrinter(o)
p.Print(&ast.StmtAltIf{ p.Print(&ast.StmtIf{
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")}, VarName: &ast.Identifier{Value: []byte("$a")},
}, },
@ -2727,7 +2732,8 @@ func TestPrinterPrintAltIf(t *testing.T) {
}, },
}, },
ElseIf: []ast.Vertex{ ElseIf: []ast.Vertex{
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$b")}, VarName: &ast.Identifier{Value: []byte("$b")},
}, },
@ -2739,14 +2745,16 @@ func TestPrinterPrintAltIf(t *testing.T) {
}, },
}, },
}, },
&ast.StmtAltElseIf{ &ast.StmtElseIf{
Alt: true,
Cond: &ast.ExprVariable{ Cond: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$c")}, VarName: &ast.Identifier{Value: []byte("$c")},
}, },
Stmt: &ast.StmtStmtList{}, Stmt: &ast.StmtStmtList{},
}, },
}, },
Else: &ast.StmtAltElse{ Else: &ast.StmtElse{
Alt: true,
Stmt: &ast.StmtStmtList{ Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.StmtExpression{Expr: &ast.ExprVariable{ &ast.StmtExpression{Expr: &ast.ExprVariable{
@ -3453,7 +3461,7 @@ func TestPrinterPrintStmtElseStmts(t *testing.T) {
}, },
}) })
expected := `else{;}` expected := `else {;}`
actual := o.String() actual := o.String()
if expected != actual { if expected != actual {