-
Notifications
You must be signed in to change notification settings - Fork 70
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
Fix indentation tokens lengths #1708
Fix indentation tokens lengths #1708
Conversation
When the dedent had some whitespace after it (not an empty line), that whitespace was considered part of the dedent token, causing the CST node range to be off
@@ -321,19 +312,20 @@ export class IndentationAwareTokenBuilder<Terminals extends string = string, Key | |||
} | |||
|
|||
const numberOfDedents = this.indentationStack.length - matchIndentIndex - 1; | |||
const newlinesBeforeDedent = text.substring(0, offset).match(/[\r\n]+$/)?.[0].length ?? 1; |
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 think this should not only backtrack through all the newline characters, but also whitespace. For example, if we dedent later:
{
value
<-- whitespace
<-- whitespace
} <-- Dedent token appears at start of this line
This seems pretty weird, as if we remove the whitespace, the dedent already appears in the 3rd line. Since this PR is there to fix behavior such as the folding, I would've thought it should keep the DEDENT
token in the 3rd line.
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.
But if emit the DEDENT
token on the beginning of the 3rd line, then it means that the whitespace after it will be detected as INDENT
, and naturally followed by another DEDENT
on the next line (and once more, for this example). These extra INDENT
/DEDENT
tokens are unexpected and will cause parsing errors.
I generally think that trailing whitespace is problematic and should be marked as a parsing error, especially in indentation-sensitive languages.
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 see. I guess that makes sense. Thanks for the explanation 👍
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.
Makes sense to me, thanks 👍
Removed the duplication of indent tokens as whitespace, and ensured that dedent tokens are 0-width (empty string).
The dedent token could previously contain the matched whitespace of the following indentation (if it matched a previous indentation and didn't introduce a new indent token).
For example, in this snippet:
the dedent token after the first
print
statement contained the whitespace before theelse
keywordAdditionally, fixed a minor issue (mostly inconvenience with folding) where the dedent tokens were placed after all newline tokens. For example, in the snippet:
the dedent was placed on the 5th line, right before the
else
keyword. This PR moves it such that it is on the 3rd line.