Skip to content

Commit

Permalink
Merge pull request #510 from ydah/refactor-binding
Browse files Browse the repository at this point in the history
Refactor for Lrama::Grammar::Binding
  • Loading branch information
ydah authored Jan 13, 2025
2 parents ae0b85e + 6b3f92d commit 557108d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 38 deletions.
47 changes: 32 additions & 15 deletions lib/lrama/grammar/binding.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions sig/generated/lrama/grammar/binding.rbs
Original file line number Diff line number Diff line change
@@ -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
21 changes: 0 additions & 21 deletions sig/lrama/grammar/binding.rbs

This file was deleted.

0 comments on commit 557108d

Please sign in to comment.