Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
07746e7
Scaffold Rust port: generated AST, visitor, traverser, dumper, Go ora…
TomasVotruba Dec 2, 2026
68197b3
Add MIT license
TomasVotruba Dec 2, 2026
8282060
Add test, Go parity and benchmark workflows
TomasVotruba Dec 2, 2026
9890021
Port printer and formatter visitors with their tests
TomasVotruba Dec 2, 2026
ac7c2bc
Port namespace resolver, class resolver, doc comment and type scope a…
TomasVotruba Dec 2, 2026
b72c4b7
Merge branch 'worktree-agent-a42f7116f786eb778'
TomasVotruba Dec 2, 2026
9d8e4e3
Fix lints and formatting
TomasVotruba Dec 2, 2026
36bcafb
Merge resolvers and typescope port
TomasVotruba Dec 2, 2026
356e6d1
Port the php8 and legacy lexers
TomasVotruba Dec 2, 2026
bf5df7a
Merge main
TomasVotruba Dec 2, 2026
bae3cb6
Let the token dumper skip inputs the Go lexer never finishes
TomasVotruba Dec 2, 2026
6812b1b
Merge branch 'worktree-agent-a346c9fb1be800ac3' into rust-port
TomasVotruba Dec 2, 2026
e85b87c
Drop duplicate dead_code allow
TomasVotruba Dec 2, 2026
54d0ba9
Port Go strconv.IsPrint exactly for dumped byte values
TomasVotruba Dec 2, 2026
62c1d29
Add php7 table-driven parser ported from the goyacc output
TomasVotruba Dec 2, 2026
f323c82
Add hand-written PHP 8 parser
TomasVotruba Dec 2, 2026
17fe590
Satisfy clippy and enable tests that need the PHP 8 parser
TomasVotruba Dec 2, 2026
57c2fb6
Add PHP 5 parser
TomasVotruba Dec 2, 2026
cad953e
Match Go's strconv.IsPrint exactly and keep dump output under the out…
TomasVotruba Dec 2, 2026
b9ee394
Merge PHP 5 parser
TomasVotruba Dec 2, 2026
5c621ba
Add hand-written php7 parser with table-driven fallback on syntax errors
TomasVotruba Dec 2, 2026
d6bdc4a
Merge PHP 7 parser
TomasVotruba Dec 2, 2026
176186f
Enable the last tests that needed a parser
TomasVotruba Dec 2, 2026
58cd53f
Document usage and benchmark results
TomasVotruba Dec 2, 2026
92fcfee
Benchmark mago-syntax next to the Rust and Go parsers
TomasVotruba Dec 2, 2026
2b58d6e
Show the mago-syntax comparison in the README
TomasVotruba Dec 2, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
44 changes: 44 additions & 0 deletions .github/workflows/benchmark.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Benchmark

on:
pull_request: null
push:
branches:
- main
schedule:
# every 12 hours
- cron: '0 */12 * * *'

jobs:
corpus:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
with:
workspaces: |
.
bench/mago
- uses: actions/setup-go@v5
with:
go-version: "1.26.6"
cache: false

- name: Clone + install Laravel corpus
run: |
git clone --depth 1 https://github.com/laravel/framework laravel
composer install --no-interaction --no-progress --ignore-platform-reqs --working-dir=laravel
# drop intentionally-broken PHP fixtures
rm -rf laravel/tests
find laravel/vendor -depth -type d -name tests -exec rm -rf {} +

- name: Run benchmark (php-parser-in-rust vs php-parser-in-go vs mago-syntax)
run: |
{
echo "## Corpus benchmark"
echo ""
echo "Parsing the full Laravel framework (\`src/\` + Composer \`vendor/\`)."
echo ""
bench/compare.sh laravel 5
} | tee -a "$GITHUB_STEP_SUMMARY"
43 changes: 43 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Tests

on:
pull_request: null
push:
branches:
- main

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2

- run: cargo fmt --check
- run: cargo clippy --all-targets -- -D warnings
- run: cargo test

# parse the Laravel corpus with both parsers and require identical dumps
go_parity:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- uses: actions/setup-go@v5
with:
go-version: "1.26.6"
cache-dependency-path: tools/go-oracle/go.sum

- name: Clone + install Laravel corpus
run: |
git clone --depth 1 https://github.com/laravel/framework laravel
composer install --no-interaction --no-progress --ignore-platform-reqs --working-dir=laravel
rm -rf laravel/tests
find laravel/vendor -depth -type d -name tests -exec rm -rf {} +

- run: tools/diff-corpus.sh 8.4 laravel
- run: tools/diff-corpus.sh 7.4 laravel
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/.claude
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "php_parser"
version = "0.1.0"
edition = "2024"

[dependencies]

[profile.release]
codegen-units = 1
lto = true
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
# php-parser-in-rust

A PHP parser written in Rust, a port of [rectorphp/php-parser-in-go](https://github.com/rectorphp/php-parser-in-go). It lexes and parses PHP 5, 7 and 8 source into the same AST, so both can be compared on speed.
A PHP parser written in Rust, a port of [rectorphp/php-parser-in-go](https://github.com/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.

Work in progress.
## Benchmark

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 | 1186 ms | 153 MB |
| [php-parser-in-go](https://github.com/rectorphp/php-parser-in-go) | 2557 ms | 4428 MB |
| [mago-syntax](https://crates.io/crates/mago-syntax) | 536 ms | 151 MB |

- The Go benchmark runs with the GC disabled, which explains most of its memory.
- mago-syntax allocates each tree in an arena and builds its own CST; it 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:

```bash
bench/compare.sh ./laravel
```

## Usage

```rust
use php_parser::{parse, Config, version::Version};
use php_parser::visitor::dumper::Dumper;

let source = b"<?php echo 'hi';";
let result = parse(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));
}
```

## Modules

- `parse(source, Config)` - entry point, picks the PHP 5, 7 or 8 grammar by version.
- `ast` - AST nodes, one struct per Go node, wrapped in the `Node` enum.
- `visitor` - `Visitor` trait, traverser, printer, dumper, formatter, namespace and class resolvers, doc comments.
- `typescope` - static type of value expressions inside a class.
- `token`, `position`, `version`, `errors`.

## Same output as Go

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 test` compares 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.

## Credits

Port of [rectorphp/php-parser-in-go](https://github.com/rectorphp/php-parser-in-go), which is based on [z7zmey/php-parser](https://github.com/z7zmey/php-parser).
53 changes: 53 additions & 0 deletions bench/compare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Parse every .php file of a corpus with php-parser-in-rust, php-parser-in-go and
# mago-syntax, and print a markdown table of average wall-clock and peak memory:
# bench/compare.sh <dir> [runs]
set -euo pipefail

dir=$1
runs=${2:-5}
root=$(cd "$(dirname "$0")/.." && pwd)
bin="$root/target/bench"
mkdir -p "$bin"

cargo build --release --quiet --manifest-path "$root/Cargo.toml" --bin benchmark
cp "$root/target/release/benchmark" "$bin/rust"

cargo build --release --quiet --manifest-path "$root/bench/mago/Cargo.toml"
cp "$root/bench/mago/target/release/bench-mago" "$bin/mago"

if [ ! -d "$bin/php-parser-in-go" ]; then
git clone --quiet --depth 1 https://github.com/rectorphp/php-parser-in-go "$bin/php-parser-in-go"
fi
(cd "$bin/php-parser-in-go" && go build -o "$bin/go" ./benchmark)

# average wall-clock in ms, as reported by the benchmark itself
average() {
total=0
for _ in $(seq 1 "$runs"); do
ms=$("$@" | awk '{print $5}')
total=$((total + ms))
done
echo $((total / runs))
}

# peak resident set size in MB of a single run
peak_memory() {
/usr/bin/time -v "$@" 2> "$bin/mem.log" > /dev/null
awk '/Maximum resident set size/{printf "%d", $NF/1024}' "$bin/mem.log"
}

files=$("$bin/rust" "$dir" | awk '{print $2}')

echo "Parsing $files files on $(nproc) cores, average over $runs runs."
echo ""
echo "| Parser | Avg | Peak mem |"
echo "|:-------|----:|---------:|"
for parser in rust go mago; do
case $parser in
rust) name="php-parser-in-rust" ;;
go) name="php-parser-in-go" ;;
mago) name="mago-syntax" ;;
esac
echo "| $name | $(average "$bin/$parser" "$dir") ms | $(peak_memory "$bin/$parser" "$dir") MB |"
done
1 change: 1 addition & 0 deletions bench/mago/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Loading
Loading