Skip to main content

php_ast/
lib.rs

1//! AST type definitions and visitor infrastructure for the PHP parser.
2//!
3//! This crate provides:
4//! - The complete set of AST node types ([`ast`] module) — statements, expressions, declarations,
5//!   type hints, operators, and all other syntactic constructs for PHP 7.4–8.5, plus PHP 8.6
6//!   partial function application (`?`/`...` call-argument placeholders).
7//! - A [`Span`] type for tracking byte-offset ranges back to the source text.
8//! - A [`visitor`] module with the [`visitor::Visitor`] and [`visitor::ScopeVisitor`] traits for
9//!   depth-first AST traversal, plus free `walk_*` functions that drive the default recursion.
10//! - A [`fold`] module with the [`fold::Fold`] trait for arena-to-arena AST transformation.
11//! - An [`owned`] module with lifetime-free mirrors of every AST type, plus owned visitor and fold
12//!   traits.
13//! - A [`resolve::NameResolver`] that resolves names to fully qualified names using the current
14//!   namespace and `use` imports.
15//! - With the `serde` feature, a `fingerprint` module for structural AST fingerprints.
16//!
17//! # Quick start
18//!
19//! Parse a PHP file with `php-rs-parser` and then walk the AST with a custom visitor:
20//!
21//! ```
22//! use php_ast::visitor::{Visitor, walk_expr};
23//! use php_ast::ast::{Expr, ExprKind};
24//! use std::ops::ControlFlow;
25//!
26//! struct FunctionCallCounter(usize);
27//!
28//! impl<'arena, 'src> Visitor<'arena, 'src> for FunctionCallCounter {
29//!     type Break = ();
30//!     fn visit_expr(&mut self, expr: &Expr<'arena, 'src>) -> ControlFlow<()> {
31//!         if matches!(expr.kind, ExprKind::FunctionCall(_)) {
32//!             self.0 += 1;
33//!         }
34//!         walk_expr(self, expr)
35//!     }
36//! }
37//! ```
38//!
39//! # Semantic AST
40//!
41//! Equivalent spellings (`array()`/`[]`, `exit`/`die`, `(integer)`/`(int)`) share one node;
42//! formatting is dropped. Use [`Span`] to recover the original text.
43//!
44//! # Serialization
45//!
46//! With the `serde` feature (off by default), AST types implement `serde::Serialize`
47//! for debugging, snapshots and tooling, and the `fingerprint` module is available.
48//! The serialized shape is **not** covered by semver and may change in any release,
49//! for example when a node is renamed. `Deserialize` is not implemented.
50
51#![warn(missing_docs)]
52
53/// Arena-allocated AST node types.
54pub mod ast;
55#[cfg(feature = "serde")]
56pub mod fingerprint;
57pub mod fold;
58pub mod owned;
59pub mod resolve;
60/// Byte-offset source spans.
61pub mod span;
62/// Read-only AST traversal.
63pub mod visitor;
64
65pub use ast::*;
66pub use span::Span;
67
68/// The arena allocator behind [`ArenaVec`] and [`fold::Fold`], re-exported so
69/// callers use the same version.
70pub use bumpalo;