-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Parser: correctly lex the prefix of keyword in macro #4659
Parser: correctly lex the prefix of keyword in macro #4659
Conversation
811addd
to
d126386
Compare
You're awesome @makenowjust 🎉 |
ping anyone. Many people need this fix. |
This just needs another review, seems like a simple fix to me. |
Sorry 😄 |
src/compiler/crystal/syntax/lexer.cr
Outdated
@@ -2139,6 +2139,12 @@ module Crystal | |||
@macro_curly_count -= 1 | |||
end | |||
else | |||
# In the following codes `check_macro_opening_keyword(beginning_of_line)` and some `next_char` may let `@rader` forward. |
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.
Small typo here: @reader
I consider another way. Add such a method: def lookahead
old_pos = @reader.pos
old_line_number, old_column_number = @line_number, @column_number
yield.tap do |result|
unless result
@reader.pos = old_pos
@line_number, @column_number = old_line_number, old_column_number
end
end
end And, use it to check keyword, like: if !delimiter_state && whitespace && lookahead { char == 'y' && next_char == 'i' && next_char == 'e' && next_char == 'l' && next_char == 'd' && !ident_part_or_end?(peek_next_char) } Perhaps it makes same result (I don't test it.) However, it is a bit heavier process than this PR way. |
@makenowjust I think I like that idea more. It seems like a cleaner and more reusable way to do it. |
Can someone please review this 🙏 |
Are we not waiting for @makenowjust to etst the suggested improvement? |
d126386
to
9ebb740
Compare
@RX14 Fixed! What do you think? |
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.
👌
Fixed #4656
I'll explain why this fix is related to #4656. Following code is minified #4656 example:
This example produces formatting error because of
IndexError
which is raised fromalign_infos
. In other words, #4656's reason is the formatter try to align incorrect line (If this incorrect line has enough columns to align, it produces incorrect result like #4656 gif, otherwise if it doesn't have, it raisesIndexError
).Then, why the formatter try to align incorrect line, is the lexer is buggy. The lexer will report incorrect line number when a newline follows the part of some keywords inside macro (for example
s
is the part ofstruct
). This bug is explained in detail on the source code comments, see them. And so, the formatter mistakes line number by this bug.@sdogruyol expected me good thing, this helped me really. Thank you 😊