Class: Tryouts::EnhancedParser
- Inherits:
-
Parsers::BaseParser
- Object
- Parsers::BaseParser
- Tryouts::EnhancedParser
- Defined in:
- lib/tryouts/parsers/enhanced_parser.rb
Overview
Enhanced parser using Prism's native comment extraction for robust parsing
The EnhancedParser is the default parser that provides syntax-aware comment extraction by leveraging Ruby's official Prism parser. This approach eliminates common parsing issues found in regex-based parsers, particularly with complex Ruby syntax.
Key Benefits
1. HEREDOC Safety
- Uses Prism's
parse_comments()to extract only actual Ruby comments - Automatically excludes content inside string literals, HEREDOCs, and interpolation
- Prevents false positive expectation detection
2. Inline Comment Handling
- Correctly handles lines with both code and comments
- Supports multiple comments per line with proper positioning
- Emits separate tokens for code and comment content
3. Syntax Awareness
- Leverages Ruby's official parser for accurate code understanding
- Handles complex Ruby syntax edge cases reliably
- More robust than regex-based parsing approaches
4. Performance
- Uses optimized C-based Prism parsing for comment extraction
- Efficient handling of large files with complex syntax
Pattern Matching
Uses Ruby 3.4+ pattern matching (case/in) for token classification,
providing clean, modern syntax for expectation type detection.
Instance Method Summary collapse
-
#parse ⇒ Tryouts::Testrun
Parse source code into a Testrun using Prism-based comment extraction.
Instance Method Details
#parse ⇒ Tryouts::Testrun
Parse source code into a Testrun using Prism-based comment extraction
This method provides the main parsing logic that converts raw Ruby source code containing tryout syntax into structured test cases. Uses Prism's native comment extraction to avoid HEREDOC parsing issues.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tryouts/parsers/enhanced_parser.rb', line 73 def parse return handle_syntax_errors if @prism_result.failure? # Line ranges of multi-line statements, computed once from the whole-file # parse. A column-0 comment strictly inside one of these spans (e.g. a # #=>-shaped line inside an unclosed array literal) cannot be a real # block boundary or expectation, no matter what it looks like lexically. @multiline_statement_ranges = multiline_statement_ranges # Use inhouse comment extraction instead of line-by-line regex parsing # This automatically excludes HEREDOC content! tokens = tokenize_content_with_inhouse_extraction test_boundaries = find_test_case_boundaries(tokens) tokens = classify_potential_descriptions_with_boundaries(tokens, test_boundaries) test_blocks = group_into_test_blocks(tokens) process_test_blocks(test_blocks) end |