Skip to content

Commit 88067a6

Browse files
committed
Make TokenKind composition and decomposition work based on String instead of SyntaxText
1 parent 81622ef commit 88067a6

File tree

3 files changed

+136
-132
lines changed

3 files changed

+136
-132
lines changed

Sources/SwiftSyntax/Raw/RawSyntaxTokenView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ struct RawSyntaxTokenView {
180180
func formKind() -> TokenKind {
181181
switch raw.rawData.payload {
182182
case .parsedToken(let dat):
183-
return TokenKind.fromRaw(kind: dat.tokenKind, text: dat.tokenText)
183+
return TokenKind.fromRaw(kind: dat.tokenKind, text: String(syntaxText: dat.tokenText))
184184
case .materializedToken(let dat):
185-
return TokenKind.fromRaw(kind: dat.tokenKind, text: dat.tokenText)
185+
return TokenKind.fromRaw(kind: dat.tokenKind, text: String(syntaxText: dat.tokenText))
186186
case .layout(_):
187187
preconditionFailure("Must be invoked on a token")
188188
}

Sources/SwiftSyntax/TokenKind.swift.gyb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,24 +231,26 @@ for token in SYNTAX_TOKENS:
231231

232232
extension TokenKind {
233233
/// If the `rawKind` has a `defaultText`, `text` can be empty.
234-
static func fromRaw(kind rawKind: RawTokenKind, text: SyntaxText) -> TokenKind {
234+
@_spi(RawSyntax)
235+
public static func fromRaw(kind rawKind: RawTokenKind, text: String) -> TokenKind {
235236
switch rawKind {
236237
case .eof: return .eof
237238
% for token in SYNTAX_TOKENS:
238239
case .${token.swift_kind()}:
239240
% if token.text:
240-
assert(text.isEmpty || rawKind.defaultText == text)
241+
assert(text.isEmpty || rawKind.defaultText.map(String.init) == text)
241242
return .${token.swift_kind()}
242243
% else:
243-
return .${token.swift_kind()}(String(syntaxText: text))
244+
return .${token.swift_kind()}(text)
244245
% end
245246
% end
246247
}
247248
}
248249

249250
/// Returns the `RawTokenKind` of this `TokenKind` and, if this `TokenKind`
250251
/// has associated text, the associated text, otherwise `nil`.
251-
func decomposeToRaw() -> (rawKind: RawTokenKind, string: String?) {
252+
@_spi(RawSyntax)
253+
public func decomposeToRaw() -> (rawKind: RawTokenKind, string: String?) {
252254
switch self {
253255
case .eof: return (.eof, nil)
254256
% for token in SYNTAX_TOKENS:

0 commit comments

Comments
 (0)