Skip to content

Commit

Permalink
Move processing of lhs_s_value method to Binding class
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Jan 2, 2025
1 parent 214c511 commit fa5031f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
15 changes: 15 additions & 0 deletions lib/lrama/grammar/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def resolve_symbol(symbol)
end
end

def concatenated_args_str(token)
"#{token.rule_name}_#{token_to_args_s_values(token).join('_')}"
end

private

def parameter_to_arg(symbol)
Expand All @@ -30,6 +34,17 @@ def parameter_to_arg(symbol)
end
arg
end

def token_to_args_s_values(token)
token.args.flat_map do |arg|
resolved = resolve_symbol(arg)
if resolved.is_a?(Lexer::Token::InstantiateRule)
[resolved.s_value] + resolved.args.map(&:s_value)
else
[resolved.s_value]
end
end
end
end
end
end
14 changes: 1 addition & 13 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def process_rhs
raise "Unexpected token. #{token}" unless parameterizing_rule

bindings = Binding.new(parameterizing_rule, token.args)
lhs_s_value = lhs_s_value(token, bindings)
lhs_s_value = bindings.concatenated_args_str(token)
if (created_lhs = @parameterizing_rule_resolver.created_lhs(lhs_s_value))
@replaced_rhs << created_lhs
else
Expand Down Expand Up @@ -174,18 +174,6 @@ def process_rhs
end
end

def lhs_s_value(token, bindings)
s_values = token.args.map do |arg|
resolved = bindings.resolve_symbol(arg)
if resolved.is_a?(Lexer::Token::InstantiateRule)
[resolved.s_value, resolved.args.map(&:s_value)]
else
resolved.s_value
end
end
"#{token.rule_name}_#{s_values.join('_')}"
end

def resolve_inline_rhs(rule_builder, inline_rhs, index, bindings = nil)
rhs.each_with_index do |token, i|
if index == i
Expand Down
2 changes: 2 additions & 0 deletions sig/lrama/grammar/binding.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ module Lrama

def initialize: (Grammar::ParameterizingRule::Rule parameterizing_rule, Array[Lexer::Token] actual_args) -> void
def resolve_symbol: (Lexer::Token symbol) -> Lexer::Token
def concatenated_args_str: (Lexer::Token::InstantiateRule token) -> String

private

def parameter_to_arg: (Lexer::Token symbol) -> Lexer::Token?
def token_to_args_s_values: (Lexer::Token::InstantiateRule token) -> Array[String]
end
end
end
1 change: 0 additions & 1 deletion sig/lrama/grammar/rule_builder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module Lrama
def preprocess_references: () -> void
def build_rules: () -> void
def process_rhs: () -> void
def lhs_s_value: (Lexer::Token::InstantiateRule token, Grammar::Binding bindings) -> String
def resolve_inline: () -> void
def resolve_inline_rhs: (RuleBuilder rule_builder, Grammar::ParameterizingRule::Rhs inline_rhs, Integer index, ?Binding bindings) -> void
def replace_inline_user_code: (Grammar::ParameterizingRule::Rhs inline_rhs, Integer index) -> Lexer::Token::UserCode?
Expand Down

0 comments on commit fa5031f

Please sign in to comment.