Skip to content

Commit 355d2ce

Browse files
committed
Add a string-based initializer for ExperimentalFeatures.
Tools (like swift-format) can use this to let users enable experimental features in the parser via interfaces like the command line, without having to handle the translation from string to option set themselves.
1 parent 38459d4 commit 355d2ce

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/ExperimentalFeaturesFile.swift

+21
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,26 @@ let experimentalFeaturesFile = SourceFileSyntax(leadingTrivia: copyrightHeader)
3939
"""
4040
)
4141
}
42+
43+
try! InitializerDeclSyntax(
44+
"""
45+
/// Creates a new value representing the experimental feature with the
46+
/// given name, or returns nil if the name is not recognized.
47+
public init?(name: String)
48+
"""
49+
) {
50+
try! SwitchExprSyntax("switch name") {
51+
SwitchCaseListSyntax {
52+
for feature in ExperimentalFeature.allCases {
53+
SwitchCaseSyntax(#"case "\#(raw: feature.rawValue)":"#) {
54+
ExprSyntax("self = .\(feature.token)")
55+
}
56+
}
57+
SwitchCaseSyntax("default:") {
58+
StmtSyntax("return nil")
59+
}
60+
}
61+
}
62+
}
4263
}
4364
}

Sources/SwiftParser/generated/ExperimentalFeatures.swift

+23
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,27 @@ extension Parser.ExperimentalFeatures {
4444

4545
/// Whether to enable the parsing of ValueGenerics.
4646
public static let valueGenerics = Self (rawValue: 1 << 6)
47+
48+
/// Creates a new value representing the experimental feature with the
49+
/// given name, or returns nil if the name is not recognized.
50+
public init?(name: String) {
51+
switch name {
52+
case "referenceBindings":
53+
self = .referenceBindings
54+
case "thenStatements":
55+
self = .thenStatements
56+
case "doExpressions":
57+
self = .doExpressions
58+
case "nonescapableTypes":
59+
self = .nonescapableTypes
60+
case "trailingComma":
61+
self = .trailingComma
62+
case "coroutineAccessors":
63+
self = .coroutineAccessors
64+
case "valueGenerics":
65+
self = .valueGenerics
66+
default:
67+
return nil
68+
}
69+
}
4770
}

0 commit comments

Comments
 (0)