Class: TRuby::ParserCombinator::TokenBodyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/parser_combinator/token/token_body_parser.rb

Overview

Token-based body parser - replaces regex-based BodyParser Provides the same interface as BodyParser.parse(lines, start_line, end_line)

Instance Method Summary collapse

Constructor Details

#initializeTokenBodyParser

Returns a new instance of TokenBodyParser.



8
9
10
# File 'lib/t_ruby/parser_combinator/token/token_body_parser.rb', line 8

def initialize
  @statement_parser = StatementParser.new
end

Instance Method Details

#parse(lines, start_line, end_line) ⇒ IR::Block

Parse method body from lines array

Parameters:

  • lines (Array<String>)

    source code lines

  • start_line (Integer)

    starting line index (0-based)

  • end_line (Integer)

    ending line index (exclusive)

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/t_ruby/parser_combinator/token/token_body_parser.rb', line 17

def parse(lines, start_line, end_line)
  # Extract the body source
  body_lines = lines[start_line...end_line]
  source = body_lines.join("\n")

  return IR::Block.new(statements: []) if source.strip.empty?

  # Scan and parse
  scanner = TRuby::Scanner.new(source)
  tokens = scanner.scan_all

  result = @statement_parser.parse_block(tokens, 0)

  if result.success?
    result.value
  else
    # Fallback to empty block on parse failure
    IR::Block.new(statements: [])
  end
end

#parse_expression(expr) ⇒ IR::Node

Parse a single expression string

Parameters:

  • expr (String)

    expression to parse

Returns:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/t_ruby/parser_combinator/token/token_body_parser.rb', line 41

def parse_expression(expr)
  return nil if expr.nil? || expr.strip.empty?

  scanner = TRuby::Scanner.new(expr)
  tokens = scanner.scan_all

  expression_parser = ExpressionParser.new
  result = expression_parser.parse_expression(tokens, 0)

  result.success? ? result.value : IR::RawCode.new(code: expr)
end