Rust PHP Parser
A fast, fault-tolerant PHP parser written in Rust. Produces a full typed AST with source spans, recovers from syntax errors, and covers PHP 8.0–8.5 syntax.
Quick Start
use parse;
let arena = new;
let result = parse;
println!;
for err in &result.errors
// Resolve byte offsets to line/column
let pos = result.source_map.offset_to_line_col;
API Reference
The three entry points you need for integration:
parse()/parse_versioned()— main parser entry points inphp-rs-parser; seecrates/php-parser/src/lib.rsVisitor/ScopeVisitor— AST traversal traits inphp-ast; seecrates/php-ast/src/visitor.rsfor the distinction between the twoParseErrorvariants — seecrates/php-parser/src/diagnostics.rsand docs/development/ERRORS.md for recovery behavior
Usage
Version-aware parsing
The parser targets PHP 8.5 by default. Use parse_versioned() to target an earlier version:
use ;
let arena = new;
let result = parse_versioned;
// Enums require PHP 8.1 — a VersionTooLow diagnostic is emitted.
assert!;
Supported versions: Php74, Php80, Php81, Php82, Php83, Php84, Php85.
Visitor API
Implement Visitor to walk the AST depth-first. Override only the node types you care about; the default implementations recurse into children automatically.
use ;
use *;
use ControlFlow;
Return ControlFlow::Break(()) to stop traversal early. Return ControlFlow::Continue(()) without calling walk_* to skip a subtree.
For scope-aware traversal (ScopeVisitor, ScopeWalker) and the PHPDoc parser, see docs/usage/VISITOR.md.
Pretty printer
let arena = new;
let result = parse;
let output = pretty_print;
// output == "echo 1 + 2;"
Use pretty_print_file to produce a complete file with a <?php\n\n prefix and trailing newline.
Architecture
Four crates, one workspace:
Source flows through Lexer → Parser → arena-allocated AST nodes. The lexer is lazy (tokens produced on demand with peeking slots); the parser is Pratt-based recursive descent with panic-mode error recovery.
Performance
This parser is optimised for modern PHP applications with full typing (PHP 7.4+, 8.x). It delivers the fastest performance on Symfony, Laravel, and other typed codebases.
The fastest full-featured PHP parser. For detailed analysis see docs/performance/. For comparative benchmarks against other PHP parsers see php-parser-benchmark.
Contributing
See CONTRIBUTING.md for build instructions, testing, and contributor guides. Full documentation is in the docs/ directory.
Acknowledgements
Inspired by and indebted to nikic/PHP-Parser — test corpus fixtures were adapted from its test suite. Thanks to the PHP community contributors.
License
BSD 3-Clause