Skip to content

Commit a1ecf4a

Browse files
authored
Merge pull request #2351 from rintaro/syntaxenum-per-base
Add SyntaxEnum per base syntax kind
2 parents 23d207d + 10d2d45 commit a1ecf4a

File tree

4 files changed

+488
-0
lines changed

4 files changed

+488
-0
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxEnumFile.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,50 @@ let syntaxEnumFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
5757
}
5858
}
5959
}
60+
61+
for base in SYNTAX_NODES where base.kind.isBase {
62+
let baseKind = base.kind
63+
let baseName = baseKind.rawValue.withFirstCharacterUppercased;
64+
let enumType: TypeSyntax = "\(raw: baseName)SyntaxEnum"
65+
66+
try! EnumDeclSyntax(
67+
"""
68+
/// Enum to exhaustively switch over all different \(raw: baseName) syntax nodes.
69+
public enum \(enumType)
70+
"""
71+
) {
72+
for node in NON_BASE_SYNTAX_NODES where node.base == baseKind {
73+
DeclSyntax(
74+
"""
75+
\(node.apiAttributes())\
76+
case \(node.varOrCaseName)(\(node.kind.syntaxType))
77+
"""
78+
)
79+
}
80+
}
81+
82+
try! ExtensionDeclSyntax(
83+
"""
84+
public extension \(baseKind.syntaxType)
85+
"""
86+
) {
87+
try FunctionDeclSyntax(
88+
"""
89+
/// Get an enum that can be used to exhaustively switch over all \(raw: baseName) syntax nodes.
90+
func `as`(_: \(enumType).Type) -> \(enumType)
91+
"""
92+
) {
93+
try SwitchExprSyntax("switch raw.kind") {
94+
for node in NON_BASE_SYNTAX_NODES where node.base == baseKind {
95+
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
96+
StmtSyntax("return .\(node.varOrCaseName)(\(node.kind.syntaxType)(self)!)")
97+
}
98+
}
99+
SwitchCaseSyntax("default:") {
100+
ExprSyntax(#"preconditionFailure("unknown \#(raw: baseName) syntax kind")"#)
101+
}
102+
}
103+
}
104+
}
105+
}
60106
}

Release Notes/511.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
- Issue: https://github.com/apple/swift-syntax/issues/2015
1717
- Pull Request: https://github.com/apple/swift-syntax/pull/2021
1818

19+
- `DeclSyntaxEnum`, `StmtSyntaxEnum`, `ExprSyntaxEnum`, `TypeSyntaxEnum`, and `PatternSyntaxEnum`
20+
- Description: Enum to exhaustively switch over all different syntax nodes of each base type.
21+
- Pull Request: https://github.com/apple/swift-syntax/pull/2351
22+
1923
## API Behavior Changes
2024

2125
## Deprecations

0 commit comments

Comments
 (0)