1212
1313/// A canonicalized representation of an identifier that strips away backticks.
1414public struct Identifier : Equatable , Hashable , Sendable {
15- /// The sanitized `text` of a token.
15+ enum IdentifierKind : Hashable {
16+ case token( raw: RawIdentifier , arena: SyntaxArenaRef )
17+ case string( String )
18+
19+ static func sanitize( string: String ) -> IdentifierKind {
20+ let backtick = " ` "
21+ if string. count > 2 && string. hasPrefix ( backtick) && string. hasSuffix ( backtick) {
22+ let startIndex = string. index ( after: string. startIndex)
23+ let endIndex = string. index ( before: string. endIndex)
24+ return . string( String ( string [ startIndex..< endIndex] ) )
25+ } else {
26+ return . string( string)
27+ }
28+ }
29+ }
30+
31+ /// The sanitized name of the identifier.
1632 public var name : String {
17- string? . name ?? String ( syntaxText: raw!. name)
33+ switch identifier {
34+ case . token( let raw, _) :
35+ String ( syntaxText: raw. name)
36+ case . string( let string) :
37+ string
38+ }
1839 }
19-
40+
2041 @_spi ( RawSyntax)
21- public let raw : RawIdentifier ?
22- public let string : StringIdentifier ?
42+ public var raw : RawIdentifier ? {
43+ switch identifier {
44+ case . token( let raw, _) :
45+ raw
46+ default :
47+ nil
48+ }
49+ }
2350
24- private let arena : SyntaxArenaRef ?
51+ let identifier : IdentifierKind
2552
2653 public init ? ( _ token: TokenSyntax ) {
2754 guard case . identifier = token. tokenKind else {
2855 return nil
2956 }
30-
31- self . raw = RawIdentifier ( token. tokenView)
32- self . arena = token. tokenView. raw. arenaReference
33- self . string = nil
57+
58+ self . identifier = . token( raw: RawIdentifier ( token. tokenView) , arena: token. tokenView. raw. arenaReference)
3459 }
3560
3661 public init ( _ string: String ) {
37- self . string = StringIdentifier ( string)
38- self . raw = nil
39- self . arena = nil
62+ self . identifier = IdentifierKind . sanitize ( string: string)
4063 }
4164
4265 public static func == ( lhs: Self , rhs: Self ) -> Bool {
43- if let lhsRaw = lhs. raw,
44- let rhsRaw = rhs. raw,
45- let lhsArena = lhs. arena,
46- let rhsArena = rhs. arena {
47- return lhsRaw == rhsRaw && lhsArena == rhsArena
48- } else {
49- return lhs. name == rhs. name
50- }
66+ lhs. name == rhs. name
5167 }
5268}
5369
@@ -67,18 +83,3 @@ public struct RawIdentifier: Equatable, Hashable, Sendable {
6783 }
6884 }
6985}
70-
71- public struct StringIdentifier : Equatable , Hashable , Sendable {
72- public let name : String
73-
74- fileprivate init ( _ string: String ) {
75- let backtick = " ` "
76- if string. count > 2 && string. hasPrefix ( backtick) && string. hasSuffix ( backtick) {
77- let startIndex = string. index ( after: string. startIndex)
78- let endIndex = string. index ( before: string. endIndex)
79- self . name = String ( string [ startIndex..< endIndex] )
80- } else {
81- self . name = string
82- }
83- }
84- }
0 commit comments