Skip to content

Commit

Permalink
Extract rule_builders selection logic into private method
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Dec 24, 2023
1 parent 904318b commit 350e4ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/lrama/grammar/parameterizing_rule_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ def add_parameterizing_rule_builder(builder)
end

def defined?(name)
@parameterizing_rule_builders.any? { |builder| builder.name == name }
!rule_builders(name).empty?
end

def build_rules(token, rule_counter, lhs_tag, line)
builder = @parameterizing_rule_builders.select { |b| b.name == token.s_value }.last
builder = rule_builders(token.s_value).last
raise "Unknown parameterizing rule #{token.s_value} at line #{token.line}" unless builder

builder.build_rules(token, token.args, rule_counter, lhs_tag, line, @parameterizing_rule_builders)
end

private

def rule_builders(name)
@parameterizing_rule_builders.select { |builder| builder.name == name }
end
end
end
end
4 changes: 4 additions & 0 deletions sig/lrama/grammar/parameterizing_rule_resolver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module Lrama
def add_parameterizing_rule_builder: (Grammar::ParameterizingRuleBuilder builder) -> void
def defined?: (String) -> bool
def build_rules: (Lexer::Token::InstantiateRule token, Counter rule_counter, untyped lhs_tag, Integer? line) -> Array[Grammar::ParameterizingRule]

private

def rule_builders: (String) -> Array[Grammar::ParameterizingRuleBuilder]
end
end
end

0 comments on commit 350e4ad

Please sign in to comment.