Class: TRuby::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/parser.rb

Overview

Enhanced Parser using Parser Combinator for complex type expressions Maintains backward compatibility with original Parser interface

Direct Known Subclasses

LegacyParser

Constant Summary collapse

VALID_TYPES =

Type names that are recognized as valid

%w[String Integer Boolean Array Hash Symbol void nil].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, use_combinator: true) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
16
17
18
# File 'lib/t_ruby/parser.rb', line 12

def initialize(source, use_combinator: true)
  @source = source
  @lines = source.split("\n")
  @use_combinator = use_combinator
  @type_parser = ParserCombinator::TypeParser.new if use_combinator
  @ir_program = nil
end

Instance Attribute Details

#ir_programObject (readonly)

Returns the value of attribute ir_program.



10
11
12
# File 'lib/t_ruby/parser.rb', line 10

def ir_program
  @ir_program
end

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/t_ruby/parser.rb', line 10

def source
  @source
end

#use_combinatorObject (readonly)

Returns the value of attribute use_combinator.



10
11
12
# File 'lib/t_ruby/parser.rb', line 10

def use_combinator
  @use_combinator
end

Instance Method Details

#parseObject



20
21
22
23
24
25
26
27
28
29
30
31
32
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
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/t_ruby/parser.rb', line 20

def parse
  functions = []
  type_aliases = []
  interfaces = []
  i = 0

  while i < @lines.length
    line = @lines[i]

    # Match type alias definitions
    if line.match?(/^\s*type\s+\w+/)
      alias_info = parse_type_alias(line)
      type_aliases << alias_info if alias_info
    end

    # Match interface definitions
    if line.match?(/^\s*interface\s+\w+/)
      interface_info, next_i = parse_interface(i)
      if interface_info
        interfaces << interface_info
        i = next_i
        next
      end
    end

    # Match function definitions
    if line.match?(/^\s*def\s+\w+/)
      func_info = parse_function_definition(line)
      functions << func_info if func_info
    end

    i += 1
  end

  result = {
    type: :success,
    functions: functions,
    type_aliases: type_aliases,
    interfaces: interfaces
  }

  # Build IR if combinator is enabled
  if @use_combinator
    builder = IR::Builder.new
    @ir_program = builder.build(result, source: @source)
  end

  result
end

#parse_to_irObject

Parse to IR directly (new API)



71
72
73
74
# File 'lib/t_ruby/parser.rb', line 71

def parse_to_ir
  parse unless @ir_program
  @ir_program
end

#parse_type(type_string) ⇒ Object

Parse a type expression using combinator (new API)



77
78
79
80
81
82
# File 'lib/t_ruby/parser.rb', line 77

def parse_type(type_string)
  return nil unless @use_combinator

  result = @type_parser.parse(type_string)
  result[:success] ? result[:type] : nil
end