Skip to content

Commit 10e9a4d

Browse files
hborlaahoppen
authored andcommitted
Parse init accessors.
1 parent 165fc6d commit 10e9a4d

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ extension Parser {
14031403
}
14041404

14051405
let accessor: RawPatternBindingSyntax.Accessor?
1406-
if self.at(.leftBrace) || (inMemberDeclList && self.at(anyIn: AccessorKind.self) != nil) {
1406+
if self.at(.leftBrace) || (inMemberDeclList && self.at(anyIn: AccessorKind.self) != nil && !self.at(.keyword(.`init`))) {
14071407
switch self.parseGetSet() {
14081408
case .accessors(let accessors):
14091409
accessor = .accessors(accessors)
@@ -1515,7 +1515,7 @@ extension Parser {
15151515
//
15161516
// set-name ::= '(' identifier ')'
15171517
let parameter: RawAccessorParameterSyntax?
1518-
if [AccessorKind.set, .willSet, .didSet].contains(introducer.kind), let lparen = self.consume(if: .leftParen) {
1518+
if [AccessorKind.set, .willSet, .didSet, .`init`].contains(introducer.kind), let lparen = self.consume(if: .leftParen) {
15191519
let (unexpectedBeforeName, name) = self.expectIdentifier()
15201520
let (unexpectedBeforeRParen, rparen) = self.expect(.rightParen)
15211521
parameter = RawAccessorParameterSyntax(

Sources/SwiftParser/TokenSpecSet.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum AccessorKind: TokenSpecSet {
2828
case `set`
2929
case `didSet`
3030
case `willSet`
31+
case `init`
3132
case unsafeAddress
3233
case addressWithOwner
3334
case addressWithNativeOwner
@@ -43,6 +44,7 @@ enum AccessorKind: TokenSpecSet {
4344
case TokenSpec(.set): self = .set
4445
case TokenSpec(.didSet): self = .didSet
4546
case TokenSpec(.willSet): self = .willSet
47+
case TokenSpec(.`init`): self = .`init`
4648
case TokenSpec(.unsafeAddress): self = .unsafeAddress
4749
case TokenSpec(.addressWithOwner): self = .addressWithOwner
4850
case TokenSpec(.addressWithNativeOwner): self = .addressWithNativeOwner
@@ -61,6 +63,7 @@ enum AccessorKind: TokenSpecSet {
6163
case .set: return .keyword(.set)
6264
case .didSet: return .keyword(.didSet)
6365
case .willSet: return .keyword(.willSet)
66+
case .`init`: return .keyword(.`init`)
6467
case .unsafeAddress: return .keyword(.unsafeAddress)
6568
case .addressWithOwner: return .keyword(.addressWithOwner)
6669
case .addressWithNativeOwner: return .keyword(.addressWithNativeOwner)

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,55 @@ final class DeclarationTests: XCTestCase {
742742
)
743743
}
744744

745+
func testInitAccessor() {
746+
assertParse(
747+
"""
748+
struct S {
749+
var value: Int {
750+
init {}
751+
get {}
752+
set {}
753+
}
754+
}
755+
"""
756+
)
757+
758+
assertParse(
759+
"""
760+
struct S {
761+
let _value: Int
762+
763+
init() {
764+
}
765+
}
766+
"""
767+
)
768+
769+
assertParse(
770+
"""
771+
struct S {
772+
var value: Int {
773+
init(newValue) {}
774+
get {}
775+
set(newValue) {}
776+
}
777+
}
778+
"""
779+
)
780+
781+
assertParse(
782+
"""
783+
struct S {
784+
var value: Int {
785+
init(newValue) {}
786+
get {}
787+
set(newValue) {}
788+
}
789+
}
790+
"""
791+
)
792+
}
793+
745794
func testExpressionMember() {
746795
assertParse(
747796
"""

0 commit comments

Comments
 (0)