|
| 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 | +} |
0 commit comments