forked from swiftlang/swift-syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenSpec.swift
314 lines (292 loc) · 14.3 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//// Automatically Generated From Tokens.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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
//
//===----------------------------------------------------------------------===//
/// Represents the specification for a Token in the TokenSyntax file.
public class TokenSpec {
public let name: String
public let kind: String
public let nameForDiagnostics: String
public let unprefixedKind: String
public let text: String?
public let classification: SyntaxClassification?
public let isKeyword: Bool
public let requiresLeadingSpace: Bool
public let requiresTrailingSpace: Bool
public var swiftKind: String {
let name = lowercaseFirstWord(name: self.name)
if isKeyword {
return name + "Keyword"
} else {
return name
}
}
init(
name: String,
kind: String,
nameForDiagnostics: String,
unprefixedKind: String? = nil,
text: String? = nil,
classification: String = "None",
isKeyword: Bool = false,
requiresLeadingSpace: Bool = false,
requiresTrailingSpace: Bool = false
) {
self.name = name
self.kind = kind
self.nameForDiagnostics = nameForDiagnostics
if let unprefixedKind = unprefixedKind {
self.unprefixedKind = unprefixedKind
} else {
self.unprefixedKind = kind
}
self.text = text
self.classification = classificationByName(classification)
self.isKeyword = isKeyword
self.requiresLeadingSpace = requiresLeadingSpace
self.requiresTrailingSpace = requiresTrailingSpace
}
}
/// Represents a keyword token.
public class KeywordSpec: TokenSpec {
init(
name: String,
text: String,
classification: String = "Keyword",
requiresLeadingSpace: Bool = false,
requiresTrailingSpace: Bool = false
) {
super.init(
name: name,
kind: "kw_\(text)",
nameForDiagnostics: text,
unprefixedKind: text,
text: text,
classification: classification,
isKeyword: true,
requiresLeadingSpace: requiresLeadingSpace,
requiresTrailingSpace: requiresTrailingSpace
)
}
}
public class SwiftKeywordSpec: KeywordSpec { }
public class DeclKeywordSpec: SwiftKeywordSpec { }
public class StmtKeywordSpec: SwiftKeywordSpec { }
public class ExprKeywordSpec: SwiftKeywordSpec { }
public class PatternKeywordSpec: SwiftKeywordSpec { }
public class SilKeywordSpec: KeywordSpec { }
public class PoundKeywordSpec: TokenSpec {
init(
name: String,
kind: String,
nameForDiagnostics: String? = nil,
text: String,
classification: String = "Keyword"
) {
super.init(
name: name,
kind: "pound_\(kind)",
nameForDiagnostics: nameForDiagnostics ?? text,
unprefixedKind: kind,
text: text,
classification: classification,
isKeyword: true,
requiresTrailingSpace: true
)
}
}
public class PoundObjectLiteralSpec: PoundKeywordSpec {
let `protocol`: String
init(
name: String,
kind: String,
text: String,
classification: String = "ObjectLiteral",
nameForDiagnostics: String,
`protocol`: String
) {
self.`protocol` = `protocol`
super.init(
name: name,
kind: kind,
nameForDiagnostics: nameForDiagnostics,
text: text,
classification: classification
)
}
}
public class PoundConfigSpec: PoundKeywordSpec { }
public class PoundDirectiveKeywordSpec: PoundKeywordSpec {
init(
name: String,
kind: String,
text: String,
classification: String = "PoundDirectiveKeyword"
) {
super.init(
name: name,
kind: kind,
text: text,
classification: classification
)
}
}
public class PoundConditionalDirectiveKeywordSpec: PoundDirectiveKeywordSpec {
override init(
name: String,
kind: String,
text: String,
classification: String = "PoundDirectiveKeyword"
) {
super.init(
name: name,
kind: kind,
text: text,
classification: classification
)
}
}
public class PunctuatorSpec: TokenSpec {
init(
name: String,
kind: String,
text: String,
classification: String = "None",
requiresLeadingSpace: Bool = false,
requiresTrailingSpace: Bool = false
) {
super.init(
name: name,
kind: kind,
nameForDiagnostics: text,
unprefixedKind: nil,
text: text,
classification: classification,
isKeyword: false,
requiresLeadingSpace: requiresLeadingSpace,
requiresTrailingSpace: requiresTrailingSpace
)
}
}
public class LiteralSpec: TokenSpec { }
public class MiscSpec: TokenSpec { }
public let SYNTAX_TOKENS: [TokenSpec] = [
DeclKeywordSpec(name: "Associatedtype", text: "associatedtype", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Class", text: "class", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Deinit", text: "deinit", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Enum", text: "enum", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Extension", text: "extension", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Func", text: "func", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Import", text: "import", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Init", text: "init", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Inout", text: "inout", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Let", text: "let", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Operator", text: "operator", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Precedencegroup", text: "precedencegroup", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Protocol", text: "protocol", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Struct", text: "struct", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Subscript", text: "subscript", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Typealias", text: "typealias", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Var", text: "var", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Fileprivate", text: "fileprivate", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Internal", text: "internal", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Private", text: "private", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Public", text: "public", requiresTrailingSpace: true),
DeclKeywordSpec(name: "Static", text: "static", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Defer", text: "defer", requiresTrailingSpace: true),
StmtKeywordSpec(name: "If", text: "if", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Guard", text: "guard", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Do", text: "do"),
StmtKeywordSpec(name: "Repeat", text: "repeat", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Else", text: "else", requiresTrailingSpace: true),
StmtKeywordSpec(name: "For", text: "for", requiresTrailingSpace: true),
StmtKeywordSpec(name: "In", text: "in", requiresTrailingSpace: true),
StmtKeywordSpec(name: "While", text: "while", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Return", text: "return", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Break", text: "break", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Continue", text: "continue", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Fallthrough", text: "fallthrough", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Switch", text: "switch", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Case", text: "case", requiresTrailingSpace: true),
StmtKeywordSpec(name: "Default", text: "default"),
StmtKeywordSpec(name: "Where", text: "where", requiresLeadingSpace: true, requiresTrailingSpace: true),
StmtKeywordSpec(name: "Catch", text: "catch", requiresLeadingSpace: true),
StmtKeywordSpec(name: "Throw", text: "throw", requiresTrailingSpace: true),
ExprKeywordSpec(name: "As", text: "as", requiresTrailingSpace: true),
ExprKeywordSpec(name: "Any", text: "Any", requiresTrailingSpace: true),
ExprKeywordSpec(name: "False", text: "false"),
ExprKeywordSpec(name: "Is", text: "is", requiresTrailingSpace: true),
ExprKeywordSpec(name: "Nil", text: "nil"),
ExprKeywordSpec(name: "Rethrows", text: "rethrows", requiresTrailingSpace: true),
ExprKeywordSpec(name: "Super", text: "super"),
ExprKeywordSpec(name: "Self", text: "self"),
ExprKeywordSpec(name: "CapitalSelf", text: "Self"),
ExprKeywordSpec(name: "True", text: "true"),
ExprKeywordSpec(name: "Try", text: "try", requiresTrailingSpace: true),
ExprKeywordSpec(name: "Throws", text: "throws", requiresTrailingSpace: true),
PatternKeywordSpec(name: "Wildcard", text: "_", requiresTrailingSpace: true),
PunctuatorSpec(name: "LeftParen", kind: "l_paren", text: "("),
PunctuatorSpec(name: "RightParen", kind: "r_paren", text: ")"),
PunctuatorSpec(name: "LeftBrace", kind: "l_brace", text: "{", requiresLeadingSpace: true),
PunctuatorSpec(name: "RightBrace", kind: "r_brace", text: "}"),
PunctuatorSpec(name: "LeftSquareBracket", kind: "l_square", text: "["),
PunctuatorSpec(name: "RightSquareBracket", kind: "r_square", text: "]"),
PunctuatorSpec(name: "LeftAngle", kind: "l_angle", text: "<", requiresLeadingSpace: true, requiresTrailingSpace: true),
PunctuatorSpec(name: "RightAngle", kind: "r_angle", text: ">", requiresLeadingSpace: true, requiresTrailingSpace: true),
PunctuatorSpec(name: "Period", kind: "period", text: "."),
PunctuatorSpec(name: "PrefixPeriod", kind: "period_prefix", text: "."),
PunctuatorSpec(name: "Comma", kind: "comma", text: ",", requiresTrailingSpace: true),
PunctuatorSpec(name: "Ellipsis", kind: "ellipsis", text: "..."),
PunctuatorSpec(name: "Colon", kind: "colon", text: ":", requiresTrailingSpace: true),
PunctuatorSpec(name: "Semicolon", kind: "semi", text: ";"),
PunctuatorSpec(name: "Equal", kind: "equal", text: "=", requiresLeadingSpace: true, requiresTrailingSpace: true),
PunctuatorSpec(name: "AtSign", kind: "at_sign", text: "@", classification: "Attribute"),
PunctuatorSpec(name: "Pound", kind: "pound", text: "#"),
PunctuatorSpec(name: "PrefixAmpersand", kind: "amp_prefix", text: "&"),
PunctuatorSpec(name: "Arrow", kind: "arrow", text: "->", requiresLeadingSpace: true, requiresTrailingSpace: true),
PunctuatorSpec(name: "Backtick", kind: "backtick", text: "`"),
PunctuatorSpec(name: "Backslash", kind: "backslash", text: "\\"),
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!"),
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?"),
PunctuatorSpec(name: "InfixQuestionMark", kind: "question_infix", text: "?"),
PunctuatorSpec(name: "StringQuote", kind: "string_quote", text: "\"", classification: "StringLiteral"),
PunctuatorSpec(name: "SingleQuote", kind: "single_quote", text: "\'", classification: "StringLiteral"),
PunctuatorSpec(name: "MultilineStringQuote", kind: "multiline_string_quote", text: "\"\"\"", classification: "StringLiteral"),
PoundKeywordSpec(name: "PoundAssert", kind: "pound_assert", text: "#assert"),
PoundDirectiveKeywordSpec(name: "PoundSourceLocation", kind: "pound_sourceLocation", text: "#sourceLocation"),
PoundDirectiveKeywordSpec(name: "PoundWarning", kind: "pound_warning", text: "#warning"),
PoundDirectiveKeywordSpec(name: "PoundError", kind: "pound_error", text: "#error"),
PoundConditionalDirectiveKeywordSpec(name: "PoundIf", kind: "pound_if", text: "#if"),
PoundConditionalDirectiveKeywordSpec(name: "PoundElse", kind: "pound_else", text: "#else"),
PoundConditionalDirectiveKeywordSpec(name: "PoundElseif", kind: "pound_elseif", text: "#elseif"),
PoundConditionalDirectiveKeywordSpec(name: "PoundEndif", kind: "pound_endif", text: "#endif"),
PoundConfigSpec(name: "PoundAvailable", kind: "pound_available", text: "#available"),
PoundConfigSpec(name: "PoundUnavailable", kind: "pound_unavailable", text: "#unavailable"),
PoundConfigSpec(name: "PoundHasSymbol", kind: "pound__hasSymbol", text: "#_hasSymbol"),
LiteralSpec(name: "IntegerLiteral", kind: "integer_literal", nameForDiagnostics: "integer literal", classification: "IntegerLiteral"),
LiteralSpec(name: "FloatingLiteral", kind: "floating_literal", nameForDiagnostics: "floating literal", classification: "FloatingLiteral"),
LiteralSpec(name: "StringLiteral", kind: "string_literal", nameForDiagnostics: "string literal", classification: "StringLiteral"),
LiteralSpec(name: "RegexLiteral", kind: "regex_literal", nameForDiagnostics: "regex literal"),
MiscSpec(name: "Unknown", kind: "unknown", nameForDiagnostics: "token"),
MiscSpec(name: "Identifier", kind: "identifier", nameForDiagnostics: "identifier", classification: "Identifier"),
MiscSpec(name: "UnspacedBinaryOperator", kind: "oper_binary_unspaced", nameForDiagnostics: "binary operator", classification: "OperatorIdentifier"),
MiscSpec(name: "SpacedBinaryOperator", kind: "oper_binary_spaced", nameForDiagnostics: "binary operator", classification: "OperatorIdentifier", requiresLeadingSpace: true, requiresTrailingSpace: true),
MiscSpec(name: "PostfixOperator", kind: "oper_postfix", nameForDiagnostics: "postfix operator", classification: "OperatorIdentifier"),
MiscSpec(name: "PrefixOperator", kind: "oper_prefix", nameForDiagnostics: "prefix operator", classification: "OperatorIdentifier"),
MiscSpec(name: "DollarIdentifier", kind: "dollarident", nameForDiagnostics: "dollar identifier", classification: "DollarIdentifier"),
MiscSpec(name: "ContextualKeyword", kind: "contextual_keyword", nameForDiagnostics: "keyword", classification: "Keyword"),
MiscSpec(name: "RawStringDelimiter", kind: "raw_string_delimiter", nameForDiagnostics: "raw string delimiter"),
MiscSpec(name: "StringSegment", kind: "string_segment", nameForDiagnostics: "string segment", classification: "StringLiteral"),
MiscSpec(name: "StringInterpolationAnchor", kind: "string_interpolation_anchor", nameForDiagnostics: "string interpolation anchor", text: ")", classification: "StringInterpolationAnchor"),
MiscSpec(name: "Yield", kind: "kw_yield", nameForDiagnostics: "yield", text: "yield"),
]
public let SYNTAX_TOKEN_MAP = Dictionary(uniqueKeysWithValues: SYNTAX_TOKENS.map { ("\($0.name)Token", $0) })