diff --git a/lib/lrama/grammar/binding.rb b/lib/lrama/grammar/binding.rb index 5e528284..2efb918a 100644 --- a/lib/lrama/grammar/binding.rb +++ b/lib/lrama/grammar/binding.rb @@ -1,40 +1,57 @@ +# rbs_inline: enabled # frozen_string_literal: true module Lrama class Grammar class Binding - attr_reader :actual_args, :count + # @rbs @actual_args: Array[Lexer::Token] + # @rbs @param_to_arg: Hash[String, Lexer::Token] - def initialize(parameterizing_rule, actual_args) - @parameters = parameterizing_rule.parameters + # @rbs (Array[Lexer::Token] params, Array[Lexer::Token] actual_args) -> void + def initialize(params, actual_args) @actual_args = actual_args - @parameter_to_arg = @parameters.zip(actual_args).map do |param, arg| - [param.s_value, arg] - end.to_h + @param_to_arg = map_params_to_args(params, @actual_args) end - def resolve_symbol(symbol) - if symbol.is_a?(Lexer::Token::InstantiateRule) - resolved_args = symbol.args.map { |arg| resolve_symbol(arg) } - Lrama::Lexer::Token::InstantiateRule.new(s_value: symbol.s_value, location: symbol.location, args: resolved_args, lhs_tag: symbol.lhs_tag) + # @rbs (Lexer::Token sym) -> Lexer::Token + def resolve_symbol(sym) + if sym.is_a?(Lexer::Token::InstantiateRule) + Lrama::Lexer::Token::InstantiateRule.new( + s_value: sym.s_value, location: sym.location, args: resolved_args(sym), lhs_tag: sym.lhs_tag + ) else - parameter_to_arg(symbol) || symbol + param_to_arg(sym) end end + # @rbs (Lexer::Token::InstantiateRule token) -> String def concatenated_args_str(token) "#{token.rule_name}_#{token_to_args_s_values(token).join('_')}" end private - def parameter_to_arg(symbol) - if (arg = @parameter_to_arg[symbol.s_value].dup) - arg.alias_name = symbol.alias_name + # @rbs (Array[Lexer::Token] params, Array[Lexer::Token] actual_args) -> Hash[String, Lexer::Token] + def map_params_to_args(params, actual_args) + params.zip(actual_args).map do |param, arg| + [param.s_value, arg] + end.to_h + end + + # @rbs (Lexer::Token::InstantiateRule sym) -> Array[Lexer::Token] + def resolved_args(sym) + sym.args.map { |arg| resolve_symbol(arg) } + end + + # @rbs (Lexer::Token sym) -> Lexer::Token + def param_to_arg(sym) + if (arg = @param_to_arg[sym.s_value].dup) + arg.alias_name = sym.alias_name end - arg + arg || sym end + # @rbs (Lexer::Token::InstantiateRule token) -> Array[String] def token_to_args_s_values(token) token.args.flat_map do |arg| resolved = resolve_symbol(arg) diff --git a/lib/lrama/grammar/rule_builder.rb b/lib/lrama/grammar/rule_builder.rb index d82da5f9..481a3780 100644 --- a/lib/lrama/grammar/rule_builder.rb +++ b/lib/lrama/grammar/rule_builder.rb @@ -73,7 +73,7 @@ def resolve_inline_rules inline_rule.rhs_list.each do |inline_rhs| rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, @parameterizing_rule_resolver, lhs_tag: lhs_tag) if token.is_a?(Lexer::Token::InstantiateRule) - resolve_inline_rhs(rule_builder, inline_rhs, i, Binding.new(inline_rule, token.args)) + resolve_inline_rhs(rule_builder, inline_rhs, i, Binding.new(inline_rule.parameters, token.args)) else resolve_inline_rhs(rule_builder, inline_rhs, i) end @@ -135,7 +135,7 @@ def process_rhs parameterizing_rule = @parameterizing_rule_resolver.find_rule(token) raise "Unexpected token. #{token}" unless parameterizing_rule - bindings = Binding.new(parameterizing_rule, token.args) + bindings = Binding.new(parameterizing_rule.parameters, token.args) lhs_s_value = bindings.concatenated_args_str(token) if (created_lhs = @parameterizing_rule_resolver.created_lhs(lhs_s_value)) @replaced_rhs << created_lhs diff --git a/sig/generated/lrama/grammar/binding.rbs b/sig/generated/lrama/grammar/binding.rbs new file mode 100644 index 00000000..c6818a2e --- /dev/null +++ b/sig/generated/lrama/grammar/binding.rbs @@ -0,0 +1,34 @@ +# Generated from lib/lrama/grammar/binding.rb with RBS::Inline + +module Lrama + class Grammar + class Binding + @actual_args: Array[Lexer::Token] + + @param_to_arg: Hash[String, Lexer::Token] + + # @rbs (Array[Lexer::Token] params, Array[Lexer::Token] actual_args) -> void + def initialize: (Array[Lexer::Token] params, Array[Lexer::Token] actual_args) -> void + + # @rbs (Lexer::Token sym) -> Lexer::Token + def resolve_symbol: (Lexer::Token sym) -> Lexer::Token + + # @rbs (Lexer::Token::InstantiateRule token) -> String + def concatenated_args_str: (Lexer::Token::InstantiateRule token) -> String + + private + + # @rbs (Array[Lexer::Token] params, Array[Lexer::Token] actual_args) -> Hash[String, Lexer::Token] + def map_params_to_args: (Array[Lexer::Token] params, Array[Lexer::Token] actual_args) -> Hash[String, Lexer::Token] + + # @rbs (Lexer::Token::InstantiateRule sym) -> Array[Lexer::Token] + def resolved_args: (Lexer::Token::InstantiateRule sym) -> Array[Lexer::Token] + + # @rbs (Lexer::Token sym) -> Lexer::Token + def param_to_arg: (Lexer::Token sym) -> Lexer::Token + + # @rbs (Lexer::Token::InstantiateRule token) -> Array[String] + def token_to_args_s_values: (Lexer::Token::InstantiateRule token) -> Array[String] + end + end +end diff --git a/sig/lrama/grammar/binding.rbs b/sig/lrama/grammar/binding.rbs deleted file mode 100644 index e8796455..00000000 --- a/sig/lrama/grammar/binding.rbs +++ /dev/null @@ -1,21 +0,0 @@ -module Lrama - class Grammar - class Binding - attr_reader actual_args: Array[Lexer::Token] - attr_reader count: Integer - - @required_parameters_count: Integer - @parameters: Array[Lexer::Token] - @parameter_to_arg: untyped - - 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