Skip to content

Commit

Permalink
Expand parameterizing rules using only those with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Dec 9, 2023
1 parent c0849c3 commit b07ce82
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
13 changes: 7 additions & 6 deletions lib/lrama/grammar/parameterizing_rule_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def defined?(name)
end

def build_rules(token, rule_counter, lhs_tag, user_code, precedence_sym, line)
@parameterizing_rule_builders.each do |builder|
build_token = builder.build_token(token)
@rules = @rules + builder.build_rules(token, build_token, rule_counter, lhs_tag, user_code, precedence_sym, line)
@tokens << build_token
@term = builder.term
end
builder = @parameterizing_rule_builders.select { |b| b.name == token.s_value }.last
raise "Unknown parameterizing rule #{token.s_value} at line #{token.line}" unless builder

build_token = builder.build_token(token)
@rules = @rules + builder.build_rules(token, build_token, rule_counter, lhs_tag, user_code, precedence_sym, line)
@tokens << build_token
@term = builder.term
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/parameterizing_rules/user_defined.y
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ static int yyerror(YYLTYPE *loc, const char *str);
| X
;

%rule unused_define(X): /* empty */
| X
;

%%

program : defined_option(number) <i>
Expand Down
8 changes: 4 additions & 4 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("YYEOF"),
lineno: 21,
lineno: 25,
),
Rule.new(
id: 1,
Expand All @@ -1249,7 +1249,7 @@
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 21,
lineno: 25,
),
Rule.new(
id: 2,
Expand All @@ -1259,7 +1259,7 @@
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 21,
lineno: 25,
),
Rule.new(
id: 3,
Expand All @@ -1271,7 +1271,7 @@
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("number"),
lineno: 21,
lineno: 25,
),
])
end
Expand Down

0 comments on commit b07ce82

Please sign in to comment.