Skip to content

Commit 9edcf21

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 c8f2371 commit 9edcf21

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,18 +2571,20 @@ extension Parser {
25712571
}
25722572
}
25732573

2574-
// MARK: Switch Statements
2574+
// MARK: Switch Statements/Expressions
25752575

25762576
extension Parser {
2577-
/// Parse a switch statement.
2577+
/// Parse a switch statement/expression.
25782578
///
25792579
/// Grammar
25802580
/// =======
25812581
///
2582-
/// switch-statement → 'switch' expression '{' switch-cases? '}'
2582+
/// switch-expression → 'switch' expression '{' switch-cases? '}'
25832583
/// switch-cases → switch-case switch-cases?
25842584
@_spi(RawSyntax)
2585-
public mutating func parseSwitchStatement(switchHandle: RecoveryConsumptionHandle) -> RawSwitchStmtSyntax {
2585+
public mutating func parseSwitchExpression(
2586+
switchHandle: RecoveryConsumptionHandle
2587+
) -> RawSwitchExprSyntax {
25862588
let (unexpectedBeforeSwitchKeyword, switchKeyword) = self.eat(switchHandle)
25872589

25882590
let subject = self.parseExpression(.basic)
@@ -2591,7 +2593,7 @@ extension Parser {
25912593
let cases = self.parseSwitchCases(allowStandaloneStmtRecovery: !lbrace.isMissing)
25922594

25932595
let (unexpectedBeforeRBrace, rbrace) = self.expectRightBrace(leftBrace: lbrace, introducer: switchKeyword)
2594-
return RawSwitchStmtSyntax(
2596+
return RawSwitchExprSyntax(
25952597
unexpectedBeforeSwitchKeyword,
25962598
switchKeyword: switchKeyword,
25972599
expression: subject,

Sources/SwiftParser/Statements.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ extension Parser {
106106
case (.guardKeyword, let handle)?:
107107
return label(self.parseGuardStatement(guardHandle: handle), with: optLabel)
108108
case (.switchKeyword, let handle)?:
109-
return label(self.parseSwitchStatement(switchHandle: handle), with: optLabel)
109+
let switchExpr = self.parseSwitchExpression(switchHandle: handle)
110+
let switchStmt = RawExpressionStmtSyntax(
111+
expression: RawExprSyntax(switchExpr),
112+
arena: self.arena
113+
)
114+
return label(switchStmt, with: optLabel)
110115
case (.breakKeyword, let handle)?:
111116
return label(self.parseBreakStatement(breakHandle: handle), with: optLabel)
112117
case (.continueKeyword, let handle)?:

Tests/SwiftSyntaxBuilderTest/SwitchTests.swift

Lines changed: 1 addition & 1 deletion
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 = SwitchStmt(expression: Expr("count")) {
19+
let syntax = SwitchExpr(expression: Expr("count")) {
2020
for num in 1..<3 {
2121
SwitchCase("case \(literal: num):") {
2222
Expr("print(count)")

0 commit comments

Comments
 (0)