Skip to content

Commit f163c3f

Browse files
Simplified logic for parsing deinit effect specifiers
1 parent 5d23aa5 commit f163c3f

File tree

2 files changed

+52
-78
lines changed

2 files changed

+52
-78
lines changed

Sources/SwiftParser/Specifiers.swift

Lines changed: 17 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -417,54 +417,6 @@ extension RawAccessorEffectSpecifiersSyntax: RawEffectSpecifiersTrait {
417417
}
418418
}
419419

420-
extension RawDeinitEffectSpecifiersSyntax {
421-
enum MisspelledAsyncTokenKinds: TokenSpecSet {
422-
case await
423-
case reasync
424-
425-
init?(lexeme: Lexer.Lexeme) {
426-
switch PrepareForKeywordMatch(lexeme) {
427-
case TokenSpec(.await, allowAtStartOfLine: false): self = .await
428-
case TokenSpec(.reasync): self = .reasync
429-
default: return nil
430-
}
431-
}
432-
433-
var spec: TokenSpec {
434-
switch self {
435-
case .await: return TokenSpec(.await, allowAtStartOfLine: false)
436-
case .reasync: return .keyword(.reasync)
437-
}
438-
}
439-
}
440-
441-
enum MisspelledThrowsTokenKinds: TokenSpecSet {
442-
case `rethrows`
443-
case `throw`
444-
case `throws`
445-
case `try`
446-
447-
init?(lexeme: Lexer.Lexeme) {
448-
switch PrepareForKeywordMatch(lexeme) {
449-
case TokenSpec(.rethrows): self = .rethrows
450-
case TokenSpec(.throw, allowAtStartOfLine: false): self = .throw
451-
case TokenSpec(.throws): self = .throws
452-
case TokenSpec(.try, allowAtStartOfLine: false): self = .try
453-
default: return nil
454-
}
455-
}
456-
457-
var spec: TokenSpec {
458-
switch self {
459-
case .rethrows: return .keyword(.rethrows)
460-
case .throw: return TokenSpec(.throw, allowAtStartOfLine: false)
461-
case .throws: return .keyword(.throws)
462-
case .try: return TokenSpec(.try, allowAtStartOfLine: false)
463-
}
464-
}
465-
}
466-
}
467-
468420
extension TokenConsumer {
469421
mutating func at<SpecSet1: TokenSpecSet, SpecSet2: TokenSpecSet>(
470422
anyIn specSet1: SpecSet1.Type,
@@ -576,45 +528,32 @@ extension Parser {
576528
var unexpectedBeforeAsync: [RawSyntax?] = []
577529
var asyncKeyword: RawTokenSyntax?
578530
var unexpectedAfterAsync: [RawSyntax?] = []
579-
580-
while true {
581-
var progressed: Bool = false
582-
583-
if let realAsync = self.consume(if: .keyword(.async)) {
584-
progressed = true
585-
if asyncKeyword?.isMissing ?? true {
586-
asyncKeyword = realAsync
587-
} else {
588-
unexpectedAfterAsync.append(realAsync.raw)
589-
}
590-
}
591-
592-
while let badAsync = self.consume(ifAnyIn: RawDeinitEffectSpecifiersSyntax.MisspelledAsyncTokenKinds.self) {
593-
progressed = true
594-
if asyncKeyword?.isMissing ?? true {
595-
unexpectedBeforeAsync.append(badAsync.raw)
531+
532+
while let (specifier, handle) = self.at(anyIn: EffectSpecifiers.self) {
533+
let beforeAsync = asyncKeyword?.isMissing ?? true
534+
switch specifier {
535+
case .async:
536+
if beforeAsync {
537+
asyncKeyword = self.eat(handle)
596538
} else {
597-
unexpectedAfterAsync.append(badAsync.raw)
539+
unexpectedAfterAsync.append(RawSyntax(self.eat(handle)))
598540
}
599-
if asyncKeyword == nil {
541+
case .await, .reasync:
542+
if beforeAsync {
600543
// Let's synthesize a missing 'async'. If we find a real async specifier
601544
// later, we will replace the missing token by the present token.
602-
asyncKeyword = missingToken(.keyword(.async))
545+
asyncKeyword = missingToken(.async)
603546
}
604-
}
605-
606-
while let badThrows = self.consume(ifAnyIn: RawDeinitEffectSpecifiersSyntax.MisspelledThrowsTokenKinds.self) {
607-
progressed = true
608-
if asyncKeyword?.isMissing ?? true {
609-
unexpectedBeforeAsync.append(badThrows.raw)
547+
fallthrough
548+
default:
549+
if beforeAsync {
550+
unexpectedBeforeAsync.append(RawSyntax(self.eat(handle)))
610551
} else {
611-
unexpectedAfterAsync.append(badThrows.raw)
552+
unexpectedAfterAsync.append(RawSyntax(self.eat(handle)))
612553
}
613554
}
614-
615-
if !progressed { break }
616555
}
617-
556+
618557
if unexpectedBeforeAsync.isEmpty && asyncKeyword == nil && unexpectedAfterAsync.isEmpty {
619558
return nil
620559
}

Sources/SwiftParser/TokenSpecSet.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,3 +919,38 @@ enum ExpressionStart: TokenSpecSet {
919919
}
920920
}
921921
}
922+
923+
enum EffectSpecifiers: TokenSpecSet {
924+
case async
925+
case await
926+
case reasync
927+
case `rethrows`
928+
case `throw`
929+
case `throws`
930+
case `try`
931+
932+
init?(lexeme: Lexer.Lexeme) {
933+
switch PrepareForKeywordMatch(lexeme) {
934+
case TokenSpec(.async): self = .async
935+
case TokenSpec(.await, allowAtStartOfLine: false): self = .await
936+
case TokenSpec(.reasync): self = .reasync
937+
case TokenSpec(.rethrows): self = .rethrows
938+
case TokenSpec(.throw, allowAtStartOfLine: false): self = .throw
939+
case TokenSpec(.throws): self = .throws
940+
case TokenSpec(.try, allowAtStartOfLine: false): self = .try
941+
default: return nil
942+
}
943+
}
944+
945+
var spec: TokenSpec {
946+
switch self {
947+
case .async: return .keyword(.async)
948+
case .await: return TokenSpec(.await, allowAtStartOfLine: false)
949+
case .reasync: return .keyword(.reasync)
950+
case .rethrows: return .keyword(.rethrows)
951+
case .throw: return TokenSpec(.throw, allowAtStartOfLine: false)
952+
case .throws: return .keyword(.throws)
953+
case .try: return TokenSpec(.try, allowAtStartOfLine: false)
954+
}
955+
}
956+
}

0 commit comments

Comments
 (0)