A Tree-sitter based PHP parser for Python, featuring compatibility with phply.
Project description
Bashe
Bashe (巴蛇, pronounced like "parser") is a fast, tree-sitter based PHP parser for Python, with optional phply-compatible AST output. The name comes from the legendary giant snake in Chinese mythology.
Features
- Parses PHP source into a structured AST using tree-sitter
- Supports PHP 7 features: scalar types, return type declarations,
null coalescing (
??), spaceship operator (<=>) - Supports PHP 8 features: constructor property promotion, named
arguments, nullsafe operator (
?->), match expressions, union types - Optional
phply.phpparse-compatible output — justpip install bashe[phply] - Minimal dependencies when phply is not needed
Installation
Python 3.10+ is supported; 3.13+ (with uv) is recommended for the best experience.
# Core parser only (tree-sitter + tree-sitter-php)
uv pip install bashe
# With phply compatibility layer
uv pip install bashe[phply]
Quick Start
from bashe import Bashe
parser = Bashe()
ast = parser.parse('<?php echo "hello, world!"; ?>')
print(ast)
For phply-compatible AST nodes (phpast.Variable, phpast.FunctionCall, etc.):
uv pip install bashe[phply]
from bashe import Bashe
parser = Bashe()
nodes = parser.parse(
'<?php namespace Foo; class Bar { function baz() { echo __METHOD__; } } ?>',
filename="/path/to/file.php",
)
# All magic constants (__FILE__, __DIR__, __NAMESPACE__, __CLASS__,
# __FUNCTION__, __METHOD__, __TRAIT__) are resolved at parse time —
# no need for resolve_magic_constants().
Performance
Bashe is approximately 2× faster than phply's lexer-based parser on equivalent inputs.
| Parser | 3.10 (10k) | Ratio (3.10) | 3.13 (10k) | Ratio (3.13) |
|---|---|---|---|---|
| bashe | 2.46s | 1.00× | 1.73s | 1.00× |
| phply | 5.76s | 2.34× | 3.66s | 2.12× |
_Benchmark: parsing bench.php (multi-line PHP with includes, evals,
function definitions) 10,000 times on CPython 3.10 and 3.13
Development
# Install dev dependencies (pytest, ipykernel, phply)
uv pip install -e ".[dev]"
# Run tests
uv run pytest
License
MIT — see LICENSE.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bashe-0.1.2.tar.gz.
File metadata
- Download URL: bashe-0.1.2.tar.gz
- Upload date:
- Size: 26.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b12cdcc0f2c42c1d2cd66d96a8bda2a8dffe5e7d7c64e5f773434fba118cfdcf
|
|
| MD5 |
f25841a9a5a940ddd0ee416cf5614e5d
|
|
| BLAKE2b-256 |
e3f64de9b0d402a867ccb9a82ffe6d22dc13c15b6f8db9dbb262bb03ee5cdb05
|
File details
Details for the file bashe-0.1.2-py3-none-any.whl.
File metadata
- Download URL: bashe-0.1.2-py3-none-any.whl
- Upload date:
- Size: 27.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.12 {"installer":{"name":"uv","version":"0.11.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edf9598bc8ebac004fc053cedfc731969c9ab03eaf6cd836d42182a3f42e2a6b
|
|
| MD5 |
eda011eafb5689c5db4e654f08625e11
|
|
| BLAKE2b-256 |
eb778fd3000ad75e5d72a474c4c72dfd4f79a8485f7e7ef95b6face7d11dd956
|