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.
Includes a corpus of test fixtures adapted from the nikic/PHP-Parser test suite.
Note: The parser targets PHP 8.5 by default. Use
parse_versioned()to target an earlier version.
Architecture
Cargo workspace with four crates:
Usage
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;
Version-aware parsing
Target a specific PHP version to catch version-gated syntax:
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.
Scope-aware traversal
ScopeVisitor and ScopeWalker provide zero-allocation lexical scope context — namespace, class name, and function/method name — at every node:
use ;
use *;
use ControlFlow;
let arena = new;
let result = parse;
let mut walker = new;
let _ = walker.walk;
// walker.into_inner().methods == ["Foo::bar"]
Scope fields:
namespace: Option<Cow<'src, str>>— current namespace,Nonein the global namespaceclass_name: Option<&'src str>— enclosing class/interface/trait/enum name,Noneoutside or in anonymous classesfunction_name: Option<&'src str>— enclosing named function/method name,Nonein closures/arrow functions
Pretty printer
let arena = new;
let result = parse;
let output = pretty_print;
// output == "echo 1 + 2;"
pretty_print_file prepends <?php\n\n and appends a trailing newline.
PHPDoc parser
let tags = parse;
Produces typed PhpDocTag variants for @param, @return, @var, @throws, @template, @property, @method, @deprecated, and Psalm/PHPStan annotations. Doc comments are attached to function, class, method, property, and constant AST nodes.
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.
Testing
Fixture files live in crates/php-parser/tests/fixtures/. All fixtures are validated against php -l in CI across PHP 8.2–8.5. Fixtures using version-gated syntax must include ===config=== with min_php=X.Y.
Documentation
Full documentation is in the docs/ directory:
- docs/INDEX.md — Documentation index
- docs/architecture/ — Design and roadmap
- docs/performance/ — Performance analysis and profiling
- docs/development/CHANGELOG.md — Release history
License
BSD 3-Clause