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

Parse raw identifiers. #2857

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

allevato
Copy link
Member

@allevato allevato commented Sep 22, 2024

The parser implementation of SE-0451.

@allevato allevato force-pushed the rich-identifiers branch 2 times, most recently from ccf9744 to 65275bd Compare October 23, 2024 13:48
@allevato allevato force-pushed the rich-identifiers branch 2 times, most recently from 1d0edfb to 7e873b7 Compare December 24, 2024 14:00
@allevato allevato marked this pull request as ready for review December 24, 2024 14:04
error = LexingDiagnostic(.unprintableAsciiCharacter, position: position)
}
}
if isEmpty && !scalar.isOperatorStartCodePoint || !scalar.isOperatorContinuationCodePoint {
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 adding parenthesis here would help readability.

Suggested change
if isEmpty && !scalar.isOperatorStartCodePoint || !scalar.isOperatorContinuationCodePoint {
if (isEmpty && !scalar.isOperatorStartCodePoint) || !scalar.isOperatorContinuationCodePoint {

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

)
]
)
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we diagnose raw identifiers that only consist of whitespace? As far as I can tell, they are forbidden by the proposal.

Copy link
Member Author

Choose a reason for hiding this comment

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

We weren't; thanks for catching that. Fixed.

This is the parser implementation for SE-0451.
@grynspan
Copy link
Contributor

grynspan commented Jan 6, 2025

I think I have a hard need for determining if an identifier wrapped in backticks is raw or just an escaped keyword. Having a member such as TokenKind.isRawIdentifier would be very useful here.

// excluding any other ASCII non-printables and Unicode separators. In
// other words, the only whitespace code points allowed in a raw
// identifier are U+0020, and U+200E/200F (LTR/RTL marks).
return (c >= 0x0009 && c <= 0x000D) as Bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to express this as isWhitespace && !isPermittedRawIdentifierWhitespace?

Copy link
Member Author

@allevato allevato Jan 6, 2025

Choose a reason for hiding this comment

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

The parser explicitly avoids the use of Unicode.Scalar.Properties because they're not necessarily stable between Unicode versions and may change depending on the Unicode tables that are built into libswiftCore, so we don't want the behavior of the parser to change depending on things like what version of the operating system the compiler is running on.

It's probably exceedingly rare that new whitespace will be added (or worse, that a non-whitespace code point would become whitespace or vice versa), but hardcoding the code points means the behavior is deterministic and we can choose to make breaks via new language modes, if needed in the future.

@allevato
Copy link
Member Author

allevato commented Jan 6, 2025

I think I have a hard need for determining if an identifier wrapped in backticks is raw or just an escaped keyword. Having a member such as TokenKind.isRawIdentifier would be very useful here.

I think you can do this today, in a somewhat roundabout fashion, using existing APIs from SwiftParser:

let id = "<some keyword or raw identifier without backticks>"
let quotedID = "`\(id)`"
let isKeywordish = id.isValidSwiftIdentifier(for: .memberAccess)
  && quotedID.isValidSwiftIdentifier(for: .memberAccess)

...since x.class and x.`class` are both valid for escaped keywords but x.some raw identifier and x.`some raw identifier` would fail the first test. There may be some failure cases I'm missing here, though...

It's not the most efficient though since it kicks off two parsers. I agree it's probably a good idea to have a separate API for this that does the same single-pass over the identifier that the lexer does.

@allevato
Copy link
Member Author

@swift-ci please test

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.

3 participants