Skip to content

Add a string-based initializer for ExperimentalFeatures. #2895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions CodeGeneration/Sources/SyntaxSupport/ExperimentalFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,28 @@ public enum ExperimentalFeature: String, CaseIterable {
case coroutineAccessors
case valueGenerics

/// The name of the feature, which is used in the doc comment.
/// The name of the feature as it is written in the compiler's `Features.def` file.
public var featureName: String {
switch self {
case .referenceBindings:
return "ReferenceBindings"
case .thenStatements:
return "ThenStatements"
case .doExpressions:
return "DoExpressions"
case .nonescapableTypes:
return "NonescapableTypes"
case .trailingComma:
return "TrailingComma"
case .coroutineAccessors:
return "CoroutineAccessors"
case .valueGenerics:
return "ValueGenerics"
}
}

/// A brief description of the feature that is used in the doc comment.
public var documentationDescription: String {
switch self {
case .referenceBindings:
return "reference bindings"
Expand All @@ -31,13 +51,13 @@ public enum ExperimentalFeature: String, CaseIterable {
case .doExpressions:
return "'do' expressions"
case .nonescapableTypes:
return "NonEscableTypes"
return "non-escapable types"
case .trailingComma:
return "trailing comma"
return "trailing commas"
case .coroutineAccessors:
return "CoroutineAccessors"
return "coroutine accessors"
case .valueGenerics:
return "ValueGenerics"
return "value generics"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,31 @@ let experimentalFeaturesFile = SourceFileSyntax(leadingTrivia: copyrightHeader)
for (i, feature) in ExperimentalFeature.allCases.enumerated() {
DeclSyntax(
"""
/// Whether to enable the parsing of \(raw: feature.featureName).
/// Whether to enable the parsing of \(raw: feature.documentationDescription).
public static let \(feature.token) = Self(rawValue: 1 << \(raw: i))
"""
)
}

try! InitializerDeclSyntax(
"""
/// Creates a new value representing the experimental feature with the
/// given name, or returns nil if the name is not recognized.
public init?(name: String)
"""
) {
try! SwitchExprSyntax("switch name") {
SwitchCaseListSyntax {
for feature in ExperimentalFeature.allCases {
SwitchCaseSyntax(#"case "\#(raw: feature.featureName)":"#) {
ExprSyntax("self = .\(feature.token)")
}
}
SwitchCaseSyntax("default:") {
StmtSyntax("return nil")
}
}
}
}
}
}
4 changes: 4 additions & 0 deletions Release Notes/601.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
- Description: This method indents a node’s contents using a provided piece of `Trivia`, optionally including the first line.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2843

- `Parser.ExperimentalFeatures` has a new `init?(name: String)` initializer.
- Description: This initializer returns the `Parser.ExperimentalFeatures` value that matches the experimental parser feature with the given name. The name must be spelled the same way as it is when passed to the compiler using the `-enable-experimental-feature` flag.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2895

## API Behavior Changes

- `SyntaxProtocol.trimmed` detaches the node
Expand Down
31 changes: 27 additions & 4 deletions Sources/SwiftParser/generated/ExperimentalFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,38 @@ extension Parser.ExperimentalFeatures {
/// Whether to enable the parsing of 'do' expressions.
public static let doExpressions = Self (rawValue: 1 << 2)

/// Whether to enable the parsing of NonEscableTypes.
/// Whether to enable the parsing of non-escapable types.
public static let nonescapableTypes = Self (rawValue: 1 << 3)

/// Whether to enable the parsing of trailing comma.
/// Whether to enable the parsing of trailing commas.
public static let trailingComma = Self (rawValue: 1 << 4)

/// Whether to enable the parsing of CoroutineAccessors.
/// Whether to enable the parsing of coroutine accessors.
public static let coroutineAccessors = Self (rawValue: 1 << 5)

/// Whether to enable the parsing of ValueGenerics.
/// Whether to enable the parsing of value generics.
public static let valueGenerics = Self (rawValue: 1 << 6)

/// Creates a new value representing the experimental feature with the
/// given name, or returns nil if the name is not recognized.
public init?(name: String) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a release note entry for this new method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

switch name {
case "ReferenceBindings":
self = .referenceBindings
case "ThenStatements":
self = .thenStatements
case "DoExpressions":
self = .doExpressions
case "NonescapableTypes":
self = .nonescapableTypes
case "TrailingComma":
self = .trailingComma
case "CoroutineAccessors":
self = .coroutineAccessors
case "ValueGenerics":
self = .valueGenerics
default:
return nil
}
}
}