Skip to main content

A Tree-sitter based PHP parser (Rust + PyO3)

Project description

Bashe

Bashe logo

GitHub release license python badge rust badge

Bashe (巴蛇) is a fast, tree-sitter based PHP parser for Python, powered by Rust + PyO3. It parses PHP source into a clean, structured AST with full PHP 7/8 support — and runs 5× faster than phply.

Features

  • Parses PHP into a structured AST via tree-sitter
  • Full PHP 7 support: scalar types, return type declarations, null coalescing (??), spaceship operator (<=>)
  • Full PHP 8 support: constructor property promotion, named arguments, nullsafe operator (?->), match expressions, union types
  • Magic constants (__FILE__, __DIR__, __NAMESPACE__, __FUNCTION__, etc.) resolved during parsing
  • Built on Rust/PyO3 — tree-sitter parsing and AST translation happen entirely in native code
  • Known limitation: curly-brace string offset ($str{0}) is not supported; this syntax was deprecated in PHP 7.4 and removed in PHP 8.0. Use bracket offset ($str[0]) instead

Installation

Python 3.10+ is supported.

pip install bashe

Quick Start

from bashe import Bashe

parser = Bashe()
ast = parser.parse('<?php echo "hello, world!"; ?>')
print(ast)

# Magic constants are resolved automatically
nodes = parser.parse(
    '<?php namespace Foo; class Bar { function baz() { echo __METHOD__; } } ?>',
    filename="/path/to/file.php",
)

Performance

Bashe is approximately 5× faster than phply's pure-Python parser.

Parser Time (10k) Ratio
bashe 0.75s 1.00×
phply 3.64s 4.85×

Benchmark: parsing bench.php (multi-line PHP with includes, evals, function definitions) 10,000 times on CPython 3.13.

Migrating from phply

Bashe is a drop-in replacement for phply:

# Before (phply)
from phply.phpparse import make_parser
parser = make_parser()
ast = parser.parse(code)

# After (bashe)
from bashe import Bashe
parser = Bashe()
ast = parser.parse(code)

Bashe uses its own fast native AST types (Variable, FunctionCall, Function, Class, etc.) — no dependency on phply.

Development

# Install dependencies and dev tools
uv sync --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

bashe-0.2.0.tar.gz (1.0 MB view details)

Uploaded Source

File details

Details for the file bashe-0.2.0.tar.gz.

File metadata

  • Download URL: bashe-0.2.0.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","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

Hashes for bashe-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3472ebc4a5dd0342ca7c1da750fae904542aaa089e4d3925fbba5788369cb6d3
MD5 786b204851a3bbb8d7bcfee82a1fecba
BLAKE2b-256 284ee04992819fcce912362d99ad7abe3515ee7c60e1786da94bcd6230605db3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page