Skip to content

[5.9] Complete regex literal lexing #1486

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

Conversation

hamishknight
Copy link
Contributor

5.9 cherry-pick of #1463

Medium risk, but fixes a whole bunch of regex literal parsing issues.


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

ahoppen and others added 12 commits April 3, 2023 11:56
- 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
Copy link
Contributor Author

swiftlang/sourcekit-lsp#725

@swift-ci please test

@hamishknight hamishknight merged commit 32027fa into swiftlang:release/5.9 Apr 5, 2023
@hamishknight hamishknight deleted the lets-go-back-to-the-island-5.9 branch April 5, 2023 12:46
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