diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go index 6ff6707..10e9084 100644 --- a/internal/php5/parser_test.go +++ b/internal/php5/parser_test.go @@ -15045,61 +15045,53 @@ func TestStmtForeach_WithRef(t *testing.T) { }, }, }, - Var: &ast.ExprReference{ + AmpersandTkn: &token.Token{ + ID: token.ID(38), + Value: []byte("&"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 24, - EndPos: 27, + EndPos: 25, }, - AmpersandTkn: &token.Token{ - ID: token.ID(38), - Value: []byte("&"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 25, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 23, - EndPos: 24, - }, + FreeFloating: []*token.Token{ + { + ID: token.T_WHITESPACE, + Value: []byte(" "), + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 23, + EndPos: 24, }, }, }, - Var: &ast.ExprVariable{ + }, + Var: &ast.ExprVariable{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + VarName: &ast.Identifier{ Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 25, EndPos: 27, }, - VarName: &ast.Identifier{ + IdentifierTkn: &token.Token{ + ID: token.T_VARIABLE, + Value: []byte("$v"), Position: &position.Position{ StartLine: 1, EndLine: 1, StartPos: 25, EndPos: 27, }, - IdentifierTkn: &token.Token{ - ID: token.T_VARIABLE, - Value: []byte("$v"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - }, - Value: []byte("$v"), }, + Value: []byte("$v"), }, }, CloseParenthesisTkn: &token.Token{ @@ -31770,7 +31762,7 @@ func TestExprClosure_Use(t *testing.T) { EndPos: 36, }, FunctionTkn: &token.Token{ - ID: token.T_FUNCTION, + ID: token.T_FUNCTION, Value: []byte("function"), Position: &position.Position{ StartLine: 1, @@ -31780,7 +31772,7 @@ func TestExprClosure_Use(t *testing.T) { }, FreeFloating: []*token.Token{ { - ID: token.T_OPEN_TAG, + ID: token.T_OPEN_TAG, Value: []byte(" &$v) {}` - - expected := &ast.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 31, - }, - Stmts: []ast.Vertex{ - &ast.StmtForeach{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 31, - }, - ForeachTkn: &token.Token{ - ID: token.T_FOREACH, - Value: []byte("foreach"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 10, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_OPEN_TAG, - Value: []byte(""), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 21, - EndPos: 23, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 20, - EndPos: 21, - }, - }, - }, - }, - Var: &ast.ExprReference{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 27, - }, - AmpersandTkn: &token.Token{ - ID: token.ID(38), - Value: []byte("&"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 25, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 23, - EndPos: 24, - }, - }, - }, - }, - Var: &ast.ExprVariable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - VarName: &ast.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - IdentifierTkn: &token.Token{ - ID: token.T_VARIABLE, - Value: []byte("$v"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - }, - Value: []byte("$v"), - }, - }, - }, - CloseParenthesisTkn: &token.Token{ - ID: token.ID(41), - Value: []byte(")"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 27, - EndPos: 28, - }, - }, - Stmt: &ast.StmtStmtList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 31, - }, - OpenCurlyBracketTkn: &token.Token{ - ID: token.ID(123), - Value: []byte("{"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 30, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 28, - EndPos: 29, - }, - }, - }, - }, - Stmts: []ast.Vertex{}, - CloseCurlyBracketTkn: &token.Token{ - ID: token.ID(125), - Value: []byte("}"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 30, - EndPos: 31, - }, - }, - }, - }, - }, - EndTkn: &token.Token{}, - } - - lexer := scanner.NewLexer([]byte(src), "5.6", nil) - php5parser := php5.NewParser(lexer, nil) - php5parser.Parse() - actual := php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - func TestExprShellExec(t *testing.T) { src := " &$v) {}` - - expected := &ast.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 31, - }, - Stmts: []ast.Vertex{ - &ast.StmtForeach{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 31, - }, - ForeachTkn: &token.Token{ - ID: token.T_FOREACH, - Value: []byte("foreach"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 10, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_OPEN_TAG, - Value: []byte(""), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 21, - EndPos: 23, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 20, - EndPos: 21, - }, - }, - }, - }, - Var: &ast.ExprReference{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 27, - }, - AmpersandTkn: &token.Token{ - ID: token.ID(38), - Value: []byte("&"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 25, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 23, - EndPos: 24, - }, - }, - }, - }, - Var: &ast.ExprVariable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - VarName: &ast.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - IdentifierTkn: &token.Token{ - ID: token.T_VARIABLE, - Value: []byte("$v"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - }, - Value: []byte("$v"), - }, - }, - }, - CloseParenthesisTkn: &token.Token{ - ID: token.ID(41), - Value: []byte(")"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 27, - EndPos: 28, - }, - }, - Stmt: &ast.StmtStmtList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 31, - }, - OpenCurlyBracketTkn: &token.Token{ - ID: token.ID(123), - Value: []byte("{"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 30, - }, - FreeFloating: []*token.Token{ - { - ID: token.T_WHITESPACE, - Value: []byte(" "), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 28, - EndPos: 29, - }, - }, - }, - }, - Stmts: []ast.Vertex{}, - CloseCurlyBracketTkn: &token.Token{ - ID: token.ID(125), - Value: []byte("}"), - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 30, - EndPos: 31, - }, - }, - }, - }, - }, - EndTkn: &token.Token{}, - } - - lexer := scanner.NewLexer([]byte(src), "7.4", nil) - php7parser := php7.NewParser(lexer, nil) - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - func TestExprShellExec(t *testing.T) { src := ""))) + p.printToken(n.AmpersandTkn, nil) p.printNode(n.Var) p.printToken(n.CloseParenthesisTkn, []byte(")")) p.printToken(n.ColonTkn, nil) diff --git a/pkg/ast/visitor/printer_test.go b/pkg/ast/visitor/printer_test.go index 571f51e..8671134 100644 --- a/pkg/ast/visitor/printer_test.go +++ b/pkg/ast/visitor/printer_test.go @@ -3799,6 +3799,39 @@ func TestPrinterPrintStmtForeach(t *testing.T) { } } +func TestPrinterPrintStmtForeach_Reference(t *testing.T) { + o := bytes.NewBufferString("") + + p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP) + n := &ast.StmtForeach{ + Expr: &ast.ExprVariable{ + VarName: &ast.Identifier{Value: []byte("$a")}, + }, + Key: &ast.ExprVariable{ + VarName: &ast.Identifier{Value: []byte("$k")}, + }, + AmpersandTkn: &token.Token{ + Value: []byte("&"), + }, + Var: &ast.ExprVariable{ + VarName: &ast.Identifier{Value: []byte("$v")}, + }, + Stmt: &ast.StmtStmtList{ + Stmts: []ast.Vertex{ + &ast.StmtNop{}, + }, + }, + } + n.Accept(p) + + expected := `foreach($a as$k=>&$v){;}` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + func TestPrinterPrintStmtFunction(t *testing.T) { o := bytes.NewBufferString("")