Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error for Lrama::Grammar::ParameterizingRule::Rhs#resolve_user_code when multiple execute method #411

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/lrama/grammar/parameterizing_rule/rhs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize
def resolve_user_code(bindings)
return unless user_code

resolved = Lexer::Token::UserCode.new(s_value: user_code.s_value, location: user_code.location)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] The same parameterizing rule can be instantiated multiple times. For example in "spec/fixtures/integration/user_defined_parameterizing_rules.y", pair is instantiated two times. Before this change, original user_code reference name is overwritten by the first instantiation and it's not overwritten by the second instantiation. Therefore we need to dup the user code.

var_to_arg = {}
symbols.each do |sym|
resolved_sym = bindings.resolve_symbol(sym)
Expand All @@ -22,14 +23,14 @@ def resolve_user_code(bindings)
end

var_to_arg.each do |var, arg|
user_code.references.each do |ref|
resolved.references.each do |ref|
if ref.name == var
ref.name = arg
end
end
end

return user_code
return resolved
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static int yyerror(YYLTYPE *loc, const char *str);
{
$$ = $1 + $2;
printf("(%d, %d)\n", $1, $2);
printf("(%d, %d)\n", $X, $2);
printf("(%d, %d)\n", $:1, $:2);
}
;
Expand Down
2 changes: 2 additions & 0 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: [])
describe "user defined parameterizing rules" do
it "prints messages corresponding to rules" do
expected = <<~STR
(2, 3)
(2, 3)
(-2, -1)
pair even odd: 5
(1, 0)
(1, 0)
(-2, -1)
pair odd even: 1
STR
Expand Down