Skip to content

Commit

Permalink
Use Lrama::Grammar::Reference
Browse files Browse the repository at this point in the history
Co-authored-by: Yuichiro Kaneko <[email protected]>
  • Loading branch information
ydah and yui-knk committed May 2, 2024
1 parent 9ec93d3 commit 02f76eb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions lib/lrama/grammar/parameterizing_rule/rhs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static int yyerror(YYLTYPE *loc, const char *str);
%token <i> number
%token <s> 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); }
;

%%
Expand Down
2 changes: 1 addition & 1 deletion spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 02f76eb

Please sign in to comment.