@@ -966,13 +966,9 @@ extension Parser {
966
966
decl = self . parseDeclaration ( in: . memberDeclList)
967
967
attachSemi = true
968
968
} else {
969
+ // Otherwise, eat the unexpected tokens into an "decl".
969
970
decl = RawDeclSyntax (
970
- self . parseUnexpectedCodeDeclaration (
971
- allowInitDecl: true ,
972
- requiresDecl: true ,
973
- skipToDeclOnly: true ,
974
- until: stopCondition
975
- )
971
+ self . parseUnexpectedCodeDeclaration ( allowInitDecl: true , requiresDecl: true , until: stopCondition)
976
972
)
977
973
attachSemi = true
978
974
}
@@ -1009,7 +1005,7 @@ extension Parser {
1009
1005
}
1010
1006
1011
1007
mutating func parseMemberDeclList(
1012
- until stopCondition: ( inout Parser ) -> Bool = { $0. at ( . rightBrace) }
1008
+ until stopCondition: ( inout Parser ) -> Bool = { $0. at ( . rightBrace) || $0 . atEndOfIfConfigClauseBody ( ) }
1013
1009
) -> RawMemberBlockItemListSyntax {
1014
1010
var elements = [ RawMemberBlockItemSyntax] ( )
1015
1011
do {
@@ -1765,7 +1761,7 @@ extension Parser {
1765
1761
// There can only be an implicit getter if no other accessors were
1766
1762
// seen before this one.
1767
1763
guard let accessorList else {
1768
- let body = parseCodeBlockItemList ( until : { $0 . at ( . rightBrace ) } )
1764
+ let body = parseCodeBlockItemList ( )
1769
1765
1770
1766
let ( unexpectedBeforeRBrace, rbrace) = self . expect ( . rightBrace)
1771
1767
return RawAccessorBlockSyntax (
@@ -2326,20 +2322,20 @@ extension Parser {
2326
2322
)
2327
2323
}
2328
2324
2329
- /// Eat tokens until a start of decl, or if `skipToDeclOnly` is not set until
2330
- /// a start of statement or expression.
2325
+ /// Eats tokens until a start of decl, statement, or expression.
2331
2326
/// Returns consumed tokens as a `RawUnexpectedCodeDeclSyntax` declaration.
2332
2327
mutating func parseUnexpectedCodeDeclaration(
2333
2328
allowInitDecl: Bool ,
2334
2329
requiresDecl: Bool ,
2335
- skipToDeclOnly: Bool ,
2336
2330
until stopCondition: ( inout Parser ) -> Bool
2337
2331
) -> RawUnexpectedCodeDeclSyntax {
2338
2332
var unexpectedTokens = [ RawSyntax] ( )
2339
- while !self . at ( . endOfFile, . semicolon) && !stopCondition( & self ) {
2340
- let numTokensToSkip = self . withLookahead ( {
2341
- $0. skipSingle ( ) ; return $0. tokensConsumed
2342
- } )
2333
+ var loopProgress = LoopProgressCondition ( )
2334
+ while !self . at ( . endOfFile, . semicolon) , !stopCondition( & self ) , self . hasProgressed ( & loopProgress) {
2335
+ let numTokensToSkip = self . withLookahead {
2336
+ $0. skipSingle ( )
2337
+ return $0. tokensConsumed
2338
+ }
2343
2339
for _ in 0 ..< numTokensToSkip {
2344
2340
unexpectedTokens. append ( RawSyntax ( self . consumeAnyTokenWithoutAdjustingNestingLevel ( ) ) )
2345
2341
}
@@ -2354,13 +2350,14 @@ extension Parser {
2354
2350
break
2355
2351
}
2356
2352
2357
- if skipToDeclOnly {
2353
+ // If a declaration is expected, ignore statements and expressions.
2354
+ if requiresDecl {
2358
2355
continue
2359
2356
}
2360
2357
if self . atStartOfStatement ( preferExpr: false ) {
2361
2358
break
2362
2359
}
2363
- // Recover to an expression only if it's on the next line.
2360
+ // Recover to an expression only if it's on a new line.
2364
2361
if self . currentToken. isAtStartOfLine && self . atStartOfExpression ( ) {
2365
2362
break
2366
2363
}
0 commit comments