From fa5031fab2b585694fb2064341c46df207a700dc Mon Sep 17 00:00:00 2001 From: ydah Date: Fri, 3 Jan 2025 01:34:57 +0900 Subject: [PATCH] Move processing of lhs_s_value method to Binding class --- lib/lrama/grammar/binding.rb | 15 +++++++++++++++ lib/lrama/grammar/rule_builder.rb | 14 +------------- sig/lrama/grammar/binding.rbs | 2 ++ sig/lrama/grammar/rule_builder.rbs | 1 - 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/lrama/grammar/binding.rb b/lib/lrama/grammar/binding.rb index 5e6e7c59..5e528284 100644 --- a/lib/lrama/grammar/binding.rb +++ b/lib/lrama/grammar/binding.rb @@ -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) @@ -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 diff --git a/lib/lrama/grammar/rule_builder.rb b/lib/lrama/grammar/rule_builder.rb index 06097eb7..d82da5f9 100644 --- a/lib/lrama/grammar/rule_builder.rb +++ b/lib/lrama/grammar/rule_builder.rb @@ -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 @@ -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 diff --git a/sig/lrama/grammar/binding.rbs b/sig/lrama/grammar/binding.rbs index 3d6b1a6c..e8796455 100644 --- a/sig/lrama/grammar/binding.rbs +++ b/sig/lrama/grammar/binding.rbs @@ -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 diff --git a/sig/lrama/grammar/rule_builder.rbs b/sig/lrama/grammar/rule_builder.rbs index fdca1f6e..45955539 100644 --- a/sig/lrama/grammar/rule_builder.rbs +++ b/sig/lrama/grammar/rule_builder.rbs @@ -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?