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

Avoid lookahead into interpolation #15518

Closed

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Jun 24, 2022

Since an interpolation is really two tokens deep, for the literal
part and the interpolated expression, one-char lookahead does not
preserve the second token on reset.

Make lookahead itself more robust by falling back to a LookaheadScanner
for this case.

Also add an internal error for the bad reset.

Fixes #15514

@som-snytt som-snytt marked this pull request as ready for review June 24, 2022 12:31
Since an interpolation is really two tokens deep, for the literal
part and the interpolated expression, one-char lookahead does not
preserve the second token on reset.

Make lookahead itself more robust by falling back to a LookaheadScanner
for this case.

Also add an internal error for the bad reset.
@som-snytt som-snytt force-pushed the issue/15514-interpolation-scan branch from 1cd734d to 99707d8 Compare June 24, 2022 12:32
@Kordyjan Kordyjan requested a review from odersky June 24, 2022 12:37
@@ -695,10 +695,10 @@ object Scanners {
getNextToken(token)
if token == END && !isEndMarker then token = IDENTIFIER

def reset() = {
def reset() =
if next.token != EMPTY && !isInstanceOf[LookaheadScanner] then report.error(s"Internal: lookAhead/reset would erase next token ${tokenString(next.token)} after ${tokenString(token)}", sourcePos())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be just as assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I got carried away because my debug mode was printStackTrace which made it hard to see my special message. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it survived the community build, maybe I'll add the assert to the other PR, assuming it must always be an error.

def lookahead: TokenData =
if next.token == EMPTY then
def lookahead: TokenData =
if next.token != EMPTY then next
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better not to burden lookahead with this logic. Can't be create a Lookahead scanner instead at the call site of lookahead when the assertion is triggered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I went back and forth on where to place the burden. There was no good answer. But the superseding PR, if it works, is conceptually much simpler, as it means lookahead never incurs the advanced token.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost went down the path of, in.canLookAhead, because maybe there are other such states. How could I ever trust lookahead again?

@som-snytt
Copy link
Contributor Author

Probably superseded by #15519 which just moves the code to postProcessToken.

@odersky odersky mentioned this pull request Jun 25, 2022
@som-snytt som-snytt deleted the issue/15514-interpolation-scan branch February 6, 2023 07:26
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.

String interpolation followed by a dot is missing string literals
3 participants