12
12
13
13
/// A canonicalized representation of an identifier that strips away backticks.
14
14
public 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.
16
32
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
+ }
18
39
}
19
-
40
+
20
41
@_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
+ }
23
50
24
- private let arena : SyntaxArenaRef ?
51
+ let identifier : IdentifierKind
25
52
26
53
public init ? ( _ token: TokenSyntax ) {
27
54
guard case . identifier = token. tokenKind else {
28
55
return nil
29
56
}
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)
34
59
}
35
60
36
61
public init ( _ string: String ) {
37
- self . string = StringIdentifier ( string)
38
- self . raw = nil
39
- self . arena = nil
62
+ self . identifier = IdentifierKind . sanitize ( string: string)
40
63
}
41
64
42
65
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
51
67
}
52
68
}
53
69
@@ -67,18 +83,3 @@ public struct RawIdentifier: Equatable, Hashable, Sendable {
67
83
}
68
84
}
69
85
}
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