diff --git a/lib/lrama/grammar/parameterizing_rule/rhs.rb b/lib/lrama/grammar/parameterizing_rule/rhs.rb index e0e2ee85..3eb92f8e 100644 --- a/lib/lrama/grammar/parameterizing_rule/rhs.rb +++ b/lib/lrama/grammar/parameterizing_rule/rhs.rb @@ -13,14 +13,23 @@ def initialize def resolve_user_code(bindings) return unless user_code - code = user_code.s_value + var_to_arg = {} symbols.each do |sym| resolved_sym = bindings.resolve_symbol(sym) if resolved_sym != sym - code = code.gsub(/\$#{sym.s_value}/, "$#{resolved_sym.s_value}") + var_to_arg[sym.s_value] = resolved_sym.s_value end end - Lrama::Lexer::Token::UserCode.new(s_value: code, location: user_code.location) + + var_to_arg.each do |var, arg| + user_code.references.each do |ref| + if ref.name == var + ref.name = arg + end + end + end + + return user_code end end end diff --git a/spec/fixtures/parameterizing_rules/user_defined/with_action_and_named_references.y b/spec/fixtures/parameterizing_rules/user_defined/with_action_and_named_references.y index 5b041c05..85ae0d5c 100644 --- a/spec/fixtures/parameterizing_rules/user_defined/with_action_and_named_references.y +++ b/spec/fixtures/parameterizing_rules/user_defined/with_action_and_named_references.y @@ -16,7 +16,7 @@ static int yyerror(YYLTYPE *loc, const char *str); %token number %token string -%rule pair(X, Y): X ',' Y { printf("(%d, %d)\n", $X, $2); } +%rule pair(X, Y): X ',' Y { printf("(%d, %d)\n", $X, $3); } ; %% diff --git a/spec/lrama/parser_spec.rb b/spec/lrama/parser_spec.rb index 861f49fa..2fddc68b 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -1719,7 +1719,7 @@ grammar.find_symbol_by_s_value!("string") ], lhs_tag: nil, - token_code: T::UserCode.new(s_value: " printf(\"(%d, %d)\\n\", $number, $2); "), + token_code: T::UserCode.new(s_value: " printf(\"(%d, %d)\\n\", $X, $3); "), nullable: false, precedence_sym: grammar.find_symbol_by_s_value!("string"), lineno: 24,