diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go index 10e9084..eeca77d 100644 --- a/internal/php5/parser_test.go +++ b/internal/php5/parser_test.go @@ -30786,61 +30786,53 @@ func TestExprArray_Items(t *testing.T) { StartPos: 15, EndPos: 18, }, - Val: &ast.ExprReference{ + AmpersandTkn: &token.Token{ + ID: token.ID(38), + Value: []byte("&"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 15, - EndPos: 18, + EndPos: 16, }, - AmpersandTkn: &token.Token{ - ID: token.ID(38), - Value: []byte("&"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 15, - EndPos: 16, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 14, - EndPos: 15, - }, + FreeFloating: []*token.Token{ + { + ID: token.T_WHITESPACE, + Value: []byte(" "), + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 14, + EndPos: 15, }, }, }, - Var: &ast.ExprVariable{ + }, + Val: &ast.ExprVariable{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 16, + EndPos: 18, + }, + VarName: &ast.Identifier{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 16, EndPos: 18, }, - VarName: &ast.Identifier{ + IdentifierTkn: &token.Token{ + ID: token.T_VARIABLE, + Value: []byte("$b"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 16, EndPos: 18, }, - IdentifierTkn: &token.Token{ - ID: token.T_VARIABLE, - Value: []byte("$b"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 16, - EndPos: 18, - }, - }, - Value: []byte("$b"), }, + Value: []byte("$b"), }, }, }, @@ -39025,61 +39017,53 @@ func TestExprShortArray_Items(t *testing.T) { StartPos: 10, EndPos: 13, }, - Val: &ast.ExprReference{ + AmpersandTkn: &token.Token{ + ID: token.ID(38), + Value: []byte("&"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 10, - EndPos: 13, + EndPos: 11, }, - AmpersandTkn: &token.Token{ - ID: token.ID(38), - Value: []byte("&"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 11, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 10, - }, + FreeFloating: []*token.Token{ + { + ID: token.T_WHITESPACE, + Value: []byte(" "), + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 10, }, }, }, - Var: &ast.ExprVariable{ + }, + Val: &ast.ExprVariable{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 11, + EndPos: 13, + }, + VarName: &ast.Identifier{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 11, EndPos: 13, }, - VarName: &ast.Identifier{ + IdentifierTkn: &token.Token{ + ID: token.T_VARIABLE, + Value: []byte("$b"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 11, EndPos: 13, }, - IdentifierTkn: &token.Token{ - ID: token.T_VARIABLE, - Value: []byte("$b"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 11, - EndPos: 13, - }, - }, - Value: []byte("$b"), }, + Value: []byte("$b"), }, }, }, diff --git a/internal/php5/php5.go b/internal/php5/php5.go index 7fbb547..392b113 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 6b33687..a1bb134 100644 --- a/internal/php5/php5.y +++ b/internal/php5/php5.y @@ -5255,14 +5255,11 @@ non_empty_array_pair_list: | non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable { arrayItem := &ast.ExprArrayItem{ - Position: yylex.(*Parser).builder.NewNodesPosition($3, $6), + Position: yylex.(*Parser).builder.NewNodesPosition($3, $6), Key: $3, DoubleArrowTkn: $4, - Val: &ast.ExprReference{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($5, $6), - AmpersandTkn: $5, - Var: $6, - }, + AmpersandTkn: $5, + Val: $6, } $1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2) @@ -5273,12 +5270,9 @@ non_empty_array_pair_list: | non_empty_array_pair_list ',' '&' w_variable { arrayItem := &ast.ExprArrayItem{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4), - Val: &ast.ExprReference{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4), - AmpersandTkn: $3, - Var: $4, - }, + Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4), + AmpersandTkn: $3, + Val: $4, } $1.(*ast.ParserSeparatedList).SeparatorTkns = append($1.(*ast.ParserSeparatedList).SeparatorTkns, $2) @@ -5291,14 +5285,11 @@ non_empty_array_pair_list: $$ = &ast.ParserSeparatedList{ Items: []ast.Vertex{ &ast.ExprArrayItem{ - Position: yylex.(*Parser).builder.NewNodesPosition($1, $4), + Position: yylex.(*Parser).builder.NewNodesPosition($1, $4), Key: $1, DoubleArrowTkn: $2, - Val: &ast.ExprReference{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4), - AmpersandTkn: $3, - Var: $4, - }, + AmpersandTkn: $3, + Val: $4, }, }, } @@ -5308,12 +5299,9 @@ non_empty_array_pair_list: $$ = &ast.ParserSeparatedList{ Items: []ast.Vertex{ &ast.ExprArrayItem{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), - Val: &ast.ExprReference{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), - AmpersandTkn: $1, - Var: $2, - }, + Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), + AmpersandTkn: $1, + Val: $2, }, }, } diff --git a/internal/php7/parser_test.go b/internal/php7/parser_test.go index eb10949..cd532ff 100644 --- a/internal/php7/parser_test.go +++ b/internal/php7/parser_test.go @@ -34718,61 +34718,53 @@ func TestExprArray_Items(t *testing.T) { StartPos: 15, EndPos: 18, }, - Val: &ast.ExprReference{ + AmpersandTkn: &token.Token{ + ID: token.ID(38), + Value: []byte("&"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 15, - EndPos: 18, + EndPos: 16, }, - AmpersandTkn: &token.Token{ - ID: token.ID(38), - Value: []byte("&"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 15, - EndPos: 16, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 14, - EndPos: 15, - }, + FreeFloating: []*token.Token{ + { + ID: token.T_WHITESPACE, + Value: []byte(" "), + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 14, + EndPos: 15, }, }, }, - Var: &ast.ExprVariable{ + }, + Val: &ast.ExprVariable{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 16, + EndPos: 18, + }, + VarName: &ast.Identifier{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 16, EndPos: 18, }, - VarName: &ast.Identifier{ + IdentifierTkn: &token.Token{ + ID: token.T_VARIABLE, + Value: []byte("$b"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 16, EndPos: 18, }, - IdentifierTkn: &token.Token{ - ID: token.T_VARIABLE, - Value: []byte("$b"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 16, - EndPos: 18, - }, - }, - Value: []byte("$b"), }, + Value: []byte("$b"), }, }, }, @@ -43983,61 +43975,53 @@ func TestExprShortArray_Items(t *testing.T) { StartPos: 10, EndPos: 13, }, - Val: &ast.ExprReference{ + AmpersandTkn: &token.Token{ + ID: token.ID(38), + Value: []byte("&"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 10, - EndPos: 13, + EndPos: 11, }, - AmpersandTkn: &token.Token{ - ID: token.ID(38), - Value: []byte("&"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 11, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 10, - }, + FreeFloating: []*token.Token{ + { + ID: token.T_WHITESPACE, + Value: []byte(" "), + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 10, }, }, }, - Var: &ast.ExprVariable{ + }, + Val: &ast.ExprVariable{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 11, + EndPos: 13, + }, + VarName: &ast.Identifier{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 11, EndPos: 13, }, - VarName: &ast.Identifier{ + IdentifierTkn: &token.Token{ + ID: token.T_VARIABLE, + Value: []byte("$b"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 11, EndPos: 13, }, - IdentifierTkn: &token.Token{ - ID: token.T_VARIABLE, - Value: []byte("$b"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 11, - EndPos: 13, - }, - }, - Value: []byte("$b"), }, + Value: []byte("$b"), }, }, }, diff --git a/internal/php7/php7.go b/internal/php7/php7.go index 85bc391..2e0135b 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 f85ad45..3e7e479 100644 --- a/internal/php7/php7.y +++ b/internal/php7/php7.y @@ -3994,25 +3994,19 @@ array_pair: | expr T_DOUBLE_ARROW '&' variable { $$ = &ast.ExprArrayItem{ - Position: yylex.(*Parser).builder.NewNodesPosition($1, $4), + Position: yylex.(*Parser).builder.NewNodesPosition($1, $4), Key: $1, DoubleArrowTkn: $2, - Val: &ast.ExprReference{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4), - AmpersandTkn: $3, - Var: $4, - }, + AmpersandTkn: $3, + Val: $4, } } | '&' variable { $$ = &ast.ExprArrayItem{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), - Val: &ast.ExprReference{ - Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), - AmpersandTkn: $1, - Var: $2, - }, + Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2), + AmpersandTkn: $1, + Val: $2, } } | T_ELLIPSIS expr diff --git a/pkg/ast/node.go b/pkg/ast/node.go index 8a020b6..62d26a5 100644 --- a/pkg/ast/node.go +++ b/pkg/ast/node.go @@ -1177,6 +1177,7 @@ type ExprArrayItem struct { EllipsisTkn *token.Token Key Vertex DoubleArrowTkn *token.Token + AmpersandTkn *token.Token Val Vertex } diff --git a/pkg/ast/visitor/dumper.go b/pkg/ast/visitor/dumper.go index dfb5a64..598ab1f 100644 --- a/pkg/ast/visitor/dumper.go +++ b/pkg/ast/visitor/dumper.go @@ -1056,6 +1056,7 @@ func (v *Dumper) ExprArrayItem(n *ast.ExprArrayItem) { v.dumpToken("EllipsisTkn", n.EllipsisTkn) v.dumpVertex("Key", n.Key) v.dumpToken("DoubleArrowTkn", n.DoubleArrowTkn) + v.dumpToken("AmpersandTkn", n.AmpersandTkn) v.dumpVertex("Val", n.Val) v.indent-- diff --git a/pkg/ast/visitor/printer.go b/pkg/ast/visitor/printer.go index ff0fac5..479c98c 100644 --- a/pkg/ast/visitor/printer.go +++ b/pkg/ast/visitor/printer.go @@ -641,6 +641,7 @@ func (p *printer) ExprArrayItem(n *ast.ExprArrayItem) { p.printToken(n.EllipsisTkn, nil) p.printNode(n.Key) p.printToken(n.DoubleArrowTkn, p.ifNode(n.Key, []byte("=>"))) + p.printToken(n.AmpersandTkn, nil) p.printNode(n.Val) } diff --git a/pkg/ast/visitor/printer_test.go b/pkg/ast/visitor/printer_test.go index 8671134..187c173 100644 --- a/pkg/ast/visitor/printer_test.go +++ b/pkg/ast/visitor/printer_test.go @@ -1559,9 +1559,31 @@ func TestPrinterPrintExprArrayItem(t *testing.T) { p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) n := &ast.ExprArrayItem{ - Val: &ast.ExprReference{Var: &ast.ExprVariable{ + Val: &ast.ExprVariable{ VarName: &ast.Identifier{Value: []byte("$world")}, - }}, + }, + } + n.Accept(p) + + expected := `$world` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestPrinterPrintExprArrayItem_Reference(t *testing.T) { + o := bytes.NewBufferString("") + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n := &ast.ExprArrayItem{ + AmpersandTkn: &token.Token{ + Value: []byte("&"), + }, + Val: &ast.ExprVariable{ + VarName: &ast.Identifier{Value: []byte("$world")}, + }, } n.Accept(p)