php-parser
A production-grade, fault-tolerant, zero-copy PHP parser written in Rust.
Features
- Zero-Copy AST: Uses
bumpaloarena allocation for high performance and zero heap allocations for AST nodes. - Fault-Tolerant: Designed to never panic. It produces error nodes and recovers from syntax errors, making it suitable for IDEs and language servers.
- PHP 8.x Support: Targets compliance with modern PHP grammar.
- Safe: Handles mixed encodings and invalid UTF-8 gracefully by operating on byte slices (
&[u8]).
Install as library
If you want to use the latest version from the GitHub repository, add this to your Cargo.toml:
[]
= { = "https://github.com/wudi/php-parser" }
= "3.19.0"
Usage
Here is a basic example of how to parse a PHP script:
use Bump;
use Lexer;
use Parser;
S-Expression Output
You can output the AST in S-expression format for easier visualization:
use Bump;
use Lexer;
use Parser;
use SExprFormatter;
Gives the output:
(program
(class "Foo" (extends Bar) (implements Baz)
(members
(property public int = (integer 1))
(method "m" (params ())
(body
(return (variable "")))))))
Performance
Test file run-tests.php from php-src with 140KB size, here are the benchmark results:
)
)
Table comparing with nikic/PHP-Parser v5.6.2
| Parser | Language | Time (ms) |
|---|---|---|
| nikic/PHP-Parser | PHP | 33 |
| tree-sitter-php | C | 15 |
| php-parser | Rust | 0.67 |
Machine specs: Apple M1 Pro, 32GB RAM
Development
Running Tests
Run the full test suite:
Snapshot Tests
This project uses insta for snapshot testing. If you make changes to the parser that affect the AST output, you may need to review and accept the new snapshots:
Corpus Testing
To verify stability against real-world codebases (like WordPress or Laravel), use the corpus test runner:
Architecture
- Lexer: Operates on
&[u8]and handles PHP's complex lexical modes (Scripting, DoubleQuote, Heredoc). - Parser: A combination of Recursive Descent and Pratt parsing for expressions.
- AST: All nodes are allocated in a
Bumparena. Strings are stored as references to the original source (&'src [u8]) or arena-allocated slices.
License
This project is licensed under the MIT License. See the LICENSE file for details.