Skip to content

Port php-parser-in-go to Rust - #1

Merged
TomasVotruba merged 26 commits into
mainfrom
rust-port
Dec 3, 2026
Merged

TomasVotruba merged 26 commits into
mainfrom
rust-port

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Port of rectorphp/php-parser-in-go to Rust, to compare parse speed.

  • AST, visitor trait, traverser and dumper generated from the Go sources
  • hand-written lexers and PHP 5, 7 and 8 parsers; on syntax errors a port of the Go grammar tables reproduces Go's errors and recovery
  • printer, formatter, namespace and class resolvers, doc comments and typescope, with their Go tests ported
  • tests/fixtures: every source the Go tests parse, with Go's dump and errors; all match byte for byte
  • CI diffs Rust vs Go dumps on the whole Laravel corpus (8.4 and 7.4) and benchmarks both

Laravel, 18157 files, 4 cores:

Parser Avg (5 runs) Peak mem
Rust 820 ms 176 MB
Go 1776 ms 4428 MB

Printer writes a tree back as PHP source with Go's default token texts; Formatter lays out tokens and free-floating whitespace. Tests that need a parser are ignored until the parsers land.
…nalysis

Add a read-only walk over borrowed trees, store resolved names in node
attributes, and port the Go tests; tests that need the parser are ignored
until it lands.
Hand-written port of the Go ragel lexers (internal/php8 and internal/scanner)
sharing one implementation, with the Go scanner tests, the position builder
tests and a test-only token dumper for differential checks against Go.
The dumper approximated Go's IsPrint, so unassigned code points such as
U+0378 were printed raw instead of escaped as \u0378.
Port of the goyacc driver, tables and grammar actions of php7.y. It
reproduces the Go parser exactly, including error messages and error
recovery.
Hand-written recursive descent parser with precedence climbing for
expressions. On a syntax error, a recognizer driven by the goyacc tables
reproduces the Go error messages and recovery points, and a guided
reparse rebuilds the recovered tree.

Also nest absolute input paths under the dump output directory, as Go
filepath.Join does, and enable the PHP 5 printer tests.
Recursive descent for statements and precedence climbing for expressions build the same tree as the goyacc grammar. Files with a syntax error are re-parsed by the table-driven parser, which reports and recovers exactly like Go.
@TomasVotruba
TomasVotruba merged commit 94295d1 into main Dec 3, 2026
3 checks passed
@TomasVotruba
TomasVotruba deleted the rust-port branch December 3, 2026 08:47
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