Skip to content

Commit

Permalink
Add support for aliased Named References for actions of rhs in Parame…
Browse files Browse the repository at this point in the history
…terizing rules
  • Loading branch information
ydah committed May 2, 2024
1 parent f8c0232 commit b6c3170
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/lrama/grammar/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ def resolve_symbol(symbol)
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.s_value] || symbol
parameter_to_arg(symbol) || symbol
end
end

private

def parameter_to_arg(symbol)
if (arg = @parameter_to_arg[symbol.s_value].dup)
arg.alias_name = symbol.alias_name
end
arg
end
end
end
end
4 changes: 4 additions & 0 deletions sig/lrama/grammar/binding.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Lrama

def initialize: (Grammar::ParameterizingRule::Rule parameterizing_rule, Array[Lexer::Token] actual_args) -> void
def resolve_symbol: (Lexer::Token symbol) -> Lexer::Token

private

def parameter_to_arg: (Lexer::Token symbol) -> Lexer::Token?
end
end
end
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, $3); }
%rule pair(X, Y): X ',' Y[alias] { printf("(%d, %d)\n", $X, $alias); }
;

%%
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\", $X, $3); "),
token_code: T::UserCode.new(s_value: " printf(\"(%d, %d)\\n\", $X, $alias); "),
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("string"),
lineno: 24,
Expand Down

0 comments on commit b6c3170

Please sign in to comment.