You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cast and clone operands reject match expressions: (int)match($x) {...} and clone match($x) {...} are parse errors #308
match expressions are rejected as the operand of a cast and of clone. These are valid PHP (8.0+), but the parser reports a syntax error for the whole file:
In a multi-line version, the tree has a MISSING ; right after (int)match ($u) (the statement ends too early), followed by an ERROR node that starts at the first arm of the match body.
Where it comes from (reading common/define-grammar.js on master; I did not try a fix or regenerate the parser)
match_expression (and yield_expression) are alternatives of expression, but they are not part of _unary_expression or primary_expression.
unary_op_expression takes $.expression as its argument, and error_suppression_expression is '@' $.expression, which is why -match, !match, @match and (int)@match work.
clone_expression takes $.primary_expression (or arguments), so clone match(...) {...} fails for the same reason.
So the cast (and clone) operand seems to need match_expression (and, for (int)yield, yield_expression).
Expected
No syntax error: (int)match(...) {...} parses as a cast of a match expression, the same way (int)$x or (int)foo() does.
Impact
A static analysis tool built on this grammar has to give up on the whole file when a single such expression exists.
I'll take a look when I get the chance, but feel free to submit PR if you have time
A static analysis tool built on this grammar has to give up on the whole file when a single such expression exists.
FYI: tree-sitter grammars are not suitable for static analyzers---they serve entirely different purposes. For PHP you'd be better off using PHPStan or Mago. If you're still wanting to build your own tools then look into PHP-Parser or the Mago package for parsing PHP code
Activity
calebdw commentedon Oct 20, 2026
I'll take a look when I get the chance, but feel free to submit PR if you have time
FYI: tree-sitter grammars are not suitable for static analyzers---they serve entirely different purposes. For PHP you'd be better off using PHPStan or Mago. If you're still wanting to build your own tools then look into PHP-Parser or the Mago package for parsing PHP code
test: cover match expressions as cast and clone operands
fix: accept general cast and clone operands
test: cover match expressions as cast and clone operands
fix: accept general cast and clone operands