Skip to content

Commit

Permalink
Remove support for alternative array syntax in PHP 8 parser
Browse files Browse the repository at this point in the history
We cannot support both this syntax and property hooks. Drop
support for the alternative syntax in the PHP 8 parser. The
PHP 7 parser still supports it.
  • Loading branch information
nikic committed Jul 19, 2024
1 parent fadccea commit beba9c5
Show file tree
Hide file tree
Showing 11 changed files with 954 additions and 816 deletions.
4 changes: 4 additions & 0 deletions grammar/php.y
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,9 @@ callable_expr:
callable_variable:
simple_variable
| array_object_dereferenceable '[' optional_expr ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
#if PHP7
| array_object_dereferenceable '{' expr '}' { $$ = Expr\ArrayDimFetch[$1, $3]; }
#endif
| function_call
| array_object_dereferenceable T_OBJECT_OPERATOR property_name argument_list
{ $$ = Expr\MethodCall[$1, $3, $4]; }
Expand Down Expand Up @@ -1284,7 +1286,9 @@ static_member:
new_variable:
simple_variable
| new_variable '[' optional_expr ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
#if PHP7
| new_variable '{' expr '}' { $$ = Expr\ArrayDimFetch[$1, $3]; }
#endif
| new_variable T_OBJECT_OPERATOR property_name { $$ = Expr\PropertyFetch[$1, $3]; }
| new_variable T_NULLSAFE_OBJECT_OPERATOR property_name { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
| class_name T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
Expand Down
1,203 changes: 590 additions & 613 deletions lib/PhpParser/Parser/Php8.php

Large diffs are not rendered by default.

335 changes: 335 additions & 0 deletions test/code/parser/expr/alternative_array_syntax.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
Alternative array syntax
-----
<?php

$a{'b'};
$a{'b'}();
$a->b{'c'};
$a->b(){'c'};
A::$b{'c'};
A{0};
A::B{0};
new $array{'className'};
new $a->b{'c'}();
-----
!!version=7.4
array(
0: Stmt_Expression(
expr: Expr_ArrayDimFetch(
var: Expr_Variable(
name: a
)
dim: Scalar_String(
value: b
)
)
)
1: Stmt_Expression(
expr: Expr_FuncCall(
name: Expr_ArrayDimFetch(
var: Expr_Variable(
name: a
)
dim: Scalar_String(
value: b
)
)
args: array(
)
)
)
2: Stmt_Expression(
expr: Expr_ArrayDimFetch(
var: Expr_PropertyFetch(
var: Expr_Variable(
name: a
)
name: Identifier(
name: b
)
)
dim: Scalar_String(
value: c
)
)
)
3: Stmt_Expression(
expr: Expr_ArrayDimFetch(
var: Expr_MethodCall(
var: Expr_Variable(
name: a
)
name: Identifier(
name: b
)
args: array(
)
)
dim: Scalar_String(
value: c
)
)
)
4: Stmt_Expression(
expr: Expr_ArrayDimFetch(
var: Expr_StaticPropertyFetch(
class: Name(
name: A
)
name: VarLikeIdentifier(
name: b
)
)
dim: Scalar_String(
value: c
)
)
)
5: Stmt_Expression(
expr: Expr_ArrayDimFetch(
var: Expr_ConstFetch(
name: Name(
name: A
)
)
dim: Scalar_Int(
value: 0
)
)
)
6: Stmt_Expression(
expr: Expr_ArrayDimFetch(
var: Expr_ClassConstFetch(
class: Name(
name: A
)
name: Identifier(
name: B
)
)
dim: Scalar_Int(
value: 0
)
)
)
7: Stmt_Expression(
expr: Expr_New(
class: Expr_ArrayDimFetch(
var: Expr_Variable(
name: array
)
dim: Scalar_String(
value: className
)
)
args: array(
)
)
)
8: Stmt_Expression(
expr: Expr_New(
class: Expr_ArrayDimFetch(
var: Expr_PropertyFetch(
var: Expr_Variable(
name: a
)
name: Identifier(
name: b
)
)
dim: Scalar_String(
value: c
)
)
args: array(
)
)
)
)
-----
<?php

$a{'b'};
$a{'b'}();
$a->b{'c'};
$a->b(){'c'};
A::$b{'c'};
A{0};
A::B{0};
new $array{'className'};
new $a->b{'c'}();
-----
Syntax error, unexpected '{' from 3:3 to 3:3
Syntax error, unexpected '{' from 4:3 to 4:3
Syntax error, unexpected '{' from 5:6 to 5:6
Syntax error, unexpected '{' from 6:8 to 6:8
Syntax error, unexpected '{' from 7:6 to 7:6
Syntax error, unexpected '{' from 8:2 to 8:2
Syntax error, unexpected '{' from 9:5 to 9:5
Syntax error, unexpected '{' from 10:11 to 10:11
Syntax error, unexpected '{' from 11:10 to 11:10
array(
0: Stmt_Expression(
expr: Expr_Variable(
name: a
)
)
1: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_String(
value: b
)
)
)
)
2: Stmt_Expression(
expr: Expr_Variable(
name: a
)
)
3: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_String(
value: b
)
)
)
)
4: Stmt_Expression(
expr: Expr_PropertyFetch(
var: Expr_Variable(
name: a
)
name: Identifier(
name: b
)
)
)
5: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_String(
value: c
)
)
)
)
6: Stmt_Expression(
expr: Expr_MethodCall(
var: Expr_Variable(
name: a
)
name: Identifier(
name: b
)
args: array(
)
)
)
7: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_String(
value: c
)
)
)
)
8: Stmt_Expression(
expr: Expr_StaticPropertyFetch(
class: Name(
name: A
)
name: VarLikeIdentifier(
name: b
)
)
)
9: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_String(
value: c
)
)
)
)
10: Stmt_Expression(
expr: Expr_ConstFetch(
name: Name(
name: A
)
)
)
11: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_Int(
value: 0
)
)
)
)
12: Stmt_Expression(
expr: Expr_ClassConstFetch(
class: Name(
name: A
)
name: Identifier(
name: B
)
)
)
13: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_Int(
value: 0
)
)
)
)
14: Stmt_Expression(
expr: Expr_New(
class: Expr_Variable(
name: array
)
args: array(
)
)
)
15: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_String(
value: className
)
)
)
)
16: Stmt_Expression(
expr: Expr_New(
class: Expr_PropertyFetch(
var: Expr_Variable(
name: a
)
name: Identifier(
name: b
)
)
args: array(
)
)
)
17: Stmt_Block(
stmts: array(
0: Stmt_Expression(
expr: Scalar_String(
value: c
)
)
)
)
)
17 changes: 1 addition & 16 deletions test/code/parser/expr/fetchAndCall/funcCall.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ ${'a'}();
$$a();
$$$a();
$a['b']();
$a{'b'}();
$a->b['c']();

// array dereferencing
Expand Down Expand Up @@ -87,20 +86,6 @@ array(
)
)
6: Stmt_Expression(
expr: Expr_FuncCall(
name: Expr_ArrayDimFetch(
var: Expr_Variable(
name: a
)
dim: Scalar_String(
value: b
)
)
args: array(
)
)
)
7: Stmt_Expression(
expr: Expr_FuncCall(
name: Expr_ArrayDimFetch(
var: Expr_PropertyFetch(
Expand All @@ -119,7 +104,7 @@ array(
)
)
)
8: Stmt_Expression(
7: Stmt_Expression(
expr: Expr_ArrayDimFetch(
var: Expr_FuncCall(
name: Name(
Expand Down
Loading

0 comments on commit beba9c5

Please sign in to comment.