@@ -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
@@ -211,7 +211,7 @@ extension Parser {
211
211
case ( . subscriptKeyword, let handle) ? :
212
212
return RawDeclSyntax ( self . parseSubscriptDeclaration ( attrs, handle) )
213
213
case ( . letKeyword, let handle) ? , ( . varKeyword, let handle) ? :
214
- return RawDeclSyntax ( self . parseLetOrVarDeclaration ( attrs, handle) )
214
+ return RawDeclSyntax ( self . parseLetOrVarDeclaration ( attrs, handle, inMemberDeclList : inMemberDeclList ) )
215
215
case ( . initKeyword, let handle) ? :
216
216
return RawDeclSyntax ( self . parseInitializerDeclaration ( attrs, handle) )
217
217
case ( . deinitKeyword, let handle) ? :
@@ -223,7 +223,7 @@ extension Parser {
223
223
case ( . actorContextualKeyword, let handle) ? :
224
224
return RawDeclSyntax ( self . parseActorDeclaration ( attrs, handle) )
225
225
case nil :
226
- if allowMissingFuncOrVarKeywordRecovery {
226
+ if inMemberDeclList {
227
227
let isProbablyVarDecl = self . at ( any: [ . identifier, . wildcardKeyword] ) && self . peek ( ) . tokenKind. is ( any: [ . colon, . equal, . comma] )
228
228
let isProbablyTupleDecl = self . at ( . leftParen) && self . peek ( ) . tokenKind. is ( any: [ . identifier, . wildcardKeyword] )
229
229
@@ -612,7 +612,7 @@ extension Parser {
612
612
if self . at ( . poundSourceLocationKeyword) {
613
613
decl = RawDeclSyntax ( self . parsePoundSourceLocationDirective ( ) )
614
614
} else {
615
- decl = self . parseDeclaration ( allowMissingFuncOrVarKeywordRecovery : true )
615
+ decl = self . parseDeclaration ( inMemberDeclList : true )
616
616
}
617
617
618
618
let semi = self . consume ( if: . semicolon)
@@ -1694,7 +1694,7 @@ extension Parser {
1694
1694
1695
1695
// Parse getter and setter.
1696
1696
let accessor : RawSyntax ?
1697
- if self . at ( . leftBrace) {
1697
+ if self . at ( . leftBrace) || self . at ( anyIn : AccessorKind . self ) != nil {
1698
1698
accessor = self . parseGetSet ( )
1699
1699
} else {
1700
1700
accessor = nil
@@ -1725,10 +1725,22 @@ extension Parser {
1725
1725
/// pattern-initializer-list → pattern-initializer | pattern-initializer ',' pattern-initializer-list
1726
1726
/// pattern-initializer → pattern initializer?
1727
1727
/// initializer → = expression
1728
+ ///
1729
+ /// If `inMemberDeclList` is `true`, we know that the next item needs to be a
1730
+ /// declaration that is started by a keyword. Thus, we in the following case
1731
+ /// we know that `set` can't start a new declaration and we can thus recover
1732
+ /// by synthesizing a missing `{` in front of `set`.
1733
+ /// ```
1734
+ /// var x: Int
1735
+ /// set {
1736
+ /// }
1737
+ /// }
1738
+ /// ```
1728
1739
@_spi ( RawSyntax)
1729
1740
public mutating func parseLetOrVarDeclaration(
1730
1741
_ attrs: DeclAttributes ,
1731
- _ handle: RecoveryConsumptionHandle
1742
+ _ handle: RecoveryConsumptionHandle ,
1743
+ inMemberDeclList: Bool = false
1732
1744
) -> RawVariableDeclSyntax {
1733
1745
let ( unexpectedBeforeIntroducer, introducer) = self . eat ( handle)
1734
1746
let hasTryBeforeIntroducer = unexpectedBeforeIntroducer? . containsToken ( where: { $0. tokenKind == . tryKeyword } ) ?? false
@@ -1790,7 +1802,7 @@ extension Parser {
1790
1802
}
1791
1803
1792
1804
let accessor : RawSyntax ?
1793
- if self . at ( . leftBrace) {
1805
+ if self . at ( . leftBrace) || ( inMemberDeclList && self . at ( anyIn : AccessorKind . self ) != nil ) {
1794
1806
accessor = self . parseGetSet ( )
1795
1807
} else {
1796
1808
accessor = nil
@@ -1816,21 +1828,6 @@ extension Parser {
1816
1828
arena: self . arena)
1817
1829
}
1818
1830
1819
- enum AccessorKind : SyntaxText , ContextualKeywords , Equatable {
1820
- case `get` = " get "
1821
- case `set` = " set "
1822
- case `didSet` = " didSet "
1823
- case `willSet` = " willSet "
1824
- case unsafeAddress = " unsafeAddress "
1825
- case addressWithOwner = " addressWithOwner "
1826
- case addressWithNativeOwner = " addressWithNativeOwner "
1827
- case unsafeMutableAddress = " unsafeMutableAddress "
1828
- case mutableAddressWithOwner = " mutableAddressWithOwner "
1829
- case mutableAddressWithNativeOwner = " mutableAddressWithNativeOwner "
1830
- case _read = " _read "
1831
- case _modify = " _modify "
1832
- }
1833
-
1834
1831
struct AccessorIntroducer {
1835
1832
var attributes : RawAttributeListSyntax ?
1836
1833
var modifier : RawDeclModifierSyntax ?
@@ -1925,7 +1922,14 @@ extension Parser {
1925
1922
@_spi ( RawSyntax)
1926
1923
public mutating func parseGetSet( ) -> RawSyntax {
1927
1924
// Parse getter and setter.
1928
- let ( unexpectedBeforeLBrace, lbrace) = self . expect ( . leftBrace)
1925
+ let unexpectedBeforeLBrace : RawUnexpectedNodesSyntax ?
1926
+ let lbrace : RawTokenSyntax
1927
+ if self . at ( anyIn: AccessorKind . self) != nil {
1928
+ unexpectedBeforeLBrace = nil
1929
+ lbrace = missingToken ( . leftBrace, text: nil )
1930
+ } else {
1931
+ ( unexpectedBeforeLBrace, lbrace) = self . expect ( . leftBrace)
1932
+ }
1929
1933
// Collect all explicit accessors to a list.
1930
1934
var elements = [ RawAccessorDeclSyntax] ( )
1931
1935
do {
0 commit comments