diff --git a/internal/php5/php5.go b/internal/php5/php5.go index e87357e..324e992 100644 Binary files a/internal/php5/php5.go and b/internal/php5/php5.go differ diff --git a/internal/php5/php5.y b/internal/php5/php5.y index fb42de1..f599ddd 100644 --- a/internal/php5/php5.y +++ b/internal/php5/php5.y @@ -241,7 +241,7 @@ import ( %type trait_method_reference_fully_qualified trait_method_reference trait_modifiers member_modifier method %type static_scalar_value static_operation static_var_list global_var_list %type ctor_arguments function_call_parameter_list echo_expr_list -%type trait_adaptations +%type trait_adaptations unset_variables %type switch_case_list %type method_body %type foreach_statement for_statement while_statement @@ -255,7 +255,7 @@ import ( %type top_statement_list namespace_name use_declarations use_function_declarations use_const_declarations %type inner_statement_list encaps_list isset_variables non_empty_array_pair_list %type array_pair_list assignment_list lexical_var_list elseif_list new_elseif_list non_empty_for_expr -%type for_expr case_list unset_variables declare_list catch_statement additional_catches +%type for_expr case_list declare_list catch_statement additional_catches %type non_empty_additional_catches parameter_list non_empty_parameter_list class_statement_list %type class_statement_list variable_modifiers method_modifiers class_variable_declaration %type interface_list non_empty_function_call_parameter_list trait_list trait_adaptation_list non_empty_trait_adaptation_list @@ -1084,17 +1084,13 @@ unticked_statement: } | T_UNSET '(' unset_variables ')' ';' { - $$ = &ast.StmtUnset{ast.Node{}, $3} + $3.(*ast.StmtUnset).UnsetTkn = $1 + $3.(*ast.StmtUnset).OpenParenthesisTkn = $2 + $3.(*ast.StmtUnset).CloseParenthesisTkn = $4 + $3.(*ast.StmtUnset).SemiColonTkn = $5 + $3.(*ast.StmtUnset).Node.Position = position.NewTokensPosition($1, $5) - // save position - $$.GetNode().Position = position.NewTokensPosition($1, $5) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Unset, $2.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.VarList, $4.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.CloseParenthesisToken, $5.SkippedTokens) - yylex.(*Parser).setToken($$, token.SemiColon, $5.SkippedTokens) + $$ = $3 } | T_FOREACH '(' variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement { @@ -1334,14 +1330,16 @@ additional_catch: unset_variables: unset_variable { - $$ = []ast.Vertex{$1} + $$ = &ast.StmtUnset{ + Vars: []ast.Vertex{$1}, + } } | unset_variables ',' unset_variable { - $$ = append($1, $3) + $1.(*ast.StmtUnset).Vars = append($1.(*ast.StmtUnset).Vars, $3) + $1.(*ast.StmtUnset).SeparatorTkns = append($1.(*ast.StmtUnset).SeparatorTkns, $2) - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) + $$ = $1 } ; diff --git a/internal/php7/php7.go b/internal/php7/php7.go index ab2cdc4..56d0fc9 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 613408f..8a2dfb4 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -264,6 +264,7 @@ import ( %type method_body %type foreach_statement for_statement while_statement %type inline_function +%type unset_variables %type extends_from %type implements_list %type interface_extends_list @@ -277,7 +278,7 @@ import ( %type encaps_list backticks_expr namespace_name catch_name_list catch_list class_const_list %type const_list for_exprs non_empty_for_exprs %type unprefixed_use_declarations inline_use_declarations property_list -%type case_list trait_adaptation_list unset_variables +%type case_list trait_adaptation_list %type use_declarations lexical_var_list isset_variables non_empty_array_pair_list %type array_pair_list non_empty_argument_list top_statement_list %type inner_statement_list parameter_list non_empty_parameter_list class_statement_list @@ -998,21 +999,14 @@ statement: } | T_UNSET '(' unset_variables possible_comma ')' ';' { - $$ = &ast.StmtUnset{ast.Node{}, $3} + $3.(*ast.StmtUnset).UnsetTkn = $1 + $3.(*ast.StmtUnset).OpenParenthesisTkn = $2 + $3.(*ast.StmtUnset).SeparatorTkns = append($3.(*ast.StmtUnset).SeparatorTkns, $4) + $3.(*ast.StmtUnset).CloseParenthesisTkn = $5 + $3.(*ast.StmtUnset).SemiColonTkn = $6 + $3.(*ast.StmtUnset).Node.Position = position.NewTokensPosition($1, $6) - // save position - $$.GetNode().Position = position.NewTokensPosition($1, $6) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) - yylex.(*Parser).setFreeFloating($$, token.Unset, $2.SkippedTokens) - if $4 != nil { - yylex.(*Parser).setFreeFloating($$, token.VarList, append($4.SkippedTokens, $5.SkippedTokens...)) - } else { - yylex.(*Parser).setFreeFloating($$, token.VarList, $5.SkippedTokens) - } - yylex.(*Parser).setFreeFloating($$, token.CloseParenthesisToken, $6.SkippedTokens) - yylex.(*Parser).setToken($$, token.SemiColon, $6.SkippedTokens) + $$ = $3 } | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement { @@ -1205,14 +1199,16 @@ finally_statement: unset_variables: unset_variable { - $$ = []ast.Vertex{$1} + $$ = &ast.StmtUnset{ + Vars: []ast.Vertex{$1}, + } } | unset_variables ',' unset_variable { - $$ = append($1, $3) + $1.(*ast.StmtUnset).Vars = append($1.(*ast.StmtUnset).Vars, $3) + $1.(*ast.StmtUnset).SeparatorTkns = append($1.(*ast.StmtUnset).SeparatorTkns, $2) - // save comments - yylex.(*Parser).setFreeFloating(lastNode($1), token.End, $2.SkippedTokens) + $$ = $1 } ; diff --git a/pkg/ast/node.go b/pkg/ast/node.go index e83b438..50235a7 100644 --- a/pkg/ast/node.go +++ b/pkg/ast/node.go @@ -783,7 +783,12 @@ func (n *StmtTry) Accept(v NodeVisitor) { // StmtUnset node type StmtUnset struct { Node - Vars []Vertex + UnsetTkn *token.Token + OpenParenthesisTkn *token.Token + Vars []Vertex + SeparatorTkns []*token.Token + CloseParenthesisTkn *token.Token + SemiColonTkn *token.Token } func (n *StmtUnset) Accept(v NodeVisitor) { diff --git a/pkg/ast/visitor/filter_tokens.go b/pkg/ast/visitor/filter_tokens.go index f3c440e..d3ad950 100644 --- a/pkg/ast/visitor/filter_tokens.go +++ b/pkg/ast/visitor/filter_tokens.go @@ -204,3 +204,12 @@ func (v *FilterTokens) StmtEcho(n *ast.StmtEcho) { func (v *FilterTokens) StmtInlineHtml(n *ast.StmtInlineHtml) { n.InlineHtmlTkn = nil } + +func (v *FilterTokens) StmtUnset(n *ast.StmtUnset) { + n.UnsetTkn = nil + n.OpenParenthesisTkn = nil + n.SeparatorTkns = nil + n.CloseParenthesisTkn = nil + n.SemiColonTkn = nil + n.SemiColonTkn = nil +} diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index b35840d..0068785 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -2905,24 +2905,12 @@ func (p *Printer) printStmtTry(n ast.Vertex) { p.printFreeFloating(nn, token.End) } -func (p *Printer) printStmtUnset(n ast.Vertex) { - nn := n.(*ast.StmtUnset) - p.printFreeFloating(nn, token.Start) - - p.write([]byte("unset")) - p.printFreeFloating(nn, token.Unset) - p.write([]byte("(")) - p.joinPrint(",", nn.Vars) - p.printFreeFloating(nn, token.VarList) - p.write([]byte(")")) - p.printFreeFloating(nn, token.CloseParenthesisToken) - - p.printFreeFloating(nn, token.SemiColon) - if n.GetNode().Tokens.IsEmpty() { - p.write([]byte(";")) - } - - p.printFreeFloating(nn, token.End) +func (p *Printer) printStmtUnset(n *ast.StmtUnset) { + p.printToken(n.UnsetTkn, "unset") + p.printToken(n.OpenParenthesisTkn, "(") + p.printSeparatedList(n.Vars, n.SeparatorTkns, ",") + p.printToken(n.CloseParenthesisTkn, ")") + p.printToken(n.SemiColonTkn, ";") } func (p *Printer) printStmtUse(n *ast.StmtUse) {