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#![allow(
303    // Core allows for parser/lexer code
304    clippy::too_many_lines,
305    clippy::module_name_repetitions,
306    clippy::cast_possible_truncation,
307    clippy::cast_sign_loss,
308    clippy::cast_precision_loss,
309    clippy::cast_possible_wrap,
310    clippy::must_use_candidate,
311    clippy::missing_errors_doc,
312    clippy::missing_panics_doc,
313
314    // Parser-specific patterns that are fine
315    clippy::wildcard_imports,
316    clippy::enum_glob_use,
317    clippy::match_same_arms,
318    clippy::if_not_else,
319    clippy::struct_excessive_bools,
320    clippy::items_after_statements,
321    clippy::return_self_not_must_use,
322    clippy::unused_self,
323    clippy::collapsible_match,
324    clippy::collapsible_if,
325    clippy::only_used_in_recursion,
326    clippy::items_after_test_module,
327    clippy::while_let_loop,
328    clippy::single_range_in_vec_init,
329    clippy::arc_with_non_send_sync,
330    clippy::needless_range_loop,
331    clippy::result_large_err,
332    clippy::if_same_then_else,
333    clippy::should_implement_trait,
334    clippy::manual_flatten,
335
336    // String handling in parsers
337    clippy::needless_raw_string_hashes,
338    clippy::single_char_pattern,
339    clippy::uninlined_format_args
340)]
341//! ## Architecture
342//!
343//! The parser follows a recursive descent design with operator precedence handling,
344//! maintaining a clean separation from the lexing phase. This modular approach
345//! enables:
346//!
347//! - Independent testing of parsing logic
348//! - Easy integration with different lexer implementations
349//! - Clear error boundaries between lexing and parsing phases
350//! - Optimal performance through single-pass parsing
351//!
352//! ## Example
353//!
354//! ```rust
355//! use perl_parser::Parser;
356//!
357//! let code = "my $x = 42;";
358//! let mut parser = Parser::new(code);
359//!
360//! match parser.parse() {
361//!     Ok(ast) => println!("AST: {}", ast.to_sexp()),
362//!     Err(e) => eprintln!("Parse error: {}", e),
363//! }
364//! ```
365
366/// Parser engine components and supporting utilities.
367pub mod engine;
368/// Legacy module aliases for moved engine components.
369pub use engine::{error, parser, position};
370
371/// Recursive descent Perl parser with error recovery and AST generation.
372pub use core::{Node, NodeKind, ParseError, ParseOutput, ParseResult, Parser, SourceLocation};
373/// Abstract Syntax Tree (AST) definitions for Perl parsing.
374pub use engine::ast;
375/// Experimental second-generation AST (work in progress).
376pub use engine::ast_v2;
377/// Edit tracking for incremental parsing.
378pub use engine::edit;
379/// Heredoc content collector with FIFO ordering and indent stripping.
380pub use engine::heredoc_collector;
381/// Parser context with error recovery support.
382pub use engine::parser_context;
383/// Pragma tracking for `use` and related directives.
384pub use engine::pragma_tracker;
385/// Parser for Perl quote and quote-like operators.
386pub use engine::quote_parser;
387#[cfg(not(target_arch = "wasm32"))]
388/// Error classification and recovery strategies for parse failures.
389pub use error::classifier as error_classifier;
390/// Error recovery strategies for resilient parsing.
391pub use error::recovery as error_recovery;
392/// Parser utilities and helpers.
393pub use perl_parser_core::util;
394
395/// Line-to-byte offset index for fast position lookups.
396pub use perl_parser_core::line_index;
397/// Line ending detection and UTF-16 position mapping for LSP compliance.
398pub use position::{LineEnding, PositionMapper};
399
400/// Facade over `perl-semantic-analyzer` for compatibility imports.
401pub mod analysis;
402/// Perl builtin function signatures and metadata.
403pub mod builtins;
404/// Facade over parser-kernel types from `perl-parser-core`.
405pub mod core;
406#[cfg(feature = "incremental")]
407/// Incremental parsing for efficient re-parsing during editing.
408pub mod incremental;
409/// Canonical convenience imports for consumers.
410pub mod prelude;
411/// Code refactoring, modernization, and import optimization.
412pub mod refactor;
413/// Test-driven development support and test generation.
414pub mod tdd;
415/// Token stream, trivia, and token wrapper utilities.
416pub mod tokens;
417/// Facade over `perl-workspace` for compatibility imports.
418pub mod workspace;
419
420pub mod compat;
421
422// =============================================================================
423// Wave D absorbed satellite crates (as internal modules)
424// =============================================================================
425
426/// AST range and insertion helpers for Perl LSP features (previously `perl-ast-utils`).
427pub mod ast_utils;
428/// Anti-pattern detection for problematic Perl heredoc patterns (previously `perl-heredoc-anti-patterns`).
429// Wave D: allow missing_docs — original crate had an explicit exception per CLAUDE.md
430#[allow(missing_docs)]
431pub mod heredoc_anti_patterns;
432/// Secure workspace-relative path normalization (previously `perl-path-normalize`; from perl-parser-core).
433pub use perl_parser_core::path_normalize;
434/// Workspace-bound path validation and traversal prevention (previously `perl-path-security`; from perl-parser-core).
435pub use perl_parser_core::path_security;
436/// Nearest-rank percentile helpers for integer latency samples (previously `perl-percentile`; from perl-parser-core).
437pub use perl_parser_core::percentile;
438/// Perl qualified-name parsing, splitting, and validation helpers (previously `perl-qualified-name`; from perl-parser-core).
439pub use perl_parser_core::qualified_name;
440/// Shared Perl source-file classification helpers (previously `perl-source-file`; from perl-parser-core).
441pub use perl_parser_core::source_file;
442/// Text-line cursor and boundary helpers (previously `perl-text-line`; from perl-parser-core).
443pub use perl_parser_core::text_line;
444
445/// Variable and subroutine declaration analysis.
446pub use analysis::declaration;
447#[cfg(not(target_arch = "wasm32"))]
448/// File and symbol indexing for workspace-wide navigation.
449pub use analysis::index;
450/// Scope analysis for variable and subroutine resolution.
451pub use analysis::scope_analyzer;
452/// Semantic model with hover information and token classification.
453pub use analysis::semantic;
454/// Symbol table, extraction, and reference tracking.
455pub use analysis::symbol;
456/// Type inference engine for Perl variable analysis.
457pub use analysis::type_inference;
458/// Builtin function signature lookup tables.
459pub use builtins::builtin_signatures;
460/// Perfect hash function (PHF) based builtin signature lookup.
461pub use builtins::builtin_signatures_phf;
462/// Dead code detection for Perl workspaces (absorbed from `perl-dead-code`).
463#[cfg(not(target_arch = "wasm32"))]
464pub mod dead_code;
465/// Backwards-compatibility alias: `perl_parser::dead_code_detector` still works.
466#[cfg(not(target_arch = "wasm32"))]
467pub use dead_code as dead_code_detector;
468
469/// Import statement analysis and optimization.
470pub use refactor::import_optimizer;
471/// Code modernization utilities for Perl best practices.
472pub use refactor::modernize;
473/// Enhanced code modernization with refactoring capabilities.
474pub use refactor::modernize_refactored;
475/// Unified refactoring engine for comprehensive code transformations.
476pub use refactor::refactoring;
477/// Token stream with position-aware iteration.
478pub use tokens::token_stream;
479/// Lightweight token wrapper for AST integration.
480pub use tokens::token_wrapper;
481/// Trivia (whitespace and comments) representation.
482pub use tokens::trivia;
483/// Parser that preserves trivia tokens for formatting.
484pub use tokens::trivia_parser;
485
486#[cfg(feature = "incremental")]
487/// Advanced AST node reuse strategies for incremental parsing.
488pub use incremental::incremental_advanced_reuse;
489#[cfg(feature = "incremental")]
490/// Checkpoint-based incremental parsing with rollback support.
491pub use incremental::incremental_checkpoint;
492#[cfg(feature = "incremental")]
493/// Document-level incremental parsing state management.
494pub use incremental::incremental_document;
495#[cfg(feature = "incremental")]
496/// Edit representation and application for incremental updates.
497pub use incremental::incremental_edit;
498#[cfg(feature = "incremental")]
499#[deprecated(note = "LSP server moved to perl-lsp; perl-parser no longer handles didChange")]
500/// Legacy incremental handler (deprecated, use `perl-lsp` crate instead).
501pub use incremental::incremental_handler_v2;
502#[cfg(feature = "incremental")]
503/// Integration layer connecting incremental parsing with the full parser.
504pub use incremental::incremental_integration;
505#[cfg(feature = "incremental")]
506/// Simplified incremental parsing interface for common use cases.
507pub use incremental::incremental_simple;
508#[cfg(feature = "incremental")]
509/// Second-generation incremental parsing with improved node reuse.
510pub use incremental::incremental_v2;
511
512/// Basic TDD utilities and test helpers.
513pub use tdd::tdd_basic;
514#[cfg(test)]
515/// TDD workflow integration for Test-Driven Development support.
516pub use tdd::tdd_workflow;
517/// Intelligent test case generation from parsed Perl code.
518pub use tdd::test_generator;
519/// Test execution and TDD support functionality.
520pub use tdd::test_runner;
521
522/// In-memory document storage for open editor buffers.
523pub use workspace::document_store;
524/// Cross-file symbol index for workspace-wide navigation.
525pub use workspace::workspace_index;
526#[cfg(not(target_arch = "wasm32"))]
527/// Multi-file refactoring operations across a workspace.
528pub use workspace::workspace_refactor;
529/// Cross-file symbol renaming with conflict detection.
530pub use workspace::workspace_rename;
531
532/// AST node, node kind enum, and source location types.
533/// Parse error and result types for parser output.
534pub use error::{RecoverySalvageClass, RecoverySalvageProfile};
535#[cfg(feature = "incremental")]
536/// Checkpointed incremental parser with simple edit tracking.
537pub use incremental_checkpoint::{CheckpointedIncrementalParser, SimpleEdit};
538/// Pragma state tracking for `use strict`, `use warnings`, etc.
539pub use pragma_tracker::{PragmaState, PragmaTracker};
540/// Token types and token stream for lexer output.
541pub use token_stream::{Token, TokenKind, TokenStream};
542/// Trivia (whitespace/comments) attached to AST nodes.
543pub use trivia::{NodeWithTrivia, Trivia, TriviaToken};
544/// Trivia-preserving parser and formatting utilities.
545pub use trivia_parser::{TriviaPreservingParser, format_with_trivia};
546
547// Incremental parsing exports (feature-gated)
548#[cfg(feature = "incremental")]
549/// Core incremental parsing types: edit representation, state, and application.
550pub use incremental::{Edit, IncrementalState, apply_edits};
551
552/// Semantic analysis types for hover, tokens, and code understanding.
553pub use semantic::{
554    HoverInfo, SemanticAnalyzer, SemanticModel, SemanticToken, SemanticTokenModifier,
555    SemanticTokenType,
556};
557/// Symbol extraction, table, and reference types for navigation.
558pub use symbol::{Symbol, SymbolExtractor, SymbolKind, SymbolReference, SymbolTable};
559
560// =============================================================================
561// LSP Feature Exports (DEPRECATED - migrated to perl-lsp crate)
562// =============================================================================
563// These exports are commented out during the migration period.
564// Use `perl_lsp` crate for LSP functionality instead.
565//
566// pub use code_actions::{CodeAction, CodeActionEdit, CodeActionKind, CodeActionsProvider};
567// pub use code_actions_enhanced::EnhancedCodeActionsProvider;
568// pub use code_actions_provider::{...};
569// pub use code_lens_provider::{CodeLens, CodeLensProvider, ...};
570// pub use completion::{CompletionContext, CompletionItem, CompletionItemKind, CompletionProvider};
571// pub use diagnostics::{Diagnostic, DiagnosticSeverity, DiagnosticTag, ...};
572// pub use document_links::compute_links;
573// pub use folding::{FoldingRange, FoldingRangeExtractor, FoldingRangeKind};
574// pub use formatting::{CodeFormatter, FormatTextEdit, FormattingOptions};
575// pub use inlay_hints::{parameter_hints, trivial_type_hints};
576// pub use lsp::protocol::{JsonRpcError, JsonRpcRequest, JsonRpcResponse};
577// pub use lsp_server::LspServer;
578// pub use on_type_formatting::compute_on_type_edit;
579// pub use rename::{RenameOptions, RenameProvider, RenameResult, TextEdit, apply_rename_edits};
580// pub use selection_range::{build_parent_map, selection_chain};
581// pub use semantic_tokens::{...};
582// pub use semantic_tokens_provider::{...};
583// pub use signature_help::{ParameterInfo, SignatureHelp, SignatureHelpProvider, SignatureInfo};
584// pub use workspace_symbols::{WorkspaceSymbol, WorkspaceSymbolsProvider};
585// =============================================================================
586
587/// Import analysis, optimization, and unused import detection.
588pub use import_optimizer::{
589    DuplicateImport, ImportAnalysis, ImportEntry, ImportOptimizer, MissingImport,
590    OrganizationSuggestion, SuggestionPriority, UnusedImport,
591};
592/// Scope analysis issue types and analyzer.
593pub use scope_analyzer::{IssueKind, ScopeAnalyzer, ScopeIssue};
594#[cfg(test)]
595/// Test generation, coverage reporting, and refactoring suggestions.
596pub use test_generator::{
597    CoverageReport, Priority, RefactoringCategory, RefactoringSuggester, RefactoringSuggestion,
598    TestCase, TestFramework, TestGenerator, TestGeneratorOptions, TestResults, TestRunner,
599};
600/// Type inference types: Perl types, constraints, and inference engine.
601pub use type_inference::{
602    PerlType, ScalarType, TypeBasedCompletion, TypeConstraint, TypeEnvironment,
603    TypeInferenceEngine, TypeLocation,
604};
605
606/// Refactoring engine types: configuration, operations, and results.
607pub use refactoring::{
608    ModernizationPattern, RefactoringConfig, RefactoringEngine, RefactoringOperation,
609    RefactoringResult, RefactoringScope, RefactoringType,
610};
611#[cfg(test)]
612/// TDD workflow types: actions, configuration, and cycle management.
613pub use tdd_workflow::{
614    AnnotationSeverity, CoverageAnnotation, TddAction, TddConfig, TddCycleResult, TddWorkflow,
615    TestType, WorkflowState, WorkflowStatus,
616};
617
618#[cfg(test)]
619mod tests {
620    use super::*;
621    use perl_tdd_support::must;
622
623    #[test]
624    fn test_basic_parsing() {
625        let mut parser = Parser::new("my $x = 42;");
626        let result = parser.parse();
627        assert!(result.is_ok());
628
629        let ast = must(result);
630        assert!(matches!(ast.kind, NodeKind::Program { .. }));
631    }
632
633    #[test]
634    fn test_variable_declaration() {
635        let cases = vec![
636            ("my $x;", "my"),
637            ("our $y;", "our"),
638            ("local $z;", "local"),
639            ("state $w;", "state"),
640        ];
641
642        for (code, declarator) in cases {
643            let mut parser = Parser::new(code);
644            let result = parser.parse();
645            assert!(result.is_ok(), "Failed to parse: {}", code);
646
647            let ast = must(result);
648            if let NodeKind::Program { statements } = &ast.kind {
649                assert_eq!(statements.len(), 1);
650                let is_var_decl =
651                    matches!(statements[0].kind, NodeKind::VariableDeclaration { .. });
652                assert!(is_var_decl, "Expected VariableDeclaration for: {}", code);
653                if let NodeKind::VariableDeclaration { declarator: decl, .. } = &statements[0].kind
654                {
655                    assert_eq!(decl, declarator);
656                }
657            }
658        }
659    }
660
661    #[test]
662    fn test_operators() {
663        // Test operators that work correctly
664        let cases = vec![
665            ("$a + $b", "+"),
666            ("$a - $b", "-"),
667            ("$a * $b", "*"),
668            ("$a . $b", "."),
669            ("$a && $b", "&&"),
670            ("$a || $b", "||"),
671        ];
672
673        for (code, expected_op) in cases {
674            let mut parser = Parser::new(code);
675            let result = parser.parse();
676            assert!(result.is_ok(), "Failed to parse: {}", code);
677
678            let ast = must(result);
679            if let NodeKind::Program { statements } = &ast.kind {
680                assert!(!statements.is_empty(), "No statements found in AST for: {}", code);
681
682                // Find the binary node, which might be wrapped in an ExpressionStatement
683                let binary_node = match &statements[0].kind {
684                    NodeKind::ExpressionStatement { expression } => match &expression.kind {
685                        NodeKind::Binary { op, left, right } => Some((op, left, right)),
686                        _ => None,
687                    },
688                    NodeKind::Binary { op, left, right } => Some((op, left, right)),
689                    _ => None,
690                };
691
692                assert!(
693                    binary_node.is_some(),
694                    "Expected Binary operator for: {}. Found: {:?}",
695                    code,
696                    statements[0].kind
697                );
698                if let Some((op, left, right)) = binary_node {
699                    assert_eq!(op, expected_op, "Operator mismatch for: {}", code);
700
701                    // Additional diagnostic information
702                    println!("Parsing: {}", code);
703                    println!("Left node: {:?}", left);
704                    println!("Right node: {:?}", right);
705                }
706            }
707            assert!(
708                matches!(ast.kind, NodeKind::Program { .. }),
709                "Expected Program node, found: {:?}",
710                ast.kind
711            );
712        }
713    }
714
715    #[test]
716    fn test_operators_with_context() {
717        // These operators require context-aware parsing to disambiguate from similar syntax:
718        // - `/` could be division or regex delimiter
719        // - `%` could be modulo or hash sigil
720        // - `**` could be exponent or glob pattern
721        // - `//` could be defined-or or regex delimiter
722        // The lexer handles disambiguation via LexerMode::ExpectTerm tracking.
723        let cases: Vec<(&str, &str)> = vec![
724            ("2 / 3", "/"),     // Division (not regex)
725            ("$a % $b", "%"),   // Modulo (not hash sigil)
726            ("$a ** $b", "**"), // Exponent (not glob)
727            ("$a // $b", "//"), // Defined-or (not regex)
728        ];
729
730        for (code, expected_op) in cases {
731            let mut parser = Parser::new(code);
732            let result = parser.parse();
733            assert!(result.is_ok(), "Failed to parse: {}", code);
734
735            let ast = must(result);
736            if let NodeKind::Program { statements } = &ast.kind {
737                assert!(!statements.is_empty(), "No statements found in AST for: {}", code);
738
739                // Find the binary node, which might be wrapped in an ExpressionStatement
740                let binary_node = match &statements[0].kind {
741                    NodeKind::ExpressionStatement { expression } => match &expression.kind {
742                        NodeKind::Binary { op, .. } => Some(op),
743                        _ => None,
744                    },
745                    NodeKind::Binary { op, .. } => Some(op),
746                    _ => None,
747                };
748
749                assert!(
750                    binary_node.is_some(),
751                    "Expected Binary operator for: {}. Found: {:?}",
752                    code,
753                    statements[0].kind
754                );
755                if let Some(op) = binary_node {
756                    assert_eq!(op, expected_op, "Operator mismatch for: {}", code);
757                }
758            }
759            assert!(
760                matches!(ast.kind, NodeKind::Program { .. }),
761                "Expected Program node, found: {:?}",
762                ast.kind
763            );
764        }
765    }
766
767    #[test]
768    fn test_string_literals() {
769        let cases = vec![r#""hello""#, r#"'world'"#, r#"qq{foo}"#, r#"q{bar}"#];
770
771        for code in cases {
772            let mut parser = Parser::new(code);
773            let result = parser.parse();
774            assert!(result.is_ok(), "Failed to parse: {}", code);
775        }
776    }
777
778    #[test]
779    fn test_arrays_and_hashes() {
780        let cases = vec![
781            "@array",
782            "%hash",
783            "$array[0]",
784            "$hash{key}",
785            "@array[1, 2, 3]",
786            "@hash{'a', 'b'}",
787        ];
788
789        for code in cases {
790            let mut parser = Parser::new(code);
791            let result = parser.parse();
792            assert!(result.is_ok(), "Failed to parse: {}", code);
793        }
794    }
795
796    #[test]
797    fn test_subroutines() {
798        let cases = vec![
799            "sub foo { }",
800            "sub bar { return 42; }",
801            "sub baz ($x, $y) { $x + $y }",
802            "sub qux :method { }",
803        ];
804
805        for code in cases {
806            let mut parser = Parser::new(code);
807            let result = parser.parse();
808            assert!(result.is_ok(), "Failed to parse: {}", code);
809
810            let ast = must(result);
811            if let NodeKind::Program { statements } = &ast.kind {
812                assert_eq!(statements.len(), 1);
813                assert!(matches!(statements[0].kind, NodeKind::Subroutine { .. }));
814            }
815        }
816    }
817
818    #[test]
819    fn test_control_flow() {
820        let cases = vec![
821            "if ($x) { }",
822            "if ($x) { } else { }",
823            "if ($x) { } elsif ($y) { } else { }",
824            "unless ($x) { }",
825            "while ($x) { }",
826            "until ($x) { }",
827            "for (my $i = 0; $i < 10; $i++) { }",
828            "foreach my $x (@array) { }",
829        ];
830
831        for code in cases {
832            let mut parser = Parser::new(code);
833            let result = parser.parse();
834            assert!(result.is_ok(), "Failed to parse: {}", code);
835        }
836    }
837
838    #[test]
839    fn test_regex() {
840        let cases = vec![
841            "/pattern/",
842            "m/pattern/",
843            "s/old/new/",
844            "tr/a-z/A-Z/",
845            r#"qr/\d+/"#,
846            "$x =~ /foo/",
847            "$x !~ /bar/",
848        ];
849
850        for code in cases {
851            let mut parser = Parser::new(code);
852            let result = parser.parse();
853            assert!(result.is_ok(), "Failed to parse: {}", code);
854        }
855    }
856
857    #[test]
858    fn test_error_cases() {
859        let cases = vec![
860            ("if (", "Unexpected end of input"),
861            ("sub (", "Unexpected end of input"),
862            ("my (", "Unexpected end of input"),
863            ("{", "Unexpected end of input"),
864        ];
865
866        for (code, _expected_error) in cases {
867            let mut parser = Parser::new(code);
868            let result = parser.parse();
869
870            // With error recovery, parse() succeeds but collects errors
871            assert!(result.is_ok(), "Parser should recover from errors for: {}", code);
872
873            // Check that errors were recorded
874            let errors = parser.errors();
875            assert!(!errors.is_empty(), "Expected recorded errors for: {}", code);
876        }
877    }
878
879    #[test]
880    fn test_modern_perl_features() {
881        let cases = vec![
882            "class Point { }",
883            "method new { }",
884            "try { } catch ($e) { }",
885            "defer { }",
886            "my $x :shared = 42;",
887        ];
888
889        for code in cases {
890            let mut parser = Parser::new(code);
891            let result = parser.parse();
892            assert!(result.is_ok(), "Failed to parse: {}", code);
893        }
894    }
895
896    #[test]
897    fn test_edge_cases() {
898        let cases = vec![
899            // Indirect object syntax
900            "print STDOUT 'hello';",
901            "new Class;",
902            // Multi-variable declarations
903            "my ($x, $y) = (1, 2);",
904            "my ($a :shared, $b :locked);",
905            // Complex expressions
906            "$x->@*",
907            "$x->%*",
908            "$x->$*",
909            // Defined-or
910            "$x // 'default'",
911            // ISA operator
912            "$obj ISA 'Class'",
913        ];
914
915        for code in cases {
916            let mut parser = Parser::new(code);
917            let result = parser.parse();
918            assert!(result.is_ok(), "Failed to parse edge case: {}", code);
919        }
920    }
921}