Skip to content

Commit a7c451d

Browse files
authored
refactor: migrate parser from regex to token-based parser combinator (#29)
* refactor: migrate parser from regex to token-based parser combinator - Replace monolithic parser_combinator.rb (2833 lines) with modular architecture - Add Scanner for tokenization with regex literal support - Create IR::InterpolatedString for string interpolation parsing - Fix type inference for interpolated strings (returns String) - Add TRuby::ParseError for unified error handling - Organize parsers into primitives/, combinators/, and token/ directories - Each file contains exactly one class (snake_case filename matches PascalCase class) * fix: enhance parser to support ternary, splat args, and statement expressions - Add ternary operator (? :) parsing in ExpressionParser - Support double splat (**opts) and single splat (*args) in method calls - Support keyword arguments (name: value) in method calls - Allow case/if/unless/begin as assignment right-hand side values - Improve generic type compatibility (Array[untyped] with Array[T]) Fixes type inference errors in keyword_args samples. * style: fix RuboCop violations and adjust metrics limits * fix: require set for Ruby 3.1 compatibility
1 parent fda0993 commit a7c451d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+6088
-1505
lines changed

.rubocop.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Metrics/BlockNesting:
4343
- "lib/**/*"
4444

4545
Metrics/ClassLength:
46-
Max: 700
46+
Max: 750
4747

4848
Metrics/ModuleLength:
4949
Max: 700
@@ -52,7 +52,7 @@ Metrics/AbcSize:
5252
Max: 120
5353

5454
Metrics/CyclomaticComplexity:
55-
Max: 30
55+
Max: 35
5656

5757
Metrics/PerceivedComplexity:
5858
Max: 30

lib/t_ruby.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
require_relative "t_ruby/string_utils"
1111
require_relative "t_ruby/ir"
1212
require_relative "t_ruby/parser_combinator"
13+
require_relative "t_ruby/scanner"
1314
require_relative "t_ruby/smt_solver"
1415

1516
# Basic components
1617
require_relative "t_ruby/type_alias_registry"
1718
require_relative "t_ruby/heredoc_detector"
18-
require_relative "t_ruby/body_parser"
1919
require_relative "t_ruby/parser"
2020
require_relative "t_ruby/union_type_parser"
2121
require_relative "t_ruby/generic_type_parser"
@@ -51,4 +51,15 @@
5151
require_relative "t_ruby/docs_badge_generator"
5252

5353
module TRuby
54+
# Parse error for T-Ruby source code
55+
class ParseError < StandardError
56+
attr_reader :line, :column, :source
57+
58+
def initialize(message, line: nil, column: nil, source: nil)
59+
@line = line
60+
@column = column
61+
@source = source
62+
super(message)
63+
end
64+
end
5465
end

lib/t_ruby/ast_type_inferrer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def infer_expression(node, env)
158158
type = case node
159159
when IR::Literal
160160
infer_literal(node)
161+
when IR::InterpolatedString
162+
"String" # Interpolated strings always produce String
161163
when IR::VariableRef
162164
infer_variable_ref(node, env)
163165
when IR::BinaryOp

0 commit comments

Comments
 (0)