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

Support for nested user-defined rules #297

Merged
merged 2 commits into from
Dec 20, 2023

Conversation

ydah
Copy link
Member

@ydah ydah commented Dec 16, 2023

This PR adds support for nested, user-defined ParameterizingRules.

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

%rule option(Y): /* empty */
               | nested_option(Y)
               ;

%%

program         : option(number)
                ;

@ydah ydah force-pushed the nested_user_def_param branch from b6e8e17 to 786059f Compare December 16, 2023 07:33
@ydah ydah force-pushed the nested_user_def_param branch from 50385f2 to 30601e6 Compare December 19, 2023 16:16
@ydah ydah changed the title [WIP] Support for nested user-defined rules Support for nested user-defined rules Dec 19, 2023
@ydah ydah marked this pull request as ready for review December 19, 2023 16:23
@@ -10,14 +10,12 @@ def initialize(name, parameters, rhs)
@required_parameters_count = parameters.count
end

def build_rules(token, rule_counter, lhs_tag, line)
def build_rules(token, actual_args, rule_counter, lhs_tag, line, rule_builders)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] I agree to not giving actual_args default value because how to construct argument map depends on the type of caller.

@@ -17,7 +17,7 @@ def build_rules(token, rule_counter, lhs_tag, line)
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

builder.build_rules(token, rule_counter, lhs_tag, line)
builder.build_rules(token, token.args, rule_counter, lhs_tag, line, @parameterizing_rule_builders)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] At the moment, parameterizing_rule_builders is passed. But it might be changed to pass resolver to share rules once it's created.


private

def build_nested_rules(token, actual_args, parameters, rule_counter, lhs_tag, line, rule_builders)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] In the future, ParameterizingRuleRhsBuilder might delegate rule building to RuleBuilder so that RHS replacement logic is put into only one place.

symbols.map do |sym|
if sym.is_a?(Lexer::Token::InstantiateRule)
sym.args.map do |arg|
idx = parameters.index { |parameter| parameter.s_value == arg.s_value }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] The logic for resolving parameter is spread. It might good to implement it into Arguments object in another PR.

@yui-knk
Copy link
Collaborator

yui-knk commented Dec 20, 2023

I left some comments to future improvement. However this PR seems good, I will merge.
Thanks for the great contribution 🎉

@yui-knk yui-knk merged commit 4eee215 into ruby:master Dec 20, 2023
15 checks passed
@ydah ydah deleted the nested_user_def_param branch December 20, 2023 09:28
ydah added a commit to ydah/lrama that referenced this pull request Dec 23, 2023
ydah added a commit to ydah/lrama that referenced this pull request Dec 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants