diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go new file mode 100644 index 0000000..638d1df --- /dev/null +++ b/internal/php5/parser_test.go @@ -0,0 +1,23147 @@ +package php5_test + +import ( + "gotest.tools/assert" + "testing" + + "github.com/z7zmey/php-parser/internal/php5" + "github.com/z7zmey/php-parser/pkg/ast" + "github.com/z7zmey/php-parser/pkg/position" +) + +func TestIdentifier(t *testing.T) { + src := `bar($a, ...$b); + foo::bar($a, ...$b); + $foo::bar($a, ...$b); + new foo($a, ...$b); + ` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 7, + StartPos: 6, + EndPos: 133, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 6, + EndPos: 21, + }, + }, + Expr: &ast.ExprFunctionCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 6, + EndPos: 20, + }, + }, + Function: &ast.NameName{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 6, + EndPos: 9, + }, + }, + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 6, + EndPos: 9, + }, + }, + Value: []byte("foo"), + }, + }, + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 9, + EndPos: 20, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 10, + EndPos: 12, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 10, + EndPos: 12, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 10, + EndPos: 12, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 14, + EndPos: 19, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 17, + EndPos: 19, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 17, + EndPos: 19, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 24, + EndPos: 40, + }, + }, + Expr: &ast.ExprFunctionCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 24, + EndPos: 39, + }, + }, + Function: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 24, + EndPos: 28, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 24, + EndPos: 28, + }, + }, + Value: []byte("foo"), + }, + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 28, + EndPos: 39, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 29, + EndPos: 31, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 29, + EndPos: 31, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 29, + EndPos: 31, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 33, + EndPos: 38, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 36, + EndPos: 38, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 36, + EndPos: 38, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 43, + EndPos: 64, + }, + }, + Expr: &ast.ExprMethodCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 43, + EndPos: 63, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 43, + EndPos: 47, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 43, + EndPos: 47, + }, + }, + Value: []byte("foo"), + }, + }, + Method: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 49, + EndPos: 52, + }, + }, + Value: []byte("bar"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 52, + EndPos: 63, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 53, + EndPos: 55, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 53, + EndPos: 55, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 53, + EndPos: 55, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 57, + EndPos: 62, + }, + }, + IsReference: false, + Variadic: true, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 60, + EndPos: 62, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 60, + EndPos: 62, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 67, + EndPos: 87, + }, + }, + Expr: &ast.ExprStaticCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 67, + EndPos: 86, + }, + }, + Class: &ast.NameName{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 67, + EndPos: 70, + }, + }, + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 67, + EndPos: 70, + }, + }, + Value: []byte("foo"), + }, + }, + }, + Call: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 72, + EndPos: 75, + }, + }, + Value: []byte("bar"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 75, + EndPos: 86, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 76, + EndPos: 78, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 76, + EndPos: 78, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 76, + EndPos: 78, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 80, + EndPos: 85, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 83, + EndPos: 85, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 83, + EndPos: 85, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 90, + EndPos: 111, + }, + }, + Expr: &ast.ExprStaticCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 90, + EndPos: 110, + }, + }, + Class: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 90, + EndPos: 94, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 90, + EndPos: 94, + }, + }, + Value: []byte("foo"), + }, + }, + Call: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 96, + EndPos: 99, + }, + }, + Value: []byte("bar"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 99, + EndPos: 110, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 100, + EndPos: 102, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 100, + EndPos: 102, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 100, + EndPos: 102, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 104, + EndPos: 109, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 107, + EndPos: 109, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 107, + EndPos: 109, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 114, + EndPos: 133, + }, + }, + Expr: &ast.ExprNew{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 114, + EndPos: 132, + }, + }, + Class: &ast.NameName{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 118, + EndPos: 121, + }, + }, + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 118, + EndPos: 121, + }, + }, + Value: []byte("foo"), + }, + }, + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 121, + EndPos: 132, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 122, + EndPos: 124, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 122, + EndPos: 124, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 122, + EndPos: 124, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 126, + EndPos: 131, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 129, + EndPos: 131, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 129, + EndPos: 131, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestPhp5ParameterNode(t *testing.T) { + src := `bar()";` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 22, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 22, + }, + }, + Expr: &ast.ScalarEncapsed{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 21, + }, + }, + Parts: []ast.Vertex{ + &ast.ScalarEncapsedStringPart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 4, + EndPos: 9, + }, + }, + Value: []byte("test "), + }, + &ast.ExprPropertyFetch{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 18, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 13, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 13, + }, + }, + Value: []byte("foo"), + }, + }, + Property: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 18, + }, + }, + Value: []byte("bar"), + }, + }, + &ast.ScalarEncapsedStringPart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("()"), + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestScalarEncapsed_DollarOpenCurlyBraces(t *testing.T) { + src := `bar()}";` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 24, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 24, + }, + }, + Expr: &ast.ScalarEncapsed{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 23, + }, + }, + Parts: []ast.Vertex{ + &ast.ScalarEncapsedStringPart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 4, + EndPos: 9, + }, + }, + Value: []byte("test "), + }, + &ast.ExprMethodCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 21, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 14, + }, + }, + Value: []byte("foo"), + }, + }, + Method: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 16, + EndPos: 19, + }, + }, + Value: []byte("bar"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 19, + EndPos: 21, + }, + }, + }, + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestScalarHeredoc_HeredocSimpleLabel(t *testing.T) { + src := ` $v) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 30, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 30, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Value: []byte("a"), + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 26, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 26, + }, + }, + Value: []byte("v"), + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 28, + EndPos: 30, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtForeach_ExprWithKey(t *testing.T) { + src := ` $v) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 30, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 30, + }, + }, + Expr: &ast.ExprShortArray{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Items: []ast.Vertex{}, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 26, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 26, + }, + }, + Value: []byte("v"), + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 28, + EndPos: 30, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtForeach_WithRef(t *testing.T) { + src := ` &$v) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 31, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 31, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Value: []byte("a"), + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprReference{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 27, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + }, + Value: []byte("v"), + }, + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtForeach_WithList(t *testing.T) { + src := ` list($v)) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 36, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 36, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Value: []byte("a"), + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 32, + }, + }, + Items: []ast.Vertex{ + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + Val: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + Value: []byte("v"), + }, + }, + }, + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 34, + EndPos: 36, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtFunction(t *testing.T) { + src := `
` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 17, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + }, + &ast.StmtInlineHtml{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 5, + EndPos: 17, + }, + }, + Value: []byte("
"), + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtInterface(t *testing.T) { + src := `1, &$b,);` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 21, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 21, + }, + }, + Expr: &ast.ExprArray{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 20, + }, + }, + Items: []ast.Vertex{ + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 13, + }, + }, + Key: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 10, + }, + }, + Value: []byte("1"), + }, + Val: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 13, + }, + }, + Value: []byte("1"), + }, + }, + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 18, + }, + }, + Val: &ast.ExprReference{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 18, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 16, + EndPos: 18, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 16, + EndPos: 18, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + &ast.ExprArrayItem{}, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprBitwiseNot(t *testing.T) { + src := `foo();` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Expr: &ast.ExprMethodCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Method: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 7, + EndPos: 10, + }, + }, + Value: []byte("foo"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 12, + }, + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprNew(t *testing.T) { + src := `foo;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Expr: &ast.ExprPropertyFetch{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 10, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Property: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 7, + EndPos: 10, + }, + }, + Value: []byte("foo"), + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprReference_ForeachWithRef(t *testing.T) { + t.Helper() + src := ` &$v) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 31, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 31, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Value: []byte("a"), + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprReference{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 27, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + }, + Value: []byte("v"), + }, + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprShellExec(t *testing.T) { + src := "1, &$b,];` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 16, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 16, + }, + }, + Expr: &ast.ExprShortArray{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 15, + }, + }, + Items: []ast.Vertex{ + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 4, + EndPos: 8, + }, + }, + Key: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 4, + EndPos: 5, + }, + }, + Value: []byte("1"), + }, + Val: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 7, + EndPos: 8, + }, + }, + Value: []byte("1"), + }, + }, + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 13, + }, + }, + Val: &ast.ExprReference{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 13, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 11, + EndPos: 13, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 11, + EndPos: 13, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + &ast.ExprArrayItem{}, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprStaticCall(t *testing.T) { + src := ` $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 18, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 18, + }, + }, + Expr: &ast.ExprYield{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 17, + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + Value: []byte("a"), + }, + }, + Value: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 17, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 17, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprYield_Expr(t *testing.T) { + src := ` 1;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 17, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 17, + }, + }, + Expr: &ast.ExprYield{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 16, + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + Value: []byte("a"), + }, + }, + Value: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 16, + }, + }, + Value: []byte("1"), + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +// expr assign + +func TestExprAssign_Assign(t *testing.T) { + src := `>= $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Expr: &ast.ExprAssignShiftRight{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 12, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 12, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +// expr binary + +func TestExprBinary_BitwiseAnd(t *testing.T) { + src := `= $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Expr: &ast.ExprBinaryGreaterOrEqual{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Left: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Right: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprBinary_Greater(t *testing.T) { + src := ` $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Expr: &ast.ExprBinaryGreater{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 10, + }, + }, + Left: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Right: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 8, + EndPos: 10, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 8, + EndPos: 10, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprBinary_Identical(t *testing.T) { + src := `> $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Expr: &ast.ExprBinaryShiftRight{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Left: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Right: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php5parser := php5.NewParser([]byte(src), "5.6") + php5parser.Parse() + actual := php5parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprBinary_SmallerOrEqual(t *testing.T) { + src := `bar($a, ...$b); + foo::bar($a, ...$b); + $foo::bar($a, ...$b); + new foo($a, ...$b); + /** anonymous class */ + new class ($a, ...$b) {}; + ` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 9, + StartPos: 6, + EndPos: 186, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 6, + EndPos: 21, + }, + }, + Expr: &ast.ExprFunctionCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 6, + EndPos: 20, + }, + }, + Function: &ast.NameName{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 6, + EndPos: 9, + }, + }, + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 6, + EndPos: 9, + }, + }, + Value: []byte("foo"), + }, + }, + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 9, + EndPos: 20, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 10, + EndPos: 12, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 10, + EndPos: 12, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 10, + EndPos: 12, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 14, + EndPos: 19, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 17, + EndPos: 19, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 2, + EndLine: 2, + StartPos: 17, + EndPos: 19, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 24, + EndPos: 40, + }, + }, + Expr: &ast.ExprFunctionCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 24, + EndPos: 39, + }, + }, + Function: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 24, + EndPos: 28, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 24, + EndPos: 28, + }, + }, + Value: []byte("foo"), + }, + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 28, + EndPos: 39, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 29, + EndPos: 31, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 29, + EndPos: 31, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 29, + EndPos: 31, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 33, + EndPos: 38, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 36, + EndPos: 38, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 3, + EndLine: 3, + StartPos: 36, + EndPos: 38, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 43, + EndPos: 64, + }, + }, + Expr: &ast.ExprMethodCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 43, + EndPos: 63, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 43, + EndPos: 47, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 43, + EndPos: 47, + }, + }, + Value: []byte("foo"), + }, + }, + Method: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 49, + EndPos: 52, + }, + }, + Value: []byte("bar"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 52, + EndPos: 63, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 53, + EndPos: 55, + }, + }, + IsReference: false, + Variadic: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 53, + EndPos: 55, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 53, + EndPos: 55, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 57, + EndPos: 62, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 60, + EndPos: 62, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 4, + EndLine: 4, + StartPos: 60, + EndPos: 62, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 67, + EndPos: 87, + }, + }, + Expr: &ast.ExprStaticCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 67, + EndPos: 86, + }, + }, + Class: &ast.NameName{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 67, + EndPos: 70, + }, + }, + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 67, + EndPos: 70, + }, + }, + Value: []byte("foo"), + }, + }, + }, + Call: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 72, + EndPos: 75, + }, + }, + Value: []byte("bar"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 75, + EndPos: 86, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 76, + EndPos: 78, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 76, + EndPos: 78, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 76, + EndPos: 78, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 80, + EndPos: 85, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 83, + EndPos: 85, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 5, + EndLine: 5, + StartPos: 83, + EndPos: 85, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 90, + EndPos: 111, + }, + }, + Expr: &ast.ExprStaticCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 90, + EndPos: 110, + }, + }, + Class: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 90, + EndPos: 94, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 90, + EndPos: 94, + }, + }, + Value: []byte("foo"), + }, + }, + Call: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 96, + EndPos: 99, + }, + }, + Value: []byte("bar"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 99, + EndPos: 110, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 100, + EndPos: 102, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 100, + EndPos: 102, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 100, + EndPos: 102, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 104, + EndPos: 109, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 107, + EndPos: 109, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 6, + EndLine: 6, + StartPos: 107, + EndPos: 109, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 114, + EndPos: 133, + }, + }, + Expr: &ast.ExprNew{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 114, + EndPos: 132, + }, + }, + Class: &ast.NameName{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 118, + EndPos: 121, + }, + }, + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 118, + EndPos: 121, + }, + }, + Value: []byte("foo"), + }, + }, + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 121, + EndPos: 132, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 122, + EndPos: 124, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 122, + EndPos: 124, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 122, + EndPos: 124, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 126, + EndPos: 131, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 129, + EndPos: 131, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 7, + EndLine: 7, + StartPos: 129, + EndPos: 131, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + }, + }, + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 161, + EndPos: 186, + }, + }, + Expr: &ast.ExprNew{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 161, + EndPos: 185, + }, + }, + Class: &ast.StmtClass{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 165, + EndPos: 185, + }, + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 171, + EndPos: 182, + }, + }, + Arguments: []ast.Vertex{ + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 172, + EndPos: 174, + }, + }, + Variadic: false, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 172, + EndPos: 174, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 172, + EndPos: 174, + }, + }, + Value: []byte("a"), + }, + }, + }, + &ast.Argument{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 176, + EndPos: 181, + }, + }, + Variadic: true, + IsReference: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 179, + EndPos: 181, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 9, + EndLine: 9, + StartPos: 179, + EndPos: 181, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestPhp7ParameterNode(t *testing.T) { + src := `bar()";` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 22, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 22, + }, + }, + Expr: &ast.ScalarEncapsed{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 21, + }, + }, + Parts: []ast.Vertex{ + &ast.ScalarEncapsedStringPart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 4, + EndPos: 9, + }, + }, + Value: []byte("test "), + }, + &ast.ExprPropertyFetch{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 18, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 13, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 13, + }, + }, + Value: []byte("foo"), + }, + }, + Property: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 18, + }, + }, + Value: []byte("bar"), + }, + }, + &ast.ScalarEncapsedStringPart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("()"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestScalarEncapsed_DollarOpenCurlyBraces(t *testing.T) { + src := `bar()}";` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 24, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 24, + }, + }, + Expr: &ast.ScalarEncapsed{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 23, + }, + }, + Parts: []ast.Vertex{ + &ast.ScalarEncapsedStringPart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 4, + EndPos: 9, + }, + }, + Value: []byte("test "), + }, + &ast.ExprMethodCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 21, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 14, + }, + }, + Value: []byte("foo"), + }, + }, + Method: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 16, + EndPos: 19, + }, + }, + Value: []byte("bar"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 19, + EndPos: 21, + }, + }, + }, + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestScalarHeredoc_HeredocSimpleLabel(t *testing.T) { + src := ` $v) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 30, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 30, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Value: []byte("a"), + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 26, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 26, + }, + }, + Value: []byte("v"), + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 28, + EndPos: 30, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtForeach_ExprWithKey(t *testing.T) { + src := ` $v) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 30, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 30, + }, + }, + Expr: &ast.ExprShortArray{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Items: []ast.Vertex{}, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 26, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 26, + }, + }, + Value: []byte("v"), + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 28, + EndPos: 30, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtForeach_WithRef(t *testing.T) { + src := ` &$v) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 31, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 31, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Value: []byte("a"), + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprReference{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 27, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + }, + Value: []byte("v"), + }, + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtForeach_WithList(t *testing.T) { + src := ` list($v)) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 36, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 36, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Value: []byte("a"), + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 32, + }, + }, + Items: []ast.Vertex{ + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + Val: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + Value: []byte("v"), + }, + }, + }, + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 34, + EndPos: 36, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtFunction(t *testing.T) { + src := `
` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 17, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtNop{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + }, + &ast.StmtInlineHtml{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 5, + EndPos: 17, + }, + }, + Value: []byte("
"), + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestStmtInterface(t *testing.T) { + src := `1, &$b,);` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 21, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 21, + }, + }, + Expr: &ast.ExprArray{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 20, + }, + }, + Items: []ast.Vertex{ + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 13, + }, + }, + Key: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 10, + }, + }, + Value: []byte("1"), + }, + Val: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 13, + }, + }, + Value: []byte("1"), + }, + }, + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 18, + }, + }, + Val: &ast.ExprReference{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 18, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 16, + EndPos: 18, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 16, + EndPos: 18, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + &ast.ExprArrayItem{}, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprArray_ItemUnpack(t *testing.T) { + src := ` $a;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 14, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 14, + }, + }, + Expr: &ast.ExprArrowFunction{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + ReturnsRef: false, + Static: false, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 11, + EndPos: 13, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 11, + EndPos: 13, + }, + }, + Value: []byte("a"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprArrowFunction_ReturnType(t *testing.T) { + src := ` $a;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 23, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 23, + }, + }, + Expr: &ast.ExprArrowFunction{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 22, + }, + }, + Static: false, + ReturnsRef: true, + ReturnType: &ast.NameName{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 13, + EndPos: 16, + }, + }, + Parts: []ast.Vertex{ + &ast.NameNamePart{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 13, + EndPos: 16, + }, + }, + Value: []byte("foo"), + }, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 20, + EndPos: 22, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 20, + EndPos: 22, + }, + }, + Value: []byte("a"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprBitwiseNot(t *testing.T) { + src := `foo();` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Expr: &ast.ExprMethodCall{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Method: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 7, + EndPos: 10, + }, + }, + Value: []byte("foo"), + }, + ArgumentList: &ast.ArgumentList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 12, + }, + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprNew(t *testing.T) { + src := `foo;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Expr: &ast.ExprPropertyFetch{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 10, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Property: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 7, + EndPos: 10, + }, + }, + Value: []byte("foo"), + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprReference_ForeachWithRef(t *testing.T) { + t.Helper() + src := ` &$v) {}` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 31, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtForeach{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 31, + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 12, + EndPos: 14, + }, + }, + Value: []byte("a"), + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 18, + EndPos: 20, + }, + }, + Value: []byte("k"), + }, + }, + Var: &ast.ExprReference{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 24, + EndPos: 27, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 25, + EndPos: 27, + }, + }, + Value: []byte("v"), + }, + }, + }, + Stmt: &ast.StmtStmtList{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 29, + EndPos: 31, + }, + }, + Stmts: []ast.Vertex{}, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprShellExec(t *testing.T) { + src := "1, &$b,];` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 16, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 16, + }, + }, + Expr: &ast.ExprShortArray{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 15, + }, + }, + Items: []ast.Vertex{ + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 4, + EndPos: 8, + }, + }, + Key: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 4, + EndPos: 5, + }, + }, + Value: []byte("1"), + }, + Val: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 7, + EndPos: 8, + }, + }, + Value: []byte("1"), + }, + }, + &ast.ExprArrayItem{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 13, + }, + }, + Val: &ast.ExprReference{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 13, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 11, + EndPos: 13, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 11, + EndPos: 13, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + &ast.ExprArrayItem{}, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprShortList(t *testing.T) { + src := ` $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 18, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 18, + }, + }, + Expr: &ast.ExprYield{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 17, + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + Value: []byte("a"), + }, + }, + Value: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 17, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 17, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprYield_Expr(t *testing.T) { + src := ` 1;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 17, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 17, + }, + }, + Expr: &ast.ExprYield{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 16, + }, + }, + Key: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + Value: []byte("a"), + }, + }, + Value: &ast.ScalarLnumber{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 15, + EndPos: 16, + }, + }, + Value: []byte("1"), + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprYieldFrom(t *testing.T) { + src := `>= $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Expr: &ast.ExprAssignShiftRight{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Var: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Expr: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 12, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 12, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprAssign_Coalesce(t *testing.T) { + src := `= $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Expr: &ast.ExprBinaryGreaterOrEqual{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Left: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Right: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprBinary_Greater(t *testing.T) { + src := ` $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Expr: &ast.ExprBinaryGreater{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 10, + }, + }, + Left: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Right: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 8, + EndPos: 10, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 8, + EndPos: 10, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprBinary_Identical(t *testing.T) { + src := `> $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Expr: &ast.ExprBinaryShiftRight{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 11, + }, + }, + Left: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Right: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 9, + EndPos: 11, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +func TestExprBinary_SmallerOrEqual(t *testing.T) { + src := ` $b;` + + expected := &ast.Root{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Stmts: []ast.Vertex{ + &ast.StmtExpression{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 13, + }, + }, + Expr: &ast.ExprBinarySpaceship{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 12, + }, + }, + Left: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 3, + EndPos: 5, + }, + }, + Value: []byte("a"), + }, + }, + Right: &ast.ExprVariable{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 12, + }, + }, + VarName: &ast.Identifier{ + Node: ast.Node{ + Position: &position.Position{ + StartLine: 1, + EndLine: 1, + StartPos: 10, + EndPos: 12, + }, + }, + Value: []byte("b"), + }, + }, + }, + }, + }, + } + + php7parser := php7.NewParser([]byte(src), "7.4") + php7parser.Parse() + actual := php7parser.GetRootNode() + assert.DeepEqual(t, expected, actual) +} + +// expr cast + +func TestExprCast_Array(t *testing.T) { + src := `>= $b;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 13, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 13, - }, - Expr: &assign.ShiftRight{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 12, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - Value: "a", - }, - }, - Expression: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 12, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 12, - }, - Value: "b", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestCoalesce(t *testing.T) { - src := `= $b;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 12, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 12, - }, - Expr: &binary.GreaterOrEqual{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 11, - }, - Left: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - Value: "a", - }, - }, - Right: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 11, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 11, - }, - Value: "b", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestGreater(t *testing.T) { - src := ` $b;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 11, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 11, - }, - Expr: &binary.Greater{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 10, - }, - Left: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - Value: "a", - }, - }, - Right: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 8, - EndPos: 10, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 8, - EndPos: 10, - }, - Value: "b", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestIdentical(t *testing.T) { - src := `> $b;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 12, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 12, - }, - Expr: &binary.ShiftRight{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 11, - }, - Left: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - Value: "a", - }, - }, - Right: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 11, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 11, - }, - Value: "b", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestSmallerOrEqual(t *testing.T) { - src := ` $b;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 13, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 13, - }, - Expr: &binary.Spaceship{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 12, - }, - Left: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - Value: "a", - }, - }, - Right: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 12, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 12, - }, - Value: "b", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/expr/binary/t_freefloating_test.go b/node/expr/binary/t_freefloating_test.go deleted file mode 100644 index 7db5cf4..0000000 --- a/node/expr/binary/t_freefloating_test.go +++ /dev/null @@ -1,117 +0,0 @@ -package binary_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/expr/binary" -) - -var expected freefloating.Collection = freefloating.Collection{ - freefloating.Start: []freefloating.String{ - { - StringType: freefloating.WhiteSpaceType, - Value: " ", - Position: nil, - }, - { - StringType: freefloating.CommentType, - Value: "//comment\n", - Position: nil, - }, - }, -} - -var nodes = []node.Node{ - &binary.BitwiseAnd{ - FreeFloating: expected, - }, - &binary.BitwiseOr{ - FreeFloating: expected, - }, - &binary.BitwiseXor{ - FreeFloating: expected, - }, - &binary.BooleanAnd{ - FreeFloating: expected, - }, - &binary.BooleanOr{ - FreeFloating: expected, - }, - &binary.Coalesce{ - FreeFloating: expected, - }, - &binary.Concat{ - FreeFloating: expected, - }, - &binary.Div{ - FreeFloating: expected, - }, - &binary.Equal{ - FreeFloating: expected, - }, - &binary.GreaterOrEqual{ - FreeFloating: expected, - }, - &binary.Greater{ - FreeFloating: expected, - }, - &binary.Identical{ - FreeFloating: expected, - }, - &binary.LogicalAnd{ - FreeFloating: expected, - }, - &binary.LogicalOr{ - FreeFloating: expected, - }, - &binary.LogicalXor{ - FreeFloating: expected, - }, - &binary.Minus{ - FreeFloating: expected, - }, - &binary.Mod{ - FreeFloating: expected, - }, - &binary.Mul{ - FreeFloating: expected, - }, - &binary.NotEqual{ - FreeFloating: expected, - }, - &binary.NotIdentical{ - FreeFloating: expected, - }, - &binary.Plus{ - FreeFloating: expected, - }, - &binary.Pow{ - FreeFloating: expected, - }, - &binary.ShiftLeft{ - FreeFloating: expected, - }, - &binary.ShiftRight{ - FreeFloating: expected, - }, - &binary.SmallerOrEqual{ - FreeFloating: expected, - }, - &binary.Smaller{ - FreeFloating: expected, - }, - &binary.Spaceship{ - FreeFloating: expected, - }, -} - -func TestMeta(t *testing.T) { - for _, n := range nodes { - actual := *n.GetFreeFloating() - assert.DeepEqual(t, expected, actual) - } -} diff --git a/node/expr/binary/t_position_test.go b/node/expr/binary/t_position_test.go deleted file mode 100644 index 079e380..0000000 --- a/node/expr/binary/t_position_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package binary_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/position" -) - -func TestPosition(t *testing.T) { - expected := position.NewPosition(1, 1, 1, 1) - for _, n := range nodes { - n.SetPosition(expected) - actual := n.GetPosition() - assert.DeepEqual(t, expected, actual) - } -} diff --git a/node/expr/binary/t_visitor_test.go b/node/expr/binary/t_visitor_test.go deleted file mode 100644 index 7962e23..0000000 --- a/node/expr/binary/t_visitor_test.go +++ /dev/null @@ -1,285 +0,0 @@ -package binary_test - -import ( - "testing" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/expr" - "github.com/z7zmey/php-parser/node/expr/binary" - "github.com/z7zmey/php-parser/walker" - "gotest.tools/assert" -) - -var nodesToTest = []struct { - node node.Node // node - expectedVisitedKeys []string // visited keys - expectedAttributes map[string]interface{} -}{ - { - &binary.BitwiseAnd{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.BitwiseOr{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.BitwiseXor{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.BooleanAnd{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.BooleanOr{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Coalesce{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Concat{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Div{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Equal{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.GreaterOrEqual{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Greater{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Identical{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.LogicalAnd{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.LogicalOr{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.LogicalXor{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Minus{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Mod{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Mul{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.NotEqual{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.NotIdentical{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Plus{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Pow{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.ShiftLeft{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.ShiftRight{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.SmallerOrEqual{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Smaller{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, - { - &binary.Spaceship{ - Left: &expr.Variable{}, - Right: &expr.Variable{}, - }, - []string{"Left", "Right"}, - nil, - }, -} - -type visitorMock struct { - visitChildren bool - visitedKeys []string -} - -func (v *visitorMock) EnterNode(n walker.Walkable) bool { return v.visitChildren } -func (v *visitorMock) LeaveNode(n walker.Walkable) {} -func (v *visitorMock) EnterChildNode(key string, w walker.Walkable) { - v.visitedKeys = append(v.visitedKeys, key) -} -func (v *visitorMock) LeaveChildNode(key string, w walker.Walkable) {} -func (v *visitorMock) EnterChildList(key string, w walker.Walkable) { - v.visitedKeys = append(v.visitedKeys, key) -} -func (v *visitorMock) LeaveChildList(key string, w walker.Walkable) {} - -func TestVisitorDisableChildren(t *testing.T) { - for _, tt := range nodesToTest { - v := &visitorMock{false, []string{}} - tt.node.Walk(v) - - expected := []string{} - actual := v.visitedKeys - - assert.DeepEqual(t, expected, actual) - } -} - -func TestVisitor(t *testing.T) { - for _, tt := range nodesToTest { - v := &visitorMock{true, []string{}} - tt.node.Walk(v) - - expected := tt.expectedVisitedKeys - actual := v.visitedKeys - - assert.DeepEqual(t, expected, actual) - } -} - -// test Attributes() - -func TestNameAttributes(t *testing.T) { - for _, tt := range nodesToTest { - expected := tt.expectedAttributes - actual := tt.node.Attributes() - - assert.DeepEqual(t, expected, actual) - } -} diff --git a/node/expr/cast/n_cast_array.go b/node/expr/cast/n_cast_array.go deleted file mode 100644 index f12458b..0000000 --- a/node/expr/cast/n_cast_array.go +++ /dev/null @@ -1,58 +0,0 @@ -package cast - -import ( - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/position" - "github.com/z7zmey/php-parser/walker" -) - -// Array node -type Array struct { - FreeFloating freefloating.Collection - Position *position.Position - Expr node.Node -} - -// NewArray node constructor -func NewArray(Expr node.Node) *Array { - return &Array{ - FreeFloating: nil, - Expr: Expr, - } -} - -// SetPosition sets node position -func (n *Array) SetPosition(p *position.Position) { - n.Position = p -} - -// GetPosition returns node positions -func (n *Array) GetPosition() *position.Position { - return n.Position -} - -func (n *Array) GetFreeFloating() *freefloating.Collection { - return &n.FreeFloating -} - -// Attributes returns node attributes as map -func (n *Array) Attributes() map[string]interface{} { - return nil -} - -// Walk traverses nodes -// Walk is invoked recursively until v.EnterNode returns true -func (n *Array) Walk(v walker.Visitor) { - if v.EnterNode(n) == false { - return - } - - if n.Expr != nil { - v.EnterChildNode("Expr", n) - n.Expr.Walk(v) - v.LeaveChildNode("Expr", n) - } - - v.LeaveNode(n) -} diff --git a/node/expr/cast/n_cast_bool.go b/node/expr/cast/n_cast_bool.go deleted file mode 100644 index a794df8..0000000 --- a/node/expr/cast/n_cast_bool.go +++ /dev/null @@ -1,58 +0,0 @@ -package cast - -import ( - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/position" - "github.com/z7zmey/php-parser/walker" -) - -// Bool node -type Bool struct { - FreeFloating freefloating.Collection - Position *position.Position - Expr node.Node -} - -// NewBool node constructor -func NewBool(Expr node.Node) *Bool { - return &Bool{ - FreeFloating: nil, - Expr: Expr, - } -} - -// SetPosition sets node position -func (n *Bool) SetPosition(p *position.Position) { - n.Position = p -} - -// GetPosition returns node positions -func (n *Bool) GetPosition() *position.Position { - return n.Position -} - -func (n *Bool) GetFreeFloating() *freefloating.Collection { - return &n.FreeFloating -} - -// Attributes returns node attributes as map -func (n *Bool) Attributes() map[string]interface{} { - return nil -} - -// Walk traverses nodes -// Walk is invoked recursively until v.EnterNode returns true -func (n *Bool) Walk(v walker.Visitor) { - if v.EnterNode(n) == false { - return - } - - if n.Expr != nil { - v.EnterChildNode("Expr", n) - n.Expr.Walk(v) - v.LeaveChildNode("Expr", n) - } - - v.LeaveNode(n) -} diff --git a/node/expr/cast/n_cast_double.go b/node/expr/cast/n_cast_double.go deleted file mode 100644 index 61dcc43..0000000 --- a/node/expr/cast/n_cast_double.go +++ /dev/null @@ -1,58 +0,0 @@ -package cast - -import ( - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/position" - "github.com/z7zmey/php-parser/walker" -) - -// Double node -type Double struct { - FreeFloating freefloating.Collection - Position *position.Position - Expr node.Node -} - -// NewDouble node constructor -func NewDouble(Expr node.Node) *Double { - return &Double{ - FreeFloating: nil, - Expr: Expr, - } -} - -// SetPosition sets node position -func (n *Double) SetPosition(p *position.Position) { - n.Position = p -} - -// GetPosition returns node positions -func (n *Double) GetPosition() *position.Position { - return n.Position -} - -func (n *Double) GetFreeFloating() *freefloating.Collection { - return &n.FreeFloating -} - -// Attributes returns node attributes as map -func (n *Double) Attributes() map[string]interface{} { - return nil -} - -// Walk traverses nodes -// Walk is invoked recursively until v.EnterNode returns true -func (n *Double) Walk(v walker.Visitor) { - if v.EnterNode(n) == false { - return - } - - if n.Expr != nil { - v.EnterChildNode("Expr", n) - n.Expr.Walk(v) - v.LeaveChildNode("Expr", n) - } - - v.LeaveNode(n) -} diff --git a/node/expr/cast/n_cast_int.go b/node/expr/cast/n_cast_int.go deleted file mode 100644 index fc3f98d..0000000 --- a/node/expr/cast/n_cast_int.go +++ /dev/null @@ -1,58 +0,0 @@ -package cast - -import ( - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/position" - "github.com/z7zmey/php-parser/walker" -) - -// Int node -type Int struct { - FreeFloating freefloating.Collection - Position *position.Position - Expr node.Node -} - -// NewInt node constructor -func NewInt(Expr node.Node) *Int { - return &Int{ - FreeFloating: nil, - Expr: Expr, - } -} - -// SetPosition sets node position -func (n *Int) SetPosition(p *position.Position) { - n.Position = p -} - -// GetPosition returns node positions -func (n *Int) GetPosition() *position.Position { - return n.Position -} - -func (n *Int) GetFreeFloating() *freefloating.Collection { - return &n.FreeFloating -} - -// Attributes returns node attributes as map -func (n *Int) Attributes() map[string]interface{} { - return nil -} - -// Walk traverses nodes -// Walk is invoked recursively until v.EnterNode returns true -func (n *Int) Walk(v walker.Visitor) { - if v.EnterNode(n) == false { - return - } - - if n.Expr != nil { - v.EnterChildNode("Expr", n) - n.Expr.Walk(v) - v.LeaveChildNode("Expr", n) - } - - v.LeaveNode(n) -} diff --git a/node/expr/cast/n_cast_object.go b/node/expr/cast/n_cast_object.go deleted file mode 100644 index ea67ce2..0000000 --- a/node/expr/cast/n_cast_object.go +++ /dev/null @@ -1,58 +0,0 @@ -package cast - -import ( - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/position" - "github.com/z7zmey/php-parser/walker" -) - -// Object node -type Object struct { - FreeFloating freefloating.Collection - Position *position.Position - Expr node.Node -} - -// NewObject node constructor -func NewObject(Expr node.Node) *Object { - return &Object{ - FreeFloating: nil, - Expr: Expr, - } -} - -// SetPosition sets node position -func (n *Object) SetPosition(p *position.Position) { - n.Position = p -} - -// GetPosition returns node positions -func (n *Object) GetPosition() *position.Position { - return n.Position -} - -func (n *Object) GetFreeFloating() *freefloating.Collection { - return &n.FreeFloating -} - -// Attributes returns node attributes as map -func (n *Object) Attributes() map[string]interface{} { - return nil -} - -// Walk traverses nodes -// Walk is invoked recursively until v.EnterNode returns true -func (n *Object) Walk(v walker.Visitor) { - if v.EnterNode(n) == false { - return - } - - if n.Expr != nil { - v.EnterChildNode("Expr", n) - n.Expr.Walk(v) - v.LeaveChildNode("Expr", n) - } - - v.LeaveNode(n) -} diff --git a/node/expr/cast/n_cast_string.go b/node/expr/cast/n_cast_string.go deleted file mode 100644 index 68a09e2..0000000 --- a/node/expr/cast/n_cast_string.go +++ /dev/null @@ -1,58 +0,0 @@ -package cast - -import ( - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/position" - "github.com/z7zmey/php-parser/walker" -) - -// String node -type String struct { - FreeFloating freefloating.Collection - Position *position.Position - Expr node.Node -} - -// NewString node constructor -func NewString(Expr node.Node) *String { - return &String{ - FreeFloating: nil, - Expr: Expr, - } -} - -// SetPosition sets node position -func (n *String) SetPosition(p *position.Position) { - n.Position = p -} - -// GetPosition returns node positions -func (n *String) GetPosition() *position.Position { - return n.Position -} - -func (n *String) GetFreeFloating() *freefloating.Collection { - return &n.FreeFloating -} - -// Attributes returns node attributes as map -func (n *String) Attributes() map[string]interface{} { - return nil -} - -// Walk traverses nodes -// Walk is invoked recursively until v.EnterNode returns true -func (n *String) Walk(v walker.Visitor) { - if v.EnterNode(n) == false { - return - } - - if n.Expr != nil { - v.EnterChildNode("Expr", n) - n.Expr.Walk(v) - v.LeaveChildNode("Expr", n) - } - - v.LeaveNode(n) -} diff --git a/node/expr/cast/n_cast_unset.go b/node/expr/cast/n_cast_unset.go deleted file mode 100644 index a792daa..0000000 --- a/node/expr/cast/n_cast_unset.go +++ /dev/null @@ -1,58 +0,0 @@ -package cast - -import ( - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/position" - "github.com/z7zmey/php-parser/walker" -) - -// Unset node -type Unset struct { - FreeFloating freefloating.Collection - Position *position.Position - Expr node.Node -} - -// NewUnset node constructor -func NewUnset(Expr node.Node) *Unset { - return &Unset{ - FreeFloating: nil, - Expr: Expr, - } -} - -// SetPosition sets node position -func (n *Unset) SetPosition(p *position.Position) { - n.Position = p -} - -// GetPosition returns node positions -func (n *Unset) GetPosition() *position.Position { - return n.Position -} - -func (n *Unset) GetFreeFloating() *freefloating.Collection { - return &n.FreeFloating -} - -// Attributes returns node attributes as map -func (n *Unset) Attributes() map[string]interface{} { - return nil -} - -// Walk traverses nodes -// Walk is invoked recursively until v.EnterNode returns true -func (n *Unset) Walk(v walker.Visitor) { - if v.EnterNode(n) == false { - return - } - - if n.Expr != nil { - v.EnterChildNode("Expr", n) - n.Expr.Walk(v) - v.LeaveChildNode("Expr", n) - } - - v.LeaveNode(n) -} diff --git a/node/expr/cast/t_cast_test.go b/node/expr/cast/t_cast_test.go deleted file mode 100644 index 0314800..0000000 --- a/node/expr/cast/t_cast_test.go +++ /dev/null @@ -1,653 +0,0 @@ -package cast_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/expr" - "github.com/z7zmey/php-parser/node/expr/cast" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php5" - "github.com/z7zmey/php-parser/php7" - "github.com/z7zmey/php-parser/position" -) - -func TestArray(t *testing.T) { - src := `1, &$b,);` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 21, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 21, - }, - Expr: &expr.Array{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 20, - }, - Items: []node.Node{ - &expr.ArrayItem{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 13, - }, - Key: &scalar.Lnumber{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 10, - }, - Value: "1", - }, - Val: &scalar.Lnumber{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 13, - }, - Value: "1", - }, - }, - &expr.ArrayItem{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 15, - EndPos: 18, - }, - Val: &expr.Reference{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 15, - EndPos: 18, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 16, - EndPos: 18, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 16, - EndPos: 18, - }, - Value: "b", - }, - }, - }, - }, - &expr.ArrayItem{}, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestArrayItemUnpack(t *testing.T) { - src := ` $a;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 14, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 14, - }, - Expr: &expr.ArrowFunction{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 13, - }, - ReturnsRef: false, - Static: false, - PhpDocComment: "", - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 11, - EndPos: 13, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 11, - EndPos: 13, - }, - Value: "a", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestArrowFunctionReturnType(t *testing.T) { - src := ` $a;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 23, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 23, - }, - Expr: &expr.ArrowFunction{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 22, - }, - Static: false, - PhpDocComment: "", - ReturnsRef: true, - ReturnType: &name.Name{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 13, - EndPos: 16, - }, - Parts: []node.Node{ - &name.NamePart{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 13, - EndPos: 16, - }, - Value: "foo", - }, - }, - }, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 20, - EndPos: 22, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 20, - EndPos: 22, - }, - Value: "a", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/expr/t_bitwise_not_test.go b/node/expr/t_bitwise_not_test.go deleted file mode 100644 index a5688b0..0000000 --- a/node/expr/t_bitwise_not_test.go +++ /dev/null @@ -1,73 +0,0 @@ -package expr_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node/expr" - "github.com/z7zmey/php-parser/position" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php5" - "github.com/z7zmey/php-parser/php7" -) - -func TestBitwiseNot(t *testing.T) { - src := `foo();` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 13, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 13, - }, - Expr: &expr.MethodCall{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 12, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - Value: "a", - }, - }, - Method: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 7, - EndPos: 10, - }, - Value: "foo", - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 12, - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/expr/t_new_test.go b/node/expr/t_new_test.go deleted file mode 100644 index d0670f8..0000000 --- a/node/expr/t_new_test.go +++ /dev/null @@ -1,323 +0,0 @@ -package expr_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node/name" - "github.com/z7zmey/php-parser/position" - - "github.com/z7zmey/php-parser/node/expr" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php5" - "github.com/z7zmey/php-parser/php7" -) - -func TestNew(t *testing.T) { - src := `foo;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 11, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 11, - }, - Expr: &expr.PropertyFetch{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 10, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - Value: "a", - }, - }, - Property: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 7, - EndPos: 10, - }, - Value: "foo", - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/expr/t_reference_test.go b/node/expr/t_reference_test.go deleted file mode 100644 index 8a9c38e..0000000 --- a/node/expr/t_reference_test.go +++ /dev/null @@ -1,117 +0,0 @@ -package expr_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node/expr" - "github.com/z7zmey/php-parser/position" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php5" - "github.com/z7zmey/php-parser/php7" -) - -func TestForeachWithRef(t *testing.T) { - t.Helper() - src := ` &$v) {}` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 31, - }, - Stmts: []node.Node{ - &stmt.Foreach{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 31, - }, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - Value: "a", - }, - }, - Key: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - Value: "k", - }, - }, - Variable: &expr.Reference{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 27, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - Value: "v", - }, - }, - }, - Stmt: &stmt.StmtList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 31, - }, - Stmts: []node.Node{}, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/expr/t_shell_exec_test.go b/node/expr/t_shell_exec_test.go deleted file mode 100644 index 4932334..0000000 --- a/node/expr/t_shell_exec_test.go +++ /dev/null @@ -1,86 +0,0 @@ -package expr_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node/scalar" - "github.com/z7zmey/php-parser/position" - - "github.com/z7zmey/php-parser/node/expr" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php5" - "github.com/z7zmey/php-parser/php7" -) - -func TestShellExec(t *testing.T) { - src := "1, &$b,];` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 16, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 16, - }, - Expr: &expr.ShortArray{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 15, - }, - Items: []node.Node{ - &expr.ArrayItem{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 4, - EndPos: 8, - }, - Key: &scalar.Lnumber{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 4, - EndPos: 5, - }, - Value: "1", - }, - Val: &scalar.Lnumber{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 7, - EndPos: 8, - }, - Value: "1", - }, - }, - &expr.ArrayItem{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 13, - }, - Val: &expr.Reference{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 13, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 11, - EndPos: 13, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 11, - EndPos: 13, - }, - Value: "b", - }, - }, - }, - }, - &expr.ArrayItem{}, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/expr/t_short_list_test.go b/node/expr/t_short_list_test.go deleted file mode 100644 index 76f0bbe..0000000 --- a/node/expr/t_short_list_test.go +++ /dev/null @@ -1,304 +0,0 @@ -package expr_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/expr" - "github.com/z7zmey/php-parser/node/expr/assign" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php7" - "github.com/z7zmey/php-parser/position" -) - -func TestShortList(t *testing.T) { - src := ` $b;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 18, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 18, - }, - Expr: &expr.Yield{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 17, - }, - Key: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 11, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 11, - }, - Value: "a", - }, - }, - Value: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 15, - EndPos: 17, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 15, - EndPos: 17, - }, - Value: "b", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestYieldExpr(t *testing.T) { - src := ` 1;` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 17, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 17, - }, - Expr: &expr.Yield{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 16, - }, - Key: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 11, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 11, - }, - Value: "a", - }, - }, - Value: &scalar.Lnumber{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 15, - EndPos: 16, - }, - Value: "1", - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestYieldFrom(t *testing.T) { - src := `bar()";` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 22, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 22, - }, - Expr: &scalar.Encapsed{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 21, - }, - Parts: []node.Node{ - &scalar.EncapsedStringPart{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 4, - EndPos: 9, - }, - Value: "test ", - }, - &expr.PropertyFetch{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 18, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 13, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 9, - EndPos: 13, - }, - Value: "foo", - }, - }, - Property: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 15, - EndPos: 18, - }, - Value: "bar", - }, - }, - &scalar.EncapsedStringPart{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - Value: "()", - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestDollarOpenCurlyBraces(t *testing.T) { - src := `bar()}";` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 24, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 24, - }, - Expr: &scalar.Encapsed{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 23, - }, - Parts: []node.Node{ - &scalar.EncapsedStringPart{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 4, - EndPos: 9, - }, - Value: "test ", - }, - &expr.MethodCall{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 21, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 14, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 10, - EndPos: 14, - }, - Value: "foo", - }, - }, - Method: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 16, - EndPos: 19, - }, - Value: "bar", - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 19, - EndPos: 21, - }, - }, - }, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/scalar/t_freefloating_test.go b/node/scalar/t_freefloating_test.go deleted file mode 100644 index c30e2ff..0000000 --- a/node/scalar/t_freefloating_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package scalar_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/scalar" -) - -var expected freefloating.Collection = freefloating.Collection{ - freefloating.Start: []freefloating.String{ - { - StringType: freefloating.WhiteSpaceType, - Value: " ", - Position: nil, - }, - { - StringType: freefloating.CommentType, - Value: "//comment\n", - Position: nil, - }, - }, -} - -var nodes = []node.Node{ - &scalar.Dnumber{ - FreeFloating: expected, - }, - &scalar.EncapsedStringPart{ - FreeFloating: expected, - }, - &scalar.Encapsed{ - FreeFloating: expected, - }, - &scalar.Heredoc{ - FreeFloating: expected, - }, - &scalar.Lnumber{ - FreeFloating: expected, - }, - &scalar.MagicConstant{ - FreeFloating: expected, - }, - &scalar.String{ - FreeFloating: expected, - }, -} - -func TestMeta(t *testing.T) { - for _, n := range nodes { - actual := *n.GetFreeFloating() - assert.DeepEqual(t, expected, actual) - } -} diff --git a/node/scalar/t_heredoc_test.go b/node/scalar/t_heredoc_test.go deleted file mode 100644 index ae0ce1f..0000000 --- a/node/scalar/t_heredoc_test.go +++ /dev/null @@ -1,336 +0,0 @@ -package scalar_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node/expr" - "github.com/z7zmey/php-parser/position" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/scalar" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php5" - "github.com/z7zmey/php-parser/php7" -) - -func TestHeredocSimpleLabel(t *testing.T) { - src := ` $v) {}` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 30, - }, - Stmts: []node.Node{ - &stmt.Foreach{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 30, - }, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - Value: "a", - }, - }, - Key: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - Value: "k", - }, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 26, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 26, - }, - Value: "v", - }, - }, - Stmt: &stmt.StmtList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 28, - EndPos: 30, - }, - Stmts: []node.Node{}, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestForeachExprWithKey(t *testing.T) { - src := ` $v) {}` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 30, - }, - Stmts: []node.Node{ - &stmt.Foreach{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 30, - }, - Expr: &expr.ShortArray{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - Items: []node.Node{}, - }, - Key: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - Value: "k", - }, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 26, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 26, - }, - Value: "v", - }, - }, - Stmt: &stmt.StmtList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 28, - EndPos: 30, - }, - Stmts: []node.Node{}, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestForeachWithRef(t *testing.T) { - src := ` &$v) {}` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 31, - }, - Stmts: []node.Node{ - &stmt.Foreach{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 31, - }, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - Value: "a", - }, - }, - Key: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - Value: "k", - }, - }, - Variable: &expr.Reference{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 27, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 25, - EndPos: 27, - }, - Value: "v", - }, - }, - }, - Stmt: &stmt.StmtList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 31, - }, - Stmts: []node.Node{}, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestForeachWithList(t *testing.T) { - src := ` list($v)) {}` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 36, - }, - Stmts: []node.Node{ - &stmt.Foreach{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 36, - }, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 12, - EndPos: 14, - }, - Value: "a", - }, - }, - Key: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 18, - EndPos: 20, - }, - Value: "k", - }, - }, - Variable: &expr.List{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 24, - EndPos: 32, - }, - Items: []node.Node{ - &expr.ArrayItem{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 31, - }, - Val: &expr.Variable{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 31, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 29, - EndPos: 31, - }, - Value: "v", - }, - }, - }, - }, - }, - Stmt: &stmt.StmtList{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 34, - EndPos: 36, - }, - Stmts: []node.Node{}, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/stmt/t_freefloating_test.go b/node/stmt/t_freefloating_test.go deleted file mode 100644 index 182c5d9..0000000 --- a/node/stmt/t_freefloating_test.go +++ /dev/null @@ -1,216 +0,0 @@ -package stmt_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/freefloating" - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/stmt" -) - -var expected freefloating.Collection = freefloating.Collection{ - freefloating.Start: []freefloating.String{ - { - StringType: freefloating.WhiteSpaceType, - Value: " ", - Position: nil, - }, - { - StringType: freefloating.CommentType, - Value: "//comment\n", - Position: nil, - }, - }, -} - -var nodes = []node.Node{ - &stmt.AltElseIf{ - FreeFloating: expected, - }, - &stmt.AltElse{ - FreeFloating: expected, - }, - &stmt.AltFor{ - FreeFloating: expected, - }, - &stmt.AltForeach{ - FreeFloating: expected, - }, - &stmt.AltIf{ - FreeFloating: expected, - }, - &stmt.AltSwitch{ - FreeFloating: expected, - }, - &stmt.AltWhile{ - FreeFloating: expected, - }, - &stmt.Break{ - FreeFloating: expected, - }, - &stmt.CaseList{ - FreeFloating: expected, - }, - &stmt.Case{ - FreeFloating: expected, - }, - &stmt.Catch{ - FreeFloating: expected, - }, - &stmt.ClassConstList{ - FreeFloating: expected, - }, - &stmt.ClassExtends{ - FreeFloating: expected, - }, - &stmt.ClassImplements{ - FreeFloating: expected, - }, - &stmt.ClassMethod{ - FreeFloating: expected, - }, - &stmt.Class{ - FreeFloating: expected, - }, - &stmt.ConstList{ - FreeFloating: expected, - }, - &stmt.Constant{ - FreeFloating: expected, - }, - &stmt.Continue{ - FreeFloating: expected, - }, - &stmt.Declare{ - FreeFloating: expected, - }, - &stmt.Default{ - FreeFloating: expected, - }, - &stmt.Do{ - FreeFloating: expected, - }, - &stmt.Echo{ - FreeFloating: expected, - }, - &stmt.ElseIf{ - FreeFloating: expected, - }, - &stmt.Else{ - FreeFloating: expected, - }, - &stmt.Expression{ - FreeFloating: expected, - }, - &stmt.Finally{ - FreeFloating: expected, - }, - &stmt.For{ - FreeFloating: expected, - }, - &stmt.Foreach{ - FreeFloating: expected, - }, - &stmt.Function{ - FreeFloating: expected, - }, - &stmt.Global{ - FreeFloating: expected, - }, - &stmt.Goto{ - FreeFloating: expected, - }, - &stmt.GroupUse{ - FreeFloating: expected, - }, - &stmt.HaltCompiler{ - FreeFloating: expected, - }, - &stmt.If{ - FreeFloating: expected, - }, - &stmt.InlineHtml{ - FreeFloating: expected, - }, - &stmt.InterfaceExtends{ - FreeFloating: expected, - }, - &stmt.Interface{ - FreeFloating: expected, - }, - &stmt.Label{ - FreeFloating: expected, - }, - &stmt.Namespace{ - FreeFloating: expected, - }, - &stmt.Nop{ - FreeFloating: expected, - }, - &stmt.PropertyList{ - FreeFloating: expected, - }, - &stmt.Property{ - FreeFloating: expected, - }, - &stmt.Return{ - FreeFloating: expected, - }, - &stmt.StaticVar{ - FreeFloating: expected, - }, - &stmt.Static{ - FreeFloating: expected, - }, - &stmt.StmtList{ - FreeFloating: expected, - }, - &stmt.Switch{ - FreeFloating: expected, - }, - &stmt.Throw{ - FreeFloating: expected, - }, - &stmt.TraitAdaptationList{ - FreeFloating: expected, - }, - &stmt.TraitMethodRef{ - FreeFloating: expected, - }, - &stmt.TraitUseAlias{ - FreeFloating: expected, - }, - &stmt.TraitUsePrecedence{ - FreeFloating: expected, - }, - &stmt.TraitUse{ - FreeFloating: expected, - }, - &stmt.Trait{ - FreeFloating: expected, - }, - &stmt.Try{ - FreeFloating: expected, - }, - &stmt.Unset{ - FreeFloating: expected, - }, - &stmt.UseList{ - FreeFloating: expected, - }, - &stmt.Use{ - FreeFloating: expected, - }, - &stmt.While{ - FreeFloating: expected, - }, -} - -func TestMeta(t *testing.T) { - for _, n := range nodes { - actual := *n.GetFreeFloating() - assert.DeepEqual(t, expected, actual) - } -} diff --git a/node/stmt/t_function_test.go b/node/stmt/t_function_test.go deleted file mode 100644 index 4b8fbe3..0000000 --- a/node/stmt/t_function_test.go +++ /dev/null @@ -1,383 +0,0 @@ -package stmt_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node/name" - "github.com/z7zmey/php-parser/node/scalar" - "github.com/z7zmey/php-parser/position" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/expr" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php5" - "github.com/z7zmey/php-parser/php7" -) - -func TestSimpleFunction(t *testing.T) { - src := `
` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 17, - }, - Stmts: []node.Node{ - &stmt.Nop{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 3, - EndPos: 5, - }, - }, - &stmt.InlineHtml{ - Position: &position.Position{ - StartLine: 1, - EndLine: 1, - StartPos: 5, - EndPos: 17, - }, - Value: "
", - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual = php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} diff --git a/node/stmt/t_interface_test.go b/node/stmt/t_interface_test.go deleted file mode 100644 index 5b018ec..0000000 --- a/node/stmt/t_interface_test.go +++ /dev/null @@ -1,224 +0,0 @@ -package stmt_test - -import ( - "testing" - - "gotest.tools/assert" - - "github.com/z7zmey/php-parser/node/name" - "github.com/z7zmey/php-parser/position" - - "github.com/z7zmey/php-parser/node" - "github.com/z7zmey/php-parser/node/stmt" - "github.com/z7zmey/php-parser/php5" - "github.com/z7zmey/php-parser/php7" -) - -func TestInterface(t *testing.T) { - src := `bar($a, ...$b); - foo::bar($a, ...$b); - $foo::bar($a, ...$b); - new foo($a, ...$b); - /** anonymous class */ - new class ($a, ...$b) {}; - ` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 2, - EndLine: 9, - StartPos: 6, - EndPos: 186, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 6, - EndPos: 21, - }, - Expr: &expr.FunctionCall{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 6, - EndPos: 20, - }, - Function: &name.Name{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 6, - EndPos: 9, - }, - Parts: []node.Node{ - &name.NamePart{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 6, - EndPos: 9, - }, - Value: "foo", - }, - }, - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 9, - EndPos: 20, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 10, - EndPos: 12, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 10, - EndPos: 12, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 10, - EndPos: 12, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 14, - EndPos: 19, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 17, - EndPos: 19, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 17, - EndPos: 19, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 24, - EndPos: 40, - }, - Expr: &expr.FunctionCall{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 24, - EndPos: 39, - }, - Function: &expr.Variable{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 24, - EndPos: 28, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 24, - EndPos: 28, - }, - Value: "foo", - }, - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 28, - EndPos: 39, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 29, - EndPos: 31, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 29, - EndPos: 31, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 29, - EndPos: 31, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 33, - EndPos: 38, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 36, - EndPos: 38, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 36, - EndPos: 38, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 43, - EndPos: 64, - }, - Expr: &expr.MethodCall{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 43, - EndPos: 63, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 43, - EndPos: 47, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 43, - EndPos: 47, - }, - Value: "foo", - }, - }, - Method: &node.Identifier{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 49, - EndPos: 52, - }, - Value: "bar", - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 52, - EndPos: 63, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 53, - EndPos: 55, - }, - IsReference: false, - Variadic: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 53, - EndPos: 55, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 53, - EndPos: 55, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 57, - EndPos: 62, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 60, - EndPos: 62, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 60, - EndPos: 62, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 67, - EndPos: 87, - }, - Expr: &expr.StaticCall{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 67, - EndPos: 86, - }, - Class: &name.Name{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 67, - EndPos: 70, - }, - Parts: []node.Node{ - &name.NamePart{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 67, - EndPos: 70, - }, - Value: "foo", - }, - }, - }, - Call: &node.Identifier{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 72, - EndPos: 75, - }, - Value: "bar", - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 75, - EndPos: 86, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 76, - EndPos: 78, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 76, - EndPos: 78, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 76, - EndPos: 78, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 80, - EndPos: 85, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 83, - EndPos: 85, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 83, - EndPos: 85, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 90, - EndPos: 111, - }, - Expr: &expr.StaticCall{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 90, - EndPos: 110, - }, - Class: &expr.Variable{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 90, - EndPos: 94, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 90, - EndPos: 94, - }, - Value: "foo", - }, - }, - Call: &node.Identifier{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 96, - EndPos: 99, - }, - Value: "bar", - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 99, - EndPos: 110, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 100, - EndPos: 102, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 100, - EndPos: 102, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 100, - EndPos: 102, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 104, - EndPos: 109, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 107, - EndPos: 109, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 107, - EndPos: 109, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 114, - EndPos: 133, - }, - Expr: &expr.New{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 114, - EndPos: 132, - }, - Class: &name.Name{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 118, - EndPos: 121, - }, - Parts: []node.Node{ - &name.NamePart{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 118, - EndPos: 121, - }, - Value: "foo", - }, - }, - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 121, - EndPos: 132, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 122, - EndPos: 124, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 122, - EndPos: 124, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 122, - EndPos: 124, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 126, - EndPos: 131, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 129, - EndPos: 131, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 129, - EndPos: 131, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 161, - EndPos: 186, - }, - Expr: &expr.New{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 161, - EndPos: 185, - }, - Class: &stmt.Class{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 165, - EndPos: 185, - }, - PhpDocComment: "/** anonymous class */", - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 171, - EndPos: 182, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 172, - EndPos: 174, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 172, - EndPos: 174, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 172, - EndPos: 174, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 176, - EndPos: 181, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 179, - EndPos: 181, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 9, - EndLine: 9, - StartPos: 179, - EndPos: 181, - }, - Value: "b", - }, - }, - }, - }, - }, - Stmts: []node.Node{}, - }, - }, - }, - }, - } - - php7parser := php7.NewParser([]byte(src), "7.4") - php7parser.Parse() - actual := php7parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestPhp5ArgumentNode(t *testing.T) { - src := `bar($a, ...$b); - foo::bar($a, ...$b); - $foo::bar($a, ...$b); - new foo($a, ...$b); - ` - - expected := &node.Root{ - Position: &position.Position{ - StartLine: 2, - EndLine: 7, - StartPos: 6, - EndPos: 133, - }, - Stmts: []node.Node{ - &stmt.Expression{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 6, - EndPos: 21, - }, - Expr: &expr.FunctionCall{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 6, - EndPos: 20, - }, - Function: &name.Name{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 6, - EndPos: 9, - }, - Parts: []node.Node{ - &name.NamePart{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 6, - EndPos: 9, - }, - Value: "foo", - }, - }, - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 9, - EndPos: 20, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 10, - EndPos: 12, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 10, - EndPos: 12, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 10, - EndPos: 12, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 14, - EndPos: 19, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 17, - EndPos: 19, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 2, - EndLine: 2, - StartPos: 17, - EndPos: 19, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 24, - EndPos: 40, - }, - Expr: &expr.FunctionCall{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 24, - EndPos: 39, - }, - Function: &expr.Variable{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 24, - EndPos: 28, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 24, - EndPos: 28, - }, - Value: "foo", - }, - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 28, - EndPos: 39, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 29, - EndPos: 31, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 29, - EndPos: 31, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 29, - EndPos: 31, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 33, - EndPos: 38, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 36, - EndPos: 38, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 3, - EndLine: 3, - StartPos: 36, - EndPos: 38, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 43, - EndPos: 64, - }, - Expr: &expr.MethodCall{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 43, - EndPos: 63, - }, - Variable: &expr.Variable{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 43, - EndPos: 47, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 43, - EndPos: 47, - }, - Value: "foo", - }, - }, - Method: &node.Identifier{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 49, - EndPos: 52, - }, - Value: "bar", - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 52, - EndPos: 63, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 53, - EndPos: 55, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 53, - EndPos: 55, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 53, - EndPos: 55, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 57, - EndPos: 62, - }, - IsReference: false, - Variadic: true, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 60, - EndPos: 62, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 4, - EndLine: 4, - StartPos: 60, - EndPos: 62, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 67, - EndPos: 87, - }, - Expr: &expr.StaticCall{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 67, - EndPos: 86, - }, - Class: &name.Name{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 67, - EndPos: 70, - }, - Parts: []node.Node{ - &name.NamePart{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 67, - EndPos: 70, - }, - Value: "foo", - }, - }, - }, - Call: &node.Identifier{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 72, - EndPos: 75, - }, - Value: "bar", - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 75, - EndPos: 86, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 76, - EndPos: 78, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 76, - EndPos: 78, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 76, - EndPos: 78, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 80, - EndPos: 85, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 83, - EndPos: 85, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 5, - EndLine: 5, - StartPos: 83, - EndPos: 85, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 90, - EndPos: 111, - }, - Expr: &expr.StaticCall{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 90, - EndPos: 110, - }, - Class: &expr.Variable{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 90, - EndPos: 94, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 90, - EndPos: 94, - }, - Value: "foo", - }, - }, - Call: &node.Identifier{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 96, - EndPos: 99, - }, - Value: "bar", - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 99, - EndPos: 110, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 100, - EndPos: 102, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 100, - EndPos: 102, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 100, - EndPos: 102, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 104, - EndPos: 109, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 107, - EndPos: 109, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 6, - EndLine: 6, - StartPos: 107, - EndPos: 109, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - &stmt.Expression{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 114, - EndPos: 133, - }, - Expr: &expr.New{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 114, - EndPos: 132, - }, - Class: &name.Name{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 118, - EndPos: 121, - }, - Parts: []node.Node{ - &name.NamePart{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 118, - EndPos: 121, - }, - Value: "foo", - }, - }, - }, - ArgumentList: &node.ArgumentList{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 121, - EndPos: 132, - }, - Arguments: []node.Node{ - &node.Argument{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 122, - EndPos: 124, - }, - Variadic: false, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 122, - EndPos: 124, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 122, - EndPos: 124, - }, - Value: "a", - }, - }, - }, - &node.Argument{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 126, - EndPos: 131, - }, - Variadic: true, - IsReference: false, - Expr: &expr.Variable{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 129, - EndPos: 131, - }, - VarName: &node.Identifier{ - Position: &position.Position{ - StartLine: 7, - EndLine: 7, - StartPos: 129, - EndPos: 131, - }, - Value: "b", - }, - }, - }, - }, - }, - }, - }, - }, - } - - php5parser := php5.NewParser([]byte(src), "5.6") - php5parser.Parse() - actual := php5parser.GetRootNode() - assert.DeepEqual(t, expected, actual) -} - -func TestPhp7ParameterNode(t *testing.T) { - src := `