Skip to content
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
60 changes: 37 additions & 23 deletions Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ extension Parser {
var keepGoing: RawTokenSyntax? = nil
var loopProgress = LoopProgressCondition()
repeat {
let (pattern, whereClause) = self.parseGuardedPattern(.catch)
let (pattern, whereClause) = self.parseGuardedCatchPattern()
keepGoing = self.consume(if: .comma)
catchItems.append(RawCatchItemSyntax(
pattern: pattern, whereClause: whereClause, trailingComma: keepGoing,
Expand All @@ -505,6 +505,38 @@ extension Parser {
body: body,
arena: self.arena)
}

/// Parse a pattern-matching clause for a catch statement,
/// including the guard expression.
///
/// Grammar
/// =======
///
/// catch-pattern → pattern where-clause?
private mutating func parseGuardedCatchPattern() -> (RawPatternSyntax?, RawWhereClauseSyntax?) {
// If this is a 'catch' clause and we have "catch {" or "catch where...",
// then we get an implicit "let error" pattern.
let pattern: RawPatternSyntax?
if self.at(any: [ .leftBrace, .whereKeyword ]) {
pattern = nil
} else {
pattern = self.parseMatchingPattern(context: .matching)
}

// Parse the optional 'where' guard.
let whereClause: RawWhereClauseSyntax?
if let whereKeyword = self.consume(if: .whereKeyword) {
let guardExpr = self.parseExpression(.basic)
whereClause = RawWhereClauseSyntax(
whereKeyword: whereKeyword,
guardResult: guardExpr,
arena: self.arena
)
} else {
whereClause = nil
}
return (pattern, whereClause)
}
}

// MARK: Iteration Statements
Expand Down Expand Up @@ -821,7 +853,7 @@ extension Parser {
var keepGoing: RawTokenSyntax? = nil
var loopProgress = LoopProgressCondition()
repeat {
let (pattern, whereClause) = self.parseGuardedPattern(.case)
let (pattern, whereClause) = self.parseGuardedCasePattern()
keepGoing = self.consume(if: .comma)
caseItems.append(RawCaseItemSyntax(
pattern: pattern, whereClause: whereClause, trailingComma: keepGoing,
Expand Down Expand Up @@ -859,39 +891,21 @@ extension Parser {
)
}

enum GuardedPatternContext {
case `case`
case `catch`
}

/// Parse a pattern-matching clause for a case or catch statement,
/// Parse a pattern-matching clause for a case statement,
/// including the guard expression.
///
/// Grammar
/// =======
///
/// case-item → pattern where-clause?
/// catch-pattern → pattern where-clause?
mutating func parseGuardedPattern(
_ context: GuardedPatternContext
) -> (RawPatternSyntax, RawWhereClauseSyntax?) {
let flavor: ExprFlavor
switch context {
// 'case' is terminated with a colon and so allows a trailing closure.
case .`case`:
flavor = .trailingClosure
// 'catch' is terminated with a brace and so cannot.
case .`catch`:
flavor = .basic
}

mutating func parseGuardedCasePattern() -> (RawPatternSyntax, RawWhereClauseSyntax?) {
let pattern = self.parseMatchingPattern(context: .matching)

// Parse the optional 'where' guard, with this particular pattern's bound
// vars in scope.
let whereClause: RawWhereClauseSyntax?
if let whereKeyword = self.consume(if: .whereKeyword) {
let guardExpr = self.parseExpression(flavor)
let guardExpr = self.parseExpression(.trailingClosure)
whereClause = RawWhereClauseSyntax(
whereKeyword: whereKeyword,
guardResult: guardExpr,
Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftParserTest/StatementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ final class StatementTests: XCTestCase {
}
"""
)

AssertParse(
"""
do {}
catch where (error as NSError) == NSError() {}
""")
}

func testReturn() {
Expand Down
1 change: 0 additions & 1 deletion Tests/SwiftParserTest/translated/ErrorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ final class ErrorsTests: XCTestCase {
diagnostics: [
// TODO: Old parser expected error on line 3: expected expression after '? ... :' in ternary expression
DiagnosticSpec(locationMarker: "1️⃣", message: "expected expression in 'do' statement"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected expression in pattern"),
]
)
}
Expand Down