Add support for aliased Named References for actions of rhs in Parameterizing rules
This PR support for aliased Named References for actions of rhs in Parameterizing rules.
Motivation
If there is a non-terminal symbol in the RHS of parameterizing rules as shown below, I want to access it in the Named Reference.
%rule plus_n(X): X term[number] { $$ = $1 + $number }
;
Can I ask the use case of this functionality? We can use arbitrary name for the variable (X) then if we want to access Y by $alias, we can write %rule pair(X, alias):.
@yui-knk I changed it to an appropriate example in PR description. For example, I think it makes sense to set an alias for nterms, which is not a parameter in rhs for parameterization rules. WDYT?
IIUC aliased Named References for non parameter in RHS is already supported.
@yui-knk Is there any blocker left for this pull request?
@ydah I'm bit confused because:
- It seems the test case passes without source code changes
diff --git a/lib/lrama/grammar/binding.rb b/lib/lrama/grammar/binding.rb
index 21b6ad5..d215ad0 100644
--- a/lib/lrama/grammar/binding.rb
+++ b/lib/lrama/grammar/binding.rb
@@ -16,7 +16,7 @@ module Lrama
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)
else
- parameter_to_arg(symbol) || symbol
+ @parameter_to_arg[symbol.s_value] || symbol
end
end
$ bundle exec rake
...
Finished in 9.62 seconds (files took 0.27494 seconds to load)
219 examples, 0 failures
- It seems PR description is misaligned
If there is a non-terminal symbol in the RHS of parameterizing rules as shown below,
%rule plus_n(X): X term[number] { $$ = $1 + $number }
;
But I guess term might be terminal (!= non-terminal).
Could you check them?
@yui-knk
- The test case was incorrect. I add commit https://github.com/ruby/lrama/commit/db76b6317eeb78b501b8ceb2d36f46e6f12c5f56 Correctly, specifying a Named Reference for a parameter results in the following error:
Failures:
1) Lrama::Parser#parse when parameterizing rules when user defined rules with action with named references expands parameterizing rules
Failure/Error: raise location.generate_error_message(message)
RuntimeError:
parameterizing_rules/user_defined/with_action_and_named_references.y:17:53: Referring symbol `summand` is not found.
%rule sum(X, Y) <i>: X[summand] '+' Y[addend] { $$ = $summand + $addend; }
- Fixed an incorrect test case, so fixed the example. What do you think?
LGTM!