Skip to content

Commit fedd548

Browse files
committed
Parse SwitchExprSyntax
Update the parser to support parsing SwitchExprSyntax. For now, this only updates the existing statement parsing, and does not attempt to parse in expression position.
1 parent bc66595 commit fedd548

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,18 +2146,20 @@ extension Parser {
21462146
}
21472147
}
21482148

2149-
// MARK: Switch Statements
2149+
// MARK: Switch Statements/Expressions
21502150

21512151
extension Parser {
2152-
/// Parse a switch statement.
2152+
/// Parse a switch statement/expression.
21532153
///
21542154
/// Grammar
21552155
/// =======
21562156
///
2157-
/// switch-statement → 'switch' expression '{' switch-cases? '}'
2157+
/// switch-expression → 'switch' expression '{' switch-cases? '}'
21582158
/// switch-cases → switch-case switch-cases?
21592159
@_spi(RawSyntax)
2160-
public mutating func parseSwitchStatement(switchHandle: RecoveryConsumptionHandle) -> RawSwitchStmtSyntax {
2160+
public mutating func parseSwitchExpression(
2161+
switchHandle: RecoveryConsumptionHandle
2162+
) -> RawSwitchExprSyntax {
21612163
let (unexpectedBeforeSwitchKeyword, switchKeyword) = self.eat(switchHandle)
21622164

21632165
let subject = self.parseExpression(.basic)
@@ -2166,7 +2168,7 @@ extension Parser {
21662168
let cases = self.parseSwitchCases(allowStandaloneStmtRecovery: !lbrace.isMissing)
21672169

21682170
let (unexpectedBeforeRBrace, rbrace) = self.expectRightBrace(leftBrace: lbrace, introducer: switchKeyword)
2169-
return RawSwitchStmtSyntax(
2171+
return RawSwitchExprSyntax(
21702172
unexpectedBeforeSwitchKeyword,
21712173
switchKeyword: switchKeyword,
21722174
expression: subject,

Sources/SwiftParser/Statements.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ extension Parser {
111111
case (.guardKeyword, let handle)?:
112112
return label(self.parseGuardStatement(guardHandle: handle), with: optLabel)
113113
case (.switchKeyword, let handle)?:
114-
return label(self.parseSwitchStatement(switchHandle: handle), with: optLabel)
114+
let switchExpr = self.parseSwitchExpression(switchHandle: handle)
115+
let switchStmt = RawExpressionStmtSyntax(
116+
expression: RawExprSyntax(switchExpr),
117+
arena: self.arena
118+
)
119+
return label(switchStmt, with: optLabel)
115120
case (.breakKeyword, let handle)?:
116121
return label(self.parseBreakStatement(breakHandle: handle), with: optLabel)
117122
case (.continueKeyword, let handle)?:

Sources/SwiftSyntaxBuilder/SyntaxNodeWithBody.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,17 @@ extension SwitchCaseSyntax {
153153
}
154154
}
155155

156-
// MARK: - SwitchStmtSyntax
157-
// SwitchStmtSyntax is a special scenario as it don't have body or members
156+
// MARK: - SwitchExprSyntax
157+
// SwitchExprSyntax is a special scenario as it don't have body or members
158158
// So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
159159

160-
public extension SwitchStmtSyntax {
160+
public extension SwitchExprSyntax {
161161
init(_ header: PartialSyntaxNodeString, @SwitchCaseListBuilder casesBuilder: () throws -> SwitchCaseListSyntax = { SwitchCaseListSyntax([]) }) throws {
162-
let stmt = StmtSyntax("\(header) {}")
163-
guard let castedStmt = stmt.as(Self.self) else {
164-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: stmt)
162+
let expr = ExprSyntax("\(header) {}")
163+
guard let switchExpr = expr.as(Self.self) else {
164+
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: expr)
165165
}
166-
self = castedStmt
166+
self = switchExpr
167167
self.cases = try casesBuilder()
168168
}
169169
}

Tests/SwiftSyntaxBuilderTest/SwitchTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntaxBuilder
1616

1717
final class SwitchTests: XCTestCase {
1818
func testSwitch() {
19-
let syntax = SwitchStmtSyntax(expression: ExprSyntax("count")) {
19+
let syntax = SwitchExprSyntax(expression: ExprSyntax("count")) {
2020
for num in 1..<3 {
2121
SwitchCaseSyntax("case \(literal: num):") {
2222
ExprSyntax("print(count)")
@@ -43,7 +43,7 @@ final class SwitchTests: XCTestCase {
4343
}
4444

4545
func testSwitchStmtSyntaxWithStringParsing() throws {
46-
let syntax = try SwitchStmtSyntax("switch count") {
46+
let syntax = try SwitchExprSyntax("switch count") {
4747
for num in 1..<3 {
4848
SwitchCaseSyntax("case \(literal: num):") {
4949
ExprSyntax("print(count)")

0 commit comments

Comments
 (0)