Skip to content

Parse the PHP 8.5 pipe operator and (void) cast in php8 - #37

Merged
TomasVotruba merged 1 commit into
mainfrom
php8-pipe-void-cast
Nov 23, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
php8-pipe-void-cast

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Two PHP 8.5 syntaxes the php8 grammar rejected, found by parsing every rector-src test fixture:

Syntax Example
pipe operator $slug = $title |> trim(...) |> strtolower(...);
(void) cast (void) $repository->save($entity);

Mirrors zend_language_parser.y / zend_language_scanner.l of PHP-8.5:

Pipe

  • scanner: |> lexes to the new T_PIPE
  • grammar: expr T_PIPE expr, %left T_PIPE between comparison and concatenation, as in php-src - $a . $b |> f(...) === 'x' parses as (($a . $b) |> f(...)) === 'x'
  • AST: new ast.ExprBinaryPipe{Left, OpTkn, Right}

(void) cast

  • scanner: (void) (any case, inner whitespace) lexes to the new T_VOID_CAST
  • grammar: only as a statement T_VOID_CAST expr ';' and as a for loop expression, like php-src; $x = (void) f(); stays a syntax error
  • AST: new ast.ExprCastVoid{CastTkn, Expr}, wrapped in the usual StmtExpression

Both nodes are wired through the Visitor interface, visitor.Null, traverser, printer, formatter and dumper. T_PIPE and T_VOID_CAST are appended to pkg/token and the %token list, so existing token IDs do not move. The grammar conflict count is unchanged (3 shift/reduce, 7 reduce/reduce).

Regenerated with ragel, latest goyacc and stringer; regenerating the unchanged sources the same way reproduces the committed files byte for byte.

Note for implementers of ast.Visitor: two methods are added (ExprBinaryPipe, ExprCastVoid); visitors embedding visitor.Null need no change. Verified downstream: reco builds and its full test suite passes against this branch.

Tests: parse shape, left-associativity, precedence, void cast in for, void cast rejected as a value, print-back round trips, printer and formatter.

$value |> trim(...) and (void) foo(); were syntax errors.

The pipe is a new ExprBinaryPipe node at PHP 8.5's precedence - looser
than concatenation, tighter than comparison - and left-associative. The
(void) cast is a new ExprCastVoid node; as in PHP it is only allowed as a
statement or a for expression, not as a value. T_PIPE and T_VOID_CAST are
appended to the token list.
@TomasVotruba
TomasVotruba merged commit 93683bf into main Nov 23, 2026
5 checks passed
@TomasVotruba
TomasVotruba deleted the php8-pipe-void-cast branch November 23, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant