Skip to main content

perl_parser_core/
lib.rs

1//! Core parser engine for Perl source code.
2//!
3//! `perl-parser-core` is the foundational crate that wires together the lexer,
4//! AST, error recovery, and position mapping into a single [`Parser`] entry
5//! point. Higher-level crates -- semantic analysis, workspace indexing, and the
6//! LSP server -- all build on top of this crate.
7//!
8//! # Key types
9//!
10//! | Type | Role |
11//! |------|------|
12//! | [`Parser`] | Recursive-descent parser; call [`Parser::parse`] to get an AST |
13//! | [`Node`] / [`NodeKind`] | AST node and its discriminant (re-exported from `perl-ast`) |
14//! | [`ParseError`] | Syntax error collected during parsing |
15//! | [`ParseOutput`] | AST + diagnostics bundle for IDE workflows |
16//! | [`Token`] / [`TokenKind`] | Lexer tokens consumed by the parser |
17//! | [`SourceLocation`] | Byte-offset span for every node |
18//!
19//! # Quick start
20//!
21//! ```rust
22//! use perl_parser_core::{Parser, Node, NodeKind};
23//!
24//! let mut parser = Parser::new("my $x = 42;");
25//! let ast = parser.parse().expect("should parse");
26//!
27//! // The root is always a Program node
28//! assert!(matches!(ast.kind, NodeKind::Program { .. }));
29//!
30//! // Non-fatal errors are collected, not returned as Err
31//! assert!(parser.errors().is_empty());
32//! ```
33//!
34//! For IDE workflows that need error-tolerant parsing, use
35//! [`Parser::parse_with_recovery`]:
36//!
37//! ```rust
38//! use perl_parser_core::Parser;
39//!
40//! let mut parser = Parser::new("if (");
41//! let output = parser.parse_with_recovery();
42//!
43//! // Always returns an AST (possibly with ERROR nodes)
44//! assert!(!output.diagnostics.is_empty());
45//! ```
46
47#![deny(unsafe_code)]
48#![deny(unreachable_pub)]
49#![warn(rust_2018_idioms)]
50#![warn(missing_docs)]
51#![cfg_attr(test, allow(clippy::panic, clippy::unwrap_used, clippy::expect_used))]
52#![allow(
53    clippy::too_many_lines,
54    clippy::module_name_repetitions,
55    clippy::cast_possible_truncation,
56    clippy::cast_sign_loss,
57    clippy::cast_precision_loss,
58    clippy::cast_possible_wrap,
59    clippy::must_use_candidate,
60    clippy::missing_errors_doc,
61    clippy::missing_panics_doc,
62    clippy::wildcard_imports,
63    clippy::enum_glob_use,
64    clippy::match_same_arms,
65    clippy::if_not_else,
66    clippy::struct_excessive_bools,
67    clippy::items_after_statements,
68    clippy::return_self_not_must_use,
69    clippy::unused_self,
70    clippy::collapsible_match,
71    clippy::collapsible_if,
72    clippy::only_used_in_recursion,
73    clippy::items_after_test_module,
74    clippy::while_let_loop,
75    clippy::single_range_in_vec_init,
76    clippy::arc_with_non_send_sync,
77    clippy::needless_range_loop,
78    clippy::result_large_err,
79    clippy::if_same_then_else,
80    clippy::should_implement_trait,
81    clippy::manual_flatten,
82    clippy::needless_raw_string_hashes,
83    clippy::single_char_pattern,
84    clippy::uninlined_format_args
85)]
86
87/// Builtin function signatures and metadata.
88pub use perl_lexer::builtins;
89/// Parser engine components and supporting utilities.
90pub mod engine;
91/// Normalized high-level constructs lowered from the parser AST.
92pub mod hir;
93/// Perl Intermediate Representation (PIR) for static analysis and tooling.
94pub mod pir;
95/// Syntax-level types absorbed from Wave D satellite crates.
96pub mod syntax;
97/// Token stream and trivia utilities for the parser.
98pub mod tokens;
99
100/// Index into the diagnostics array in [`ParseOutput`] (from `ast_v2`).
101pub use ast_v2::{DiagnosticId, MissingKind};
102/// Abstract Syntax Tree (AST) definitions for Perl parsing.
103pub use engine::ast;
104/// Experimental second-generation AST (work in progress).
105pub use engine::ast_v2;
106/// Parser context with error recovery support.
107pub use engine::parser_context;
108/// Pragma tracking for `use` and related directives.
109pub use engine::pragma_tracker;
110/// Parser for Perl quote and quote-like operators.
111pub use engine::quote_parser;
112/// Legacy module aliases for moved engine components.
113pub use engine::{error, parser, position};
114/// Parser utilities and helpers.
115pub use perl_lexer::tokenizer::util;
116/// Edit tracking for incremental parsing (internal module, previously `perl-edit`).
117pub use syntax::edit;
118/// Heredoc content collector with FIFO ordering and indent stripping (internal module, previously `perl-heredoc`).
119pub use syntax::heredoc as heredoc_collector;
120/// Secure workspace-relative path normalization (previously `perl-path-normalize`).
121pub use syntax::path_normalize;
122/// Workspace-bound path validation and traversal prevention (previously `perl-path-security`).
123pub use syntax::path_security;
124/// Percentile helpers for integer metric samples (previously `perl-percentile`).
125pub use syntax::percentile;
126/// Perl qualified-name parsing, splitting, and validation helpers (previously `perl-qualified-name`).
127pub use syntax::qualified_name;
128/// Canonical qw/q/qq operator content extractor shared across the workspace.
129pub use syntax::quote::{parse_quote_operator_content, parse_qw_words};
130/// Perl source-file classification helpers (previously `perl-source-file`).
131pub use syntax::source_file;
132/// Text-line cursor and boundary helpers (previously `perl-text-line`).
133pub use syntax::text_line;
134
135/// Recursive-descent parser -- the main entry point for parsing Perl source.
136pub use engine::parser::Parser;
137
138/// Error classification and recovery strategies for parse failures.
139pub use error::classifier as error_classifier;
140/// Error recovery helpers and strategies.
141pub use error::recovery as error_recovery;
142/// Result of an error recovery attempt.
143pub use error_recovery::RecoveryResult;
144
145/// Line indexing and position mapping utilities.
146pub mod line_index {
147    /// Fast lookup from byte offset to line number.
148    pub use perl_position_tracking::LineIndex;
149}
150/// Line ending detection for mixed-EOL source files.
151pub use position::{LineEnding, PositionMapper};
152
153/// Core AST types re-exported for convenience.
154pub use ast::{Node, NodeKind, SourceLocation};
155/// Recovery-salvage metrics and classifier for accuracy closeout reporting.
156pub use error::classifier::{RecoverySalvageMetrics, classify_recovery_salvage};
157/// Parse error, budget, and output types.
158pub use error::{
159    BudgetTracker, ParseBudget, ParseError, ParseOutput, ParseResult, RecoverySalvageClass,
160    RecoverySalvageProfile,
161};
162
163/// Builtin function signature lookup tables.
164pub use builtins::builtin_signatures;
165/// Perfect hash function (PHF) based builtin signature lookup.
166pub use builtins::builtin_signatures_phf;
167
168/// Token stream module for lexer-to-parser bridge.
169pub use tokens::token_stream;
170/// Lightweight token wrapper for AST integration.
171pub use tokens::token_wrapper;
172/// Trivia (whitespace and comments) representation.
173pub use tokens::trivia;
174/// Trivia-preserving parser and formatting utilities.
175pub use tokens::trivia_parser;
176
177/// Individual token, its classification, and the streaming iterator.
178pub use token_stream::{Token, TokenKind, TokenStream};
179/// Trivia types attached to AST nodes for formatting preservation.
180pub use trivia::{NodeWithTrivia, Trivia, TriviaToken};
181/// Trivia-preserving parser and source formatting helper.
182pub use trivia_parser::{TriviaPreservingParser, format_with_trivia};