Class: Tryouts::Parsers::BaseParser Abstract

Inherits:
Object
  • Object
show all
Includes:
SharedMethods
Defined in:
lib/tryouts/parsers/base_parser.rb

Overview

This class is abstract.

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:

  1. Implement the abstract parse method
  2. Return a Tryouts::Testrun object
  3. Handle syntax errors appropriately
  4. Provide a unique parser_type identifier

Examples:

Implementing a custom parser

class MyCustomParser < Tryouts::Parsers::BaseParser
  def parse
    # Your parsing logic here
    # Must return a Tryouts::Testrun object
  end

  private

  def parser_type
    :custom
  end
end

See Also:

Since:

  • 3.0.0

Direct Known Subclasses

EnhancedParser

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • source_path (String)

    Absolute path to the tryout source file

  • options (Hash) (defaults to: {})

    Configuration options for parsing behavior

Options Hash (options):

  • :strict (Boolean)

    Enable strict mode validation

  • :warnings (Boolean)

    Enable warning collection (default: true)

Raises:

  • (Errno::ENOENT)

    If source file doesn't exist

  • (Errno::EACCES)

    If source file isn't readable

Since:

  • 3.0.0



92
93
94
95
96
97
98
99
100
# File 'lib/tryouts/parsers/base_parser.rb', line 92

def initialize(source_path, options = {})
  @source_path  = source_path
  @source       = File.read(source_path)
  @lines        = @source.lines.map(&:chomp)
  @prism_result = Prism.parse(@source)
  @parsed_at    = Time.now
  @options      = options
  @warnings     = []
end

Instance Attribute Details

#warningsArray<Tryouts::ParserWarning> (readonly)

Returns Collection of parsing warnings.

Returns:



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, options = {})
    @source_path  = source_path
    @source       = File.read(source_path)
    @lines        = @source.lines.map(&:chomp)
    @prism_result = Prism.parse(@source)
    @parsed_at    = Time.now
    @options      = 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

#parseTryouts::Testrun

This method is abstract.

Subclasses must implement this method

Parse the source file into structured test data

Returns:

  • (Tryouts::Testrun)

    Parsed test structure with setup, tests, teardown, and warnings

Raises:

  • (NotImplementedError)

    If called directly on BaseParser

Since:

  • 3.0.0



107
108
109
# File 'lib/tryouts/parsers/base_parser.rb', line 107

def parse
  raise NotImplementedError, "Subclasses must implement #parse"
end