Skip to content

Commit c17f6b0

Browse files
authored
Merge pull request #1282 from ahoppen/ahoppen/misplaced-attribute-recovery
Improve recovery of misplaced attributes on statements
2 parents 929dad4 + 7048cec commit c17f6b0

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

Sources/SwiftParser/Statements.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ extension TokenConsumer {
2020
/// - Seealso: ``Parser/parseStatement()``
2121
func atStartOfStatement(allowRecovery: Bool = false) -> Bool {
2222
var lookahead = self.lookahead()
23+
if allowRecovery {
24+
// Attributes are not allowed on statements. But for recovery, skip over
25+
// misplaced attributes.
26+
_ = lookahead.consumeAttributeList()
27+
}
2328
return lookahead.isStartOfStatement(allowRecovery: allowRecovery)
2429
}
2530
}

Sources/SwiftParser/TokenPrecedence.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public enum TokenPrecedence: Comparable {
135135
case // Chaining punctuators
136136
.infixQuestionMark, .period, .postfixQuestionMark, .exclamationMark,
137137
// Misc
138-
.backslash, .backtick, .colon, .comma, .ellipsis, .equal, .prefixAmpersand:
138+
.atSign, .backslash, .backtick, .colon, .comma, .ellipsis, .equal, .prefixAmpersand:
139139
self = .weakPunctuator
140140

141141
// MARK: Weak bracket close
@@ -159,8 +159,6 @@ public enum TokenPrecedence: Comparable {
159159
.semicolon,
160160
// Arrow is a strong indicator in a function type that we are now in the return type
161161
.arrow,
162-
// '@' typically occurs at the start of declarations
163-
.atSign,
164162
// EOF is here because it is a very stong marker and doesn't belong anywhere else
165163
.eof:
166164
self = .strongPunctuator

Tests/SwiftParserTest/StatementTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ final class StatementTests: XCTestCase {
265265
}
266266
""",
267267
diagnostics: [
268-
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '@s return' in function"),
269-
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '@unknown return' in function"),
268+
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '@s' before 'return' statement"),
269+
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '@unknown' before 'return' statement"),
270270
]
271271
)
272272
}

Tests/SwiftParserTest/translated/SwitchTests.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ final class SwitchTests: XCTestCase {
950950
}
951951
""",
952952
diagnostics: [
953-
DiagnosticSpec(message: "expected label in switch case")
953+
DiagnosticSpec(message: "unexpected code '@moreGarbage' in switch case")
954954
]
955955
)
956956
}
@@ -986,22 +986,21 @@ final class SwitchTests: XCTestCase {
986986
switch Whatever.Thing {
987987
case .Thing:
988988
break
989-
@unknown2️⃣
990989
@unknown
990+
2️⃣@unknown
991991
case _:
992992
break
993993
}
994994
switch Whatever.Thing {
995-
@unknown 3️⃣@garbage4️⃣(foobar)
995+
@unknown 3️⃣@garbage(foobar)
996996
case _:
997997
break
998998
}
999999
""",
10001000
diagnostics: [
10011001
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '(garbage)' in switch case"),
1002-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected label in switch case"),
1003-
DiagnosticSpec(locationMarker: "3️⃣", message: "expected label in switch case"),
1004-
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code '(foobar)' in switch case"),
1002+
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '@unknown' in switch case"),
1003+
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code '@garbage(foobar)' in switch case"),
10051004
]
10061005
)
10071006
}

0 commit comments

Comments
 (0)