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/// Syntax-level types absorbed from Wave D satellite crates.
94pub mod syntax;
95/// Token stream and trivia utilities for the parser.
96pub mod tokens;
97
98/// Index into the diagnostics array in [`ParseOutput`] (from `ast_v2`).
99pub use ast_v2::{DiagnosticId, MissingKind};
100/// Abstract Syntax Tree (AST) definitions for Perl parsing.
101pub use engine::ast;
102/// Experimental second-generation AST (work in progress).
103pub use engine::ast_v2;
104/// Parser context with error recovery support.
105pub use engine::parser_context;
106/// Pragma tracking for `use` and related directives.
107pub use engine::pragma_tracker;
108/// Parser for Perl quote and quote-like operators.
109pub use engine::quote_parser;
110/// Legacy module aliases for moved engine components.
111pub use engine::{error, parser, position};
112/// Parser utilities and helpers.
113pub use perl_lexer::tokenizer::util;
114/// Edit tracking for incremental parsing (internal module, previously `perl-edit`).
115pub use syntax::edit;
116/// Heredoc content collector with FIFO ordering and indent stripping (internal module, previously `perl-heredoc`).
117pub use syntax::heredoc as heredoc_collector;
118/// Secure workspace-relative path normalization (previously `perl-path-normalize`).
119pub use syntax::path_normalize;
120/// Workspace-bound path validation and traversal prevention (previously `perl-path-security`).
121pub use syntax::path_security;
122/// Percentile helpers for integer metric samples (previously `perl-percentile`).
123pub use syntax::percentile;
124/// Perl qualified-name parsing, splitting, and validation helpers (previously `perl-qualified-name`).
125pub use syntax::qualified_name;
126/// Perl source-file classification helpers (previously `perl-source-file`).
127pub use syntax::source_file;
128/// Text-line cursor and boundary helpers (previously `perl-text-line`).
129pub use syntax::text_line;
130
131/// Recursive-descent parser -- the main entry point for parsing Perl source.
132pub use engine::parser::Parser;
133
134/// Error classification and recovery strategies for parse failures.
135pub use error::classifier as error_classifier;
136/// Error recovery helpers and strategies.
137pub use error::recovery as error_recovery;
138/// Result of an error recovery attempt.
139pub use error_recovery::RecoveryResult;
140
141/// Line indexing and position mapping utilities.
142pub mod line_index {
143    /// Fast lookup from byte offset to line number.
144    pub use perl_position_tracking::LineIndex;
145}
146/// Line ending detection for mixed-EOL source files.
147pub use position::{LineEnding, PositionMapper};
148
149/// Core AST types re-exported for convenience.
150pub use ast::{Node, NodeKind, SourceLocation};
151/// Recovery-salvage metrics and classifier for accuracy closeout reporting.
152pub use error::classifier::{RecoverySalvageMetrics, classify_recovery_salvage};
153/// Parse error, budget, and output types.
154pub use error::{
155    BudgetTracker, ParseBudget, ParseError, ParseOutput, ParseResult, RecoverySalvageClass,
156    RecoverySalvageProfile,
157};
158
159/// Builtin function signature lookup tables.
160pub use builtins::builtin_signatures;
161/// Perfect hash function (PHF) based builtin signature lookup.
162pub use builtins::builtin_signatures_phf;
163
164/// Token stream module for lexer-to-parser bridge.
165pub use tokens::token_stream;
166/// Lightweight token wrapper for AST integration.
167pub use tokens::token_wrapper;
168/// Trivia (whitespace and comments) representation.
169pub use tokens::trivia;
170/// Trivia-preserving parser and formatting utilities.
171pub use tokens::trivia_parser;
172
173/// Individual token, its classification, and the streaming iterator.
174pub use token_stream::{Token, TokenKind, TokenStream};
175/// Trivia types attached to AST nodes for formatting preservation.
176pub use trivia::{NodeWithTrivia, Trivia, TriviaToken};
177/// Trivia-preserving parser and source formatting helper.
178pub use trivia_parser::{TriviaPreservingParser, format_with_trivia};