Expand description
AST type definitions and visitor infrastructure for the PHP parser.
This crate provides:
- The complete set of AST node types (
astmodule) — statements, expressions, declarations, type hints, operators, and all other syntactic constructs for PHP 7.4–8.5, plus PHP 8.6 partial function application (?/...call-argument placeholders). - A
Spantype for tracking byte-offset ranges back to the source text. - A
visitormodule with thevisitor::Visitorandvisitor::ScopeVisitortraits for depth-first AST traversal, plus freewalk_*functions that drive the default recursion. - A
resolve::NameResolverthat resolves names to fully qualified names using the current namespace anduseimports.
§Quick start
Parse a PHP file with php-rs-parser and then walk the AST with a custom visitor:
use php_ast::visitor::{Visitor, walk_expr};
use php_ast::ast::{Expr, ExprKind};
use std::ops::ControlFlow;
struct FunctionCallCounter(usize);
impl<'arena, 'src> Visitor<'arena, 'src> for FunctionCallCounter {
fn visit_expr(&mut self, expr: &Expr<'arena, 'src>) -> ControlFlow<()> {
if matches!(expr.kind, ExprKind::FunctionCall(_)) {
self.0 += 1;
}
walk_expr(self, expr)
}
}§Serialization
With the serde feature (off by default), AST types implement serde::Serialize
for debugging, snapshots and tooling, and the fingerprint module is available.
The serialized shape is not covered by semver and may change in any release,
for example when a node is renamed. Deserialize is not implemented.
Re-exports§
Modules§
- ast
- Arena-allocated AST node types.
- fingerprint
- Position-independent fingerprints of AST nodes.
- fold
- Arena-to-arena AST transformation via the
Foldtrait. - owned
- Owned (lifetime-free) versions of all AST types.
- resolve
- Namespace-aware name resolution for
useimports. - span
- Byte-offset source spans.
- visitor
- Read-only AST traversal.