Skip to content

Commit d21d996

Browse files
authored
Merge pull request #1904 from ahoppen/ahoppen/borrowing-getter
Support parsing of `borrowing get`
2 parents 7ed925d + eb573ba commit d21d996

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ extension Parser {
14211421
// Check there is an identifier before consuming
14221422
var look = self.lookahead()
14231423
let _ = look.consumeAttributeList()
1424-
let hasModifier = look.consume(if: .keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming)) != nil
1424+
let hasModifier = look.consume(ifAnyIn: AccessorModifier.self) != nil
14251425
guard let (kind, _) = look.at(anyIn: AccessorDeclSyntax.AccessorSpecifierOptions.self) ?? forcedKind else {
14261426
return nil
14271427
}
@@ -1432,7 +1432,7 @@ extension Parser {
14321432
// get and set.
14331433
let modifier: RawDeclModifierSyntax?
14341434
if hasModifier {
1435-
let (unexpectedBeforeName, name) = self.expect(.keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming), default: .keyword(.mutating))
1435+
let (unexpectedBeforeName, name) = self.expect(anyIn: AccessorModifier.self, default: .mutating)
14361436
modifier = RawDeclModifierSyntax(
14371437
unexpectedBeforeName,
14381438
name: name,

Sources/SwiftParser/TokenSpecSet.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ protocol TokenSpecSet: CaseIterable {
2323

2424
// MARK: - Subsets
2525

26+
enum AccessorModifier: TokenSpecSet {
27+
case __consuming
28+
case consuming
29+
case borrowing
30+
case mutating
31+
case nonmutating
32+
33+
init?(lexeme: Lexer.Lexeme) {
34+
switch PrepareForKeywordMatch(lexeme) {
35+
case TokenSpec(.__consuming): self = .__consuming
36+
case TokenSpec(.consuming): self = .consuming
37+
case TokenSpec(.borrowing): self = .borrowing
38+
case TokenSpec(.mutating): self = .mutating
39+
case TokenSpec(.nonmutating): self = .nonmutating
40+
default: return nil
41+
}
42+
}
43+
44+
var spec: TokenSpec {
45+
switch self {
46+
case .__consuming: return .keyword(.__consuming)
47+
case .consuming: return .keyword(.consuming)
48+
case .borrowing: return .keyword(.borrowing)
49+
case .mutating: return .keyword(.mutating)
50+
case .nonmutating: return .keyword(.nonmutating)
51+
}
52+
}
53+
}
54+
2655
enum CanBeStatementStart: TokenSpecSet {
2756
case _forget // NOTE: support for deprecated _forget
2857
case `break`

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,4 +2607,16 @@ final class DeclarationTests: XCTestCase {
26072607
"""
26082608
)
26092609
}
2610+
2611+
func testBorrowingGetAccessor() {
2612+
assertParse(
2613+
"""
2614+
struct Foo {
2615+
var x: Int {
2616+
borrowing get {}
2617+
}
2618+
}
2619+
"""
2620+
)
2621+
}
26102622
}

0 commit comments

Comments
 (0)