-
Notifications
You must be signed in to change notification settings - Fork 28
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 adjusting Index to Inline #394
Conversation
[note] |
lib/lrama/grammar/rule_builder.rb
Outdated
@@ -204,6 +207,9 @@ def replace_inline_user_code(inline_rhs, index) | |||
return user_code if user_code.nil? | |||
|
|||
code = user_code.s_value.gsub(/\$#{index + 1}/, inline_rhs.user_code.s_value) | |||
user_code.references[(index+1)..-1]&.each do |ref| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering several points for this method.
1. Order of user_code.references
The order of user_code.references
is the order of reference appearance. For example if the code is $3 $2 $1 $$
, the order of user_code.references
is also [$3, $2, $1, $$]
. This means user_code.references[(index+1)..-1]
might not what we want to do. I think it will be like:
user_code.references.each do |ref|
next if ref.index.nil? || ref.index <= index # nil is a case for `$$`
...
end
2. Type of reference
user_code.references
includes both $n
and @n
, we might need to take care of @n
in the future
3. How to overwrite references
Right now this code overwrite code string, however another approach is to dup the code and overwrite reference index like below.
Manipulating references is helpful if we handle named references.
result = user_code.dup
result.references[(index+1)..-1]&.each do |ref|
ref.index += inline_rhs.symbols.count-1
end
result
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your review!
I fixed 1 and 2 and added a commit.
However, for 3, I changed the way to write references overwrites, but I tried rewriting it like this:. However, when I wrote a test to check UserCode references in parser.spec, the index didn't seem to change.
def replace_inline_user_code(inline_rhs, index)
return user_code if inline_rhs.user_code.nil?
return user_code if user_code.nil?
code = user_code.s_value.gsub(/\$#{index + 1}/, inline_rhs.user_code.s_value)
replaced_code = Lrama::Lexer::Token::UserCode.new(s_value: code, location: user_code.location)
replaced_code.references.each do |ref|
next if ref.index.nil? || ref.index <= index # nil is a case for `$$`
ref.index += inline_rhs.symbols.count
end
replaced_code
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm missing something, please point it out 🙏
@ydah The direction seems good. Could you check the logic of reference overwrite, #394 (comment) ? |
@yui-knk Thank you for your review! I updated this PR. |
|
``` ❯ exe/lrama -d sample/calc.y -o calc.c && gcc -Wall calc.c -o calc && ./calc Enter the formula: 1 parse error: syntax error, unexpected NUM [1] 52278 segmentation fault ./calc ```
@yui-knk I rebased and fix conflict. |
LGTM! |
No description provided.