tree-sitter-php

Incremental PHP grammar for the tree-sitter parsing library

Library
Cargo
v0.24.2
232stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
58/100Fair
Development Activity44
Maintenance28
Community80
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
72/100Good
Architecture76
Code Quality78
Innovation65
Learning Curve70

tree-sitter-php is the official PHP grammar for tree-sitter, the incremental parsing library used by editors and developer tools to build and maintain concrete syntax trees as source code changes. It exposes both a full PHP grammar (with the <?php tag handling) and a php-only variant for embedded/inline PHP contexts, plus bundled queries for syntax highlighting, injections, and symbol tagging.

Because tree-sitter grammars re-parse only the changed portion of a file, tools built on top of tree-sitter-php get fast, incremental syntax trees suitable for editor-grade features — highlighting, code folding, structural navigation — without re-parsing the entire file on every keystroke.

What You Get

  • A complete PHP grammar (LANGUAGE_PHP) covering the <?php ... ?> tag syntax
  • A php-only grammar variant (LANGUAGE_PHP_ONLY) for contexts without PHP tags
  • Bundled node-types.json metadata describing the grammar’s AST node shapes
  • Prebuilt tree-sitter queries for syntax highlighting, language injection, and symbol tagging
  • Official bindings for multiple ecosystems: Rust, Node.js, Python, Go, C, and Swift

Common Use Cases

  • Powering PHP syntax highlighting in editors and terminal tools built on tree-sitter
  • Building code-navigation features (go-to-definition, outline view) for PHP files
  • Static analysis or linting tools that need a concrete PHP syntax tree
  • Embedding incremental PHP parsing inside a larger multi-language tooling platform

Under The Hood

Architecture: The grammar itself is defined in JavaScript (php/grammar.js, php_only/grammar.js) using tree-sitter’s grammar DSL, then compiled ahead of time into a generated C parser (php/src/parser.c, ~196k lines) that ships in the published crate. The Rust binding (bindings/rust/lib.rs, build.rs) links this C parser via extern "C" FFI and wraps the two entry points (tree_sitter_php, tree_sitter_php_only) as safe LanguageFn constants, following the same shape as every other official tree-sitter grammar crate.

Tech Stack: The repo is a genuine polyglot: JavaScript for the grammar DSL/tooling, generated C for the actual parser, and native bindings for Rust, Node (binding.gyp), Python (setup.py/pyproject.toml), Go (go.mod), Swift (Package.swift), and CMake for C consumers. The Rust crate’s only runtime dependency is tree-sitter-language, with cc as a build-time dependency to compile the bundled C source.

Code Quality: The Rust binding includes inline doctest examples and a #[cfg(test)] module exercising both the full and php-only grammars end-to-end (parsing sample PHP and asserting no syntax errors). CI badges in the README indicate automated testing across the maintained language bindings; the grammar itself is maintained by the core tree-sitter organization with community PRs for edge-case PHP syntax.

API Design: The public API is intentionally minimal — two LanguageFn constants plus query string constants for highlighting/injections/tags — mirroring the tree-sitter ecosystem convention so switching between language grammars requires no API relearning. Getting started requires only tree_sitter::Parser::set_language(), but effective use still assumes familiarity with the broader tree-sitter query and node-traversal API, which lives in a separate crate.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search