[PoC] Intoroduce parameterizing rules with conditional statement#418
Draft
ydah wants to merge 6 commits intoruby:masterfrom
Draft
[PoC] Intoroduce parameterizing rules with conditional statement#418ydah wants to merge 6 commits intoruby:masterfrom
ydah wants to merge 6 commits intoruby:masterfrom
Conversation
2cb042d to
c15c765
Compare
9a48181 to
6ba14f2
Compare
6ba14f2 to
b90290f
Compare
b90290f to
a044fe0
Compare
a044fe0 to
dd5808c
Compare
I would like to propose a new grammar in this PR.
I believe that more parameterizing rules can handle more abstract rules if we can switch between rules and actions that are expanded by conditions in order to make rules common.
Syntax is as follows:
```
%rule defined_rule(X, condition): /* empty */
| X { $$ = $1; } %if(condition) /* 1 */
| %if(condition) X %endif X { $$ = $1; } /* 2 */
;
%%
r_true : defined_rule(number, %true)
;
r_false : defined_rule(number, %false)
;
```
1. It's like a postfix if in Ruby. If condition is false, it is equivalent to missing this line.
2. If statementIf condition is false, it is equivalent to missing RHS between `%if` and`% endif`.
I believe it will solve the problem mentioned in the article below with the tight coupling with Lexer "to disable certain generation rules under certain conditions" and I would like to propose this feature to solve this problem.
https://yui-knk.hatenablog.com/entry/2023/04/04/190413
We can trace the RHS to [f_args](https://github.com/ruby/ruby/blob/2f916812a9b818b432ee7c299e021ec62d4727fb/parse.y#L5523-L5575) > [args_tail](https://github.com/ruby/ruby/blob/2f916812a9b818b432ee7c299e021ec62d4727fb/parse.y#L5487-L5503) > [args_forward](https://github.com/ruby/ruby/blob/2f916812a9b818b432ee7c299e021ec62d4727fb/parse.y#L5586-L5597), where f_args is the RHS of both the lambda argument (f_larglist) and the method definition argument (f_arglist).
So if we can switch between RHS and actions by passing parameters, we can break up the Lexer/Parser coupling here.
```
%rule defined_rule(X, condition): X { $$ = $1; } %if(condition)
;
```
dd5808c to
77218fe
Compare
yui-knk
reviewed
Jul 28, 2024
parser.y
Outdated
| | rule_rhs_list "|" rule_rhs if_clause? | ||
| { | ||
| builder = val[2] | ||
| builder.symbols << val[3] if val[3] |
Collaborator
There was a problem hiding this comment.
builder (is the instance of Grammar::ParameterizingRule::Rhs) has one or zero if_clause. Then I think it's reasonable for builder has attribute for if_clause than holding if_clause on symbols.
Collaborator
|
I agree to the feature.
Sorry for the late response. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I would like to propose a new grammar in this PR.
I believe that more parameterizing rules can handle more abstract rules if we can switch between rules and actions that are expanded by conditions in order to make rules common.
Syntax is as follows:
It's like a postfix if in Ruby. If condition is false, it is equivalent to missing this line.
Old design
DesignDoc: https://gist.github.com/ydah/62008655a6f6c3118ab01134dc91da6f
Syntax is as follows:
%ifand% endif.Motivation
I believe it will solve the problem mentioned in the article below with the tight coupling with Lexer "to disable certain generation rules under certain conditions" and I would like to propose this feature to solve this problem.
https://yui-knk.hatenablog.com/entry/2023/04/04/190413
We can trace the RHS to f_args > args_tail > args_forward, where f_args is the RHS of both the lambda argument (f_larglist) and the method definition argument (f_arglist).
So if we can switch between RHS and actions by passing parameters, we can break up the Lexer/Parser coupling here.