|
14 | 14 | public struct Identifier: Equatable, Hashable, Sendable {
|
15 | 15 | /// The sanitized `text` of a token.
|
16 | 16 | public var name: String {
|
17 |
| - String(syntaxText: rawIdentifier.name) |
| 17 | + String(syntaxText: raw.name) |
18 | 18 | }
|
19 | 19 |
|
20 | 20 | @_spi(RawSyntax)
|
21 |
| - public let rawIdentifier: RawIdentifier |
22 |
| - |
23 |
| - let arena: RetainedSyntaxArena |
| 21 | + public let raw: RawIdentifier |
24 | 22 |
|
25 | 23 | public init?(_ token: TokenSyntax) {
|
26 |
| - guard case .identifier(let text) = token.tokenKind else { |
| 24 | + guard case .identifier = token.tokenKind else { |
27 | 25 | return nil
|
28 | 26 | }
|
29 | 27 |
|
30 |
| - var rawText = text.contains("`") ? text.trimmingCharacters(in: "`") : Substring(text) |
| 28 | + self.raw = RawIdentifier(token.tokenView) |
| 29 | + } |
| 30 | +} |
31 | 31 |
|
32 |
| - let syntaxArena = SyntaxArena() |
| 32 | +@_spi(RawSyntax) |
| 33 | +public struct RawIdentifier: Equatable, Hashable, Sendable { |
| 34 | + public let name: SyntaxText |
33 | 35 |
|
34 |
| - let name = rawText.withUTF8 { |
35 |
| - syntaxArena.intern( |
36 |
| - SyntaxText(buffer: SyntaxArenaAllocatedBufferPointer<UInt8>($0)) |
37 |
| - ) |
38 |
| - } |
| 36 | + private let arena: RetainedSyntaxArena |
39 | 37 |
|
40 |
| - self.rawIdentifier = RawIdentifier(name: name) |
41 |
| - self.arena = RetainedSyntaxArena(syntaxArena) |
| 38 | + @_spi(RawSyntax) |
| 39 | + fileprivate init(_ raw: RawSyntaxTokenView) { |
| 40 | + let backtick = SyntaxText(StaticString(stringLiteral: "`")) |
| 41 | + let arena = SyntaxArena() |
| 42 | + if raw.rawText.count > 2 && raw.rawText.hasPrefix(backtick) && raw.rawText.hasSuffix(backtick) { |
| 43 | + let startIndex = raw.rawText.index(after: raw.rawText.startIndex) |
| 44 | + let endIndex = raw.rawText.index(before: raw.rawText.endIndex) |
| 45 | + |
| 46 | + self.name = arena.intern(SyntaxText(rebasing: raw.rawText[startIndex..<endIndex])) |
| 47 | + } else { |
| 48 | + self.name = arena.intern(raw.rawText) |
| 49 | + } |
| 50 | + self.arena = RetainedSyntaxArena(arena) |
42 | 51 | }
|
43 | 52 |
|
44 |
| - public static func == (lhs: Identifier, rhs: Identifier) -> Bool { |
45 |
| - lhs.rawIdentifier == rhs.rawIdentifier |
| 53 | + public static func == (lhs: RawIdentifier, rhs: RawIdentifier) -> Bool { |
| 54 | + lhs.name == rhs.name |
46 | 55 | }
|
47 | 56 |
|
48 | 57 | public func hash(into hasher: inout Hasher) {
|
49 |
| - hasher.combine(rawIdentifier) |
| 58 | + hasher.combine(name) |
50 | 59 | }
|
51 | 60 | }
|
52 |
| - |
53 |
| -@_spi(RawSyntax) |
54 |
| -public struct RawIdentifier: Equatable, Hashable, Sendable { |
55 |
| - public let name: SyntaxText |
56 |
| -} |
|
0 commit comments