Class: TRuby::Parser
- Inherits:
-
Object
- Object
- TRuby::Parser
- Defined in:
- lib/t_ruby/parser.rb
Overview
Enhanced Parser using Parser Combinator for complex type expressions Maintains backward compatibility with original Parser interface
Constant Summary collapse
- VALID_TYPES =
Type names that are recognized as valid
%w[String Integer Boolean Array Hash Symbol void nil].freeze
- IDENTIFIER_CHAR =
Pattern for method/variable names that supports Unicode characters pL matches any Unicode letter, pN matches any Unicode number
'[\p{L}\p{N}_]'- METHOD_NAME_PATTERN =
Method names can end with ? or !
"#{IDENTIFIER_CHAR}+[?!]?".freeze
- VISIBILITY_PATTERN =
Visibility modifiers for method definitions
'(?:(?:private|protected|public)\s+)?'
Instance Attribute Summary collapse
-
#ir_program ⇒ Object
readonly
TODO: Replace regex-based parsing with TokenDeclarationParser See: lib/t_ruby/parser_combinator/token/token_declaration_parser.rb.
-
#source ⇒ Object
readonly
TODO: Replace regex-based parsing with TokenDeclarationParser See: lib/t_ruby/parser_combinator/token/token_declaration_parser.rb.
Instance Method Summary collapse
-
#initialize(source, parse_body: true) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
-
#parse_to_ir ⇒ Object
Parse to IR directly (new API).
-
#parse_type(type_string) ⇒ Object
Parse a type expression using combinator.
Constructor Details
#initialize(source, parse_body: true) ⇒ Parser
Returns a new instance of Parser.
23 24 25 26 27 28 29 30 |
# File 'lib/t_ruby/parser.rb', line 23 def initialize(source, parse_body: true) @source = source @lines = source.split("\n") @parse_body = parse_body @type_parser = ParserCombinator::TypeParser.new @body_parser = ParserCombinator::TokenBodyParser.new if parse_body @ir_program = nil end |
Instance Attribute Details
#ir_program ⇒ Object (readonly)
TODO: Replace regex-based parsing with TokenDeclarationParser See: lib/t_ruby/parser_combinator/token/token_declaration_parser.rb
21 22 23 |
# File 'lib/t_ruby/parser.rb', line 21 def ir_program @ir_program end |
#source ⇒ Object (readonly)
TODO: Replace regex-based parsing with TokenDeclarationParser See: lib/t_ruby/parser_combinator/token/token_declaration_parser.rb
21 22 23 |
# File 'lib/t_ruby/parser.rb', line 21 def source @source end |
Instance Method Details
#parse ⇒ Object
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 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 98 99 100 101 102 103 104 105 |
# File 'lib/t_ruby/parser.rb', line 32 def parse functions = [] type_aliases = [] interfaces = [] classes = [] i = 0 # Pre-detect heredoc regions to skip heredoc_ranges = HeredocDetector.detect(@lines) while i < @lines.length # Skip lines inside heredoc content if HeredocDetector.inside_heredoc?(i, heredoc_ranges) i += 1 next end 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 class definitions if line.match?(/^\s*class\s+\w+/) class_info, next_i = parse_class(i) if class_info classes << class_info i = next_i next end end # Match function definitions (top-level only, not inside class) if line.match?(/^\s*#{VISIBILITY_PATTERN}def\s+#{IDENTIFIER_CHAR}+/) func_info, next_i = parse_function_with_body(i) if func_info functions << func_info i = next_i next end end i += 1 end result = { type: :success, functions: functions, type_aliases: type_aliases, interfaces: interfaces, classes: classes, } # Build IR builder = IR::Builder.new @ir_program = builder.build(result, source: @source) result rescue Scanner::ScanError => e raise ParseError.new(e., line: e.line, column: e.column) end |
#parse_to_ir ⇒ Object
Parse to IR directly (new API)
108 109 110 111 |
# File 'lib/t_ruby/parser.rb', line 108 def parse_to_ir parse unless @ir_program @ir_program end |
#parse_type(type_string) ⇒ Object
Parse a type expression using combinator
114 115 116 117 |
# File 'lib/t_ruby/parser.rb', line 114 def parse_type(type_string) result = @type_parser.parse(type_string) result[:success] ? result[:type] : nil end |