diff --git a/internal/php5/php5.go b/internal/php5/php5.go index 040152d..5cc2459 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 1502664..bcdf386 100644 --- a/internal/php5/php5.y +++ b/internal/php5/php5.y @@ -3123,17 +3123,25 @@ instance_call: new_expr: T_NEW class_name_reference ctor_arguments { - if $3 != nil { - $$ = &ast.ExprNew{ast.Node{}, $2, $3.(*ast.ArgumentList)} - $$.GetNode().Position = position.NewTokenNodePosition($1, $3) + $$ = &ast.ExprNew{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $3), + }, + NewTkn: $1, + Class: $2, + OpenParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn, + Arguments: $3.(*ast.ArgumentList).Arguments, + CloseParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn, + } } else { - $$ = &ast.ExprNew{ast.Node{}, $2, nil} - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) + $$ = &ast.ExprNew{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + Class: $2, + } } - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) } ; @@ -3195,12 +3203,27 @@ expr_without_variable: | variable '=' '&' T_NEW class_name_reference ctor_arguments { var _new *ast.ExprNew - - if $6 != nil { - _new = &ast.ExprNew{ast.Node{}, $5, $6.(*ast.ArgumentList)} + if $3 != nil { + _new = &ast.ExprNew{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($4, $6), + }, + NewTkn: $4, + Class: $5, + OpenParenthesisTkn: $6.(*ast.ArgumentList).OpenParenthesisTkn, + Arguments: $6.(*ast.ArgumentList).Arguments, + CloseParenthesisTkn: $6.(*ast.ArgumentList).OpenParenthesisTkn, + } } else { - _new = &ast.ExprNew{ast.Node{}, $5, nil} + _new = &ast.ExprNew{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($4, $5), + }, + NewTkn: $4, + Class: $5, + } } + $$ = &ast.ExprAssignReference{ast.Node{}, $1, _new} // save position @@ -3360,45 +3383,43 @@ expr_without_variable: } | rw_variable T_INC { - $$ = &ast.ExprPostInc{ast.Node{}, $1} - - // save position - $$.GetNode().Position = position.NewNodeTokenPosition($1, $2) - - // save comments - yylex.(*Parser).MoveFreeFloating($1, $$) - yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens) + $$ = &ast.ExprPostInc{ + Node: ast.Node{ + Position: position.NewNodeTokenPosition($1, $2), + }, + Var: $1, + IncTkn: $2, + } } | T_INC rw_variable { - $$ = &ast.ExprPreInc{ast.Node{}, $2} - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.ExprPreInc{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + IncTkn: $1, + Var: $2, + } } | rw_variable T_DEC { - $$ = &ast.ExprPostDec{ast.Node{}, $1} - - // save position - $$.GetNode().Position = position.NewNodeTokenPosition($1, $2) - - // save comments - yylex.(*Parser).MoveFreeFloating($1, $$) - yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens) + $$ = &ast.ExprPostDec{ + Node: ast.Node{ + Position: position.NewNodeTokenPosition($1, $2), + }, + Var: $1, + DecTkn: $2, + } } | T_DEC rw_variable { - $$ = &ast.ExprPreDec{ast.Node{}, $2} - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.ExprPreDec{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + DecTkn: $1, + Var: $2, + } } | expr T_BOOLEAN_OR expr { diff --git a/internal/php7/php7.go b/internal/php7/php7.go index 3979201..cfb8eb3 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 ca0a1c2..581403b 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -2778,25 +2778,35 @@ new_expr: T_NEW class_name_reference ctor_arguments { if $3 != nil { - $$ = &ast.ExprNew{ast.Node{}, $2, $3.(*ast.ArgumentList)} - $$.GetNode().Position = position.NewTokenNodePosition($1, $3) + $$ = &ast.ExprNew{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $3), + }, + NewTkn: $1, + Class: $2, + OpenParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn, + Arguments: $3.(*ast.ArgumentList).Arguments, + CloseParenthesisTkn: $3.(*ast.ArgumentList).OpenParenthesisTkn, + } } else { - $$ = &ast.ExprNew{ast.Node{}, $2, nil} - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) + $$ = &ast.ExprNew{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + NewTkn: $1, + Class: $2, + } } - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) } | T_NEW anonymous_class { - $$ = &ast.ExprNew{ast.Node{}, $2, nil} - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.ExprNew{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + NewTkn: $1, + Class: $2, + } } ; @@ -3018,45 +3028,43 @@ expr_without_variable: } | variable T_INC { - $$ = &ast.ExprPostInc{ast.Node{}, $1} - - // save position - $$.GetNode().Position = position.NewNodeTokenPosition($1, $2) - - // save comments - yylex.(*Parser).MoveFreeFloating($1, $$) - yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens) + $$ = &ast.ExprPostInc{ + Node: ast.Node{ + Position: position.NewNodeTokenPosition($1, $2), + }, + Var: $1, + IncTkn: $2, + } } | T_INC variable { - $$ = &ast.ExprPreInc{ast.Node{}, $2} - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.ExprPreInc{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + IncTkn: $1, + Var: $2, + } } | variable T_DEC { - $$ = &ast.ExprPostDec{ast.Node{}, $1} - - // save position - $$.GetNode().Position = position.NewNodeTokenPosition($1, $2) - - // save comments - yylex.(*Parser).MoveFreeFloating($1, $$) - yylex.(*Parser).setFreeFloating($$, token.Var, $2.SkippedTokens) + $$ = &ast.ExprPostDec{ + Node: ast.Node{ + Position: position.NewNodeTokenPosition($1, $2), + }, + Var: $1, + DecTkn: $2, + } } | T_DEC variable { - $$ = &ast.ExprPreDec{ast.Node{}, $2} - - // save position - $$.GetNode().Position = position.NewTokenNodePosition($1, $2) - - // save comments - yylex.(*Parser).setFreeFloating($$, token.Start, $1.SkippedTokens) + $$ = &ast.ExprPreDec{ + Node: ast.Node{ + Position: position.NewTokenNodePosition($1, $2), + }, + DecTkn: $1, + Var: $2, + } } | expr T_BOOLEAN_OR expr { diff --git a/pkg/ast/node.go b/pkg/ast/node.go index 9f90db7..cadaa12 100644 --- a/pkg/ast/node.go +++ b/pkg/ast/node.go @@ -1230,8 +1230,11 @@ func (n *ExprMethodCall) Accept(v NodeVisitor) { // ExprNew node type ExprNew struct { Node - Class Vertex - ArgumentList *ArgumentList + NewTkn *token.Token + Class Vertex + OpenParenthesisTkn *token.Token + Arguments []Vertex + CloseParenthesisTkn *token.Token } func (n *ExprNew) Accept(v NodeVisitor) { @@ -1241,7 +1244,8 @@ func (n *ExprNew) Accept(v NodeVisitor) { // ExprPostDec node type ExprPostDec struct { Node - Var Vertex + Var Vertex + DecTkn *token.Token } func (n *ExprPostDec) Accept(v NodeVisitor) { @@ -1251,7 +1255,8 @@ func (n *ExprPostDec) Accept(v NodeVisitor) { // ExprPostInc node type ExprPostInc struct { Node - Var Vertex + Var Vertex + IncTkn *token.Token } func (n *ExprPostInc) Accept(v NodeVisitor) { @@ -1261,7 +1266,8 @@ func (n *ExprPostInc) Accept(v NodeVisitor) { // ExprPreDec node type ExprPreDec struct { Node - Var Vertex + DecTkn *token.Token + Var Vertex } func (n *ExprPreDec) Accept(v NodeVisitor) { @@ -1271,7 +1277,8 @@ func (n *ExprPreDec) Accept(v NodeVisitor) { // ExprPreInc node type ExprPreInc struct { Node - Var Vertex + IncTkn *token.Token + Var Vertex } func (n *ExprPreInc) Accept(v NodeVisitor) { diff --git a/pkg/ast/traverser/dfs.go b/pkg/ast/traverser/dfs.go index fecf672..ad492b0 100644 --- a/pkg/ast/traverser/dfs.go +++ b/pkg/ast/traverser/dfs.go @@ -1415,10 +1415,12 @@ func (t *DFS) Traverse(n ast.Vertex) { t.Traverse(nn.Class) t.visitor.Leave("Class", true) } - if nn.ArgumentList != nil { - t.visitor.Enter("ArgumentList", true) - t.Traverse(nn.ArgumentList) - t.visitor.Leave("ArgumentList", true) + if nn.Arguments != nil { + t.visitor.Enter("Arguments", false) + for _, c := range nn.Arguments { + t.Traverse(c) + } + t.visitor.Leave("Arguments", false) } case *ast.ExprPostDec: if nn == nil { diff --git a/pkg/printer/pretty_printer.go b/pkg/printer/pretty_printer.go index ef01e44..a840cea 100644 --- a/pkg/printer/pretty_printer.go +++ b/pkg/printer/pretty_printer.go @@ -1143,9 +1143,9 @@ func (p *PrettyPrinter) printExprNew(n ast.Vertex) { io.WriteString(p.w, "new ") p.Print(nn.Class) - if nn.ArgumentList != nil { + if nn.Arguments != nil { io.WriteString(p.w, "(") - p.joinPrint(", ", nn.ArgumentList.Arguments) + p.joinPrint(", ", nn.Arguments) io.WriteString(p.w, ")") } } diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 8791841..58ac6eb 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -1710,10 +1710,10 @@ func (p *Printer) printExprNew(n ast.Vertex) { p.bufStart = " " p.Print(nn.Class) - if nn.ArgumentList != nil { - p.printFreeFloatingOrDefault(nn.ArgumentList, token.Start, "(") - p.joinPrint(",", nn.ArgumentList.Arguments) - p.printFreeFloatingOrDefault(nn.ArgumentList, token.End, ")") + if nn.Arguments != nil { + p.printToken(nn.OpenParenthesisTkn, "(") + p.joinPrint(",", nn.Arguments) + p.printToken(nn.CloseParenthesisTkn, ")") } p.printFreeFloating(nn, token.End)