perl_ast/lib.rs
1//! Perl AST library -- typed syntax tree for Perl source code.
2//!
3//! This crate defines the Abstract Syntax Tree used by `perl-parser-core` and
4//! downstream analysis tools. Every parsed Perl construct is represented as a
5//! [`Node`] carrying a [`NodeKind`] discriminant and a [`SourceLocation`]
6//! (byte-offset span).
7//!
8//! # Modules
9//!
10//! - [`ast`] -- The primary AST used by the current recursive-descent parser.
11//! - [`v2`] -- Experimental second-generation AST with full position tracking
12//! for incremental parsing.
13//!
14//! # Quick start
15//!
16//! ```rust
17//! use perl_ast::{Node, NodeKind, SourceLocation};
18//!
19//! // Build a small AST by hand
20//! let loc = SourceLocation { start: 0, end: 2 };
21//! let num = Node::new(NodeKind::Number { value: "42".to_string() }, loc);
22//!
23//! assert_eq!(num.kind.kind_name(), "Number");
24//! assert_eq!(num.location.start, 0);
25//! assert_eq!(num.location.end, 2);
26//! ```
27//!
28//! In practice the AST is produced by the parser (requires `perl-parser-core`):
29//!
30//! ```rust,ignore
31//! use perl_parser_core::Parser;
32//! use perl_ast::NodeKind;
33//!
34//! let mut parser = Parser::new("my $x = 42;");
35//! let ast = parser.parse().expect("should parse");
36//! assert!(matches!(ast.kind, NodeKind::Program { .. }));
37//! ```
38//!
39//! # Traversal
40//!
41//! [`Node`] exposes `to_sexp()` for a tree-sitter-compatible S-expression and
42//! `count_nodes()` for a quick size metric. For deeper inspection, match on
43//! [`NodeKind`] variants and recurse into child nodes.
44
45pub mod ast;
46
47/// Incremental parsing AST types extracted into a dedicated microcrate.
48pub use perl_ast_v2 as v2;
49
50/// Primary AST node -- the building block of every syntax tree.
51pub use ast::{Node, NodeKind};
52/// Byte-offset span indicating where a node appears in source text.
53pub use perl_position_tracking::SourceLocation;