Skip to content

Complete regex literal lexing #1463

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 12 commits into from
Mar 31, 2023

Conversation

hamishknight
Copy link
Contributor

The previous regex literal lexing logic didn't implement all the heuristics needed to parse all the regex literals that the old C++ parser handles. Update it with the heuristics it needs, and better handle diagnostics for invalid cases.

This unfortunately requires a bit of lexical hackery, including a heuristic to classify previous token kinds to determine if we're in regex literal position, but it's needed to handle pathological cases such as x /^ y/ where there's an ambiguity with a binary operator.

Resolves #1059
rdar://102213809

@hamishknight hamishknight requested a review from ahoppen as a code owner March 29, 2023 11:33
@hamishknight hamishknight force-pushed the lets-go-back-to-the-island branch 2 times, most recently from 5cc3792 to 03cbd45 Compare March 29, 2023 13:23
@hamishknight
Copy link
Contributor Author

@ahoppen Addressed review feedback, let me know what you think. Also noticed that we needed to properly classify the prefix operators we're splitting, so pushed a commit to fix that.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

I kept two discussions as unresolved (bump pointer allocator slab size + performance impact of passing previousKeyword around). Once those are resolved, this looks good to me.

ahoppen and others added 12 commits March 31, 2023 20:37
- Run swift-format over them
- Update expectations now that we diagnose
unterminated regex literals
- Remove semantic errors
- Fix test splitting in a few cases
- Add TODOs where we should error
We currently track the previous raw token kind,
but that is not sufficient to identify keyword
kinds. Additionally store the keyword kind.
The SwiftSyntax parser doesn't have a way of
disabling bare slash regex literals, so this is
unnecessary.
Many of these can fall through to the default
case to lex the operator. Also factor out
`lexPostfixOptionalChain` in preparation for a
bit of custom state transition logic.
Generalize in preparation for its use by regex literals.
The previous regex literal lexing logic didn't
implement all the heuristics needed to parse all
the regex literals that the old C++ parser handles.
Update it with the heuristics it needs, and better
handle diagnostics for invalid cases.

This unfortunately requires a bit of lexical
hackery, including a heuristic to classify previous
token kinds to determine if we're in regex literal
position, but it's needed to handle pathological
cases such as `x /^ y/` where there's an ambiguity
with a binary operator.
Previously we always produced `.prefixOperator`,
which is wrong for cases like prefix `&`, `=`, and
`?` which should produce different token kinds.
Factor out the classification code, and use it
to produce the correct token kind.
We can confidently lex unless we have a previous
token that indicates that we're in an argument
list. In such a case, an unapplied operator can
be parsed, and we want to ensure we maintain that
behavior.
@hamishknight hamishknight force-pushed the lets-go-back-to-the-island branch from 3b4c494 to b2a2e75 Compare March 31, 2023 19:38
@hamishknight
Copy link
Contributor Author

swiftlang/sourcekit-lsp#724

@swift-ci please test

@hamishknight
Copy link
Contributor Author

swiftlang/sourcekit-lsp#724

@swift-ci please test Windows

@hamishknight hamishknight merged commit 92178c0 into swiftlang:main Mar 31, 2023
@hamishknight hamishknight deleted the lets-go-back-to-the-island branch March 31, 2023 22:42
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.

Regex literal not being processed as an operator when it could be
2 participants