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

Fix indentation tokens lengths #1708

Merged
merged 3 commits into from
Oct 14, 2024

Conversation

aabounegm
Copy link
Member

@aabounegm aabounegm commented Oct 2, 2024

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:

if True:
    if False:
        print('impossible')
    else:
        print('makes sense')

the dedent token after the first print statement contained the whitespace before the else keyword

Additionally, fixed a minor issue (mostly inconvenience with folding) where the dedent tokens were placed after all newline tokens. For example, in the snippet:

if True:
    print('yes')


else:
    print('no')

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.

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;
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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 👍

Copy link
Member

@msujew msujew left a 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 👍

@msujew msujew merged commit d0522c1 into eclipse-langium:main Oct 14, 2024
4 checks passed
@aabounegm aabounegm deleted the indentation-tokens-length branch October 14, 2024 09:17
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