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

Add support for aliased Named References for actions of rhs in Parameterizing rules #410

Merged
merged 3 commits into from
Jun 1, 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
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 @@ -10,18 +10,16 @@ static int yyerror(YYLTYPE *loc, const char *str);

%union {
int i;
char *s;
}

%token <i> number
%token <s> string

%rule pair(X, Y): X ',' Y { printf("(%d, %d)\n", $X, $3); }
;
%rule sum(X, Y) <i>: X[summand] '+' Y[addend] { $$ = $summand + $addend; }
;

%%

program : pair(number, string) { printf("pair odd even\n"); }
program : sum(number, number) { printf("sum number\n"); }
;

%%
Expand Down
28 changes: 14 additions & 14 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1692,9 +1692,9 @@

it "expands parameterizing rules" do
expect(grammar.nterms.sort_by(&:number)).to match_symbols([
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "pair_number_string"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 1, nullable: false),
Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 8, tag: nil, term: false, token_id: 2, nullable: false),
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 5, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "sum_number_number"), alias_name: nil, number: 6, tag: T::Tag.new(s_value: "<i>"), term: false, token_id: 1, nullable: false),
Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 2, nullable: false),
])

expect(grammar.rules).to eq([
Expand All @@ -1708,33 +1708,33 @@
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("YYEOF"),
lineno: 24,
lineno: 22,
),
Rule.new(
id: 1,
lhs: grammar.find_symbol_by_s_value!("pair_number_string"),
lhs: grammar.find_symbol_by_s_value!("sum_number_number"),
rhs: [
grammar.find_symbol_by_s_value!("number"),
grammar.find_symbol_by_number!(5),
grammar.find_symbol_by_s_value!("string")
grammar.find_symbol_by_s_value!("'+'"),
grammar.find_symbol_by_s_value!("number")
],
lhs_tag: nil,
token_code: T::UserCode.new(s_value: " printf(\"(%d, %d)\\n\", $X, $3); "),
lhs_tag: T::Tag.new(s_value: "<i>"),
token_code: T::UserCode.new(s_value: " $$ = $summand + $addend; "),
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("string"),
lineno: 24,
precedence_sym: grammar.find_symbol_by_s_value!("number"),
lineno: 22,
),
Rule.new(
id: 2,
lhs: grammar.find_symbol_by_s_value!("program"),
rhs: [
grammar.find_symbol_by_s_value!("pair_number_string"),
grammar.find_symbol_by_s_value!("sum_number_number"),
],
lhs_tag: nil,
token_code: T::UserCode.new(s_value: " printf(\"pair odd even\\n\"); "),
token_code: T::UserCode.new(s_value: " printf(\"sum number\\n\"); "),
nullable: false,
precedence_sym: nil,
lineno: 24,
lineno: 22,
),
])
end
Expand Down