Skip to content

Commit 0f2b2d1

Browse files
committed
Add a nameForDiagnostics to tokens
1 parent 4f1286f commit 0f2b2d1

File tree

6 files changed

+319
-68
lines changed

6 files changed

+319
-68
lines changed

Sources/SwiftSyntax/TokenKind.swift.gyb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ public enum RawTokenKind: Equatable, Hashable {
161161
}
162162
}
163163

164+
public var nameForDiagnostics: String {
165+
switch self {
166+
case .eof: return "end of file"
167+
% for token in SYNTAX_TOKENS:
168+
case .${token.swift_kind()}: return "${token.name_for_diagnostics}"
169+
% end
170+
}
171+
}
172+
164173
/// Returns `true` if the token is a Swift keyword.
165174
///
166175
/// Keywords are reserved unconditionally for use by Swift and may not

Sources/SwiftSyntax/gyb_generated/TokenKind.swift

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,136 @@ public enum RawTokenKind: Equatable, Hashable {
13661366
}
13671367
}
13681368

1369+
public var nameForDiagnostics: String {
1370+
switch self {
1371+
case .eof: return "end of file"
1372+
case .associatedtypeKeyword: return "associatedtype"
1373+
case .classKeyword: return "class"
1374+
case .deinitKeyword: return "deinit"
1375+
case .enumKeyword: return "enum"
1376+
case .extensionKeyword: return "extension"
1377+
case .funcKeyword: return "func"
1378+
case .importKeyword: return "import"
1379+
case .initKeyword: return "init"
1380+
case .inoutKeyword: return "inout"
1381+
case .letKeyword: return "let"
1382+
case .operatorKeyword: return "operator"
1383+
case .precedencegroupKeyword: return "precedencegroup"
1384+
case .protocolKeyword: return "protocol"
1385+
case .structKeyword: return "struct"
1386+
case .subscriptKeyword: return "subscript"
1387+
case .typealiasKeyword: return "typealias"
1388+
case .varKeyword: return "var"
1389+
case .fileprivateKeyword: return "fileprivate"
1390+
case .internalKeyword: return "internal"
1391+
case .privateKeyword: return "private"
1392+
case .publicKeyword: return "public"
1393+
case .staticKeyword: return "static"
1394+
case .deferKeyword: return "defer"
1395+
case .ifKeyword: return "if"
1396+
case .guardKeyword: return "guard"
1397+
case .doKeyword: return "do"
1398+
case .repeatKeyword: return "repeat"
1399+
case .elseKeyword: return "else"
1400+
case .forKeyword: return "for"
1401+
case .inKeyword: return "in"
1402+
case .whileKeyword: return "while"
1403+
case .returnKeyword: return "return"
1404+
case .breakKeyword: return "break"
1405+
case .continueKeyword: return "continue"
1406+
case .fallthroughKeyword: return "fallthrough"
1407+
case .switchKeyword: return "switch"
1408+
case .caseKeyword: return "case"
1409+
case .defaultKeyword: return "default"
1410+
case .whereKeyword: return "where"
1411+
case .catchKeyword: return "catch"
1412+
case .throwKeyword: return "throw"
1413+
case .asKeyword: return "as"
1414+
case .anyKeyword: return "Any"
1415+
case .falseKeyword: return "false"
1416+
case .isKeyword: return "is"
1417+
case .nilKeyword: return "nil"
1418+
case .rethrowsKeyword: return "rethrows"
1419+
case .superKeyword: return "super"
1420+
case .selfKeyword: return "self"
1421+
case .capitalSelfKeyword: return "Self"
1422+
case .trueKeyword: return "true"
1423+
case .tryKeyword: return "try"
1424+
case .throwsKeyword: return "throws"
1425+
case .__file__Keyword: return "__FILE__"
1426+
case .__line__Keyword: return "__LINE__"
1427+
case .__column__Keyword: return "__COLUMN__"
1428+
case .__function__Keyword: return "__FUNCTION__"
1429+
case .__dso_handle__Keyword: return "__DSO_HANDLE__"
1430+
case .wildcardKeyword: return "_"
1431+
case .leftParen: return "("
1432+
case .rightParen: return ")"
1433+
case .leftBrace: return "{"
1434+
case .rightBrace: return "}"
1435+
case .leftSquareBracket: return "["
1436+
case .rightSquareBracket: return "]"
1437+
case .leftAngle: return "<"
1438+
case .rightAngle: return ">"
1439+
case .period: return "."
1440+
case .prefixPeriod: return "."
1441+
case .comma: return ","
1442+
case .ellipsis: return "..."
1443+
case .colon: return ":"
1444+
case .semicolon: return ";"
1445+
case .equal: return "="
1446+
case .atSign: return "@"
1447+
case .pound: return "#"
1448+
case .prefixAmpersand: return "&"
1449+
case .arrow: return "->"
1450+
case .backtick: return "`"
1451+
case .backslash: return "\\"
1452+
case .exclamationMark: return "!"
1453+
case .postfixQuestionMark: return "?"
1454+
case .infixQuestionMark: return "?"
1455+
case .stringQuote: return "\""
1456+
case .singleQuote: return "\'"
1457+
case .multilineStringQuote: return "\"\"\""
1458+
case .poundKeyPathKeyword: return "#keyPath"
1459+
case .poundLineKeyword: return "#line"
1460+
case .poundSelectorKeyword: return "#selector"
1461+
case .poundFileKeyword: return "#file"
1462+
case .poundFileIDKeyword: return "#fileID"
1463+
case .poundFilePathKeyword: return "#filePath"
1464+
case .poundColumnKeyword: return "#column"
1465+
case .poundFunctionKeyword: return "#function"
1466+
case .poundDsohandleKeyword: return "#dsohandle"
1467+
case .poundAssertKeyword: return "#assert"
1468+
case .poundSourceLocationKeyword: return "#sourceLocation"
1469+
case .poundWarningKeyword: return "#warning"
1470+
case .poundErrorKeyword: return "#error"
1471+
case .poundIfKeyword: return "#if"
1472+
case .poundElseKeyword: return "#else"
1473+
case .poundElseifKeyword: return "#elseif"
1474+
case .poundEndifKeyword: return "#endif"
1475+
case .poundAvailableKeyword: return "#available"
1476+
case .poundUnavailableKeyword: return "#unavailable"
1477+
case .poundFileLiteralKeyword: return "file reference"
1478+
case .poundImageLiteralKeyword: return "image"
1479+
case .poundColorLiteralKeyword: return "color"
1480+
case .integerLiteral: return "integer literal"
1481+
case .floatingLiteral: return "floating literal"
1482+
case .stringLiteral: return "string literal"
1483+
case .regexLiteral: return "regex literal"
1484+
case .unknown: return "token"
1485+
case .identifier: return "identifier"
1486+
case .unspacedBinaryOperator: return "binary operator"
1487+
case .spacedBinaryOperator: return "binary operator"
1488+
case .postfixOperator: return "postfix operator"
1489+
case .prefixOperator: return "prefix operator"
1490+
case .dollarIdentifier: return "dollar identifier"
1491+
case .contextualKeyword: return "keyword"
1492+
case .rawStringDelimiter: return "raw string delimiter"
1493+
case .stringSegment: return "string segment"
1494+
case .stringInterpolationAnchor: return "string interpolation anchor"
1495+
case .yield: return "yield"
1496+
}
1497+
}
1498+
13691499
/// Returns `true` if the token is a Swift keyword.
13701500
///
13711501
/// Keywords are reserved unconditionally for use by Swift and may not

Sources/generate-swift-syntax-builder/TokenSpec.swift.gyb

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TokenSpec {
2323
let name: String
2424
let kind: String
2525
let serializationCode: Int
26+
let nameForDiagnostics: String
2627
let unprefixedKind: String
2728
let text: String?
2829
let classification: String
@@ -44,6 +45,7 @@ class TokenSpec {
4445
name: String,
4546
kind: String,
4647
serializationCode: Int,
48+
nameForDiagnostics: String,
4749
unprefixedKind: String? = nil,
4850
text: String? = nil,
4951
classification: String = "None",
@@ -54,6 +56,7 @@ class TokenSpec {
5456
self.name = name
5557
self.kind = kind
5658
self.serializationCode = serializationCode
59+
self.nameForDiagnostics = nameForDiagnostics
5760
if let unprefixedKind = unprefixedKind {
5861
self.unprefixedKind = unprefixedKind
5962
} else {
@@ -81,6 +84,7 @@ class KeywordSpec: TokenSpec {
8184
name: name,
8285
kind: "kw_\(text)",
8386
serializationCode: serializationCode,
87+
nameForDiagnostics: text,
8488
unprefixedKind: text,
8589
text: text,
8690
classification: classification,
@@ -108,13 +112,15 @@ class PoundKeywordSpec: TokenSpec {
108112
name: String,
109113
kind: String,
110114
serializationCode: Int,
115+
nameForDiagnostics: String? = nil,
111116
text: String,
112117
classification: String = "Keyword"
113118
) {
114119
super.init(
115120
name: name,
116121
kind: "pound_\(kind)",
117122
serializationCode: serializationCode,
123+
nameForDiagnostics: nameForDiagnostics ?? text,
118124
unprefixedKind: kind,
119125
text: text,
120126
classification: classification,
@@ -125,7 +131,6 @@ class PoundKeywordSpec: TokenSpec {
125131
}
126132

127133
class PoundObjectLiteralSpec: PoundKeywordSpec {
128-
let description: String
129134
let `protocol`: String
130135

131136
init(
@@ -134,15 +139,15 @@ class PoundObjectLiteralSpec: PoundKeywordSpec {
134139
serializationCode: Int,
135140
text: String,
136141
classification: String = "ObjectLiteral",
137-
description: String,
142+
nameForDiagnostics: String,
138143
`protocol`: String
139144
) {
140-
self.description = `description`
141145
self.`protocol` = `protocol`
142146
super.init(
143147
name: name,
144148
kind: kind,
145149
serializationCode: serializationCode,
150+
nameForDiagnostics: nameForDiagnostics,
146151
text: text,
147152
classification: classification
148153
)
@@ -152,7 +157,7 @@ class PoundObjectLiteralSpec: PoundKeywordSpec {
152157
class PoundConfigSpec: PoundKeywordSpec { }
153158

154159
class PoundDirectiveKeywordSpec: PoundKeywordSpec {
155-
override init(
160+
init(
156161
name: String,
157162
kind: String,
158163
serializationCode: Int,
@@ -187,7 +192,30 @@ class PoundConditionalDirectiveKeywordSpec: PoundDirectiveKeywordSpec {
187192
}
188193
}
189194

190-
class PunctuatorSpec: TokenSpec { }
195+
class PunctuatorSpec: TokenSpec {
196+
init(
197+
name: String,
198+
kind: String,
199+
serializationCode: Int,
200+
text: String,
201+
classification: String = "None",
202+
requiresLeadingSpace: Bool = false,
203+
requiresTrailingSpace: Bool = false
204+
) {
205+
super.init(
206+
name: name,
207+
kind: kind,
208+
serializationCode: serializationCode,
209+
nameForDiagnostics: text,
210+
unprefixedKind: nil,
211+
text: text,
212+
classification: classification,
213+
isKeyword: false,
214+
requiresLeadingSpace: requiresLeadingSpace,
215+
requiresTrailingSpace: requiresTrailingSpace
216+
)
217+
}
218+
}
191219

192220
class LiteralSpec: TokenSpec { }
193221

@@ -203,11 +231,14 @@ let SYNTAX_TOKENS: [TokenSpec] = [
203231
% parameters += ["kind: \"%s\"" % token.kind, "serializationCode: %s" % token.serialization_code, "text: \"%s\"" % token.text]
204232
% elif class_name in ['Punctuator', 'Misc', 'Literal']:
205233
% parameters += ["kind: \"%s\"" % token.kind, "serializationCode: %s" % token.serialization_code]
234+
% if class_name != 'Punctuator':
235+
% parameters += ["nameForDiagnostics: \"%s\"" % token.name_for_diagnostics]
236+
% end
206237
% if token.text:
207238
% parameters += ["text: \"%s\"" % token.text]
208239
% end
209240
% elif class_name == 'PoundObjectLiteral':
210-
% parameters += ["kind: \"%s\"" % token.kind, "serializationCode: %s" % token.serialization_code, "text: \"%s\"" % token.text, "description: \"%s\"" % token.description, "protocol: \"%s\"" % token.protocol]
241+
% parameters += ["kind: \"%s\"" % token.kind, "serializationCode: %s" % token.serialization_code, "text: \"%s\"" % token.text, "nameForDiagnostics: \"%s\"" % token.name_for_diagnostics, "protocol: \"%s\"" % token.protocol]
211242
% else:
212243
% print("Unknown token `%s`", (token.name), file=sys.stderr)
213244
% sys.exit(1)

0 commit comments

Comments
 (0)