Skip to content

Parse 0.2 after as float literal, not member access #1277

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

Merged
merged 1 commit into from
Feb 2, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jan 26, 2023

Deciding whether 0.2 should be lexed as a float literal or a member access is a little more difficult than just looking at the previous character because 0.2 might be preceeded by an operator like or .^., in which case it should be lexed as a float literal and not a member access.

We might be able to do some disambiguation magic on whether the character before the period is also an operator continuation point but that seems fairly brittle to me. The sanest way of doing this, is to store the previously lexed token’s kind in the cursor and checking that.

I measured and did not see a performance regregssion when parsing MovieSwiftUI.

rdar://103273988

@ahoppen ahoppen requested a review from bnbarham January 26, 2023 10:20
@ahoppen ahoppen force-pushed the ahoppen/float-literal-lexing branch from 193e7aa to 9cf5678 Compare January 26, 2023 10:28
@ahoppen
Copy link
Member Author

ahoppen commented Jan 26, 2023

@swift-ci Please test

@@ -335,6 +337,8 @@ extension Lexer.Cursor {
flags.insert(.isAtStartOfLine)
}

self.previousTokenKind = result.tokenKind.base
Copy link
Contributor

@bnbarham bnbarham Jan 26, 2023

Choose a reason for hiding this comment

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

There's Cursor.backUp where previousTokenKind wouldn't be correct any more. We also don't set the state in there, but I suppose the assumption there is that we're always in normal when splitting tokens.

(Just have to pass the token kind through to backUp)

Copy link
Member Author

Choose a reason for hiding this comment

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

Cursor.backUp is still correct because the previous token still has the same kind if we resetForSplit, which is the only caller of this function (and should remain to be the only caller).

If we have just lexed <.. as a binary operator, previousToken is binaryOperator. If we now back up by two characters to consume only <, we are now placed at the first . but the previous token continues to be of kind binaryOperator. So no change of previousTokenKind is necessary here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. It would be nice to have a comment mentioning this (just in case we ever end up calling backUp elsewhere). Thanks for the explanation!

Copy link
Member Author

Choose a reason for hiding this comment

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

Added one

Deciding whether `0.2` should be lexed as a float literal or a member access is a little more difficult than just looking at the previous character because `0.2` might be preceeded by an operator like `…` or `.^.`, in which case it should be lexed as a float literal and not a member access.

We might be able to do some disambiguation magic on whether the character before the period is also an operator continuation point but that seems fairly brittle to me. The sanest way of doing this, is to store the previously lexed token’s kind in the cursor and checking that.

I measured and did not see a performance regregssion when parsing MovieSwiftUI.

rdar://103273988
@ahoppen ahoppen force-pushed the ahoppen/float-literal-lexing branch from 9cf5678 to da28695 Compare January 31, 2023 19:18
@ahoppen
Copy link
Member Author

ahoppen commented Jan 31, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 1, 2023

@swift-ci Please test Linux

@ahoppen
Copy link
Member Author

ahoppen commented Feb 1, 2023

@swift-ci Please test Linux

@ahoppen ahoppen merged commit 343f75a into swiftlang:main Feb 2, 2023
@ahoppen ahoppen deleted the ahoppen/float-literal-lexing branch February 2, 2023 07:08
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