Skip to content

Commit ebd8e2e

Browse files
committed
Clean up parsing of effect specifiers a bit
Teach `parseThrowsClause(after:)` to check for the `(` itself, so we have just the one place where we deal with the presence or absence of the thrown error type specification.
1 parent b1767ac commit ebd8e2e

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

Sources/SwiftParser/Specifiers.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,18 @@ extension TokenConsumer {
582582
// MARK: - Parsing effect specifiers
583583

584584
extension Parser {
585+
/// Parse a throws clause after we've already parsed the 'throws' keyword to introduce it.
585586
private mutating func parseThrowsClause(after throwsKeyword: RawTokenSyntax) -> RawThrowsClauseSyntax {
587+
guard self.at(.leftParen) && experimentalFeatures.contains(.typedThrows) else {
588+
return RawThrowsClauseSyntax(
589+
throwsSpecifier: throwsKeyword,
590+
leftParen: nil,
591+
type: nil,
592+
rightParen: nil,
593+
arena: self.arena
594+
)
595+
}
596+
586597
let (unexpectedBetweenThrowsSpecifierAndLeftParen, leftParen) = self.expect(.leftParen)
587598
let type = self.parseType()
588599
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
@@ -601,7 +612,6 @@ extension Parser {
601612
var unexpectedBeforeAsync: [RawSyntax] = []
602613
var asyncKeyword: RawTokenSyntax? = nil
603614
var unexpectedBeforeThrows: [RawSyntax] = []
604-
var throwsKeyword: RawTokenSyntax?
605615
var throwsClause: RawThrowsClauseSyntax?
606616
var unexpectedAfterThrowsClause: [RawSyntax] = []
607617

@@ -625,10 +635,10 @@ extension Parser {
625635
unexpectedBeforeThrows.append(RawSyntax(misspelledAsync))
626636
} else if let misspelledThrows = self.consume(ifAnyIn: S.MisspelledThrowsTokenKinds.self) {
627637
unexpectedBeforeThrows.append(RawSyntax(misspelledThrows))
628-
if throwsKeyword == nil {
638+
if throwsClause == nil {
629639
// Let's synthesize a missing 'throws'. If we find a real throws specifier
630640
// later, we will replace the missing token by the present token.
631-
throwsKeyword = missingToken(.keyword(.throws))
641+
throwsClause = RawThrowsClauseSyntax(throwsSpecifier: missingToken(.keyword(.throws)), leftParen: nil, type: nil, rightParen: nil, arena: self.arena)
632642
}
633643
} else {
634644
break
@@ -638,11 +648,8 @@ extension Parser {
638648
if let (_, handle) = self.canRecoverTo(anyIn: S.CorrectThrowsTokenKinds.self) {
639649
let (unexpected, throwsKw) = self.eat(handle)
640650
unexpectedBeforeThrows.append(contentsOf: unexpected?.elements ?? [])
641-
throwsKeyword = throwsKw
642651

643-
if self.at(.leftParen) && experimentalFeatures.contains(.typedThrows) {
644-
throwsClause = parseThrowsClause(after: throwsKw)
645-
}
652+
throwsClause = parseThrowsClause(after: throwsKw)
646653
}
647654

648655
var unexpectedAfterThrownErrorLoopProgress = LoopProgressCondition()
@@ -662,19 +669,12 @@ extension Parser {
662669
}
663670
}
664671

665-
if unexpectedBeforeAsync.isEmpty && asyncKeyword == nil && unexpectedBeforeThrows.isEmpty && throwsKeyword == nil && throwsClause == nil
672+
if unexpectedBeforeAsync.isEmpty && asyncKeyword == nil && unexpectedBeforeThrows.isEmpty && throwsClause == nil
666673
&& unexpectedAfterThrowsClause.isEmpty
667674
{
668675
return nil
669676
}
670677

671-
// If needed, form a throws clause from just the throws keyword.
672-
throwsClause =
673-
throwsClause
674-
?? throwsKeyword.map {
675-
RawThrowsClauseSyntax(throwsSpecifier: $0, leftParen: nil, type: nil, rightParen: nil, arena: self.arena)
676-
}
677-
678678
return S(
679679
RawUnexpectedNodesSyntax(unexpectedBeforeAsync, arena: self.arena),
680680
asyncSpecifier: asyncKeyword,

0 commit comments

Comments
 (0)