Skip to main content

A Tree-sitter based PHP parser for Python, featuring compatibility with phply.

Project description

Bashe

Bashe logo

GitHub release license python version badge

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 — just pip 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

bashe-0.1.1.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bashe-0.1.1-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bashe-0.1.1.tar.gz
  • Upload date:
  • Size: 25.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

Hashes for bashe-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e0ef4d1d3bef94ec28838cff44a626eb891cc3fd315290f1eaf1262b9d1f524c
MD5 d28439b9800dc160f3cfd9086088a78f
BLAKE2b-256 1411b7081fa5dc06696159248c94139f90ac3b25f355768c23b4630d66d6c500

See more details on using hashes here.

File details

Details for the file bashe-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: bashe-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 26.1 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

Hashes for bashe-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a59941101640e35b6a317d3ebd22aa5e7031c50551122d46e7fcda3f34a4ff0e
MD5 82e04e35ebe8b4ff4f4e4b04a3cbe63e
BLAKE2b-256 688070a4bfaabb1a6291c3a0c5652540fe6c5bd7f5f655c43bad11d34ffc7974

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