Skip to content

Commit 0bb4ce3

Browse files
committed
Move nameForDiagnostics from RawTokenKind to TokenKind
1 parent f7da070 commit 0bb4ce3

File tree

6 files changed

+121
-121
lines changed

6 files changed

+121
-121
lines changed

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/TokenKindFile.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ let tokenKindFile = SourceFileSyntax {
9696
}
9797
}
9898

99+
try! VariableDeclSyntax("public var nameForDiagnostics: String") {
100+
try! SwitchExprSyntax("switch self") {
101+
SwitchCaseSyntax("case .eof:") {
102+
StmtSyntax(#"return "end of file""#)
103+
}
104+
105+
for token in SYNTAX_TOKENS where token.swiftKind != "keyword" {
106+
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
107+
StmtSyntax("return #\"\(raw: token.nameForDiagnostics)\"#")
108+
}
109+
}
110+
SwitchCaseSyntax("case .keyword(let keyword):") {
111+
StmtSyntax("return String(syntaxText: keyword.defaultText)")
112+
}
113+
}
114+
}
115+
99116
try VariableDeclSyntax(
100117
"""
101118
/// Returns `true` if the token is a Swift keyword.
@@ -287,23 +304,6 @@ let tokenKindFile = SourceFileSyntax {
287304
}
288305
}
289306

290-
try! VariableDeclSyntax("public var nameForDiagnostics: String") {
291-
try! SwitchExprSyntax("switch self.base") {
292-
SwitchCaseSyntax("case .eof:") {
293-
StmtSyntax(#"return "end of file""#)
294-
}
295-
296-
for token in SYNTAX_TOKENS where token.swiftKind != "keyword" {
297-
SwitchCaseSyntax("case .\(raw: token.swiftKind):") {
298-
StmtSyntax("return #\"\(raw: token.nameForDiagnostics)\"#")
299-
}
300-
}
301-
SwitchCaseSyntax("case .keyword:") {
302-
StmtSyntax("return String(syntaxText: self.keyword.defaultText)")
303-
}
304-
}
305-
}
306-
307307
try! VariableDeclSyntax(
308308
"""
309309
/// Returns `true` if the token is a Swift keyword.

Sources/SwiftParserDiagnostics/MissingNodesError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fileprivate enum NodesDescriptionPart {
4242
if let childName = token.childNameInParent {
4343
return childName
4444
}
45-
return token.tokenKind.decomposeToRaw().rawKind.nameForDiagnostics
45+
return token.tokenKind.nameForDiagnostics
4646
case .node(let node):
4747
var walk: Syntax = node
4848
while true {

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
251251
unexpectedTokenCondition: { AsyncEffectSpecifier(token: $0) != nil },
252252
correctTokens: [node.asyncSpecifier],
253253
message: { AsyncMustPrecedeThrows(asyncKeywords: $0, throwsKeyword: throwsSpecifier) },
254-
moveFixIt: { MoveTokensInFrontOfFixIt(movedTokens: $0, inFrontOf: throwsSpecifier.rawTokenKind) },
254+
moveFixIt: { MoveTokensInFrontOfFixIt(movedTokens: $0, inFrontOf: throwsSpecifier.tokenKind) },
255255
removeRedundantFixIt: { RemoveRedundantFixIt(removeTokens: $0) }
256256
)
257257
}

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public struct MoveTokensAfterFixIt: ParserFixIt {
490490
public let movedTokens: [TokenSyntax]
491491

492492
/// The token after which `movedTokens` should be moved
493-
public let after: RawTokenKind
493+
public let after: TokenKind
494494

495495
public var message: String {
496496
"move \(nodesDescription(movedTokens, format: false)) after '\(after.nameForDiagnostics)'"
@@ -502,7 +502,7 @@ public struct MoveTokensInFrontOfFixIt: ParserFixIt {
502502
public let movedTokens: [TokenSyntax]
503503

504504
/// The token after which 'try' should be moved
505-
public let inFrontOf: RawTokenKind
505+
public let inFrontOf: TokenKind
506506

507507
public var message: String {
508508
"move \(nodesDescription(movedTokens, format: false)) in front of '\(inFrontOf.nameForDiagnostics)'"

Sources/SwiftParserDiagnostics/PresenceUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class PresentMaker: SyntaxRewriter {
5252
if let text = text, (!text.isEmpty || rawKind == .stringSegment) { // string segments can have empty text
5353
presentToken = TokenSyntax(token.tokenKind, presence: .present)
5454
} else {
55-
let newKind = TokenKind.fromRaw(kind: rawKind, text: rawKind.defaultText.map(String.init) ?? "<#\(rawKind.nameForDiagnostics)#>")
55+
let newKind = TokenKind.fromRaw(kind: rawKind, text: rawKind.defaultText.map(String.init) ?? "<#\(token.tokenKind.nameForDiagnostics)#>")
5656
presentToken = TokenSyntax(newKind, leadingTrivia: token.leadingTrivia, trailingTrivia: token.trailingTrivia, presence: .present)
5757
}
5858
return BasicFormat().visit(presentToken)

Sources/SwiftSyntax/generated/TokenKind.swift

Lines changed: 99 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,105 @@ public enum TokenKind: Hashable {
279279
}
280280
}
281281

282+
public var nameForDiagnostics: String {
283+
switch self {
284+
case .eof:
285+
return "end of file"
286+
case .wildcard:
287+
return #"wildcard"#
288+
case .leftParen:
289+
return #"("#
290+
case .rightParen:
291+
return #")"#
292+
case .leftBrace:
293+
return #"{"#
294+
case .rightBrace:
295+
return #"}"#
296+
case .leftSquareBracket:
297+
return #"["#
298+
case .rightSquareBracket:
299+
return #"]"#
300+
case .leftAngle:
301+
return #"<"#
302+
case .rightAngle:
303+
return #">"#
304+
case .period:
305+
return #"."#
306+
case .comma:
307+
return #","#
308+
case .ellipsis:
309+
return #"..."#
310+
case .colon:
311+
return #":"#
312+
case .semicolon:
313+
return #";"#
314+
case .equal:
315+
return #"="#
316+
case .atSign:
317+
return #"@"#
318+
case .pound:
319+
return #"#"#
320+
case .prefixAmpersand:
321+
return #"&"#
322+
case .arrow:
323+
return #"->"#
324+
case .backtick:
325+
return #"`"#
326+
case .backslash:
327+
return #"\"#
328+
case .exclamationMark:
329+
return #"!"#
330+
case .postfixQuestionMark:
331+
return #"?"#
332+
case .infixQuestionMark:
333+
return #"?"#
334+
case .stringQuote:
335+
return #"""#
336+
case .singleQuote:
337+
return #"'"#
338+
case .multilineStringQuote:
339+
return #"""""#
340+
case .poundSourceLocationKeyword:
341+
return #"#sourceLocation"#
342+
case .poundIfKeyword:
343+
return #"#if"#
344+
case .poundElseKeyword:
345+
return #"#else"#
346+
case .poundElseifKeyword:
347+
return #"#elseif"#
348+
case .poundEndifKeyword:
349+
return #"#endif"#
350+
case .poundAvailableKeyword:
351+
return #"#available"#
352+
case .poundUnavailableKeyword:
353+
return #"#unavailable"#
354+
case .integerLiteral:
355+
return #"integer literal"#
356+
case .floatingLiteral:
357+
return #"floating literal"#
358+
case .regexLiteral:
359+
return #"regex literal"#
360+
case .unknown:
361+
return #"token"#
362+
case .identifier:
363+
return #"identifier"#
364+
case .binaryOperator:
365+
return #"binary operator"#
366+
case .postfixOperator:
367+
return #"postfix operator"#
368+
case .prefixOperator:
369+
return #"prefix operator"#
370+
case .dollarIdentifier:
371+
return #"dollar identifier"#
372+
case .rawStringDelimiter:
373+
return #"raw string delimiter"#
374+
case .stringSegment:
375+
return #"string segment"#
376+
case .keyword(let keyword):
377+
return String(syntaxText: keyword.defaultText)
378+
}
379+
}
380+
282381
/// Returns `true` if the token is a Swift keyword.
283382
///
284383
/// Keywords are reserved unconditionally for use by Swift and may not
@@ -996,105 +1095,6 @@ public struct RawTokenKind: Equatable, Hashable {
9961095
}
9971096
}
9981097

999-
public var nameForDiagnostics: String {
1000-
switch self.base {
1001-
case .eof:
1002-
return "end of file"
1003-
case .wildcard:
1004-
return #"wildcard"#
1005-
case .leftParen:
1006-
return #"("#
1007-
case .rightParen:
1008-
return #")"#
1009-
case .leftBrace:
1010-
return #"{"#
1011-
case .rightBrace:
1012-
return #"}"#
1013-
case .leftSquareBracket:
1014-
return #"["#
1015-
case .rightSquareBracket:
1016-
return #"]"#
1017-
case .leftAngle:
1018-
return #"<"#
1019-
case .rightAngle:
1020-
return #">"#
1021-
case .period:
1022-
return #"."#
1023-
case .comma:
1024-
return #","#
1025-
case .ellipsis:
1026-
return #"..."#
1027-
case .colon:
1028-
return #":"#
1029-
case .semicolon:
1030-
return #";"#
1031-
case .equal:
1032-
return #"="#
1033-
case .atSign:
1034-
return #"@"#
1035-
case .pound:
1036-
return #"#"#
1037-
case .prefixAmpersand:
1038-
return #"&"#
1039-
case .arrow:
1040-
return #"->"#
1041-
case .backtick:
1042-
return #"`"#
1043-
case .backslash:
1044-
return #"\"#
1045-
case .exclamationMark:
1046-
return #"!"#
1047-
case .postfixQuestionMark:
1048-
return #"?"#
1049-
case .infixQuestionMark:
1050-
return #"?"#
1051-
case .stringQuote:
1052-
return #"""#
1053-
case .singleQuote:
1054-
return #"'"#
1055-
case .multilineStringQuote:
1056-
return #"""""#
1057-
case .poundSourceLocationKeyword:
1058-
return #"#sourceLocation"#
1059-
case .poundIfKeyword:
1060-
return #"#if"#
1061-
case .poundElseKeyword:
1062-
return #"#else"#
1063-
case .poundElseifKeyword:
1064-
return #"#elseif"#
1065-
case .poundEndifKeyword:
1066-
return #"#endif"#
1067-
case .poundAvailableKeyword:
1068-
return #"#available"#
1069-
case .poundUnavailableKeyword:
1070-
return #"#unavailable"#
1071-
case .integerLiteral:
1072-
return #"integer literal"#
1073-
case .floatingLiteral:
1074-
return #"floating literal"#
1075-
case .regexLiteral:
1076-
return #"regex literal"#
1077-
case .unknown:
1078-
return #"token"#
1079-
case .identifier:
1080-
return #"identifier"#
1081-
case .binaryOperator:
1082-
return #"binary operator"#
1083-
case .postfixOperator:
1084-
return #"postfix operator"#
1085-
case .prefixOperator:
1086-
return #"prefix operator"#
1087-
case .dollarIdentifier:
1088-
return #"dollar identifier"#
1089-
case .rawStringDelimiter:
1090-
return #"raw string delimiter"#
1091-
case .stringSegment:
1092-
return #"string segment"#
1093-
case .keyword:
1094-
return String(syntaxText: self.keyword.defaultText)
1095-
}
1096-
}
1097-
10981098
/// Returns `true` if the token is a Swift keyword.
10991099
///
11001100
/// Keywords are reserved unconditionally for use by Swift and may not

0 commit comments

Comments
 (0)