swiftlang-keywords is a lightweight library that provides precomputed Swift keyword sets for specific Swift language versions. Initially designed for package-resources-cli for code generation with swiftlang-snippets.
Import SwiftKeywords to use the default keyword set selected by package traits:
import SwiftKeywords
"actor".isSwiftKeyword // true
"actor".isReservedSwiftKeyword // true
"userName".isSwiftKeyword // false
"userName".isReservedSwiftKeyword // false
"Any".isSwiftKeyword // true
"Any".isReservedSwiftKeyword // false
Set.swiftKeywords.contains("protocol") // true
Set.reservedSwiftKeywords.contains("protocol") // trueThe SwiftKeywords product resolves its default version from the enabled trait. Latest is enabled by default and currently points to Swift 6.3.
You can temporarily use another keyword set with withKeywords:
import SwiftKeywords
import KeywordsSwift6_0
let result = withKeywords(.v6_0) {
"borrow".isSwiftKeyword
}You can also prepare a process-wide default before calling the convenience APIs:
import SwiftKeywords
import KeywordsSwift6_2
// Must be called before accessing
// primary APIs
prepareKeywords(with: .v6_2)If you need direct access to a generated container, depend on one of the version products:
import KeywordsSwift6_3
let keywords = KeywordsContainer.v6_3
keywords.all // all
keywords.lexerClassified // reserved
keywords.experimental // always empty yetYou can add swiftlang-keywords to an Xcode project by adding it as a package dependency.
- From the File menu, select Swift Packages › Add Package Dependency…
- Enter
"https://github.com/capturecontext/swiftlang-keywords"into the package repository URL text field - Choose products you need to link to your project.
If you use SwiftPM for your project structure, add swiftlang-keywords dependency to your package file
.package(
url: "https://github.com/capturecontext/swiftlang-keywords.git",
.upToNextMinor(from: "0.0.1"),
traits: ["Latest"]
)Available traits:
Latest(default, enablesSwift6_3)Swift6_3Swift6_2Swift6_1Swift6_0
Add the product to your target dependencies:
.product(
name: "SwiftKeywords",
package: "swiftlang-keywords"
)Generated version products are also available when you want an explicit keyword container:
.product(
name: "KeywordsSwift6_3",
package: "swiftlang-keywords"
)Keyword source files are generated from swift-syntax by the local generator package.
Run the full generation sweep from the package root:
scripts/generate-keywords.shThe script builds and runs the generator for:
600.0.0->Sources/SwiftVersions/Swift6_0/Keywords.swift601.0.0->Sources/SwiftVersions/Swift6_1/Keywords.swift602.0.0->Sources/SwiftVersions/Swift6_2/Keywords.swift603.0.0->Sources/SwiftVersions/Swift6_3/Keywords.swift
To generate a single version manually:
GENERATE_SWIFT_SYNTAX_VERSION=603.0.0 \
GENERATE_SWIFT_KEYWORDS_PACKAGE_ROOT="$PWD" \
swift run --package-path generator keywords-generator-cliThis library is released under the MIT license. See LICENSE for details.