Class: Tryouts::Parsers::BaseParser Abstract
- Inherits:
-
Object
- Object
- Tryouts::Parsers::BaseParser
- Includes:
- SharedMethods
- Defined in:
- lib/tryouts/parsers/base_parser.rb
Overview
Subclass and implement #parse to create a concrete parser
Base class for all tryout parsers providing common functionality
BaseParser establishes the foundation for parsing tryout files by handling file loading, Prism integration, and providing shared parsing infrastructure. All concrete parser implementations (EnhancedParser) inherit from this class.
Shared Functionality
1. File and Source Management
- Automatic file reading and line splitting
- UTF-8 encoding handling
- Path normalization and validation
2. Prism Integration
- Automatic Prism parsing of source code
- Syntax error detection and handling
- AST access for advanced parsing needs
3. Warning System
- Centralized warning collection and management
- Type-safe warning objects with context
- Integration with output formatters
4. Shared Methods
- Token grouping and classification logic
- Test case boundary detection
- Common utility methods for all parsers
Parser Requirements
Concrete parser implementations must:
- Implement the abstract
parsemethod - Return a
Tryouts::Testrunobject - Handle syntax errors appropriately
- Provide a unique
parser_typeidentifier
Direct Known Subclasses
Instance Attribute Summary collapse
-
#warnings ⇒ Array<Tryouts::ParserWarning>
readonly
Collection of parsing warnings.
Instance Method Summary collapse
-
#initialize(source_path, options = {}) ⇒ BaseParser
constructor
Initialize a new parser instance.
-
#parse ⇒ Tryouts::Testrun
abstract
Parse the source file into structured test data.
Methods included from SharedMethods
#add_context_to_block, #add_warning, #block_has_content?, #build_orphan_block, #build_setup, #build_teardown, #build_test_case, #calculate_block_range, #calculate_end_line, #classify_blocks, #classify_potential_descriptions_with_boundaries, #collect_code_tokens, #collect_unnamed_test_warning, #extract_code_content, #extract_pure_code_from_blocks, #find_test_case_boundaries, #find_test_case_end, #first_code_line, #group_into_test_blocks, #handle_syntax_errors, #has_following_test_pattern?, #is_expectation_type?, #new_test_block, #parse_expectation, #parse_ruby_line, #positioned_code_tokens, #process_test_blocks, #validate_strict_mode
Constructor Details
#initialize(source_path, options = {}) ⇒ BaseParser
Initialize a new parser instance
92 93 94 95 96 97 98 99 100 |
# File 'lib/tryouts/parsers/base_parser.rb', line 92 def initialize(source_path, = {}) @source_path = source_path @source = File.read(source_path) @lines = @source.lines.map(&:chomp) @prism_result = Prism.parse(@source) @parsed_at = Time.now @options = @warnings = [] end |
Instance Attribute Details
#warnings ⇒ Array<Tryouts::ParserWarning> (readonly)
Returns Collection of parsing warnings.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/tryouts/parsers/base_parser.rb', line 81 class BaseParser include Tryouts::Parsers::SharedMethods # Initialize a new parser instance # # @param source_path [String] Absolute path to the tryout source file # @param options [Hash] Configuration options for parsing behavior # @option options [Boolean] :strict Enable strict mode validation # @option options [Boolean] :warnings Enable warning collection (default: true) # @raise [Errno::ENOENT] If source file doesn't exist # @raise [Errno::EACCES] If source file isn't readable def initialize(source_path, = {}) @source_path = source_path @source = File.read(source_path) @lines = @source.lines.map(&:chomp) @prism_result = Prism.parse(@source) @parsed_at = Time.now @options = @warnings = [] end # Parse the source file into structured test data # # @abstract Subclasses must implement this method # @return [Tryouts::Testrun] Parsed test structure with setup, tests, teardown, and warnings # @raise [NotImplementedError] If called directly on BaseParser def parse raise NotImplementedError, "Subclasses must implement #parse" end protected # Get the parser type identifier # # @abstract Subclasses should override to provide unique identifier # @return [Symbol] Parser type identifier def parser_type :base end # Access to instance variables for subclasses attr_reader :source_path, :source, :lines, :prism_result, :parsed_at, :options end |
Instance Method Details
#parse ⇒ Tryouts::Testrun
Subclasses must implement this method
Parse the source file into structured test data
107 108 109 |
# File 'lib/tryouts/parsers/base_parser.rb', line 107 def parse raise NotImplementedError, "Subclasses must implement #parse" end |