refactoring: update ast structure of "Foreach" node

This commit is contained in:
Vadym Slizov 2020-12-26 19:38:45 +02:00
parent b85bae2ec1
commit 8bf1fa822d
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
12 changed files with 310 additions and 820 deletions

View File

@ -15045,61 +15045,53 @@ func TestStmtForeach_WithRef(t *testing.T) {
}, },
}, },
}, },
Var: &ast.ExprReference{ AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 24, StartPos: 24,
EndPos: 27, EndPos: 25,
}, },
AmpersandTkn: &token.Token{ FreeFloating: []*token.Token{
ID: token.ID(38), {
Value: []byte("&"), ID: token.T_WHITESPACE,
Position: &position.Position{ Value: []byte(" "),
StartLine: 1, Position: &position.Position{
EndLine: 1, StartLine: 1,
StartPos: 24, EndLine: 1,
EndPos: 25, StartPos: 23,
}, EndPos: 24,
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 23,
EndPos: 24,
},
}, },
}, },
}, },
Var: &ast.ExprVariable{ },
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
VarName: &ast.Identifier{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 25, StartPos: 25,
EndPos: 27, EndPos: 27,
}, },
VarName: &ast.Identifier{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$v"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 25, StartPos: 25,
EndPos: 27, EndPos: 27,
}, },
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$v"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
},
Value: []byte("$v"),
}, },
Value: []byte("$v"),
}, },
}, },
CloseParenthesisTkn: &token.Token{ CloseParenthesisTkn: &token.Token{
@ -31770,7 +31762,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 36, EndPos: 36,
}, },
FunctionTkn: &token.Token{ FunctionTkn: &token.Token{
ID: token.T_FUNCTION, ID: token.T_FUNCTION,
Value: []byte("function"), Value: []byte("function"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31780,7 +31772,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_OPEN_TAG, ID: token.T_OPEN_TAG,
Value: []byte("<?"), Value: []byte("<?"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31790,7 +31782,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31802,7 +31794,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
OpenParenthesisTkn: &token.Token{ OpenParenthesisTkn: &token.Token{
ID: token.ID(40), ID: token.ID(40),
Value: []byte("("), Value: []byte("("),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31834,7 +31826,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 14, EndPos: 14,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$a"), Value: []byte("$a"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31869,7 +31861,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 18, EndPos: 18,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$b"), Value: []byte("$b"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31879,7 +31871,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31897,7 +31889,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
SeparatorTkns: []*token.Token{ SeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),
Value: []byte(","), Value: []byte(","),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31908,7 +31900,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
CloseParenthesisTkn: &token.Token{ CloseParenthesisTkn: &token.Token{
ID: token.ID(41), ID: token.ID(41),
Value: []byte(")"), Value: []byte(")"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31918,7 +31910,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
UseTkn: &token.Token{ UseTkn: &token.Token{
ID: token.T_USE, ID: token.T_USE,
Value: []byte("use"), Value: []byte("use"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31928,7 +31920,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31940,7 +31932,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
UseOpenParenthesisTkn: &token.Token{ UseOpenParenthesisTkn: &token.Token{
ID: token.ID(40), ID: token.ID(40),
Value: []byte("("), Value: []byte("("),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31950,7 +31942,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -31984,7 +31976,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 27, EndPos: 27,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$c"), Value: []byte("$c"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32005,7 +31997,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 32, EndPos: 32,
}, },
AmpersandTkn: &token.Token{ AmpersandTkn: &token.Token{
ID: token.ID(38), ID: token.ID(38),
Value: []byte("&"), Value: []byte("&"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32015,7 +32007,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32041,7 +32033,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 32, EndPos: 32,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$d"), Value: []byte("$d"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32057,7 +32049,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
UseSeparatorTkns: []*token.Token{ UseSeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),
Value: []byte(","), Value: []byte(","),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32068,7 +32060,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
UseCloseParenthesisTkn: &token.Token{ UseCloseParenthesisTkn: &token.Token{
ID: token.ID(41), ID: token.ID(41),
Value: []byte(")"), Value: []byte(")"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32078,7 +32070,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
OpenCurlyBracketTkn: &token.Token{ OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123), ID: token.ID(123),
Value: []byte("{"), Value: []byte("{"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32088,7 +32080,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32101,7 +32093,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
CloseCurlyBracketTkn: &token.Token{ CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32112,7 +32104,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
SemiColonTkn: &token.Token{ SemiColonTkn: &token.Token{
ID: token.ID(59), ID: token.ID(59),
Value: []byte(";"), Value: []byte(";"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32123,8 +32115,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
}, },
EndTkn: &token.Token{ EndTkn: &token.Token{},
},
} }
lexer := scanner.NewLexer([]byte(src), "5.6", nil) lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -32160,7 +32151,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 36, EndPos: 36,
}, },
FunctionTkn: &token.Token{ FunctionTkn: &token.Token{
ID: token.T_FUNCTION, ID: token.T_FUNCTION,
Value: []byte("function"), Value: []byte("function"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32170,7 +32161,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_OPEN_TAG, ID: token.T_OPEN_TAG,
Value: []byte("<?"), Value: []byte("<?"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32180,7 +32171,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32192,7 +32183,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
OpenParenthesisTkn: &token.Token{ OpenParenthesisTkn: &token.Token{
ID: token.ID(40), ID: token.ID(40),
Value: []byte("("), Value: []byte("("),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32224,7 +32215,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 14, EndPos: 14,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$a"), Value: []byte("$a"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32259,7 +32250,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 18, EndPos: 18,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$b"), Value: []byte("$b"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32269,7 +32260,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32287,7 +32278,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
SeparatorTkns: []*token.Token{ SeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),
Value: []byte(","), Value: []byte(","),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32298,7 +32289,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
CloseParenthesisTkn: &token.Token{ CloseParenthesisTkn: &token.Token{
ID: token.ID(41), ID: token.ID(41),
Value: []byte(")"), Value: []byte(")"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32308,7 +32299,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
UseTkn: &token.Token{ UseTkn: &token.Token{
ID: token.T_USE, ID: token.T_USE,
Value: []byte("use"), Value: []byte("use"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32318,7 +32309,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32330,7 +32321,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
UseOpenParenthesisTkn: &token.Token{ UseOpenParenthesisTkn: &token.Token{
ID: token.ID(40), ID: token.ID(40),
Value: []byte("("), Value: []byte("("),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32340,7 +32331,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32360,7 +32351,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 28, EndPos: 28,
}, },
AmpersandTkn: &token.Token{ AmpersandTkn: &token.Token{
ID: token.ID(38), ID: token.ID(38),
Value: []byte("&"), Value: []byte("&"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32384,7 +32375,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 28, EndPos: 28,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$c"), Value: []byte("$c"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32419,7 +32410,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 32, EndPos: 32,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$d"), Value: []byte("$d"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32429,7 +32420,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32447,7 +32438,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
UseSeparatorTkns: []*token.Token{ UseSeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),
Value: []byte(","), Value: []byte(","),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32458,7 +32449,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
UseCloseParenthesisTkn: &token.Token{ UseCloseParenthesisTkn: &token.Token{
ID: token.ID(41), ID: token.ID(41),
Value: []byte(")"), Value: []byte(")"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32468,7 +32459,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
OpenCurlyBracketTkn: &token.Token{ OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123), ID: token.ID(123),
Value: []byte("{"), Value: []byte("{"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32478,7 +32469,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32491,7 +32482,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
CloseCurlyBracketTkn: &token.Token{ CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32502,7 +32493,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
SemiColonTkn: &token.Token{ SemiColonTkn: &token.Token{
ID: token.ID(59), ID: token.ID(59),
Value: []byte(";"), Value: []byte(";"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -32513,8 +32504,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
}, },
EndTkn: &token.Token{ EndTkn: &token.Token{},
},
} }
lexer := scanner.NewLexer([]byte(src), "5.6", nil) lexer := scanner.NewLexer([]byte(src), "5.6", nil)
@ -38567,309 +38557,6 @@ func TestExprPropertyFetch(t *testing.T) {
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }
func TestExprReference_ForeachWithRef(t *testing.T) {
t.Helper()
src := `<? foreach ($a as $k => &$v) {}`
expected := &ast.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 31,
},
Stmts: []ast.Vertex{
&ast.StmtForeach{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 31,
},
ForeachTkn: &token.Token{
ID: token.T_FOREACH,
Value: []byte("foreach"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 10,
},
FreeFloating: []*token.Token{
{
ID: token.T_OPEN_TAG,
Value: []byte("<?"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 2,
},
},
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 2,
EndPos: 3,
},
},
},
},
OpenParenthesisTkn: &token.Token{
ID: token.ID(40),
Value: []byte("("),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 11,
},
},
},
},
Expr: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$a"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
},
Value: []byte("$a"),
},
},
AsTkn: &token.Token{
ID: token.T_AS,
Value: []byte("as"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 17,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 15,
},
},
},
},
Key: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$k"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 18,
},
},
},
},
Value: []byte("$k"),
},
},
DoubleArrowTkn: &token.Token{
ID: token.T_DOUBLE_ARROW,
Value: []byte("=>"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 21,
EndPos: 23,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 21,
},
},
},
},
Var: &ast.ExprReference{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 27,
},
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 25,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 23,
EndPos: 24,
},
},
},
},
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$v"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
},
Value: []byte("$v"),
},
},
},
CloseParenthesisTkn: &token.Token{
ID: token.ID(41),
Value: []byte(")"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 28,
},
},
Stmt: &ast.StmtStmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 29,
EndPos: 31,
},
OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123),
Value: []byte("{"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 29,
EndPos: 30,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 28,
EndPos: 29,
},
},
},
},
Stmts: []ast.Vertex{},
CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125),
Value: []byte("}"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 31,
},
},
},
},
},
EndTkn: &token.Token{},
}
lexer := scanner.NewLexer([]byte(src), "5.6", nil)
php5parser := php5.NewParser(lexer, nil)
php5parser.Parse()
actual := php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestExprShellExec(t *testing.T) { func TestExprShellExec(t *testing.T) {
src := "<? `cmd $a`;" src := "<? `cmd $a`;"

BIN
internal/php5/php5.go generated

Binary file not shown.

View File

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/token" "github.com/z7zmey/php-parser/pkg/token"
) )
@ -983,39 +984,63 @@ unticked_statement:
} }
| T_FOREACH '(' variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement | T_FOREACH '(' variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement
{ {
$8.(*ast.StmtForeach).ForeachTkn = $1 foreach := $8.(*ast.StmtForeach)
$8.(*ast.StmtForeach).OpenParenthesisTkn = $2
$8.(*ast.StmtForeach).Expr = $3
$8.(*ast.StmtForeach).AsTkn = $4
if $6 == nil {
$8.(*ast.StmtForeach).Var = $5
} else {
$8.(*ast.StmtForeach).Key = $5
$8.(*ast.StmtForeach).DoubleArrowTkn = $6.(*ast.StmtForeach).DoubleArrowTkn
$8.(*ast.StmtForeach).Var = $6.(*ast.StmtForeach).Var
}
$8.(*ast.StmtForeach).CloseParenthesisTkn = $7
$8.(*ast.StmtForeach).Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $8)
$$ = $8 foreach.Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $8)
foreach.ForeachTkn = $1
foreach.OpenParenthesisTkn = $2
foreach.Expr = $3
foreach.AsTkn = $4
foreach.Var = $5
foreach.CloseParenthesisTkn = $7
if $6 != nil {
foreach.Key = foreach.Var
foreach.DoubleArrowTkn = $6.(*ast.StmtForeach).DoubleArrowTkn
foreach.Var = $6.(*ast.StmtForeach).Var
}
if val, ok := foreach.Key.(*ast.ExprReference); ok {
yylex.(*Parser).errHandlerFunc(errors.NewError("Key element cannot be a reference", val.AmpersandTkn.Position))
foreach.Key = val.Var
}
if val, ok := foreach.Var.(*ast.ExprReference); ok {
foreach.AmpersandTkn = val.AmpersandTkn
foreach.Var = val.Var
}
$$ = foreach
} }
| T_FOREACH '(' expr_without_variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement | T_FOREACH '(' expr_without_variable T_AS foreach_variable foreach_optional_arg ')' foreach_statement
{ {
$8.(*ast.StmtForeach).ForeachTkn = $1 foreach := $8.(*ast.StmtForeach)
$8.(*ast.StmtForeach).OpenParenthesisTkn = $2
$8.(*ast.StmtForeach).Expr = $3
$8.(*ast.StmtForeach).AsTkn = $4
if $6 == nil {
$8.(*ast.StmtForeach).Var = $5
} else {
$8.(*ast.StmtForeach).Key = $5
$8.(*ast.StmtForeach).DoubleArrowTkn = $6.(*ast.StmtForeach).DoubleArrowTkn
$8.(*ast.StmtForeach).Var = $6.(*ast.StmtForeach).Var
}
$8.(*ast.StmtForeach).CloseParenthesisTkn = $7
$8.(*ast.StmtForeach).Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $8)
$$ = $8 foreach.Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $8)
foreach.ForeachTkn = $1
foreach.OpenParenthesisTkn = $2
foreach.Expr = $3
foreach.AsTkn = $4
foreach.Var = $5
foreach.CloseParenthesisTkn = $7
if $6 != nil {
foreach.Key = foreach.Var
foreach.DoubleArrowTkn = $6.(*ast.StmtForeach).DoubleArrowTkn
foreach.Var = $6.(*ast.StmtForeach).Var
}
if val, ok := foreach.Key.(*ast.ExprReference); ok {
yylex.(*Parser).errHandlerFunc(errors.NewError("Key element cannot be a reference", val.AmpersandTkn.Position))
foreach.Key = val.Var
}
if val, ok := foreach.Var.(*ast.ExprReference); ok {
foreach.AmpersandTkn = val.AmpersandTkn
foreach.Var = val.Var
}
$$ = foreach
} }
| T_DECLARE '(' declare_list ')' declare_statement | T_DECLARE '(' declare_list ')' declare_statement
{ {

View File

@ -16524,61 +16524,53 @@ func TestStmtForeach_WithRef(t *testing.T) {
}, },
}, },
}, },
Var: &ast.ExprReference{ AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 24, StartPos: 24,
EndPos: 27, EndPos: 25,
}, },
AmpersandTkn: &token.Token{ FreeFloating: []*token.Token{
ID: token.ID(38), {
Value: []byte("&"), ID: token.T_WHITESPACE,
Position: &position.Position{ Value: []byte(" "),
StartLine: 1, Position: &position.Position{
EndLine: 1, StartLine: 1,
StartPos: 24, EndLine: 1,
EndPos: 25, StartPos: 23,
}, EndPos: 24,
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 23,
EndPos: 24,
},
}, },
}, },
}, },
Var: &ast.ExprVariable{ },
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
VarName: &ast.Identifier{
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 25, StartPos: 25,
EndPos: 27, EndPos: 27,
}, },
VarName: &ast.Identifier{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$v"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
EndLine: 1, EndLine: 1,
StartPos: 25, StartPos: 25,
EndPos: 27, EndPos: 27,
}, },
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$v"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
},
Value: []byte("$v"),
}, },
Value: []byte("$v"),
}, },
}, },
CloseParenthesisTkn: &token.Token{ CloseParenthesisTkn: &token.Token{
@ -36268,7 +36260,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 36, EndPos: 36,
}, },
FunctionTkn: &token.Token{ FunctionTkn: &token.Token{
ID: token.T_FUNCTION, ID: token.T_FUNCTION,
Value: []byte("function"), Value: []byte("function"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36278,7 +36270,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_OPEN_TAG, ID: token.T_OPEN_TAG,
Value: []byte("<?"), Value: []byte("<?"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36288,7 +36280,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36300,7 +36292,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
OpenParenthesisTkn: &token.Token{ OpenParenthesisTkn: &token.Token{
ID: token.ID(40), ID: token.ID(40),
Value: []byte("("), Value: []byte("("),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36332,7 +36324,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 14, EndPos: 14,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$a"), Value: []byte("$a"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36367,7 +36359,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 18, EndPos: 18,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$b"), Value: []byte("$b"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36377,7 +36369,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36395,7 +36387,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
SeparatorTkns: []*token.Token{ SeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),
Value: []byte(","), Value: []byte(","),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36406,7 +36398,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
CloseParenthesisTkn: &token.Token{ CloseParenthesisTkn: &token.Token{
ID: token.ID(41), ID: token.ID(41),
Value: []byte(")"), Value: []byte(")"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36416,7 +36408,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
UseTkn: &token.Token{ UseTkn: &token.Token{
ID: token.T_USE, ID: token.T_USE,
Value: []byte("use"), Value: []byte("use"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36426,7 +36418,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36438,7 +36430,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
UseOpenParenthesisTkn: &token.Token{ UseOpenParenthesisTkn: &token.Token{
ID: token.ID(40), ID: token.ID(40),
Value: []byte("("), Value: []byte("("),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36448,7 +36440,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36482,7 +36474,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 27, EndPos: 27,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$c"), Value: []byte("$c"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36503,7 +36495,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 32, EndPos: 32,
}, },
AmpersandTkn: &token.Token{ AmpersandTkn: &token.Token{
ID: token.ID(38), ID: token.ID(38),
Value: []byte("&"), Value: []byte("&"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36513,7 +36505,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36539,7 +36531,7 @@ func TestExprClosure_Use(t *testing.T) {
EndPos: 32, EndPos: 32,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$d"), Value: []byte("$d"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36555,7 +36547,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
UseSeparatorTkns: []*token.Token{ UseSeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),
Value: []byte(","), Value: []byte(","),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36566,7 +36558,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
UseCloseParenthesisTkn: &token.Token{ UseCloseParenthesisTkn: &token.Token{
ID: token.ID(41), ID: token.ID(41),
Value: []byte(")"), Value: []byte(")"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36576,7 +36568,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
OpenCurlyBracketTkn: &token.Token{ OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123), ID: token.ID(123),
Value: []byte("{"), Value: []byte("{"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36586,7 +36578,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36599,7 +36591,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
CloseCurlyBracketTkn: &token.Token{ CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36610,7 +36602,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
SemiColonTkn: &token.Token{ SemiColonTkn: &token.Token{
ID: token.ID(59), ID: token.ID(59),
Value: []byte(";"), Value: []byte(";"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36621,8 +36613,7 @@ func TestExprClosure_Use(t *testing.T) {
}, },
}, },
}, },
EndTkn: &token.Token{ EndTkn: &token.Token{},
},
} }
lexer := scanner.NewLexer([]byte(src), "7.4", nil) lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -36658,7 +36649,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 36, EndPos: 36,
}, },
FunctionTkn: &token.Token{ FunctionTkn: &token.Token{
ID: token.T_FUNCTION, ID: token.T_FUNCTION,
Value: []byte("function"), Value: []byte("function"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36668,7 +36659,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_OPEN_TAG, ID: token.T_OPEN_TAG,
Value: []byte("<?"), Value: []byte("<?"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36678,7 +36669,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36690,7 +36681,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
OpenParenthesisTkn: &token.Token{ OpenParenthesisTkn: &token.Token{
ID: token.ID(40), ID: token.ID(40),
Value: []byte("("), Value: []byte("("),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36722,7 +36713,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 14, EndPos: 14,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$a"), Value: []byte("$a"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36757,7 +36748,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 18, EndPos: 18,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$b"), Value: []byte("$b"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36767,7 +36758,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36785,7 +36776,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
SeparatorTkns: []*token.Token{ SeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),
Value: []byte(","), Value: []byte(","),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36796,7 +36787,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
CloseParenthesisTkn: &token.Token{ CloseParenthesisTkn: &token.Token{
ID: token.ID(41), ID: token.ID(41),
Value: []byte(")"), Value: []byte(")"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36806,7 +36797,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
UseTkn: &token.Token{ UseTkn: &token.Token{
ID: token.T_USE, ID: token.T_USE,
Value: []byte("use"), Value: []byte("use"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36816,7 +36807,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36828,7 +36819,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
UseOpenParenthesisTkn: &token.Token{ UseOpenParenthesisTkn: &token.Token{
ID: token.ID(40), ID: token.ID(40),
Value: []byte("("), Value: []byte("("),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36838,7 +36829,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36858,7 +36849,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 28, EndPos: 28,
}, },
AmpersandTkn: &token.Token{ AmpersandTkn: &token.Token{
ID: token.ID(38), ID: token.ID(38),
Value: []byte("&"), Value: []byte("&"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36882,7 +36873,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 28, EndPos: 28,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$c"), Value: []byte("$c"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36917,7 +36908,7 @@ func TestExprClosure_Use2(t *testing.T) {
EndPos: 32, EndPos: 32,
}, },
IdentifierTkn: &token.Token{ IdentifierTkn: &token.Token{
ID: token.T_VARIABLE, ID: token.T_VARIABLE,
Value: []byte("$d"), Value: []byte("$d"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36927,7 +36918,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36945,7 +36936,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
UseSeparatorTkns: []*token.Token{ UseSeparatorTkns: []*token.Token{
{ {
ID: token.ID(44), ID: token.ID(44),
Value: []byte(","), Value: []byte(","),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36956,7 +36947,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
UseCloseParenthesisTkn: &token.Token{ UseCloseParenthesisTkn: &token.Token{
ID: token.ID(41), ID: token.ID(41),
Value: []byte(")"), Value: []byte(")"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36966,7 +36957,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
OpenCurlyBracketTkn: &token.Token{ OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123), ID: token.ID(123),
Value: []byte("{"), Value: []byte("{"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36976,7 +36967,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
FreeFloating: []*token.Token{ FreeFloating: []*token.Token{
{ {
ID: token.T_WHITESPACE, ID: token.T_WHITESPACE,
Value: []byte(" "), Value: []byte(" "),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -36989,7 +36980,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
Stmts: []ast.Vertex{}, Stmts: []ast.Vertex{},
CloseCurlyBracketTkn: &token.Token{ CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125), ID: token.ID(125),
Value: []byte("}"), Value: []byte("}"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -37000,7 +36991,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
SemiColonTkn: &token.Token{ SemiColonTkn: &token.Token{
ID: token.ID(59), ID: token.ID(59),
Value: []byte(";"), Value: []byte(";"),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -37011,8 +37002,7 @@ func TestExprClosure_Use2(t *testing.T) {
}, },
}, },
}, },
EndTkn: &token.Token{ EndTkn: &token.Token{},
},
} }
lexer := scanner.NewLexer([]byte(src), "7.4", nil) lexer := scanner.NewLexer([]byte(src), "7.4", nil)
@ -43525,309 +43515,6 @@ func TestExprPropertyFetch(t *testing.T) {
assert.DeepEqual(t, expected, actual) assert.DeepEqual(t, expected, actual)
} }
func TestExprReference_ForeachWithRef(t *testing.T) {
t.Helper()
src := `<? foreach ($a as $k => &$v) {}`
expected := &ast.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 31,
},
Stmts: []ast.Vertex{
&ast.StmtForeach{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 31,
},
ForeachTkn: &token.Token{
ID: token.T_FOREACH,
Value: []byte("foreach"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 10,
},
FreeFloating: []*token.Token{
{
ID: token.T_OPEN_TAG,
Value: []byte("<?"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 2,
},
},
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 2,
EndPos: 3,
},
},
},
},
OpenParenthesisTkn: &token.Token{
ID: token.ID(40),
Value: []byte("("),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 11,
},
},
},
},
Expr: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$a"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 14,
},
},
Value: []byte("$a"),
},
},
AsTkn: &token.Token{
ID: token.T_AS,
Value: []byte("as"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 17,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 15,
},
},
},
},
Key: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$k"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 17,
EndPos: 18,
},
},
},
},
Value: []byte("$k"),
},
},
DoubleArrowTkn: &token.Token{
ID: token.T_DOUBLE_ARROW,
Value: []byte("=>"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 21,
EndPos: 23,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 21,
},
},
},
},
Var: &ast.ExprReference{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 27,
},
AmpersandTkn: &token.Token{
ID: token.ID(38),
Value: []byte("&"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 25,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 23,
EndPos: 24,
},
},
},
},
Var: &ast.ExprVariable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
VarName: &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
IdentifierTkn: &token.Token{
ID: token.T_VARIABLE,
Value: []byte("$v"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 25,
EndPos: 27,
},
},
Value: []byte("$v"),
},
},
},
CloseParenthesisTkn: &token.Token{
ID: token.ID(41),
Value: []byte(")"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 27,
EndPos: 28,
},
},
Stmt: &ast.StmtStmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 29,
EndPos: 31,
},
OpenCurlyBracketTkn: &token.Token{
ID: token.ID(123),
Value: []byte("{"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 29,
EndPos: 30,
},
FreeFloating: []*token.Token{
{
ID: token.T_WHITESPACE,
Value: []byte(" "),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 28,
EndPos: 29,
},
},
},
},
Stmts: []ast.Vertex{},
CloseCurlyBracketTkn: &token.Token{
ID: token.ID(125),
Value: []byte("}"),
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 31,
},
},
},
},
},
EndTkn: &token.Token{},
}
lexer := scanner.NewLexer([]byte(src), "7.4", nil)
php7parser := php7.NewParser(lexer, nil)
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestExprShellExec(t *testing.T) { func TestExprShellExec(t *testing.T) {
src := "<? `cmd $a`;" src := "<? `cmd $a`;"

BIN
internal/php7/php7.go generated

Binary file not shown.

View File

@ -962,29 +962,43 @@ statement:
} }
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
{ {
$7.(*ast.StmtForeach).ForeachTkn = $1 foreach := $7.(*ast.StmtForeach)
$7.(*ast.StmtForeach).OpenParenthesisTkn = $2
$7.(*ast.StmtForeach).Expr = $3
$7.(*ast.StmtForeach).AsTkn = $4
$7.(*ast.StmtForeach).Var = $5
$7.(*ast.StmtForeach).CloseParenthesisTkn = $6
$7.(*ast.StmtForeach).Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $7)
$$ = $7 foreach.Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $7)
foreach.ForeachTkn = $1
foreach.OpenParenthesisTkn = $2
foreach.Expr = $3
foreach.AsTkn = $4
foreach.Var = $5
foreach.CloseParenthesisTkn = $6
if val, ok := $5.(*ast.ExprReference); ok {
foreach.AmpersandTkn = val.AmpersandTkn
foreach.Var = val.Var
}
$$ = foreach
} }
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement | T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
{ {
$9.(*ast.StmtForeach).ForeachTkn = $1 foreach := $9.(*ast.StmtForeach)
$9.(*ast.StmtForeach).OpenParenthesisTkn = $2
$9.(*ast.StmtForeach).Expr = $3
$9.(*ast.StmtForeach).AsTkn = $4
$9.(*ast.StmtForeach).Key = $5
$9.(*ast.StmtForeach).DoubleArrowTkn = $6
$9.(*ast.StmtForeach).Var = $7
$9.(*ast.StmtForeach).CloseParenthesisTkn = $8
$9.(*ast.StmtForeach).Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $9)
$$ = $9 foreach.Position = yylex.(*Parser).builder.NewTokenNodePosition($1, $9)
foreach.ForeachTkn = $1
foreach.OpenParenthesisTkn = $2
foreach.Expr = $3
foreach.AsTkn = $4
foreach.Key = $5
foreach.DoubleArrowTkn = $6
foreach.Var = $7
foreach.CloseParenthesisTkn = $8
if val, ok := $7.(*ast.ExprReference); ok {
foreach.AmpersandTkn = val.AmpersandTkn
foreach.Var = val.Var
}
$$ = foreach
} }
| T_DECLARE '(' const_list ')' declare_statement | T_DECLARE '(' const_list ')' declare_statement
{ {

View File

@ -574,6 +574,7 @@ type StmtForeach struct {
AsTkn *token.Token AsTkn *token.Token
Key Vertex Key Vertex
DoubleArrowTkn *token.Token DoubleArrowTkn *token.Token
AmpersandTkn *token.Token
Var Vertex Var Vertex
CloseParenthesisTkn *token.Token CloseParenthesisTkn *token.Token
ColonTkn *token.Token ColonTkn *token.Token

View File

@ -552,6 +552,7 @@ func (v *Dumper) StmtForeach(n *ast.StmtForeach) {
v.dumpToken("AsTkn", n.AsTkn) v.dumpToken("AsTkn", n.AsTkn)
v.dumpVertex("Key", n.Key) v.dumpVertex("Key", n.Key)
v.dumpToken("DoubleArrowTkn", n.DoubleArrowTkn) v.dumpToken("DoubleArrowTkn", n.DoubleArrowTkn)
v.dumpToken("AmpersandTkn", n.AmpersandTkn)
v.dumpVertex("Var", n.Var) v.dumpVertex("Var", n.Var)
v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn) v.dumpToken("CloseParenthesisTkn", n.CloseParenthesisTkn)
v.dumpToken("ColonTkn", n.ColonTkn) v.dumpToken("ColonTkn", n.ColonTkn)

View File

@ -557,6 +557,9 @@ func (f *formatter) StmtForeach(n *ast.StmtForeach) {
} }
f.addFreeFloating(token.T_WHITESPACE, []byte(" ")) f.addFreeFloating(token.T_WHITESPACE, []byte(" "))
if n.AmpersandTkn != nil {
n.AmpersandTkn = f.newToken('&', []byte("&"))
}
n.Var.Accept(f) n.Var.Accept(f)
n.CloseParenthesisTkn = f.newToken(')', []byte(")")) n.CloseParenthesisTkn = f.newToken(')', []byte(")"))

View File

@ -1372,6 +1372,44 @@ func TestFormatter_StmtForeach(t *testing.T) {
} }
} }
func TestFormatter_StmtForeach_Reference(t *testing.T) {
o := bytes.NewBufferString("")
n := &ast.StmtForeach{
Expr: &ast.ExprVariable{
VarName: &ast.Identifier{
Value: []byte("$foo"),
},
},
AmpersandTkn: &token.Token{},
Var: &ast.ExprVariable{
VarName: &ast.Identifier{
Value: []byte("$val"),
},
},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
}
f := visitor.NewFormatter().WithState(visitor.FormatterStatePHP).WithIndent(1)
n.Accept(f)
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n.Accept(p)
expected := `foreach($foo as &$val) {
;
}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestFormatter_StmtForeach_Key(t *testing.T) { func TestFormatter_StmtForeach_Key(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")

View File

@ -356,6 +356,7 @@ func (p *printer) StmtForeach(n *ast.StmtForeach) {
p.printToken(n.AsTkn, []byte("as")) p.printToken(n.AsTkn, []byte("as"))
p.printNode(n.Key) p.printNode(n.Key)
p.printToken(n.DoubleArrowTkn, p.ifNode(n.Key, []byte("=>"))) p.printToken(n.DoubleArrowTkn, p.ifNode(n.Key, []byte("=>")))
p.printToken(n.AmpersandTkn, nil)
p.printNode(n.Var) p.printNode(n.Var)
p.printToken(n.CloseParenthesisTkn, []byte(")")) p.printToken(n.CloseParenthesisTkn, []byte(")"))
p.printToken(n.ColonTkn, nil) p.printToken(n.ColonTkn, nil)

View File

@ -3799,6 +3799,39 @@ func TestPrinterPrintStmtForeach(t *testing.T) {
} }
} }
func TestPrinterPrintStmtForeach_Reference(t *testing.T) {
o := bytes.NewBufferString("")
p := visitor.NewPrinter(o).WithState(visitor.PrinterStatePHP)
n := &ast.StmtForeach{
Expr: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$a")},
},
Key: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$k")},
},
AmpersandTkn: &token.Token{
Value: []byte("&"),
},
Var: &ast.ExprVariable{
VarName: &ast.Identifier{Value: []byte("$v")},
},
Stmt: &ast.StmtStmtList{
Stmts: []ast.Vertex{
&ast.StmtNop{},
},
},
}
n.Accept(p)
expected := `foreach($a as$k=>&$v){;}`
actual := o.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestPrinterPrintStmtFunction(t *testing.T) { func TestPrinterPrintStmtFunction(t *testing.T) {
o := bytes.NewBufferString("") o := bytes.NewBufferString("")