diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go index 6d72de0..a33aa36 100644 --- a/internal/php5/parser_test.go +++ b/internal/php5/parser_test.go @@ -5819,7 +5819,7 @@ func TestStmtEcho_Parenthesis(t *testing.T) { } func TestStmtExpression(t *testing.T) { - src := ` 0 { - yylex.(*Parser).setFreeFloatingTokens($$, token.If, $2.GetNode().Tokens[token.OpenParenthesisToken][:len($2.GetNode().Tokens[token.OpenParenthesisToken])-1]); delete($2.GetNode().Tokens, token.OpenParenthesisToken) - } - if len($2.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens($$, token.Expr, $2.GetNode().Tokens[token.CloseParenthesisToken][:len($2.GetNode().Tokens[token.CloseParenthesisToken])-1]); delete($2.GetNode().Tokens, token.CloseParenthesisToken) - } yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } | T_IF parenthesis_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';' { - exprBrackets := &ast.ParserBrackets{ast.Node{}, $2} stmts := &ast.StmtStmtList{ast.Node{}, $4} stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} - $$ = &ast.StmtAltIf{ast.Node{}, exprBrackets, stmtsBrackets, $5, $6} + $$ = &ast.StmtAltIf{ast.Node{}, $2, stmtsBrackets, $5, $6} // save position - exprBrackets.GetNode().Position = position.NewNodePosition($2) stmts.GetNode().Position = position.NewNodeListPosition($4) stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($3, $4) $$.GetNode().Position = position.NewTokensPosition($1, $8) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - if len($2.GetNode().Tokens[token.OpenParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.GetNode().Tokens[token.OpenParenthesisToken]); delete($2.GetNode().Tokens, token.OpenParenthesisToken) - } - if len($2.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $2.GetNode().Tokens[token.CloseParenthesisToken]); delete($2.GetNode().Tokens, token.CloseParenthesisToken) - } yylex.(*Parser).setFreeFloatingTokens(stmtsBrackets, token.Start, $3.Tokens) if $6 != nil { yylex.(*Parser).setFreeFloating($6.(*ast.StmtAltElse).Stmt, token.End, append($7.Tokens, $8.Tokens...)) @@ -977,12 +963,6 @@ unticked_statement: // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - if len($2.GetNode().Tokens[token.OpenParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens($$, token.While, $2.GetNode().Tokens[token.OpenParenthesisToken][:len($2.GetNode().Tokens[token.OpenParenthesisToken])-1]); delete($2.GetNode().Tokens, token.OpenParenthesisToken) - } - if len($2.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens($$, token.Expr, $2.GetNode().Tokens[token.CloseParenthesisToken][:len($2.GetNode().Tokens[token.CloseParenthesisToken])-1]); delete($2.GetNode().Tokens, token.CloseParenthesisToken) - } yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -996,13 +976,7 @@ unticked_statement: // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.Tokens) - if len($4.GetNode().Tokens[token.OpenParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens($$, token.While, $4.GetNode().Tokens[token.OpenParenthesisToken][:len($4.GetNode().Tokens[token.OpenParenthesisToken])-1]); delete($4.GetNode().Tokens, token.OpenParenthesisToken) - } - if len($4.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens($$, token.Expr, $4.GetNode().Tokens[token.CloseParenthesisToken][:len($4.GetNode().Tokens[token.CloseParenthesisToken])-1]); delete($4.GetNode().Tokens, token.CloseParenthesisToken) - } - yylex.(*Parser).setFreeFloating($$, token.Cond, $5.Tokens) + yylex.(*Parser).setFreeFloating($4, token.End, $5.Tokens) yylex.(*Parser).setToken($$, token.SemiColon, $5.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) @@ -1052,12 +1026,6 @@ unticked_statement: // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - if len($2.GetNode().Tokens[token.OpenParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens($$, token.Switch, $2.GetNode().Tokens[token.OpenParenthesisToken][:len($2.GetNode().Tokens[token.OpenParenthesisToken])-1]); delete($2.GetNode().Tokens, token.OpenParenthesisToken) - } - if len($2.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens($$, token.Expr, $2.GetNode().Tokens[token.CloseParenthesisToken][:len($2.GetNode().Tokens[token.CloseParenthesisToken])-1]); delete($2.GetNode().Tokens, token.CloseParenthesisToken) - } yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -2163,12 +2131,6 @@ elseif_list: // save comments yylex.(*Parser).setFreeFloating(_elseIf, token.Start, $2.Tokens) - if len($3.GetNode().Tokens[token.OpenParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens(_elseIf, token.ElseIf, $3.GetNode().Tokens[token.OpenParenthesisToken][:len($3.GetNode().Tokens[token.OpenParenthesisToken])-1]); delete($3.GetNode().Tokens, token.OpenParenthesisToken) - } - if len($3.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens(_elseIf, token.Expr, $3.GetNode().Tokens[token.CloseParenthesisToken][:len($3.GetNode().Tokens[token.CloseParenthesisToken])-1]); delete($3.GetNode().Tokens, token.CloseParenthesisToken) - } yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -2184,26 +2146,18 @@ new_elseif_list: } | new_elseif_list T_ELSEIF parenthesis_expr ':' inner_statement_list { - exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} stmts := &ast.StmtStmtList{ast.Node{}, $5} stmtsBrackets := &ast.ParserBrackets{ast.Node{}, stmts} - _elseIf := &ast.StmtAltElseIf{ast.Node{}, exprBrackets, stmtsBrackets} + _elseIf := &ast.StmtAltElseIf{ast.Node{}, $3, stmtsBrackets} $$ = append($1, _elseIf) // save position - exprBrackets.GetNode().Position = position.NewNodePosition($3) stmts.GetNode().Position = position.NewNodeListPosition($5) - exprBrackets.GetNode().Position = position.NewTokenNodeListPosition($4, $5) + stmtsBrackets.GetNode().Position = position.NewTokenNodeListPosition($4, $5) _elseIf.GetNode().Position = position.NewTokenNodeListPosition($2, $5) // save comments yylex.(*Parser).setFreeFloating(_elseIf, token.Start, $2.Tokens) - if len($3.GetNode().Tokens[token.OpenParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $3.GetNode().Tokens[token.OpenParenthesisToken]); delete($3.GetNode().Tokens, token.OpenParenthesisToken) - } - if len($3.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $3.GetNode().Tokens[token.CloseParenthesisToken]); delete($3.GetNode().Tokens, token.CloseParenthesisToken) - } yylex.(*Parser).setFreeFloatingTokens(stmtsBrackets, token.Start, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) @@ -4089,9 +4043,6 @@ expr_without_variable: $$ = $1 yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) - - yylex.(*Parser).setFreeFloatingTokens($1, token.Start, append($1.GetNode().Tokens[token.OpenParenthesisToken], $1.GetNode().Tokens[token.Start]...)); delete($1.GetNode().Tokens, token.OpenParenthesisToken) - yylex.(*Parser).setFreeFloatingTokens($1, token.End, append($1.GetNode().Tokens[token.End], $1.GetNode().Tokens[token.CloseParenthesisToken]...)); delete($1.GetNode().Tokens, token.CloseParenthesisToken) } | new_expr { @@ -4258,15 +4209,14 @@ expr_without_variable: } | T_EXIT exit_expr { - e := $2.(*ast.ExprExit) - $$ = $2 + $$ = &ast.ExprExit{ast.Node{}, false, $2} if (bytes.EqualFold($1.Value, []byte("die"))) { - e.Die = true + $$.(*ast.ExprExit).Die = true } // save position - if $2.GetNode().Position == nil { + if $2 == nil { $$.GetNode().Position = position.NewTokenPosition($1) } else { $$.GetNode().Position = position.NewTokenNodePosition($1, $2) @@ -4930,39 +4880,28 @@ dynamic_class_name_variable_property: exit_expr: /* empty */ { - $$ = &ast.ExprExit{ast.Node{}, false, nil}; + $$ = nil yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } | '(' ')' { - $$ = &ast.ExprExit{ast.Node{}, false, nil}; + $$ = &ast.ParserBrackets{ast.Node{}, nil} // save position $$.GetNode().Position = position.NewTokensPosition($1, $2) // save comments - yylex.(*Parser).setFreeFloatingTokens($$, token.Exit, $1.Tokens) - yylex.(*Parser).setFreeFloatingTokens($$, token.Expr, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.End, $2.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } | parenthesis_expr { - $$ = &ast.ExprExit{ast.Node{}, false, $1}; - - // save position - if bytes.Compare(yylex.(*Parser).currentToken.Value, []byte(")")) == 0 { - $$.GetNode().Position = position.NewTokenPosition(yylex.(*Parser).currentToken) - } else { - $$.GetNode().Position = position.NewNodePosition($1) - } + $$ = $1 yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) - - // save comments - yylex.(*Parser).setFreeFloatingTokens($$, token.Exit, $1.GetNode().Tokens[token.OpenParenthesisToken]); delete($1.GetNode().Tokens, token.OpenParenthesisToken) - yylex.(*Parser).setFreeFloatingTokens($$, token.Expr, $1.GetNode().Tokens[token.CloseParenthesisToken]); delete($1.GetNode().Tokens, token.CloseParenthesisToken) } ; @@ -5945,33 +5884,27 @@ expr: parenthesis_expr: '(' expr ')' { - $$ = $2 + $$ = &ast.ParserBrackets{ast.Node{}, $2} + + // save position + $$.GetNode().Position = position.NewTokensPosition($1, $3) // save comments - if len($2.GetNode().Tokens[token.OpenParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloating($2, token.Start, append($2.GetNode().Tokens[token.OpenParenthesisToken], $2.GetNode().Tokens[token.Start]...)) - } - if len($2.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloating($2, token.End, append($2.GetNode().Tokens[token.End], $2.GetNode().Tokens[token.CloseParenthesisToken]...)) - } - yylex.(*Parser).setFreeFloatingTokens($2, token.OpenParenthesisToken, $1.Tokens) - yylex.(*Parser).setFreeFloatingTokens($2, token.CloseParenthesisToken, $3.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } | '(' yield_expr ')' { - $$ = $2 + $$ = &ast.ParserBrackets{ast.Node{}, $2} + + // save position + $$.GetNode().Position = position.NewTokensPosition($1, $3) // save comments - if len($2.GetNode().Tokens[token.OpenParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloating($2, token.Start, append($2.GetNode().Tokens[token.OpenParenthesisToken], $2.GetNode().Tokens[token.Start]...)) - } - if len($2.GetNode().Tokens[token.CloseParenthesisToken]) > 0 { - yylex.(*Parser).setFreeFloating($2, token.End, append($2.GetNode().Tokens[token.End], $2.GetNode().Tokens[token.CloseParenthesisToken]...)) - } - yylex.(*Parser).setFreeFloatingTokens($2, token.OpenParenthesisToken, $1.Tokens) - yylex.(*Parser).setFreeFloatingTokens($2, token.CloseParenthesisToken, $3.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -6943,29 +6876,33 @@ internal_functions_in_yacc: } | T_EMPTY '(' variable ')' { - $$ = &ast.ExprEmpty{ast.Node{}, $3} + exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} + $$ = &ast.ExprEmpty{ast.Node{}, exprBrackets} // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Empty, $2.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $4.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } | T_EMPTY '(' expr ')' { - $$ = &ast.ExprEmpty{ast.Node{}, $3} + exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} + $$ = &ast.ExprEmpty{ast.Node{}, exprBrackets} // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Empty, $2.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $4.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -6995,15 +6932,17 @@ internal_functions_in_yacc: } | T_EVAL '(' expr ')' { - $$ = &ast.ExprEval{ast.Node{}, $3} + exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} + $$ = &ast.ExprEval{ast.Node{}, exprBrackets} // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Eval, $2.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $4.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } diff --git a/internal/php5/php5_test.go b/internal/php5/php5_test.go index 07cbd58..b1a1ada 100644 --- a/internal/php5/php5_test.go +++ b/internal/php5/php5_test.go @@ -10048,7 +10048,7 @@ func TestPhp5(t *testing.T) { StartLine: 175, EndLine: 175, StartPos: 3593, - EndPos: 3601, + EndPos: 3602, }, }, Expr: &ast.ExprVariable{ @@ -12426,7 +12426,7 @@ func TestPhp5(t *testing.T) { StartLine: 223, EndLine: 223, StartPos: 4168, - EndPos: 4176, + EndPos: 4177, }, }, Expr: &ast.ExprVariable{ diff --git a/internal/php7/parser_test.go b/internal/php7/parser_test.go index 99ac89f..9ef81b7 100644 --- a/internal/php7/parser_test.go +++ b/internal/php7/parser_test.go @@ -15577,7 +15577,7 @@ func TestExprClone_Brackets(t *testing.T) { StartLine: 1, EndLine: 1, StartPos: 3, - EndPos: 11, + EndPos: 12, }, }, Expr: &ast.ExprVariable{ @@ -19422,7 +19422,7 @@ func TestExprPrint(t *testing.T) { StartLine: 1, EndLine: 1, StartPos: 3, - EndPos: 11, + EndPos: 12, }, }, Expr: &ast.ExprVariable{ diff --git a/internal/php7/php7.go b/internal/php7/php7.go index 0c50734..89954b6 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 e125503..d43148c 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -993,38 +993,42 @@ statement: } | T_WHILE '(' expr ')' while_statement { + exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} + switch n := $5.(type) { case *ast.StmtWhile : - n.Cond = $3 + n.Cond = exprBrackets case *ast.StmtAltWhile : - n.Cond = $3 + n.Cond = exprBrackets } $$ = $5 // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) $$.GetNode().Position = position.NewTokenNodePosition($1, $5) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - yylex.(*Parser).setFreeFloating($$, token.While, $2.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $4.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } | T_DO statement T_WHILE '(' expr ')' ';' { - $$ = &ast.StmtDo{ast.Node{}, $2, $5} + exprBrackets := &ast.ParserBrackets{ast.Node{}, $5} + $$ = &ast.StmtDo{ast.Node{}, $2, exprBrackets} // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($4, $6) $$.GetNode().Position = position.NewTokensPosition($1, $7) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) yylex.(*Parser).setFreeFloating($$, token.Stmts, $3.Tokens) - yylex.(*Parser).setFreeFloating($$, token.While, $4.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $6.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Cond, $7.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $4.Tokens) + yylex.(*Parser).setFreeFloating(exprBrackets, token.End, append($6.Tokens, $7.Tokens...)) yylex.(*Parser).setToken($$, token.SemiColon, $7.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) @@ -1058,11 +1062,13 @@ statement: } | T_SWITCH '(' expr ')' switch_case_list { + exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} + switch n := $5.(type) { case *ast.StmtSwitch: - n.Cond = $3 + n.Cond = exprBrackets case *ast.StmtAltSwitch: - n.Cond = $3 + n.Cond = exprBrackets default: panic("unexpected node type") } @@ -1070,12 +1076,13 @@ statement: $$ = $5 // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) $$.GetNode().Position = position.NewTokenNodePosition($1, $5) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Switch, $2.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $4.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -1977,33 +1984,37 @@ while_statement: if_stmt_without_else: T_IF '(' expr ')' statement { - $$ = &ast.StmtIf{ast.Node{}, $3, $5, nil, nil} + exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} + $$ = &ast.StmtIf{ast.Node{}, exprBrackets, $5, nil, nil} // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) $$.GetNode().Position = position.NewTokenNodePosition($1, $5) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - yylex.(*Parser).setFreeFloating($$, token.If, $2.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $4.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } | if_stmt_without_else T_ELSEIF '(' expr ')' statement { - _elseIf := &ast.StmtElseIf{ast.Node{}, $4, $6} + exprBrackets := &ast.ParserBrackets{ast.Node{}, $4} + _elseIf := &ast.StmtElseIf{ast.Node{}, exprBrackets, $6} $1.(*ast.StmtIf).ElseIf = append($1.(*ast.StmtIf).ElseIf, _elseIf) $$ = $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.Tokens) - yylex.(*Parser).setFreeFloating(_elseIf, token.ElseIf, $3.Tokens) - yylex.(*Parser).setFreeFloating(_elseIf, token.Expr, $5.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $3.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $5.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -3862,11 +3873,14 @@ expr_without_variable: } | '(' expr ')' { - $$ = $2; + $$ = &ast.ParserBrackets{ast.Node{}, $2} + + // save position + $$.GetNode().Position = position.NewTokensPosition($1, $3) // save comments - yylex.(*Parser).setFreeFloatingTokens($$, token.Start, append($1.Tokens, $$.GetNode().Tokens[token.Start]...)) - yylex.(*Parser).setFreeFloatingTokens($$, token.End, append($$.GetNode().Tokens[token.End], $3.Tokens...)) + yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -4016,17 +4030,10 @@ expr_without_variable: } | T_EXIT exit_expr { - var e *ast.ExprExit; - if $2 != nil { - e = $2.(*ast.ExprExit) - } else { - e = &ast.ExprExit{ast.Node{}, false, nil} - } - - $$ = e + $$ = &ast.ExprExit{ast.Node{}, false, $2} if (bytes.EqualFold($1.Value, []byte("die"))) { - e.Die = true + $$.(*ast.ExprExit).Die = true } // save position @@ -4405,14 +4412,14 @@ exit_expr: } | '(' optional_expr ')' { - $$ = &ast.ExprExit{ast.Node{}, false, $2}; + $$ = &ast.ParserBrackets{ast.Node{}, $2} // save position $$.GetNode().Position = position.NewTokensPosition($1, $3) // save comments - yylex.(*Parser).setFreeFloatingTokens($$, token.Exit, $1.Tokens) - yylex.(*Parser).setFreeFloatingTokens($$, token.Expr, $3.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -4780,11 +4787,14 @@ dereferencable: } | '(' expr ')' { - $$ = $2; + $$ = &ast.ParserBrackets{ast.Node{}, $2} + + // save position + $$.GetNode().Position = position.NewTokensPosition($1, $3) // save comments - yylex.(*Parser).setFreeFloatingTokens($$, token.Start, append($1.Tokens, $$.GetNode().Tokens[token.Start]...)) - yylex.(*Parser).setFreeFloatingTokens($$, token.End, append($$.GetNode().Tokens[token.End], $3.Tokens...)) + yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -4805,11 +4815,14 @@ callable_expr: } | '(' expr ')' { - $$ = $2; + $$ = &ast.ParserBrackets{ast.Node{}, $2} + + // save position + $$.GetNode().Position = position.NewTokensPosition($1, $3) // save comments - yylex.(*Parser).setFreeFloatingTokens($$, token.Start, append($1.Tokens, $$.GetNode().Tokens[token.Start]...)) - yylex.(*Parser).setFreeFloatingTokens($$, token.End, append($$.GetNode().Tokens[token.End], $3.Tokens...)) + yylex.(*Parser).setFreeFloatingTokens($$, token.Start, $1.Tokens) + yylex.(*Parser).setFreeFloatingTokens($$, token.End, $3.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -5528,15 +5541,17 @@ internal_functions_in_yacc: } | T_EMPTY '(' expr ')' { - $$ = &ast.ExprEmpty{ast.Node{}, $3} + exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} + $$ = &ast.ExprEmpty{ast.Node{}, exprBrackets} // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Empty, $2.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $4.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } @@ -5566,15 +5581,17 @@ internal_functions_in_yacc: } | T_EVAL '(' expr ')' { - $$ = &ast.ExprEval{ast.Node{}, $3} + exprBrackets := &ast.ParserBrackets{ast.Node{}, $3} + $$ = &ast.ExprEval{ast.Node{}, exprBrackets} // save position + exprBrackets.GetNode().Position = position.NewTokensPosition($2, $4) $$.GetNode().Position = position.NewTokensPosition($1, $4) // save comments yylex.(*Parser).setFreeFloating($$, token.Start, $1.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Eval, $2.Tokens) - yylex.(*Parser).setFreeFloating($$, token.Expr, $4.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.Start, $2.Tokens) + yylex.(*Parser).setFreeFloatingTokens(exprBrackets, token.End, $4.Tokens) yylex.(*Parser).returnTokenToPool(yyDollar, &yyVAL) } diff --git a/internal/php7/php7_test.go b/internal/php7/php7_test.go index 49f450b..68364f5 100644 --- a/internal/php7/php7_test.go +++ b/internal/php7/php7_test.go @@ -11046,7 +11046,7 @@ func TestPhp7(t *testing.T) { StartLine: 187, EndLine: 187, StartPos: 3780, - EndPos: 3788, + EndPos: 3789, }, }, Expr: &ast.ExprVariable{ @@ -13123,7 +13123,7 @@ func TestPhp7(t *testing.T) { StartLine: 232, EndLine: 232, StartPos: 4298, - EndPos: 4306, + EndPos: 4307, }, }, Expr: &ast.ExprVariable{ @@ -18632,7 +18632,7 @@ func TestPhp7(t *testing.T) { Position: &position.Position{ StartLine: 332, EndLine: 332, - StartPos: 5518, + StartPos: 5517, EndPos: 5532, }, }, @@ -18641,7 +18641,7 @@ func TestPhp7(t *testing.T) { Position: &position.Position{ StartLine: 332, EndLine: 332, - StartPos: 5518, + StartPos: 5517, EndPos: 5531, }, }, @@ -18696,7 +18696,7 @@ func TestPhp7(t *testing.T) { Position: &position.Position{ StartLine: 333, EndLine: 333, - StartPos: 5534, + StartPos: 5533, EndPos: 5545, }, }, @@ -18705,7 +18705,7 @@ func TestPhp7(t *testing.T) { Position: &position.Position{ StartLine: 333, EndLine: 333, - StartPos: 5534, + StartPos: 5533, EndPos: 5544, }, }, diff --git a/pkg/ast/visitor/filter_parser_nodes.go b/pkg/ast/visitor/filter_parser_nodes.go index a7f5529..f1a5655 100644 --- a/pkg/ast/visitor/filter_parser_nodes.go +++ b/pkg/ast/visitor/filter_parser_nodes.go @@ -42,8 +42,12 @@ func (v *FilterParserNodes) StmtUseDeclaration(n *ast.StmtUseDeclaration) { } func (v *FilterParserNodes) StmtAltIf(n *ast.StmtAltIf) { - if nn, ok := n.Cond.(*ast.ParserBrackets); ok { - n.Cond = nn.Child + for { + if nn, ok := n.Cond.(*ast.ParserBrackets); ok { + n.Cond = nn.Child + } else { + break + } } if nn, ok := n.Stmt.(*ast.ParserBrackets); ok { @@ -52,8 +56,12 @@ func (v *FilterParserNodes) StmtAltIf(n *ast.StmtAltIf) { } func (v *FilterParserNodes) StmtAltElseIf(n *ast.StmtAltElseIf) { - if nn, ok := n.Cond.(*ast.ParserBrackets); ok { - n.Cond = nn.Child + for { + if nn, ok := n.Cond.(*ast.ParserBrackets); ok { + n.Cond = nn.Child + } else { + break + } } if nn, ok := n.Stmt.(*ast.ParserBrackets); ok { @@ -66,3 +74,1194 @@ func (v *FilterParserNodes) StmtAltElse(n *ast.StmtAltElse) { 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) { + for { + if nn, ok := n.Cond.(*ast.ParserBrackets); ok { + n.Cond = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtAltWhile(n *ast.StmtAltWhile) { + for { + if nn, ok := n.Cond.(*ast.ParserBrackets); ok { + n.Cond = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtDo(n *ast.StmtDo) { + for { + if nn, ok := n.Cond.(*ast.ParserBrackets); ok { + n.Cond = nn.Child + } else { + break + } + } +} + +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 { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtContinue(n *ast.StmtContinue) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtBreak(n *ast.StmtBreak) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprClone(n *ast.ExprClone) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprPrint(n *ast.ExprPrint) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtExpression(n *ast.StmtExpression) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtEcho(n *ast.StmtEcho) { + for k, v := range n.Exprs { + for { + if nn, ok := v.(*ast.ParserBrackets); ok { + v = nn.Child + } else { + break + } + } + + n.Exprs[k] = v + } +} + +func (v *FilterParserNodes) ExprIsset(n *ast.ExprIsset) { + for k, v := range n.Vars { + for { + if nn, ok := v.(*ast.ParserBrackets); ok { + v = nn.Child + } else { + break + } + } + + n.Vars[k] = v + } +} + +func (v *FilterParserNodes) StmtReturn(n *ast.StmtReturn) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtForeach(n *ast.StmtForeach) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtAltForeach(n *ast.StmtAltForeach) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprYield(n *ast.ExprYield) { + for { + if nn, ok := n.Key.(*ast.ParserBrackets); ok { + n.Key = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Value.(*ast.ParserBrackets); ok { + n.Value = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) Argument(n *ast.Argument) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtThrow(n *ast.StmtThrow) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtCase(n *ast.StmtCase) { + for { + if nn, ok := n.Cond.(*ast.ParserBrackets); ok { + n.Cond = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprVariable(n *ast.ExprVariable) { + for { + if nn, ok := n.VarName.(*ast.ParserBrackets); ok { + n.VarName = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) StmtFor(n *ast.StmtFor) { + for k, v := range n.Init { + for { + if nn, ok := v.(*ast.ParserBrackets); ok { + v = nn.Child + } else { + break + } + } + + n.Init[k] = v + } + + for k, v := range n.Cond { + for { + if nn, ok := v.(*ast.ParserBrackets); ok { + v = nn.Child + } else { + break + } + } + + n.Cond[k] = v + } + + for k, v := range n.Loop { + for { + if nn, ok := v.(*ast.ParserBrackets); ok { + v = nn.Child + } else { + break + } + } + + n.Loop[k] = v + } +} + +func (v *FilterParserNodes) ExprAssign(n *ast.ExprAssign) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignBitwiseAnd(n *ast.ExprAssignBitwiseAnd) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignBitwiseOr(n *ast.ExprAssignBitwiseOr) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignBitwiseXor(n *ast.ExprAssignBitwiseXor) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignCoalesce(n *ast.ExprAssignCoalesce) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignConcat(n *ast.ExprAssignConcat) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignDiv(n *ast.ExprAssignDiv) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignMinus(n *ast.ExprAssignMinus) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignMod(n *ast.ExprAssignMod) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignMul(n *ast.ExprAssignMul) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignPlus(n *ast.ExprAssignPlus) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignPow(n *ast.ExprAssignPow) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignShiftLeft(n *ast.ExprAssignShiftLeft) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprAssignShiftRight(n *ast.ExprAssignShiftRight) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} +func (v *FilterParserNodes) ExprBinaryBitwiseAnd(n *ast.ExprBinaryBitwiseAnd) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryBitwiseOr(n *ast.ExprBinaryBitwiseOr) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryBitwiseXor(n *ast.ExprBinaryBitwiseXor) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryBooleanAnd(n *ast.ExprBinaryBooleanAnd) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryBooleanOr(n *ast.ExprBinaryBooleanOr) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryCoalesce(n *ast.ExprBinaryCoalesce) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryConcat(n *ast.ExprBinaryConcat) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryDiv(n *ast.ExprBinaryDiv) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryEqual(n *ast.ExprBinaryEqual) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryGreater(n *ast.ExprBinaryGreater) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryGreaterOrEqual(n *ast.ExprBinaryGreaterOrEqual) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryIdentical(n *ast.ExprBinaryIdentical) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryLogicalAnd(n *ast.ExprBinaryLogicalAnd) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryLogicalOr(n *ast.ExprBinaryLogicalOr) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryLogicalXor(n *ast.ExprBinaryLogicalXor) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryMinus(n *ast.ExprBinaryMinus) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryMod(n *ast.ExprBinaryMod) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryMul(n *ast.ExprBinaryMul) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryNotEqual(n *ast.ExprBinaryNotEqual) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryNotIdentical(n *ast.ExprBinaryNotIdentical) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryPlus(n *ast.ExprBinaryPlus) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryPow(n *ast.ExprBinaryPow) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryShiftLeft(n *ast.ExprBinaryShiftLeft) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinaryShiftRight(n *ast.ExprBinaryShiftRight) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinarySmaller(n *ast.ExprBinarySmaller) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinarySmallerOrEqual(n *ast.ExprBinarySmallerOrEqual) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBinarySpaceship(n *ast.ExprBinarySpaceship) { + for { + if nn, ok := n.Left.(*ast.ParserBrackets); ok { + n.Left = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Right.(*ast.ParserBrackets); ok { + n.Right = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprUnaryMinus(n *ast.ExprUnaryMinus) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprUnaryPlus(n *ast.ExprUnaryPlus) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBooleanNot(n *ast.ExprBooleanNot) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprBitwiseNot(n *ast.ExprBitwiseNot) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprInstanceOf(n *ast.ExprInstanceOf) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprTernary(n *ast.ExprTernary) { + for { + if nn, ok := n.Condition.(*ast.ParserBrackets); ok { + n.Condition = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.IfTrue.(*ast.ParserBrackets); ok { + n.IfTrue = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.IfFalse.(*ast.ParserBrackets); ok { + n.IfFalse = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprCastArray(n *ast.ExprCastArray) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprCastBool(n *ast.ExprCastBool) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprCastDouble(n *ast.ExprCastDouble) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprCastInt(n *ast.ExprCastInt) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprCastObject(n *ast.ExprCastObject) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprCastString(n *ast.ExprCastString) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprCastUnset(n *ast.ExprCastUnset) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprErrorSuppress(n *ast.ExprErrorSuppress) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprArrayDimFetch(n *ast.ExprArrayDimFetch) { + for { + if nn, ok := n.Dim.(*ast.ParserBrackets); ok { + n.Dim = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprArrayItem(n *ast.ExprArrayItem) { + for { + if nn, ok := n.Key.(*ast.ParserBrackets); ok { + n.Key = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Val.(*ast.ParserBrackets); ok { + n.Val = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprEmpty(n *ast.ExprEmpty) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprInclude(n *ast.ExprInclude) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprIncludeOnce(n *ast.ExprIncludeOnce) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprEval(n *ast.ExprEval) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprRequire(n *ast.ExprRequire) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprRequireOnce(n *ast.ExprRequireOnce) { + for { + if nn, ok := n.Expr.(*ast.ParserBrackets); ok { + n.Expr = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprPropertyFetch(n *ast.ExprPropertyFetch) { + for { + if nn, ok := n.Var.(*ast.ParserBrackets); ok { + n.Var = nn.Child + } else { + break + } + } + + for { + if nn, ok := n.Property.(*ast.ParserBrackets); ok { + n.Property = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprFunctionCall(n *ast.ExprFunctionCall) { + for { + if nn, ok := n.Function.(*ast.ParserBrackets); ok { + n.Function = nn.Child + } else { + break + } + } +} + +func (v *FilterParserNodes) ExprStaticCall(n *ast.ExprStaticCall) { + for { + if nn, ok := n.Call.(*ast.ParserBrackets); ok { + n.Call = nn.Child + } else { + break + } + } +} diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 64a32c9..3951b6f 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -1530,11 +1530,16 @@ func (p *Printer) printExprEmpty(n ast.Vertex) { nn := n.(*ast.ExprEmpty) p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "empty") - p.printFreeFloating(nn, token.Empty) - io.WriteString(p.w, "(") + + if _, ok := nn.Expr.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Expr) - p.printFreeFloating(nn, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Expr.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } p.printFreeFloating(nn, token.End) } @@ -1551,12 +1556,18 @@ func (p *Printer) printExprErrorSuppress(n ast.Vertex) { func (p *Printer) printExprEval(n ast.Vertex) { nn := n.(*ast.ExprEval) p.printFreeFloating(nn, token.Start) + io.WriteString(p.w, "eval") - p.printFreeFloating(nn, token.Eval) - io.WriteString(p.w, "(") + + if _, ok := nn.Expr.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Expr) - p.printFreeFloating(nn, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Expr.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } p.printFreeFloating(nn, token.End) } @@ -1570,7 +1581,6 @@ func (p *Printer) printExprExit(n ast.Vertex) { } else { io.WriteString(p.w, "exit") } - p.printFreeFloating(nn, token.Exit) if nn.Expr != nil && nn.Expr.GetNode().Tokens.IsEmpty() && nn.GetNode().Tokens.IsEmpty() { io.WriteString(p.w, " ") @@ -2163,11 +2173,17 @@ func (p *Printer) printStmtAltSwitch(n ast.Vertex) { p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "switch") - p.printFreeFloating(nn, token.Switch) - io.WriteString(p.w, "(") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Cond) - p.printFreeFloating(nn, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } + p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, ":") @@ -2192,11 +2208,17 @@ func (p *Printer) printStmtAltWhile(n ast.Vertex) { p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "while") - p.printFreeFloating(nn, token.While) - io.WriteString(p.w, "(") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Cond) - p.printFreeFloating(nn, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } + p.printFreeFloating(nn, token.Cond) io.WriteString(p.w, ":") @@ -2543,11 +2565,17 @@ func (p *Printer) printStmtDo(n ast.Vertex) { p.printFreeFloating(nn, token.Stmts) io.WriteString(p.w, "while") - p.printFreeFloating(nn, token.While) - io.WriteString(p.w, "(") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Cond) - p.printFreeFloating(nn, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } + p.printFreeFloating(nn, token.Cond) p.printFreeFloating(nn, token.SemiColon) @@ -2587,11 +2615,16 @@ func (p *Printer) printStmtElseif(n ast.Vertex) { p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "elseif") - p.printFreeFloating(nn, token.ElseIf) - io.WriteString(p.w, "(") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Cond) - p.printFreeFloating(nn, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } p.Print(nn.Stmt) @@ -2794,11 +2827,16 @@ func (p *Printer) printStmtIf(n ast.Vertex) { p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "if") - p.printFreeFloating(n, token.If) - io.WriteString(p.w, "(") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Cond) - p.printFreeFloating(n, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } p.Print(nn.Stmt) @@ -3020,11 +3058,16 @@ func (p *Printer) printStmtSwitch(n ast.Vertex) { p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "switch") - p.printFreeFloating(nn, token.Switch) - io.WriteString(p.w, "(") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Cond) - p.printFreeFloating(nn, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } p.printFreeFloating(nn.CaseList, token.Start) io.WriteString(p.w, "{") @@ -3292,11 +3335,16 @@ func (p *Printer) printStmtWhile(n ast.Vertex) { p.printFreeFloating(nn, token.Start) io.WriteString(p.w, "while") - p.printFreeFloating(nn, token.While) - io.WriteString(p.w, "(") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, "(") + } + p.Print(nn.Cond) - p.printFreeFloating(nn, token.Expr) - io.WriteString(p.w, ")") + + if _, ok := nn.Cond.(*ast.ParserBrackets); !ok { + io.WriteString(p.w, ")") + } p.Print(nn.Stmt) diff --git a/pkg/token/position.go b/pkg/token/position.go index 329f8aa..2ea40d4 100644 --- a/pkg/token/position.go +++ b/pkg/token/position.go @@ -27,28 +27,19 @@ const ( Namespace Static - Class Use - While For - Switch Foreach Declare Label Finally List Default - If - ElseIf - Else Function Alias Equal - Exit Array Isset - Empty - Eval Echo Try Catch @@ -68,7 +59,6 @@ const ( AdaptationList LexicalVarList - OpenParenthesisToken CloseParenthesisToken ) diff --git a/pkg/token/position_string.go b/pkg/token/position_string.go index 389ec94..ac7c7b5 100644 --- a/pkg/token/position_string.go +++ b/pkg/token/position_string.go @@ -30,52 +30,42 @@ func _() { _ = x[Cond-19] _ = x[Namespace-20] _ = x[Static-21] - _ = x[Class-22] - _ = x[Use-23] - _ = x[While-24] - _ = x[For-25] - _ = x[Switch-26] - _ = x[Foreach-27] - _ = x[Declare-28] - _ = x[Label-29] - _ = x[Finally-30] - _ = x[List-31] - _ = x[Default-32] - _ = x[If-33] - _ = x[ElseIf-34] - _ = x[Else-35] - _ = x[Function-36] - _ = x[Alias-37] - _ = x[Equal-38] - _ = x[Exit-39] - _ = x[Array-40] - _ = x[Isset-41] - _ = x[Empty-42] - _ = x[Eval-43] - _ = x[Echo-44] - _ = x[Try-45] - _ = x[Catch-46] - _ = x[Unset-47] - _ = x[Stmts-48] - _ = x[VarList-49] - _ = x[ConstList-50] - _ = x[NameList-51] - _ = x[ParamList-52] - _ = x[ModifierList-53] - _ = x[ArrayPairList-54] - _ = x[CaseListStart-55] - _ = x[CaseListEnd-56] - _ = x[PropertyList-57] - _ = x[ParameterList-58] - _ = x[AdaptationList-59] - _ = x[LexicalVarList-60] - _ = x[OpenParenthesisToken-61] - _ = x[CloseParenthesisToken-62] + _ = x[Use-22] + _ = x[For-23] + _ = x[Foreach-24] + _ = x[Declare-25] + _ = x[Label-26] + _ = x[Finally-27] + _ = x[List-28] + _ = x[Default-29] + _ = x[Function-30] + _ = x[Alias-31] + _ = x[Equal-32] + _ = x[Array-33] + _ = x[Isset-34] + _ = x[Echo-35] + _ = x[Try-36] + _ = x[Catch-37] + _ = x[Unset-38] + _ = x[Stmts-39] + _ = x[VarList-40] + _ = x[ConstList-41] + _ = x[NameList-42] + _ = x[ParamList-43] + _ = x[ModifierList-44] + _ = x[ArrayPairList-45] + _ = x[CaseListStart-46] + _ = x[CaseListEnd-47] + _ = x[PropertyList-48] + _ = x[ParameterList-49] + _ = x[AdaptationList-50] + _ = x[LexicalVarList-51] + _ = x[CloseParenthesisToken-52] } -const _Position_name = "StartEndSemiColonAltEndAmpersandNameKeyVarReturnTypeCaseSeparatorLexicalVarsParamsRefCastExprInitExprCondExprIncExprTrueCondNamespaceStaticClassUseWhileForSwitchForeachDeclareLabelFinallyListDefaultIfElseIfElseFunctionAliasEqualExitArrayIssetEmptyEvalEchoTryCatchUnsetStmtsVarListConstListNameListParamListModifierListArrayPairListCaseListStartCaseListEndPropertyListParameterListAdaptationListLexicalVarListOpenParenthesisTokenCloseParenthesisToken" +const _Position_name = "StartEndSemiColonAltEndAmpersandNameKeyVarReturnTypeCaseSeparatorLexicalVarsParamsRefCastExprInitExprCondExprIncExprTrueCondNamespaceStaticUseForForeachDeclareLabelFinallyListDefaultFunctionAliasEqualArrayIssetEchoTryCatchUnsetStmtsVarListConstListNameListParamListModifierListArrayPairListCaseListStartCaseListEndPropertyListParameterListAdaptationListLexicalVarListCloseParenthesisToken" -var _Position_index = [...]uint16{0, 5, 8, 17, 23, 32, 36, 39, 42, 52, 65, 76, 82, 85, 89, 93, 101, 109, 116, 120, 124, 133, 139, 144, 147, 152, 155, 161, 168, 175, 180, 187, 191, 198, 200, 206, 210, 218, 223, 228, 232, 237, 242, 247, 251, 255, 258, 263, 268, 273, 280, 289, 297, 306, 318, 331, 344, 355, 367, 380, 394, 408, 428, 449} +var _Position_index = [...]uint16{0, 5, 8, 17, 23, 32, 36, 39, 42, 52, 65, 76, 82, 85, 89, 93, 101, 109, 116, 120, 124, 133, 139, 142, 145, 152, 159, 164, 171, 175, 182, 190, 195, 200, 205, 210, 214, 217, 222, 227, 232, 239, 248, 256, 265, 277, 290, 303, 314, 326, 339, 353, 367, 388} func (i Position) String() string { if i < 0 || i >= Position(len(_Position_index)-1) {