Skip to content

Commit 1113a19

Browse files
authored
Merge pull request #1174 from ahoppen/ahoppen/merge-keywords
Handle contextual and non-contextual keywords in the same way
2 parents 060082e + 9d94925 commit 1113a19

File tree

105 files changed

+3368
-5463
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+3368
-5463
lines changed
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
public struct KeywordSpec {
14+
public var name: String
15+
public var isLexerClassified: Bool
16+
public var requiresLeadingSpace: Bool
17+
public var requiresTrailingSpace: Bool
18+
19+
public var escapedName: String {
20+
if isLexerClassified || name == "Type" || name == "Protocol" {
21+
return "`\(name)`"
22+
} else {
23+
return name
24+
}
25+
}
26+
27+
/// `isLexerClassified` determines whether the token kind is switched from being an identifier to a keyword in the lexer.
28+
/// This is true for keywords that used to be considered non-contextual.
29+
init(_ name: String, isLexerClassified: Bool = false, requiresLeadingSpace: Bool = false, requiresTrailingSpace: Bool = false) {
30+
self.name = name
31+
self.isLexerClassified = isLexerClassified
32+
self.requiresLeadingSpace = requiresLeadingSpace
33+
self.requiresTrailingSpace = requiresTrailingSpace
34+
}
35+
}
36+
37+
public let KEYWORDS: [KeywordSpec] = [
38+
// Please keep these sorted alphabetically
39+
40+
KeywordSpec("__consuming"),
41+
KeywordSpec("__objc_bridged"),
42+
KeywordSpec("__owned"),
43+
KeywordSpec("__raw_doc_comment"),
44+
KeywordSpec("__setter_access"),
45+
KeywordSpec("__shared"),
46+
KeywordSpec("__synthesized_protocol"),
47+
KeywordSpec("_alignment"),
48+
KeywordSpec("_alwaysEmitConformanceMetadata"),
49+
KeywordSpec("_alwaysEmitIntoClient"),
50+
KeywordSpec("_assemblyVision"),
51+
KeywordSpec("_backDeploy"),
52+
KeywordSpec("_borrow"),
53+
KeywordSpec("_borrowed"),
54+
KeywordSpec("_cdecl"),
55+
KeywordSpec("_clangImporterSynthesizedType"),
56+
KeywordSpec("_Class"),
57+
KeywordSpec("_compilerInitialized"),
58+
KeywordSpec("_const"),
59+
KeywordSpec("_custom"),
60+
KeywordSpec("_disfavoredOverload"),
61+
KeywordSpec("_documentation"),
62+
KeywordSpec("_dynamicReplacement"),
63+
KeywordSpec("_eagerMove"),
64+
KeywordSpec("_effects"),
65+
KeywordSpec("_exported"),
66+
KeywordSpec("_expose"),
67+
KeywordSpec("_fixed_layout"),
68+
KeywordSpec("_forbidSerializingReference"),
69+
KeywordSpec("_forward"),
70+
KeywordSpec("_frozen"),
71+
KeywordSpec("_hasInitialValue"),
72+
KeywordSpec("_hasMissingDesignatedInitializers"),
73+
KeywordSpec("_hasStorage"),
74+
KeywordSpec("_implementationOnly"),
75+
KeywordSpec("_implements"),
76+
KeywordSpec("_implicitSelfCapture"),
77+
KeywordSpec("_inheritActorContext"),
78+
KeywordSpec("_inheritsConvenienceInitializers"),
79+
KeywordSpec("_linear"),
80+
KeywordSpec("_local"),
81+
KeywordSpec("_marker"),
82+
KeywordSpec("_modify"),
83+
KeywordSpec("_move"),
84+
KeywordSpec("_moveOnly"),
85+
KeywordSpec("_NativeClass"),
86+
KeywordSpec("_NativeRefCountedObject"),
87+
KeywordSpec("_noAllocation"),
88+
KeywordSpec("_noEagerMove"),
89+
KeywordSpec("_noImplicitCopy"),
90+
KeywordSpec("_noLocks"),
91+
KeywordSpec("_noMetadata"),
92+
KeywordSpec("_nonEphemeral"),
93+
KeywordSpec("_nonoverride"),
94+
KeywordSpec("_nonSendable"),
95+
KeywordSpec("_objc_non_lazy_realization"),
96+
KeywordSpec("_objcImplementation"),
97+
KeywordSpec("_objcRuntimeName"),
98+
KeywordSpec("_opaqueReturnTypeOf"),
99+
KeywordSpec("_optimize"),
100+
KeywordSpec("_originallyDefinedIn"),
101+
KeywordSpec("_PackageDescription"),
102+
KeywordSpec("_private"),
103+
KeywordSpec("_projectedValueProperty"),
104+
KeywordSpec("_read"),
105+
KeywordSpec("_RefCountedObject"),
106+
KeywordSpec("_restatedObjCConformance"),
107+
KeywordSpec("_semantics"),
108+
KeywordSpec("_show_in_interface"),
109+
KeywordSpec("_silgen_name"),
110+
KeywordSpec("_specialize"),
111+
KeywordSpec("_specializeExtension"),
112+
KeywordSpec("_spi"),
113+
KeywordSpec("_spi_available"),
114+
KeywordSpec("_spiOnly"),
115+
KeywordSpec("_staticInitializeObjCMetadata"),
116+
KeywordSpec("_swift_native_objc_runtime_base"),
117+
KeywordSpec("_transparent"),
118+
KeywordSpec("_Trivial"),
119+
KeywordSpec("_TrivialAtMost"),
120+
KeywordSpec("_typeEraser"),
121+
KeywordSpec("_unavailableFromAsync"),
122+
KeywordSpec("_UnknownLayout"),
123+
KeywordSpec("_unsafeInheritExecutor"),
124+
KeywordSpec("_weakLinked"),
125+
KeywordSpec("actor"),
126+
KeywordSpec("addressWithNativeOwner"),
127+
KeywordSpec("addressWithOwner"),
128+
KeywordSpec("any"),
129+
KeywordSpec("Any", isLexerClassified: true, requiresTrailingSpace: true),
130+
KeywordSpec("as", isLexerClassified: true, requiresTrailingSpace: true),
131+
KeywordSpec("assignment"),
132+
KeywordSpec("associatedtype", isLexerClassified: true, requiresTrailingSpace: true),
133+
KeywordSpec("associativity"),
134+
KeywordSpec("async", requiresTrailingSpace: true),
135+
KeywordSpec("autoclosure"),
136+
KeywordSpec("availability"),
137+
KeywordSpec("available"),
138+
KeywordSpec("await"),
139+
KeywordSpec("break", isLexerClassified: true, requiresTrailingSpace: true),
140+
KeywordSpec("case", isLexerClassified: true, requiresTrailingSpace: true),
141+
KeywordSpec("catch", isLexerClassified: true, requiresLeadingSpace: true),
142+
KeywordSpec("class", isLexerClassified: true, requiresTrailingSpace: true),
143+
KeywordSpec("continue", isLexerClassified: true, requiresTrailingSpace: true),
144+
KeywordSpec("convenience"),
145+
KeywordSpec("convention"),
146+
KeywordSpec("default", isLexerClassified: true),
147+
KeywordSpec("defer", isLexerClassified: true, requiresTrailingSpace: true),
148+
KeywordSpec("deinit", isLexerClassified: true, requiresTrailingSpace: true),
149+
KeywordSpec("deprecated"),
150+
KeywordSpec("derivative"),
151+
KeywordSpec("didSet"),
152+
KeywordSpec("differentiable"),
153+
KeywordSpec("discardableResult"),
154+
KeywordSpec("distributed"),
155+
KeywordSpec("do", isLexerClassified: true),
156+
KeywordSpec("dynamic"),
157+
KeywordSpec("dynamicCallable"),
158+
KeywordSpec("dynamicMemberLookup"),
159+
KeywordSpec("each"),
160+
KeywordSpec("else", isLexerClassified: true, requiresTrailingSpace: true),
161+
KeywordSpec("enum", isLexerClassified: true, requiresTrailingSpace: true),
162+
KeywordSpec("escaping"),
163+
KeywordSpec("exclusivity"),
164+
KeywordSpec("exported"),
165+
KeywordSpec("extension", isLexerClassified: true, requiresTrailingSpace: true),
166+
KeywordSpec("fallthrough", isLexerClassified: true, requiresTrailingSpace: true),
167+
KeywordSpec("false", isLexerClassified: true),
168+
KeywordSpec("fileprivate", isLexerClassified: true, requiresTrailingSpace: true),
169+
KeywordSpec("final"),
170+
KeywordSpec("for", isLexerClassified: true, requiresTrailingSpace: true),
171+
KeywordSpec("frozen"),
172+
KeywordSpec("func", isLexerClassified: true, requiresTrailingSpace: true),
173+
KeywordSpec("get"),
174+
KeywordSpec("GKInspectable"),
175+
KeywordSpec("globalActor"),
176+
KeywordSpec("guard", isLexerClassified: true, requiresTrailingSpace: true),
177+
KeywordSpec("higherThan"),
178+
KeywordSpec("IBAction"),
179+
KeywordSpec("IBDesignable"),
180+
KeywordSpec("IBInspectable"),
181+
KeywordSpec("IBOutlet"),
182+
KeywordSpec("IBSegueAction"),
183+
KeywordSpec("if", isLexerClassified: true, requiresTrailingSpace: true),
184+
KeywordSpec("import", isLexerClassified: true, requiresTrailingSpace: true),
185+
KeywordSpec("in", isLexerClassified: true, requiresLeadingSpace: true, requiresTrailingSpace: true),
186+
KeywordSpec("indirect"),
187+
KeywordSpec("infix"),
188+
KeywordSpec("init", isLexerClassified: true, requiresTrailingSpace: true),
189+
KeywordSpec("inlinable"),
190+
KeywordSpec("inline"),
191+
KeywordSpec("inout", isLexerClassified: true, requiresTrailingSpace: true),
192+
KeywordSpec("internal", isLexerClassified: true, requiresTrailingSpace: true),
193+
KeywordSpec("introduced"),
194+
KeywordSpec("is", isLexerClassified: true, requiresTrailingSpace: true),
195+
KeywordSpec("isolated"),
196+
KeywordSpec("kind"),
197+
KeywordSpec("lazy"),
198+
KeywordSpec("let", isLexerClassified: true, requiresTrailingSpace: true),
199+
KeywordSpec("LLDBDebuggerFunction"),
200+
KeywordSpec("lowerThan"),
201+
KeywordSpec("macro"),
202+
KeywordSpec("main"),
203+
KeywordSpec("message"),
204+
KeywordSpec("mutableAddressWithNativeOwner"),
205+
KeywordSpec("mutableAddressWithOwner"),
206+
KeywordSpec("mutating"),
207+
KeywordSpec("nil", isLexerClassified: true),
208+
KeywordSpec("noasync"),
209+
KeywordSpec("noDerivative"),
210+
KeywordSpec("noescape"),
211+
KeywordSpec("nonisolated"),
212+
KeywordSpec("nonmutating"),
213+
KeywordSpec("nonobjc"),
214+
KeywordSpec("NSApplicationMain"),
215+
KeywordSpec("NSCopying"),
216+
KeywordSpec("NSManaged"),
217+
KeywordSpec("objc"),
218+
KeywordSpec("objcMembers"),
219+
KeywordSpec("obsoleted"),
220+
KeywordSpec("of"),
221+
KeywordSpec("open"),
222+
KeywordSpec("operator", isLexerClassified: true, requiresTrailingSpace: true),
223+
KeywordSpec("optional"),
224+
KeywordSpec("override"),
225+
KeywordSpec("package"),
226+
KeywordSpec("postfix"),
227+
KeywordSpec("precedencegroup", isLexerClassified: true, requiresTrailingSpace: true),
228+
KeywordSpec("preconcurrency"),
229+
KeywordSpec("prefix"),
230+
KeywordSpec("private", isLexerClassified: true, requiresTrailingSpace: true),
231+
KeywordSpec("propertyWrapper"),
232+
KeywordSpec("Protocol"),
233+
KeywordSpec("protocol", isLexerClassified: true, requiresTrailingSpace: true),
234+
KeywordSpec("public", isLexerClassified: true, requiresTrailingSpace: true),
235+
KeywordSpec("reasync"),
236+
KeywordSpec("renamed"),
237+
KeywordSpec("repeat", isLexerClassified: true, requiresTrailingSpace: true),
238+
KeywordSpec("required"),
239+
KeywordSpec("requires_stored_property_inits"),
240+
KeywordSpec("resultBuilder"),
241+
KeywordSpec("rethrows", isLexerClassified: true, requiresTrailingSpace: true),
242+
KeywordSpec("return", isLexerClassified: true, requiresTrailingSpace: true),
243+
KeywordSpec("reverse"),
244+
KeywordSpec("runtimeMetadata"),
245+
KeywordSpec("safe"),
246+
KeywordSpec("self", isLexerClassified: true),
247+
KeywordSpec("Self", isLexerClassified: true),
248+
KeywordSpec("Sendable"),
249+
KeywordSpec("set"),
250+
KeywordSpec("some"),
251+
KeywordSpec("spi"),
252+
KeywordSpec("spiModule"),
253+
KeywordSpec("static", isLexerClassified: true, requiresTrailingSpace: true),
254+
KeywordSpec("struct", isLexerClassified: true, requiresTrailingSpace: true),
255+
KeywordSpec("subscript", isLexerClassified: true, requiresTrailingSpace: true),
256+
KeywordSpec("super", isLexerClassified: true),
257+
KeywordSpec("swift"),
258+
KeywordSpec("switch", isLexerClassified: true, requiresTrailingSpace: true),
259+
KeywordSpec("target"),
260+
KeywordSpec("testable"),
261+
KeywordSpec("throw", isLexerClassified: true, requiresTrailingSpace: true),
262+
KeywordSpec("throws", isLexerClassified: true, requiresTrailingSpace: true),
263+
KeywordSpec("transpose"),
264+
KeywordSpec("true", isLexerClassified: true),
265+
KeywordSpec("try", isLexerClassified: true, requiresTrailingSpace: true),
266+
KeywordSpec("Type"),
267+
KeywordSpec("typealias", isLexerClassified: true, requiresTrailingSpace: true),
268+
KeywordSpec("typeWrapper"),
269+
KeywordSpec("typeWrapperIgnored"),
270+
KeywordSpec("UIApplicationMain"),
271+
KeywordSpec("unavailable"),
272+
KeywordSpec("unchecked"),
273+
KeywordSpec("unowned"),
274+
KeywordSpec("unsafe"),
275+
KeywordSpec("unsafe_no_objc_tagged_pointer"),
276+
KeywordSpec("unsafeAddress"),
277+
KeywordSpec("unsafeMutableAddress"),
278+
KeywordSpec("usableFromInline"),
279+
KeywordSpec("var", isLexerClassified: true, requiresTrailingSpace: true),
280+
KeywordSpec("warn_unqualified_access"),
281+
KeywordSpec("weak"),
282+
KeywordSpec("where", isLexerClassified: true, requiresLeadingSpace: true, requiresTrailingSpace: true),
283+
KeywordSpec("while", isLexerClassified: true, requiresTrailingSpace: true),
284+
KeywordSpec("willSet"),
285+
KeywordSpec("witness_method"),
286+
KeywordSpec("wrt"),
287+
KeywordSpec("yield"),
288+
]
289+
290+
public func keywordsByLength() -> [(Int, [KeywordSpec])] {
291+
var result: [Int: [KeywordSpec]] = [:]
292+
for keyword in KEYWORDS {
293+
result[keyword.name.count, default: []].append(keyword)
294+
}
295+
return result.sorted(by: { $0.key < $1.key })
296+
}

CodeGeneration/Sources/SyntaxSupport/TokenSpec.swift.gyb

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,41 +70,6 @@ public class TokenSpec {
7070
}
7171
}
7272

73-
/// Represents a keyword token.
74-
public class KeywordSpec: TokenSpec {
75-
init(
76-
name: String,
77-
text: String,
78-
classification: String = "Keyword",
79-
requiresLeadingSpace: Bool = false,
80-
requiresTrailingSpace: Bool = false
81-
) {
82-
super.init(
83-
name: name,
84-
kind: "kw_\(text)",
85-
nameForDiagnostics: text,
86-
unprefixedKind: text,
87-
text: text,
88-
classification: classification,
89-
isKeyword: true,
90-
requiresLeadingSpace: requiresLeadingSpace,
91-
requiresTrailingSpace: requiresTrailingSpace
92-
)
93-
}
94-
}
95-
96-
public class SwiftKeywordSpec: KeywordSpec { }
97-
98-
public class DeclKeywordSpec: SwiftKeywordSpec { }
99-
100-
public class StmtKeywordSpec: SwiftKeywordSpec { }
101-
102-
public class ExprKeywordSpec: SwiftKeywordSpec { }
103-
104-
public class PatternKeywordSpec: SwiftKeywordSpec { }
105-
106-
public class SilKeywordSpec: KeywordSpec { }
107-
10873
public class PoundKeywordSpec: TokenSpec {
10974
init(
11075
name: String,

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeKinds.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,9 +1435,9 @@ public let DECL_ATTR_KINDS: [Attribute] = [
14351435
/// '@' symbol, add an entry to the `DECL_ATTR_KINDS` array instead.
14361436
public let DECL_MODIFIER_KINDS: [Attribute] = [
14371437
BuiltinDeclModifier(name: "static",
1438-
swiftName: "staticKeyword"),
1438+
swiftName: "`static`"),
14391439
BuiltinDeclModifier(name: "class",
1440-
swiftName: "classKeyword"),
1440+
swiftName: "`class`"),
14411441
ContextualSimpleDeclAttribute(name: "final",
14421442
className: "Final",
14431443
options:
@@ -1615,16 +1615,16 @@ public let DECL_MODIFIER_KINDS: [Attribute] = [
16151615
"APIStableToAdd",
16161616
"APIStableToRemove",
16171617
code: 46,
1618-
swiftName: "privateKeyword"),
1618+
swiftName: "`private`"),
16191619
DeclAttributeAlias(name: "fileprivate",
16201620
className: "AccessControl",
1621-
swiftName: "fileprivateKeyword"),
1621+
swiftName: "`fileprivate`"),
16221622
DeclAttributeAlias(name: "internal",
16231623
className: "AccessControl",
1624-
swiftName: "internalKeyword"),
1624+
swiftName: "`internal`"),
16251625
DeclAttributeAlias(name: "public",
16261626
className: "AccessControl",
1627-
swiftName: "publicKeyword"),
1627+
swiftName: "`public`"),
16281628
ContextualDeclAttributeAlias(name: "package",
16291629
className: "AccessControl",
16301630
swiftName: "package"),

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,9 @@ public let ATTRIBUTE_NODES: [Node] = [
430430
Child(name: "Parameter",
431431
kind: "Token",
432432
tokenChoices: [
433-
"Self",
434433
"Identifier",
435-
"IntegerLiteral"
434+
"IntegerLiteral",
435+
"Keyword"
436436
]),
437437
Child(name: "TrailingComma",
438438
kind: "CommaToken",

0 commit comments

Comments
 (0)