Skip to main content

perl_parser/
lib.rs

1//! # perl-parser — Production-grade Perl parser and Language Server Protocol engine
2//!
3//! A comprehensive Perl parser built on recursive descent principles, providing robust AST
4//! generation, LSP feature providers, workspace indexing, and test-driven development support.
5//!
6//! ## Key Features
7//!
8//! - **Tree-sitter Compatible**: AST with kinds, fields, and position tracking compatible with tree-sitter grammar
9//! - **Comprehensive Parsing**: ~100% edge case coverage for Perl 5.8-5.40 syntax
10//! - **LSP Integration**: Full Language Server Protocol feature set (100% compliance, LSP 3.18)
11//! - **TDD Workflow**: Intelligent test generation with return value analysis
12//! - **Incremental Parsing**: Efficient re-parsing for real-time editing
13//! - **Error Recovery**: Graceful handling of malformed input with detailed diagnostics
14//! - **Workspace Navigation**: Cross-file symbol resolution and reference tracking
15//!
16//! ## Quick Start
17//!
18//! ### Basic Parsing
19//!
20//! ```rust
21//! use perl_parser::Parser;
22//!
23//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
24//! let code = r#"sub hello { print "Hello, world!\n"; }"#;
25//! let mut parser = Parser::new(code);
26//!
27//! match parser.parse() {
28//!     Ok(ast) => {
29//!         println!("AST: {}", ast.to_sexp());
30//!         println!("Parsed {} nodes", ast.count_nodes());
31//!     }
32//!     Err(e) => eprintln!("Parse error: {}", e),
33//! }
34//! # Ok(())
35//! # }
36//! ```
37//!
38//! ### Test-Driven Development
39//!
40//! Generate tests automatically from parsed code:
41//!
42//! ```rust
43//! use perl_parser::Parser;
44//! use perl_parser::tdd::test_generator::{TestGenerator, TestFramework};
45//!
46//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
47//! let code = r#"sub add { my ($a, $b) = @_; return $a + $b; }"#;
48//! let mut parser = Parser::new(code);
49//! let ast = parser.parse()?;
50//!
51//! let generator = TestGenerator::new(TestFramework::TestMore);
52//! let tests = generator.generate_tests(&ast, code);
53//!
54//! // Returns test cases with intelligent assertions
55//! assert!(!tests.is_empty());
56//! # Ok(())
57//! # }
58//! ```
59//!
60//! ### LSP Integration
61//!
62//! Use as a library for LSP features (see `perl-lsp` for the standalone server):
63//!
64//! ```rust
65//! use perl_parser::Parser;
66//! use perl_parser::analysis::semantic::SemanticAnalyzer;
67//!
68//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
69//! let code = "my $x = 42;";
70//! let mut parser = Parser::new(code);
71//! let ast = parser.parse()?;
72//!
73//! // Semantic analysis for hover, completion, etc.
74//! let model = SemanticAnalyzer::analyze(&ast);
75//! # Ok(())
76//! # }
77//! ```
78//!
79//! ## Architecture
80//!
81//! The parser is organized into distinct layers for maintainability and testability:
82//!
83//! ### Core Engine ([`engine`])
84//!
85//! - **[`parser`]**: Recursive descent parser with operator precedence
86//! - **[`ast`]**: Abstract Syntax Tree definitions and node types
87//! - **[`error`]**: Error classification, recovery strategies, and diagnostics
88//! - **[`position`]**: UTF-16 position mapping for LSP protocol compliance
89//! - **[`quote_parser`]**: Specialized parser for quote-like operators
90//! - **[`heredoc_collector`]**: FIFO heredoc collection with indent stripping
91//!
92//! ### IDE Integration (LSP Provider Crates)
93//!
94//! LSP provider modules were removed from `perl-parser` as part of #4414 (microcrate
95//! collapse, PR #0). Import directly from the provider crates:
96//!
97//! - `perl_lsp_completion` — context-aware completion providers
98//! - `perl_lsp_diagnostics` — diagnostics generation and formatting
99//! - `perl_lsp_navigation` — references, document links, type definitions, workspace symbols
100//! - `perl_lsp_rename` — rename providers with validation
101//! - `perl_lsp_semantic_tokens` — semantic token generation
102//! - `perl_lsp_inlay_hints` — inlay hint providers
103//! - `perl_lsp_code_actions` — code action providers
104//!
105//! ### Analysis ([`analysis`])
106//!
107//! - **[`scope_analyzer`]**: Variable and subroutine scoping resolution
108//! - **[`type_inference`]**: Perl type inference engine
109//! - **[`semantic`]**: Semantic model with hover information
110//! - **[`symbol`]**: Symbol table and reference tracking
111//! - **[`dead_code_detector`]**: Unused code detection
112//!
113//! ### Workspace ([`workspace`])
114//!
115//! - **[`workspace_index`]**: Cross-file symbol indexing
116//! - **[`workspace_rename`]**: Multi-file refactoring
117//! - **[`document_store`]**: Document state management
118//!
119//! ### Refactoring ([`refactor`])
120//!
121//! - **[`refactoring`]**: Unified refactoring engine
122//! - **[`modernize`]**: Code modernization utilities
123//! - **[`import_optimizer`]**: Import statement analysis and optimization
124//!
125//! ### Test Support ([`tdd`])
126//!
127//! - **[`test_generator`]**: Intelligent test case generation
128//! - **[`test_runner`]**: Test execution and validation
129//! - **`tdd_workflow`** *(test-only)*: TDD cycle management and coverage tracking
130//!
131//! ## LSP Feature Support
132//!
133//! This crate provides the engine for LSP features. The public standalone server is in
134//! `perllsp`, backed by the `perl-lsp-rs` implementation crate.
135//!
136//! ### Implemented Features
137//!
138//! - **Completion**: Context-aware code completion with type inference
139//! - **Hover**: Documentation and type information on hover
140//! - **Definition**: Go-to-definition with cross-file support
141//! - **References**: Find all references with workspace indexing
142//! - **Rename**: Symbol renaming with conflict detection
143//! - **Diagnostics**: Syntax errors and semantic warnings
144//! - **Formatting**: Code formatting via perltidy integration
145//! - **Folding**: Code folding for blocks and regions
146//! - **Semantic Tokens**: Fine-grained syntax highlighting
147//! - **Call Hierarchy**: Function call navigation
148//! - **Type Hierarchy**: Class inheritance navigation
149//!
150//! See `docs/reference/LSP_CAPABILITY_POLICY.md` for the complete capability matrix.
151//!
152//! ## Incremental Parsing
153//!
154//! Enable efficient re-parsing for real-time editing:
155//!
156//! ```rust,ignore
157//! use perl_parser::{IncrementalState, apply_edits, Edit};
158//!
159//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
160//! let mut state = IncrementalState::new("my $x = 1;");
161//! let ast = state.parse()?;
162//!
163//! // Apply an edit
164//! let edit = Edit {
165//!     start_byte: 3,
166//!     old_end_byte: 5,
167//!     new_end_byte: 5,
168//!     text: "$y".to_string(),
169//! };
170//! apply_edits(&mut state, vec![edit]);
171//!
172//! // Incremental re-parse reuses unchanged nodes
173//! let new_ast = state.parse()?;
174//! # Ok(())
175//! # }
176//! ```
177//!
178//! ## Error Recovery
179//!
180//! The parser uses intelligent error recovery to continue parsing after errors:
181//!
182//! ```rust
183//! use perl_parser::Parser;
184//!
185//! let code = "sub broken { if (";  // Incomplete code
186//! let mut parser = Parser::new(code);
187//!
188//! // Parser recovers and builds partial AST
189//! let result = parser.parse();
190//! assert!(result.is_ok());
191//!
192//! // Check recorded errors
193//! let errors = parser.errors();
194//! assert!(!errors.is_empty());
195//! ```
196//!
197//! ## Workspace Indexing
198//!
199//! Build cross-file indexes for workspace-wide navigation:
200//!
201//! ```rust,ignore
202//! use perl_parser::workspace_index::WorkspaceIndex;
203//!
204//! let mut index = WorkspaceIndex::new();
205//! index.index_file("lib/Foo.pm", "package Foo; sub bar { }");
206//! index.index_file("lib/Baz.pm", "use Foo; Foo::bar();");
207//!
208//! // Find all references to Foo::bar
209//! let refs = index.find_references("Foo::bar");
210//! ```
211//!
212//! ## Testing with perl-corpus
213//!
214//! The parser is tested against the comprehensive `perl-corpus` test suite:
215//!
216//! ```bash
217//! # Run parser tests with full corpus coverage
218//! cargo test -p perl-parser
219//!
220//! # Run specific test category
221//! cargo test -p perl-parser --test regex_tests
222//!
223//! # Validate documentation examples
224//! cargo test --doc
225//! ```
226//!
227//! ## Command-Line Tools
228//!
229//! Build and install the LSP server binary:
230//!
231//! ```bash
232//! # Build LSP server
233//! cargo build -p perllsp --release
234//!
235//! # Install globally
236//! cargo install --path crates/perllsp
237//!
238//! # Run LSP server
239//! perllsp --stdio
240//!
241//! # Check server health
242//! perllsp --health
243//! ```
244//!
245//! ## Integration Examples
246//!
247//! ### VSCode Extension
248//!
249//! Configure the LSP server in VSCode settings:
250//!
251//! ```json
252//! {
253//!   "perl.lsp.path": "/path/to/perllsp",
254//!   "perl.lsp.args": ["--stdio"]
255//! }
256//! ```
257//!
258//! ### Neovim Integration
259//!
260//! ```lua
261//! require'lspconfig'.perl.setup{
262//!   cmd = { "/path/to/perllsp", "--stdio" },
263//! }
264//! ```
265//!
266//! ## Performance Characteristics
267//!
268//! - **Single-pass parsing**: O(n) complexity for well-formed input
269//! - **UTF-16 mapping**: Fast bidirectional offset conversion for LSP
270//! - **Incremental updates**: Reuses unchanged AST nodes for efficiency
271//! - **Memory efficiency**: Streaming token processing with bounded lookahead
272//!
273//! ## Compatibility
274//!
275//! - **Perl Versions**: 5.8 through 5.40 (covers 99% of CPAN)
276//! - **LSP Protocol**: LSP 3.18 specification
277//! - **Tree-sitter**: Compatible AST format and position tracking
278//! - **UTF-16**: Full Unicode support with correct LSP position mapping
279//!
280//! ## Related Crates
281//!
282//! - `perllsp`: Public Cargo entry point for the standalone LSP server
283//! - `perl-lsp-rs`: Standalone LSP server runtime implementation (moved from this crate)
284//! - `perl-lexer`: Context-aware Perl tokenizer
285//! - `perl-corpus`: Comprehensive test corpus and generators
286//! - `perl-dap`: Debug Adapter Protocol implementation
287//!
288//! ## Documentation
289//!
290//! - **API Docs**: See module documentation below
291//! - **LSP Guide**: `docs/reference/LSP_IMPLEMENTATION_GUIDE.md`
292//! - **Capability Policy**: `docs/reference/LSP_CAPABILITY_POLICY.md`
293//! - **Commands**: `docs/reference/COMMANDS_REFERENCE.md`
294//! - **Current Status**: `docs/project/CURRENT_STATUS.md`
295
296#![deny(unsafe_code)]
297#![deny(unreachable_pub)] // prevent stray pub items from escaping
298#![warn(rust_2018_idioms)]
299// NOTE: missing_docs enabled with baseline enforcement (Issue #197)
300// Baseline enforced via ci/missing_docs_baseline.txt
301#![warn(missing_docs)]
302#![warn(clippy::all)]
303#![allow(
304    // Core allows for parser/lexer code
305    clippy::too_many_lines,
306    clippy::module_name_repetitions,
307    clippy::cast_possible_truncation,
308    clippy::cast_sign_loss,
309    clippy::cast_precision_loss,
310    clippy::cast_possible_wrap,
311    clippy::must_use_candidate,
312    clippy::missing_errors_doc,
313    clippy::missing_panics_doc,
314
315    // Parser-specific patterns that are fine
316    clippy::wildcard_imports,
317    clippy::enum_glob_use,
318    clippy::match_same_arms,
319    clippy::if_not_else,
320    clippy::struct_excessive_bools,
321    clippy::items_after_statements,
322    clippy::return_self_not_must_use,
323    clippy::unused_self,
324    clippy::collapsible_match,
325    clippy::collapsible_if,
326    clippy::only_used_in_recursion,
327    clippy::items_after_test_module,
328    clippy::while_let_loop,
329    clippy::single_range_in_vec_init,
330    clippy::arc_with_non_send_sync,
331    clippy::needless_range_loop,
332    clippy::result_large_err,
333    clippy::if_same_then_else,
334    clippy::should_implement_trait,
335    clippy::manual_flatten,
336
337    // String handling in parsers
338    clippy::needless_raw_string_hashes,
339    clippy::single_char_pattern,
340    clippy::uninlined_format_args
341)]
342//! ## Architecture
343//!
344//! The parser follows a recursive descent design with operator precedence handling,
345//! maintaining a clean separation from the lexing phase. This modular approach
346//! enables:
347//!
348//! - Independent testing of parsing logic
349//! - Easy integration with different lexer implementations
350//! - Clear error boundaries between lexing and parsing phases
351//! - Optimal performance through single-pass parsing
352//!
353//! ## Example
354//!
355//! ```rust
356//! use perl_parser::Parser;
357//!
358//! let code = "my $x = 42;";
359//! let mut parser = Parser::new(code);
360//!
361//! match parser.parse() {
362//!     Ok(ast) => println!("AST: {}", ast.to_sexp()),
363//!     Err(e) => eprintln!("Parse error: {}", e),
364//! }
365//! ```
366
367/// Parser engine components and supporting utilities.
368pub mod engine;
369/// Legacy module aliases for moved engine components.
370pub use engine::{error, parser, position};
371
372/// Abstract Syntax Tree (AST) definitions for Perl parsing.
373pub use engine::ast;
374/// Experimental second-generation AST (work in progress).
375pub use engine::ast_v2;
376/// Edit tracking for incremental parsing.
377pub use engine::edit;
378/// Heredoc content collector with FIFO ordering and indent stripping.
379pub use engine::heredoc_collector;
380/// Recursive descent Perl parser with error recovery and AST generation.
381pub use engine::parser::Parser;
382/// Parser context with error recovery support.
383pub use engine::parser_context;
384/// Pragma tracking for `use` and related directives.
385pub use engine::pragma_tracker;
386/// Parser for Perl quote and quote-like operators.
387pub use engine::quote_parser;
388#[cfg(not(target_arch = "wasm32"))]
389/// Error classification and recovery strategies for parse failures.
390pub use error::classifier as error_classifier;
391/// Error recovery strategies for resilient parsing.
392pub use error::recovery as error_recovery;
393/// Parser utilities and helpers.
394pub use perl_parser_core::util;
395
396/// Line-to-byte offset index for fast position lookups.
397pub use perl_parser_core::line_index;
398/// Line ending detection and UTF-16 position mapping for LSP compliance.
399pub use position::{LineEnding, PositionMapper};
400
401/// Semantic analysis, scope resolution, and type inference.
402pub mod analysis;
403/// Perl builtin function signatures and metadata.
404pub mod builtins;
405#[cfg(feature = "incremental")]
406/// Incremental parsing for efficient re-parsing during editing.
407pub mod incremental;
408/// Code refactoring, modernization, and import optimization.
409pub mod refactor;
410/// Test-driven development support and test generation.
411pub mod tdd;
412/// Token stream, trivia, and token wrapper utilities.
413pub mod tokens;
414/// Workspace indexing, document store, and cross-file operations.
415pub mod workspace;
416
417// =============================================================================
418// Wave D absorbed satellite crates (as internal modules)
419// =============================================================================
420
421/// AST range and insertion helpers for Perl LSP features (previously `perl-ast-utils`).
422pub mod ast_utils;
423/// Anti-pattern detection for problematic Perl heredoc patterns (previously `perl-heredoc-anti-patterns`).
424// Wave D: allow missing_docs — original crate had an explicit exception per CLAUDE.md
425#[allow(missing_docs)]
426pub mod heredoc_anti_patterns;
427/// Secure workspace-relative path normalization (previously `perl-path-normalize`; from perl-parser-core).
428pub use perl_parser_core::path_normalize;
429/// Workspace-bound path validation and traversal prevention (previously `perl-path-security`; from perl-parser-core).
430pub use perl_parser_core::path_security;
431/// Nearest-rank percentile helpers for integer latency samples (previously `perl-percentile`; from perl-parser-core).
432pub use perl_parser_core::percentile;
433/// Perl qualified-name parsing, splitting, and validation helpers (previously `perl-qualified-name`; from perl-parser-core).
434pub use perl_parser_core::qualified_name;
435/// Shared Perl source-file classification helpers (previously `perl-source-file`; from perl-parser-core).
436pub use perl_parser_core::source_file;
437/// Text-line cursor and boundary helpers (previously `perl-text-line`; from perl-parser-core).
438pub use perl_parser_core::text_line;
439
440/// Variable and subroutine declaration analysis.
441pub use analysis::declaration;
442#[cfg(not(target_arch = "wasm32"))]
443/// File and symbol indexing for workspace-wide navigation.
444pub use analysis::index;
445/// Scope analysis for variable and subroutine resolution.
446pub use analysis::scope_analyzer;
447/// Semantic model with hover information and token classification.
448pub use analysis::semantic;
449/// Symbol table, extraction, and reference tracking.
450pub use analysis::symbol;
451/// Type inference engine for Perl variable analysis.
452pub use analysis::type_inference;
453/// Builtin function signature lookup tables.
454pub use builtins::builtin_signatures;
455/// Perfect hash function (PHF) based builtin signature lookup.
456pub use builtins::builtin_signatures_phf;
457/// Dead code detection for Perl workspaces (absorbed from `perl-dead-code`).
458#[cfg(not(target_arch = "wasm32"))]
459pub mod dead_code;
460/// Backwards-compatibility alias: `perl_parser::dead_code_detector` still works.
461#[cfg(not(target_arch = "wasm32"))]
462pub use dead_code as dead_code_detector;
463
464/// Import statement analysis and optimization.
465pub use refactor::import_optimizer;
466/// Code modernization utilities for Perl best practices.
467pub use refactor::modernize;
468/// Enhanced code modernization with refactoring capabilities.
469pub use refactor::modernize_refactored;
470/// Unified refactoring engine for comprehensive code transformations.
471pub use refactor::refactoring;
472/// Token stream with position-aware iteration.
473pub use tokens::token_stream;
474/// Lightweight token wrapper for AST integration.
475pub use tokens::token_wrapper;
476/// Trivia (whitespace and comments) representation.
477pub use tokens::trivia;
478/// Parser that preserves trivia tokens for formatting.
479pub use tokens::trivia_parser;
480
481#[cfg(feature = "incremental")]
482/// Advanced AST node reuse strategies for incremental parsing.
483pub use incremental::incremental_advanced_reuse;
484#[cfg(feature = "incremental")]
485/// Checkpoint-based incremental parsing with rollback support.
486pub use incremental::incremental_checkpoint;
487#[cfg(feature = "incremental")]
488/// Document-level incremental parsing state management.
489pub use incremental::incremental_document;
490#[cfg(feature = "incremental")]
491/// Edit representation and application for incremental updates.
492pub use incremental::incremental_edit;
493#[cfg(feature = "incremental")]
494#[deprecated(note = "LSP server moved to perl-lsp; perl-parser no longer handles didChange")]
495/// Legacy incremental handler (deprecated, use `perl-lsp` crate instead).
496pub use incremental::incremental_handler_v2;
497#[cfg(feature = "incremental")]
498/// Integration layer connecting incremental parsing with the full parser.
499pub use incremental::incremental_integration;
500#[cfg(feature = "incremental")]
501/// Simplified incremental parsing interface for common use cases.
502pub use incremental::incremental_simple;
503#[cfg(feature = "incremental")]
504/// Second-generation incremental parsing with improved node reuse.
505pub use incremental::incremental_v2;
506
507/// Basic TDD utilities and test helpers.
508pub use tdd::tdd_basic;
509#[cfg(test)]
510/// TDD workflow integration for Test-Driven Development support.
511pub use tdd::tdd_workflow;
512/// Intelligent test case generation from parsed Perl code.
513pub use tdd::test_generator;
514/// Test execution and TDD support functionality.
515pub use tdd::test_runner;
516
517/// In-memory document storage for open editor buffers.
518pub use workspace::document_store;
519/// Cross-file symbol index for workspace-wide navigation.
520pub use workspace::workspace_index;
521#[cfg(not(target_arch = "wasm32"))]
522/// Multi-file refactoring operations across a workspace.
523pub use workspace::workspace_refactor;
524/// Cross-file symbol renaming with conflict detection.
525pub use workspace::workspace_rename;
526
527/// AST node, node kind enum, and source location types.
528pub use ast::{Node, NodeKind, SourceLocation};
529/// Parse error and result types for parser output.
530pub use error::{ParseError, ParseResult, RecoverySalvageClass, RecoverySalvageProfile};
531#[cfg(feature = "incremental")]
532/// Checkpointed incremental parser with simple edit tracking.
533pub use incremental_checkpoint::{CheckpointedIncrementalParser, SimpleEdit};
534/// Pragma state tracking for `use strict`, `use warnings`, etc.
535pub use pragma_tracker::{PragmaState, PragmaTracker};
536/// Token types and token stream for lexer output.
537pub use token_stream::{Token, TokenKind, TokenStream};
538/// Trivia (whitespace/comments) attached to AST nodes.
539pub use trivia::{NodeWithTrivia, Trivia, TriviaToken};
540/// Trivia-preserving parser and formatting utilities.
541pub use trivia_parser::{TriviaPreservingParser, format_with_trivia};
542
543// Incremental parsing exports (feature-gated)
544#[cfg(feature = "incremental")]
545/// Core incremental parsing types: edit representation, state, and application.
546pub use incremental::{Edit, IncrementalState, apply_edits};
547
548/// Semantic analysis types for hover, tokens, and code understanding.
549pub use semantic::{
550    HoverInfo, SemanticAnalyzer, SemanticModel, SemanticToken, SemanticTokenModifier,
551    SemanticTokenType,
552};
553/// Symbol extraction, table, and reference types for navigation.
554pub use symbol::{Symbol, SymbolExtractor, SymbolKind, SymbolReference, SymbolTable};
555
556// =============================================================================
557// LSP Feature Exports (DEPRECATED - migrated to perl-lsp crate)
558// =============================================================================
559// These exports are commented out during the migration period.
560// Use `perl_lsp` crate for LSP functionality instead.
561//
562// pub use code_actions::{CodeAction, CodeActionEdit, CodeActionKind, CodeActionsProvider};
563// pub use code_actions_enhanced::EnhancedCodeActionsProvider;
564// pub use code_actions_provider::{...};
565// pub use code_lens_provider::{CodeLens, CodeLensProvider, ...};
566// pub use completion::{CompletionContext, CompletionItem, CompletionItemKind, CompletionProvider};
567// pub use diagnostics::{Diagnostic, DiagnosticSeverity, DiagnosticTag, ...};
568// pub use document_links::compute_links;
569// pub use folding::{FoldingRange, FoldingRangeExtractor, FoldingRangeKind};
570// pub use formatting::{CodeFormatter, FormatTextEdit, FormattingOptions};
571// pub use inlay_hints::{parameter_hints, trivial_type_hints};
572// pub use lsp::protocol::{JsonRpcError, JsonRpcRequest, JsonRpcResponse};
573// pub use lsp_server::LspServer;
574// pub use on_type_formatting::compute_on_type_edit;
575// pub use rename::{RenameOptions, RenameProvider, RenameResult, TextEdit, apply_rename_edits};
576// pub use selection_range::{build_parent_map, selection_chain};
577// pub use semantic_tokens::{...};
578// pub use semantic_tokens_provider::{...};
579// pub use signature_help::{ParameterInfo, SignatureHelp, SignatureHelpProvider, SignatureInfo};
580// pub use workspace_symbols::{WorkspaceSymbol, WorkspaceSymbolsProvider};
581// =============================================================================
582
583/// Import analysis, optimization, and unused import detection.
584pub use import_optimizer::{
585    DuplicateImport, ImportAnalysis, ImportEntry, ImportOptimizer, MissingImport,
586    OrganizationSuggestion, SuggestionPriority, UnusedImport,
587};
588/// Scope analysis issue types and analyzer.
589pub use scope_analyzer::{IssueKind, ScopeAnalyzer, ScopeIssue};
590#[cfg(test)]
591/// Test generation, coverage reporting, and refactoring suggestions.
592pub use test_generator::{
593    CoverageReport, Priority, RefactoringCategory, RefactoringSuggester, RefactoringSuggestion,
594    TestCase, TestFramework, TestGenerator, TestGeneratorOptions, TestResults, TestRunner,
595};
596/// Type inference types: Perl types, constraints, and inference engine.
597pub use type_inference::{
598    PerlType, ScalarType, TypeBasedCompletion, TypeConstraint, TypeEnvironment,
599    TypeInferenceEngine, TypeLocation,
600};
601
602/// Refactoring engine types: configuration, operations, and results.
603pub use refactoring::{
604    ModernizationPattern, RefactoringConfig, RefactoringEngine, RefactoringOperation,
605    RefactoringResult, RefactoringScope, RefactoringType,
606};
607#[cfg(test)]
608/// TDD workflow types: actions, configuration, and cycle management.
609pub use tdd_workflow::{
610    AnnotationSeverity, CoverageAnnotation, TddAction, TddConfig, TddCycleResult, TddWorkflow,
611    TestType, WorkflowState, WorkflowStatus,
612};
613
614#[cfg(test)]
615mod tests {
616    use super::*;
617    use perl_tdd_support::must;
618
619    #[test]
620    fn test_basic_parsing() {
621        let mut parser = Parser::new("my $x = 42;");
622        let result = parser.parse();
623        assert!(result.is_ok());
624
625        let ast = must(result);
626        assert!(matches!(ast.kind, NodeKind::Program { .. }));
627    }
628
629    #[test]
630    fn test_variable_declaration() {
631        let cases = vec![
632            ("my $x;", "my"),
633            ("our $y;", "our"),
634            ("local $z;", "local"),
635            ("state $w;", "state"),
636        ];
637
638        for (code, declarator) in cases {
639            let mut parser = Parser::new(code);
640            let result = parser.parse();
641            assert!(result.is_ok(), "Failed to parse: {}", code);
642
643            let ast = must(result);
644            if let NodeKind::Program { statements } = &ast.kind {
645                assert_eq!(statements.len(), 1);
646                let is_var_decl =
647                    matches!(statements[0].kind, NodeKind::VariableDeclaration { .. });
648                assert!(is_var_decl, "Expected VariableDeclaration for: {}", code);
649                if let NodeKind::VariableDeclaration { declarator: decl, .. } = &statements[0].kind
650                {
651                    assert_eq!(decl, declarator);
652                }
653            }
654        }
655    }
656
657    #[test]
658    fn test_operators() {
659        // Test operators that work correctly
660        let cases = vec![
661            ("$a + $b", "+"),
662            ("$a - $b", "-"),
663            ("$a * $b", "*"),
664            ("$a . $b", "."),
665            ("$a && $b", "&&"),
666            ("$a || $b", "||"),
667        ];
668
669        for (code, expected_op) in cases {
670            let mut parser = Parser::new(code);
671            let result = parser.parse();
672            assert!(result.is_ok(), "Failed to parse: {}", code);
673
674            let ast = must(result);
675            if let NodeKind::Program { statements } = &ast.kind {
676                assert!(!statements.is_empty(), "No statements found in AST for: {}", code);
677
678                // Find the binary node, which might be wrapped in an ExpressionStatement
679                let binary_node = match &statements[0].kind {
680                    NodeKind::ExpressionStatement { expression } => match &expression.kind {
681                        NodeKind::Binary { op, left, right } => Some((op, left, right)),
682                        _ => None,
683                    },
684                    NodeKind::Binary { op, left, right } => Some((op, left, right)),
685                    _ => None,
686                };
687
688                assert!(
689                    binary_node.is_some(),
690                    "Expected Binary operator for: {}. Found: {:?}",
691                    code,
692                    statements[0].kind
693                );
694                if let Some((op, left, right)) = binary_node {
695                    assert_eq!(op, expected_op, "Operator mismatch for: {}", code);
696
697                    // Additional diagnostic information
698                    println!("Parsing: {}", code);
699                    println!("Left node: {:?}", left);
700                    println!("Right node: {:?}", right);
701                }
702            }
703            assert!(
704                matches!(ast.kind, NodeKind::Program { .. }),
705                "Expected Program node, found: {:?}",
706                ast.kind
707            );
708        }
709    }
710
711    #[test]
712    fn test_operators_with_context() {
713        // These operators require context-aware parsing to disambiguate from similar syntax:
714        // - `/` could be division or regex delimiter
715        // - `%` could be modulo or hash sigil
716        // - `**` could be exponent or glob pattern
717        // - `//` could be defined-or or regex delimiter
718        // The lexer handles disambiguation via LexerMode::ExpectTerm tracking.
719        let cases: Vec<(&str, &str)> = vec![
720            ("2 / 3", "/"),     // Division (not regex)
721            ("$a % $b", "%"),   // Modulo (not hash sigil)
722            ("$a ** $b", "**"), // Exponent (not glob)
723            ("$a // $b", "//"), // Defined-or (not regex)
724        ];
725
726        for (code, expected_op) in cases {
727            let mut parser = Parser::new(code);
728            let result = parser.parse();
729            assert!(result.is_ok(), "Failed to parse: {}", code);
730
731            let ast = must(result);
732            if let NodeKind::Program { statements } = &ast.kind {
733                assert!(!statements.is_empty(), "No statements found in AST for: {}", code);
734
735                // Find the binary node, which might be wrapped in an ExpressionStatement
736                let binary_node = match &statements[0].kind {
737                    NodeKind::ExpressionStatement { expression } => match &expression.kind {
738                        NodeKind::Binary { op, .. } => Some(op),
739                        _ => None,
740                    },
741                    NodeKind::Binary { op, .. } => Some(op),
742                    _ => None,
743                };
744
745                assert!(
746                    binary_node.is_some(),
747                    "Expected Binary operator for: {}. Found: {:?}",
748                    code,
749                    statements[0].kind
750                );
751                if let Some(op) = binary_node {
752                    assert_eq!(op, expected_op, "Operator mismatch for: {}", code);
753                }
754            }
755            assert!(
756                matches!(ast.kind, NodeKind::Program { .. }),
757                "Expected Program node, found: {:?}",
758                ast.kind
759            );
760        }
761    }
762
763    #[test]
764    fn test_string_literals() {
765        let cases = vec![r#""hello""#, r#"'world'"#, r#"qq{foo}"#, r#"q{bar}"#];
766
767        for code in cases {
768            let mut parser = Parser::new(code);
769            let result = parser.parse();
770            assert!(result.is_ok(), "Failed to parse: {}", code);
771        }
772    }
773
774    #[test]
775    fn test_arrays_and_hashes() {
776        let cases = vec![
777            "@array",
778            "%hash",
779            "$array[0]",
780            "$hash{key}",
781            "@array[1, 2, 3]",
782            "@hash{'a', 'b'}",
783        ];
784
785        for code in cases {
786            let mut parser = Parser::new(code);
787            let result = parser.parse();
788            assert!(result.is_ok(), "Failed to parse: {}", code);
789        }
790    }
791
792    #[test]
793    fn test_subroutines() {
794        let cases = vec![
795            "sub foo { }",
796            "sub bar { return 42; }",
797            "sub baz ($x, $y) { $x + $y }",
798            "sub qux :method { }",
799        ];
800
801        for code in cases {
802            let mut parser = Parser::new(code);
803            let result = parser.parse();
804            assert!(result.is_ok(), "Failed to parse: {}", code);
805
806            let ast = must(result);
807            if let NodeKind::Program { statements } = &ast.kind {
808                assert_eq!(statements.len(), 1);
809                assert!(matches!(statements[0].kind, NodeKind::Subroutine { .. }));
810            }
811        }
812    }
813
814    #[test]
815    fn test_control_flow() {
816        let cases = vec![
817            "if ($x) { }",
818            "if ($x) { } else { }",
819            "if ($x) { } elsif ($y) { } else { }",
820            "unless ($x) { }",
821            "while ($x) { }",
822            "until ($x) { }",
823            "for (my $i = 0; $i < 10; $i++) { }",
824            "foreach my $x (@array) { }",
825        ];
826
827        for code in cases {
828            let mut parser = Parser::new(code);
829            let result = parser.parse();
830            assert!(result.is_ok(), "Failed to parse: {}", code);
831        }
832    }
833
834    #[test]
835    fn test_regex() {
836        let cases = vec![
837            "/pattern/",
838            "m/pattern/",
839            "s/old/new/",
840            "tr/a-z/A-Z/",
841            r#"qr/\d+/"#,
842            "$x =~ /foo/",
843            "$x !~ /bar/",
844        ];
845
846        for code in cases {
847            let mut parser = Parser::new(code);
848            let result = parser.parse();
849            assert!(result.is_ok(), "Failed to parse: {}", code);
850        }
851    }
852
853    #[test]
854    fn test_error_cases() {
855        let cases = vec![
856            ("if (", "Unexpected end of input"),
857            ("sub (", "Unexpected end of input"),
858            ("my (", "Unexpected end of input"),
859            ("{", "Unexpected end of input"),
860        ];
861
862        for (code, _expected_error) in cases {
863            let mut parser = Parser::new(code);
864            let result = parser.parse();
865
866            // With error recovery, parse() succeeds but collects errors
867            assert!(result.is_ok(), "Parser should recover from errors for: {}", code);
868
869            // Check that errors were recorded
870            let errors = parser.errors();
871            assert!(!errors.is_empty(), "Expected recorded errors for: {}", code);
872        }
873    }
874
875    #[test]
876    fn test_modern_perl_features() {
877        let cases = vec![
878            "class Point { }",
879            "method new { }",
880            "try { } catch ($e) { }",
881            "defer { }",
882            "my $x :shared = 42;",
883        ];
884
885        for code in cases {
886            let mut parser = Parser::new(code);
887            let result = parser.parse();
888            assert!(result.is_ok(), "Failed to parse: {}", code);
889        }
890    }
891
892    #[test]
893    fn test_edge_cases() {
894        let cases = vec![
895            // Indirect object syntax
896            "print STDOUT 'hello';",
897            "new Class;",
898            // Multi-variable declarations
899            "my ($x, $y) = (1, 2);",
900            "my ($a :shared, $b :locked);",
901            // Complex expressions
902            "$x->@*",
903            "$x->%*",
904            "$x->$*",
905            // Defined-or
906            "$x // 'default'",
907            // ISA operator
908            "$obj ISA 'Class'",
909        ];
910
911        for code in cases {
912            let mut parser = Parser::new(code);
913            let result = parser.parse();
914            assert!(result.is_ok(), "Failed to parse edge case: {}", code);
915        }
916    }
917}