Expand description
Fast, fault-tolerant PHP parser that produces a fully typed AST.
This crate parses PHP source code (PHP 8.0–8.5) into a php_ast::Program
tree, recovering from syntax errors so that downstream tools always receive
a complete AST.
§Quick start
let arena = bumpalo::Bump::new();
let result = php_rs_parser::parse(&arena, "<?php echo 'hello';");
assert!(result.errors.is_empty());§Version-aware parsing
Use parse_versioned to target a specific PHP version. Syntax that
requires a higher version is still parsed into the AST, but a
diagnostics::ParseError::VersionTooLow diagnostic is emitted.
let arena = bumpalo::Bump::new();
let result = php_rs_parser::parse_versioned(
&arena,
"<?php enum Status { case Active; }",
php_rs_parser::PhpVersion::Php80,
);
assert!(!result.errors.is_empty()); // enums require PHP 8.1§Reusing arenas across re-parses (LSP usage)
Use ParserContext to avoid allocator churn when the same document is
re-parsed on every edit. The context owns a bumpalo::Bump arena and resets
it in O(1) before each parse, reusing the backing memory once it has grown
to a stable size.
let mut ctx = php_rs_parser::ParserContext::new();
let result = ctx.reparse("<?php echo 1;");
assert!(result.errors.is_empty());
drop(result); // must be dropped before the next reparse
let result = ctx.reparse("<?php echo 2;");
assert!(result.errors.is_empty());Re-exports§
pub use version::PhpVersion;
Modules§
- diagnostics
- instrument
- Lightweight instrumentation for profiling array parsing, expression parsing, and statement parsing.
- phpdoc
- PHPDoc comment parser.
- source_
map - version
Structs§
- Parse
Result - The result of parsing a PHP source string.
- Parser
Context - A reusable parse context that keeps a
bumpalo::Bumparena alive between re-parses, resetting it (O(1)) instead of dropping and reallocating.
Functions§
- parse
- Parse PHP
sourceusing the latest supported PHP version (currently 8.5). - parse_
versioned - Parse
sourcetargeting the given PHPversion.