-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathTokenSpec.swift
290 lines (274 loc) · 9.88 KB
/
TokenSpec.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftSyntax
/// Represents the specification for a Token in the TokenSyntax file.
public struct TokenSpec: IdentifierConvertible {
public enum Kind {
case punctuation
/// The `keyword` TokenKind that contains the actual keyword as an associated value
case keyword
case other
}
/// The name of the token as an identifier.
public let identifier: TokenSyntax
/// The experimental feature the token is part of, or `nil` if this isn't
/// for an experimental feature.
public let experimentalFeature: ExperimentalFeature?
/// The name of the token that can be shown in diagnostics.
public let nameForDiagnostics: String
/// The actual text of the token, if available.
public let text: String?
/// The kind of the token.
public let kind: Kind
/// Indicates if the token is part of an experimental language feature.
///
/// If `true`, this keyword is for an experimental language feature, and any public
/// API generated should be marked as SPI
public var isExperimental: Bool { experimentalFeature != nil }
/// The attributes that should be printed on any API for the generated keyword.
///
/// This is typically used to mark APIs as SPI when the keyword is part of an experimental language feature.
public var apiAttributes: AttributeListSyntax {
guard isExperimental else { return "" }
return AttributeListSyntax("@_spi(ExperimentalLanguageFeatures)").with(\.trailingTrivia, .newline)
}
/// Initializes a new `TokenSpec` instance.
///
/// - Parameters:
/// - name: A name of the token.
/// - experimentalFeature: The experimental feature the token is part of, or `nil` if this isn't for an experimental feature.
/// - nameForDiagnostics: A name of the token that can be shown in diagnostics.
/// - text: An actual text of the token, if available.
/// - kind: A kind of the token.
fileprivate init(
name: String,
experimentalFeature: ExperimentalFeature? = nil,
nameForDiagnostics: String,
text: String? = nil,
kind: Kind
) {
self.identifier = .identifier(name)
self.experimentalFeature = experimentalFeature
self.nameForDiagnostics = nameForDiagnostics
self.text = text
self.kind = kind
}
/// Creates a new `TokenSpec` instance representing a punctuation token.
///
/// - Parameters:
/// - name: A name of the token.
/// - text: An actual text of the punctuation token.
static func punctuator(name: String, text: String) -> TokenSpec {
return TokenSpec(
name: name,
nameForDiagnostics: text,
text: text,
kind: .punctuation
)
}
/// Creates a new `TokenSpec` instance representing a pound keyword token.
///
/// - Parameters:
/// - name: A name of the token.
/// - text: An actual text of the pound keyword token.
static func poundKeyword(name: String, text: String) -> TokenSpec {
return TokenSpec(
name: name,
nameForDiagnostics: text,
text: text,
kind: .other
)
}
/// Creates a new `TokenSpec` instance representing an other token.
///
/// - Parameters:
/// - name: A name of the token.
/// - nameForDiagnostics: A name of the token that can be shown in diagnostics.
/// - text: An actual text of the token, if available.
static func other(name: String, nameForDiagnostics: String, text: String? = nil) -> TokenSpec {
return TokenSpec(
name: name,
nameForDiagnostics: nameForDiagnostics,
text: text,
kind: .other
)
}
}
public enum Token: CaseIterable {
// Please keep this list sorted alphabetically
case arrow
case atSign
case backslash
case backtick
case binaryOperator
case colon
case comma
case dollarIdentifier
case ellipsis
case endOfFile
case equal
case exclamationMark
case floatLiteral
case identifier
case infixQuestionMark
case integerLiteral
case keyword
case leadingBoxCorner
case leadingBoxJunction
case leftAngle
case leftBrace
case leftParen
case leftSquare
case multilineStringQuote
case period
case postfixOperator
case postfixQuestionMark
case pound
case poundAvailable
case poundElse
case poundElseif
case poundEndif
case poundIf
case poundSourceLocation
case poundUnavailable
case prefixAmpersand
case prefixOperator
case rawStringPoundDelimiter
case regexLiteralPattern
case regexPoundDelimiter
case regexSlash
case rightAngle
case rightBrace
case rightParen
case rightSquare
case semicolon
case shebang
case singleQuote
case stringQuote
case stringSegment
case trailingBoxCorner
case trailingBoxJunction
case unknown
case wildcard
public var spec: TokenSpec {
switch self {
case .arrow:
return .punctuator(name: "arrow", text: "->")
case .atSign:
return .punctuator(name: "atSign", text: "@")
case .backslash:
return .punctuator(name: "backslash", text: "\\")
case .backtick:
return .punctuator(name: "backtick", text: "`")
case .binaryOperator:
return .other(name: "binaryOperator", nameForDiagnostics: "binary operator")
case .colon:
return .punctuator(name: "colon", text: ":")
case .comma:
return .punctuator(name: "comma", text: ",")
case .dollarIdentifier:
return .other(name: "dollarIdentifier", nameForDiagnostics: "dollar identifier")
case .ellipsis:
return .punctuator(name: "ellipsis", text: "...")
case .endOfFile:
return .other(name: "endOfFile", nameForDiagnostics: "end of file", text: "")
case .equal:
return .punctuator(name: "equal", text: "=")
case .exclamationMark:
return .punctuator(name: "exclamationMark", text: "!")
case .floatLiteral:
return .other(name: "floatLiteral", nameForDiagnostics: "float literal")
case .identifier:
return .other(name: "identifier", nameForDiagnostics: "identifier")
case .infixQuestionMark:
return .punctuator(name: "infixQuestionMark", text: "?")
case .integerLiteral:
return .other(name: "integerLiteral", nameForDiagnostics: "integer literal")
case .keyword:
return TokenSpec(name: "keyword", nameForDiagnostics: "keyword", text: nil, kind: .keyword)
case .leadingBoxCorner:
return .punctuator(name: "leadingBoxCorner", text: "╗")
case .leadingBoxJunction:
return .punctuator(name: "leadingBoxJunction", text: "╣")
case .leftAngle:
return .punctuator(name: "leftAngle", text: "<")
case .leftBrace:
return .punctuator(name: "leftBrace", text: "{")
case .leftParen:
return .punctuator(name: "leftParen", text: "(")
case .leftSquare:
return .punctuator(name: "leftSquare", text: "[")
case .multilineStringQuote:
return .punctuator(name: "multilineStringQuote", text: "\"\"\"")
case .period:
return .punctuator(name: "period", text: ".")
case .postfixOperator:
return .other(name: "postfixOperator", nameForDiagnostics: "postfix operator")
case .postfixQuestionMark:
return .punctuator(name: "postfixQuestionMark", text: "?")
case .pound:
return .punctuator(name: "pound", text: "#")
case .poundAvailable:
return .poundKeyword(name: "poundAvailable", text: "#available")
case .poundElse:
return .poundKeyword(name: "poundElse", text: "#else")
case .poundElseif:
return .poundKeyword(name: "poundElseif", text: "#elseif")
case .poundEndif:
return .poundKeyword(name: "poundEndif", text: "#endif")
case .poundIf:
return .poundKeyword(name: "poundIf", text: "#if")
case .poundSourceLocation:
return .poundKeyword(name: "poundSourceLocation", text: "#sourceLocation")
case .poundUnavailable:
return .poundKeyword(name: "poundUnavailable", text: "#unavailable")
case .prefixAmpersand:
return .punctuator(name: "prefixAmpersand", text: "&")
case .prefixOperator:
return .other(name: "prefixOperator", nameForDiagnostics: "prefix operator")
case .rawStringPoundDelimiter:
return .other(name: "rawStringPoundDelimiter", nameForDiagnostics: "raw string delimiter")
case .regexLiteralPattern:
return .other(name: "regexLiteralPattern", nameForDiagnostics: "regex pattern")
case .regexPoundDelimiter:
return .other(name: "regexPoundDelimiter", nameForDiagnostics: "extended delimiter")
case .regexSlash:
return .punctuator(name: "regexSlash", text: "/")
case .rightAngle:
return .punctuator(name: "rightAngle", text: ">")
case .rightBrace:
return .punctuator(name: "rightBrace", text: "}")
case .rightParen:
return .punctuator(name: "rightParen", text: ")")
case .rightSquare:
return .punctuator(name: "rightSquare", text: "]")
case .semicolon:
return .punctuator(name: "semicolon", text: ";")
case .shebang:
return .other(name: "shebang", nameForDiagnostics: "shebang")
case .singleQuote:
return .punctuator(name: "singleQuote", text: "\'")
case .stringQuote:
return .punctuator(name: "stringQuote", text: "\"")
case .stringSegment:
return .other(name: "stringSegment", nameForDiagnostics: "string segment")
case .trailingBoxCorner:
return .punctuator(name: "trailingBoxCorner", text: "╚")
case .trailingBoxJunction:
return .punctuator(name: "trailingBoxJunction", text: "╠")
case .unknown:
return .other(name: "unknown", nameForDiagnostics: "token")
case .wildcard:
return .other(name: "wildcard", nameForDiagnostics: "wildcard", text: "_")
}
}
}