@@ -149,10 +149,10 @@ extension Parser {
149
149
///
150
150
/// declarations → declaration declarations?
151
151
///
152
- /// If `allowMissingFuncOrVarKeywordRecovery ` is `true`, this methods tries to
153
- /// synthesize `func` or `var` keywords where necessary .
152
+ /// If `inMemberDeclList ` is `true`, we know that the next item must be a
153
+ /// declaration and thus start with a keyword. This allows futher recovery .
154
154
@_spi ( RawSyntax)
155
- public mutating func parseDeclaration( allowMissingFuncOrVarKeywordRecovery : Bool = false ) -> RawDeclSyntax {
155
+ public mutating func parseDeclaration( inMemberDeclList : Bool = false ) -> RawDeclSyntax {
156
156
switch self . at ( anyIn: PoundDeclarationStart . self) {
157
157
case ( . poundIfKeyword, _) ? :
158
158
let directive = self . parsePoundIfDirective { parser in
@@ -207,7 +207,7 @@ extension Parser {
207
207
case ( . subscriptKeyword, let handle) ? :
208
208
return RawDeclSyntax ( self . parseSubscriptDeclaration ( attrs, handle) )
209
209
case ( . letKeyword, let handle) ? , ( . varKeyword, let handle) ? :
210
- return RawDeclSyntax ( self . parseLetOrVarDeclaration ( attrs, handle) )
210
+ return RawDeclSyntax ( self . parseLetOrVarDeclaration ( attrs, handle, inMemberDeclList : inMemberDeclList ) )
211
211
case ( . initKeyword, let handle) ? :
212
212
return RawDeclSyntax ( self . parseInitializerDeclaration ( attrs, handle) )
213
213
case ( . deinitKeyword, let handle) ? :
@@ -219,7 +219,7 @@ extension Parser {
219
219
case ( . actorContextualKeyword, let handle) ? :
220
220
return RawDeclSyntax ( self . parseActorDeclaration ( attrs, handle) )
221
221
case nil :
222
- if allowMissingFuncOrVarKeywordRecovery {
222
+ if inMemberDeclList {
223
223
let isProbablyVarDecl = self . at ( any: [ . identifier, . wildcardKeyword] ) && self . peek ( ) . tokenKind. is ( any: [ . colon, . equal, . comma] )
224
224
let isProbablyTupleDecl = self . at ( . leftParen) && self . peek ( ) . tokenKind. is ( any: [ . identifier, . wildcardKeyword] )
225
225
@@ -608,7 +608,7 @@ extension Parser {
608
608
if self . at ( . poundSourceLocationKeyword) {
609
609
decl = RawDeclSyntax ( self . parsePoundSourceLocationDirective ( ) )
610
610
} else {
611
- decl = self . parseDeclaration ( allowMissingFuncOrVarKeywordRecovery : true )
611
+ decl = self . parseDeclaration ( inMemberDeclList : true )
612
612
}
613
613
614
614
let semi = self . consume ( if: . semicolon)
@@ -1690,7 +1690,7 @@ extension Parser {
1690
1690
1691
1691
// Parse getter and setter.
1692
1692
let accessor : RawSyntax ?
1693
- if self . at ( . leftBrace) {
1693
+ if self . at ( . leftBrace) || self . at ( anyIn : AccessorKind . self ) != nil {
1694
1694
accessor = self . parseGetSet ( )
1695
1695
} else {
1696
1696
accessor = nil
@@ -1721,10 +1721,22 @@ extension Parser {
1721
1721
/// pattern-initializer-list → pattern-initializer | pattern-initializer ',' pattern-initializer-list
1722
1722
/// pattern-initializer → pattern initializer?
1723
1723
/// initializer → = expression
1724
+ ///
1725
+ /// If `inMemberDeclList` is `true`, we know that the next item needs to be a
1726
+ /// declaration that is started by a keyword. Thus, we in the following case
1727
+ /// we know that `set` can't start a new declaration and we can thus recover
1728
+ /// by synthesizing a missing `{` in front of `set`.
1729
+ /// ```
1730
+ /// var x: Int
1731
+ /// set {
1732
+ /// }
1733
+ /// }
1734
+ /// ```
1724
1735
@_spi ( RawSyntax)
1725
1736
public mutating func parseLetOrVarDeclaration(
1726
1737
_ attrs: DeclAttributes ,
1727
- _ handle: RecoveryConsumptionHandle
1738
+ _ handle: RecoveryConsumptionHandle ,
1739
+ inMemberDeclList: Bool = false
1728
1740
) -> RawVariableDeclSyntax {
1729
1741
let ( unexpectedBeforeIntroducer, introducer) = self . eat ( handle)
1730
1742
let hasTryBeforeIntroducer = unexpectedBeforeIntroducer? . containsToken ( where: { $0. tokenKind == . tryKeyword } ) ?? false
@@ -1786,7 +1798,7 @@ extension Parser {
1786
1798
}
1787
1799
1788
1800
let accessor : RawSyntax ?
1789
- if self . at ( . leftBrace) {
1801
+ if self . at ( . leftBrace) || ( inMemberDeclList && self . at ( anyIn : AccessorKind . self ) != nil ) {
1790
1802
accessor = self . parseGetSet ( )
1791
1803
} else {
1792
1804
accessor = nil
@@ -1812,21 +1824,6 @@ extension Parser {
1812
1824
arena: self . arena)
1813
1825
}
1814
1826
1815
- enum AccessorKind : SyntaxText , ContextualKeywords , Equatable {
1816
- case `get` = " get "
1817
- case `set` = " set "
1818
- case `didSet` = " didSet "
1819
- case `willSet` = " willSet "
1820
- case unsafeAddress = " unsafeAddress "
1821
- case addressWithOwner = " addressWithOwner "
1822
- case addressWithNativeOwner = " addressWithNativeOwner "
1823
- case unsafeMutableAddress = " unsafeMutableAddress "
1824
- case mutableAddressWithOwner = " mutableAddressWithOwner "
1825
- case mutableAddressWithNativeOwner = " mutableAddressWithNativeOwner "
1826
- case _read = " _read "
1827
- case _modify = " _modify "
1828
- }
1829
-
1830
1827
struct AccessorIntroducer {
1831
1828
var attributes : RawAttributeListSyntax ?
1832
1829
var modifier : RawDeclModifierSyntax ?
@@ -1921,7 +1918,14 @@ extension Parser {
1921
1918
@_spi ( RawSyntax)
1922
1919
public mutating func parseGetSet( ) -> RawSyntax {
1923
1920
// Parse getter and setter.
1924
- let ( unexpectedBeforeLBrace, lbrace) = self . expect ( . leftBrace)
1921
+ let unexpectedBeforeLBrace : RawUnexpectedNodesSyntax ?
1922
+ let lbrace : RawTokenSyntax
1923
+ if self . at ( anyIn: AccessorKind . self) != nil {
1924
+ unexpectedBeforeLBrace = nil
1925
+ lbrace = missingToken ( . leftBrace, text: nil )
1926
+ } else {
1927
+ ( unexpectedBeforeLBrace, lbrace) = self . expect ( . leftBrace)
1928
+ }
1925
1929
// Collect all explicit accessors to a list.
1926
1930
var elements = [ RawAccessorDeclSyntax] ( )
1927
1931
do {
0 commit comments