Class: TRuby::ParserCombinator::TokenDeclarationParser

Inherits:
Object
  • Object
show all
Includes:
TokenDSL
Defined in:
lib/t_ruby/parser_combinator/token/token_declaration_parser.rb

Overview

Token Declaration Parser - Parse top-level declarations

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TokenDSL

#keyword, #token

Constructor Details

#initializeTokenDeclarationParser

Returns a new instance of TokenDeclarationParser.



27
28
29
30
31
# File 'lib/t_ruby/parser_combinator/token/token_declaration_parser.rb', line 27

def initialize
  @statement_parser = StatementParser.new
  @expression_parser = ExpressionParser.new
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



25
26
27
# File 'lib/t_ruby/parser_combinator/token/token_declaration_parser.rb', line 25

def errors
  @errors
end

Instance Method Details

#has_errors?Boolean

Check if parsing encountered any errors

Returns:



100
101
102
# File 'lib/t_ruby/parser_combinator/token/token_declaration_parser.rb', line 100

def has_errors?
  !@errors.empty?
end

#parse_declaration(tokens, position = 0) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/t_ruby/parser_combinator/token/token_declaration_parser.rb', line 33

def parse_declaration(tokens, position = 0)
  return TokenParseResult.failure("End of input", tokens, position) if position >= tokens.length

  position = skip_newlines(tokens, position)
  return TokenParseResult.failure("End of input", tokens, position) if position >= tokens.length

  token = tokens[position]

  case token.type
  when :def
    parse_method_def(tokens, position)
  when :public, :private, :protected
    parse_visibility_method(tokens, position)
  when :class
    parse_class(tokens, position)
  when :module
    parse_module(tokens, position)
  when :type
    parse_type_alias(tokens, position)
  when :interface
    parse_interface(tokens, position)
  else
    TokenParseResult.failure("Expected declaration, got #{token.type}", tokens, position)
  end
end

#parse_program(tokens, position = 0) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/t_ruby/parser_combinator/token/token_declaration_parser.rb', line 59

def parse_program(tokens, position = 0)
  declarations = []
  @errors = []

  loop do
    position = skip_newlines(tokens, position)
    break if position >= tokens.length
    break if tokens[position].type == :eof

    token = tokens[position]

    # Check if this looks like a declaration keyword
    unless declaration_keyword?(token.type)
      # Not a declaration - skip to next line (top-level expression is allowed)
      position = skip_to_next_line(tokens, position)
      next
    end

    result = parse_declaration(tokens, position)

    if result.failure?
      # Collect error and try to recover
      # Use result.position for accurate error location (where the error actually occurred)
      error_pos = result.position
      error_token = tokens[error_pos] if error_pos < tokens.length
      @errors << ParseError.new(result.error, token: error_token)

      # Try to skip to next declaration (find next 'def', 'class', etc.)
      position = skip_to_next_declaration(tokens, position)
      next
    end

    declarations << result.value
    position = result.position
  end

  program = IR::Program.new(declarations: declarations)
  TokenParseResult.success(program, tokens, position)
end