Skip to content

Commit 5c6cde3

Browse files
authored
Merge pull request #1907 from ahoppen/ahoppen/5.9/borrowing-getter
[5.9] Support parsing of `borrowing get`
2 parents 4679895 + 8d6aed2 commit 5c6cde3

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
@@ -1458,7 +1458,7 @@ extension Parser {
14581458
// Check there is an identifier before consuming
14591459
var look = self.lookahead()
14601460
let _ = look.consumeAttributeList()
1461-
let hasModifier = look.consume(if: .keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming)) != nil
1461+
let hasModifier = look.consume(ifAnyIn: AccessorModifier.self) != nil
14621462
guard let (kind, _) = look.at(anyIn: AccessorKind.self) ?? forcedKind else {
14631463
return nil
14641464
}
@@ -1469,7 +1469,7 @@ extension Parser {
14691469
// get and set.
14701470
let modifier: RawDeclModifierSyntax?
14711471
if hasModifier {
1472-
let (unexpectedBeforeName, name) = self.expect(.keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming), default: .keyword(.mutating))
1472+
let (unexpectedBeforeName, name) = self.expect(anyIn: AccessorModifier.self, default: .mutating)
14731473
modifier = RawDeclModifierSyntax(
14741474
unexpectedBeforeName,
14751475
name: name,

Sources/SwiftParser/TokenSpecSet.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,35 @@ enum AccessorKind: TokenSpecSet {
7676
}
7777
}
7878

79+
enum AccessorModifier: TokenSpecSet {
80+
case __consuming
81+
case consuming
82+
case borrowing
83+
case mutating
84+
case nonmutating
85+
86+
init?(lexeme: Lexer.Lexeme) {
87+
switch PrepareForKeywordMatch(lexeme) {
88+
case TokenSpec(.__consuming): self = .__consuming
89+
case TokenSpec(.consuming): self = .consuming
90+
case TokenSpec(.borrowing): self = .borrowing
91+
case TokenSpec(.mutating): self = .mutating
92+
case TokenSpec(.nonmutating): self = .nonmutating
93+
default: return nil
94+
}
95+
}
96+
97+
var spec: TokenSpec {
98+
switch self {
99+
case .__consuming: return .keyword(.__consuming)
100+
case .consuming: return .keyword(.consuming)
101+
case .borrowing: return .keyword(.borrowing)
102+
case .mutating: return .keyword(.mutating)
103+
case .nonmutating: return .keyword(.nonmutating)
104+
}
105+
}
106+
}
107+
79108
enum CanBeStatementStart: TokenSpecSet {
80109
case _forget // NOTE: support for deprecated _forget
81110
case `break`

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,4 +2031,16 @@ final class DeclarationTests: XCTestCase {
20312031
"""
20322032
)
20332033
}
2034+
2035+
func testBorrowingGetAccessor() {
2036+
assertParse(
2037+
"""
2038+
struct Foo {
2039+
var x: Int {
2040+
borrowing get {}
2041+
}
2042+
}
2043+
"""
2044+
)
2045+
}
20342046
}

0 commit comments

Comments
 (0)