A PHP parser written in Rust, a port of rectorphp/php-parser-in-go. It lexes and parses PHP 5, 7 and 8 source into the same AST as the Go parser, so both can be compared on speed.
Parsing the full Laravel framework (src/ + Composer vendor/, 18157 files) on 4 cores, average over 5 runs, in CI:
| Parser | Avg | Peak mem |
|---|---|---|
| php-parser-in-rust | 251 ms | 85 MB |
| php-parser-in-go | 1508 ms | 4429 MB |
| mago-syntax | 340 ms | 138 MB |
- The Go benchmark runs with the GC disabled, which explains most of its memory.
- php-parser-in-rust and mago-syntax allocate each tree in an arena.
- mago-syntax builds its own CST and has no PHP version setting. The others parse as PHP 8.3 into the Go AST.
CI reruns this on every push and every 12 hours, see the Summary of the Benchmark workflow. Run it locally on any corpus:
bench/compare.sh ./laravelWhat keeps it fast:
- the whole tree lives in one arena, so there is no allocation per node and nothing to free node by node
- the lexer allocates each token once; nodes hold a reference to it
- comments and strings are scanned with SIMD search (
memchr)
use php_parser::{parse, Arena, Config, version::Version};
use php_parser::visitor::dumper::Dumper;
// the arena owns the whole tree and frees it at once
let arena = Arena::new();
let source = b"<?php echo 'hi';";
let result = parse(&arena, source, Config { version: Some(Version::new(8, 3)) }).unwrap();
for error in &result.errors {
eprintln!("{error}");
}
if let Some(root) = &result.root {
print!("{}", Dumper::new().dump(root));
}parse(&arena, source, Config)- entry point, picks the PHP 5, 7 or 8 grammar by version.ast- AST nodes, one struct per Go node, wrapped in theNodeenum and allocated in anArena.visitor-Visitortrait, traverser, printer, dumper, formatter, namespace and class resolvers, doc comments.typescope- static type of value expressions inside a class.token,position,version,errors.
The dumper prints trees byte-identical to the Go dumper, so both parsers can be diffed:
tests/fixtures- every source the Go test suite parses, with the Go dump and errors;cargo testcompares them.tools/diff-corpus.sh <version> <dir>- dumps a whole corpus with the Go oracle (tools/go-oracle) and the Rust parser and diffs them. CI runs it on Laravel at PHP 8.4 and 7.4.
Valid files go through hand-written recursive descent parsers. A file with a syntax error is parsed again by a port of the Go grammar tables, so error messages and recovered trees match Go too.
Port of rectorphp/php-parser-in-go, which is based on z7zmey/php-parser.