Skip to content

Commit 6268c80

Browse files
committed
tbs
1 parent 2a97e0e commit 6268c80

File tree

4 files changed

+27
-45
lines changed

4 files changed

+27
-45
lines changed

Sources/SwiftParser/Statements.swift

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ extension TokenConsumer {
3434
}
3535
}
3636

37-
extension Parser.Lookahead {
38-
mutating func atStartOfSwitchCaseItem() -> Bool {
39-
while self.consume(if: .atSign) != nil {
40-
self.consume(if: .identifier)
41-
}
42-
43-
return self.at(anyIn: SwitchCaseStart.self) != nil
44-
}
45-
}
46-
4737
extension Parser {
4838
/// Parse a statement.
4939
///
@@ -700,6 +690,9 @@ extension Parser {
700690
if self.atStartOfStatement(preferExpr: true) || self.atStartOfDeclaration() {
701691
return false
702692
}
693+
if self.atStartOfLine && self.withLookahead({ $0.atStartOfSwitchCase() }) {
694+
return false
695+
}
703696
return true
704697
}
705698

@@ -1037,12 +1030,10 @@ extension Parser.Lookahead {
10371030
/// - Note: This function must be kept in sync with `parseStatement()`.
10381031
/// - Seealso: ``Parser/parseStatement()``
10391032
mutating func atStartOfStatement(preferExpr: Bool) -> Bool {
1040-
if (self.at(anyIn: SwitchCaseStart.self) != nil || self.at(.atSign))
1041-
&& withLookahead({ $0.atStartOfSwitchCaseItem() })
1042-
{
1043-
// We consider SwitchCaseItems statements so we don't parse the start of a new case item as trailing parts of an expression.
1044-
return true
1045-
}
1033+
// if self.atStartOfSwitchCase() {
1034+
// // We consider 'case' statements so we don't parse the start of a new case item as trailing parts of an expression.
1035+
// return true
1036+
// }
10461037

10471038
_ = self.consume(if: .identifier, followedBy: .colon)
10481039
switch self.at(anyIn: CanBeStatementStart.self)?.0 {
@@ -1095,7 +1086,7 @@ extension Parser.Lookahead {
10951086
// Check for and consume attributes. The only valid attribute is `@unknown`
10961087
// but that's a semantic restriction.
10971088
var lookahead = self.lookahead()
1098-
1089+
10991090
let hasAttribute = lookahead.consumeAttributeList()
11001091
if hasAttribute && lookahead.at(.rightBrace) {
11011092
// If we are at an attribute that's the last token in the SwitchCase, parse

Sources/SwiftParser/TopLevel.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ extension Parser {
7474
var loopProgress = LoopProgressCondition()
7575
while !stopCondition(&self), !self.at(.endOfFile), self.hasProgressed(&loopProgress) {
7676
let newItemAtStartOfLine = self.atStartOfLine
77-
guard let newElement = self.parseCodeBlockItem(allowInitDecl: allowInitDecl, until: stopCondition) else {
77+
guard let newItem = self.parseCodeBlockItem(allowInitDecl: allowInitDecl, until: stopCondition) else {
7878
break
7979
}
8080
if let lastItem = elements.last,
81-
lastItem.semicolon == nil && !newItemAtStartOfLine && !newElement.item.is(RawUnexpectedCodeDeclSyntax.self)
81+
lastItem.semicolon == nil,
82+
!newItemAtStartOfLine,
83+
!newItem.item.is(RawUnexpectedCodeDeclSyntax.self)
8284
{
8385
elements[elements.count - 1] = RawCodeBlockItemSyntax(
8486
lastItem.unexpectedBeforeItem,
@@ -89,7 +91,7 @@ extension Parser {
8991
arena: self.arena
9092
)
9193
}
92-
elements.append(newElement)
94+
elements.append(newItem)
9395
}
9496
return .init(elements: elements, arena: self.arena)
9597
}
@@ -178,18 +180,6 @@ extension Parser {
178180
)
179181
}
180182

181-
if self.at(.keyword(.case), .keyword(.default)) {
182-
// 'case' and 'default' are invalid in code block items.
183-
// Parse them and put them in their own CodeBlockItem but as an unexpected node.
184-
let switchCase = self.parseSwitchCase()
185-
return RawCodeBlockItemSyntax(
186-
RawUnexpectedNodesSyntax([switchCase], arena: self.arena),
187-
item: .init(expr: RawMissingExprSyntax(arena: self.arena)),
188-
semicolon: nil,
189-
arena: self.arena
190-
)
191-
}
192-
193183
let item: RawCodeBlockItemSyntax.Item
194184
let attachSemi: Bool
195185
if self.at(.poundIf) && !self.withLookahead({ $0.consumeIfConfigOfAttributes() }) {
@@ -221,7 +211,16 @@ extension Parser {
221211
// expression or statement starting with an attribute.
222212
item = .decl(self.parseDeclaration())
223213
attachSemi = true
224-
214+
} else if self.withLookahead({ $0.atStartOfSwitchCase() }) {
215+
// 'case' and 'default' are invalid in code block items.
216+
// Parse them and put them in their own CodeBlockItem but as an unexpected node.
217+
let switchCase = self.parseSwitchCase()
218+
return RawCodeBlockItemSyntax(
219+
RawUnexpectedNodesSyntax([switchCase], arena: self.arena),
220+
item: .init(expr: RawMissingExprSyntax(arena: self.arena)),
221+
semicolon: nil,
222+
arena: self.arena
223+
)
225224
} else {
226225
// Otherwise, eat the unexpected tokens into an "decl".
227226
item = .decl(

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,12 +2669,14 @@ final class StatementExpressionTests: ParserTestCase {
26692669
DiagnosticSpec(
26702670
locationMarker: "1️⃣",
26712671
// FIXME: "expected attribute name after '@'".
2672-
message: "expected type in attribute", fixIts: ["insert type"]
2672+
message: "expected type in attribute",
2673+
fixIts: ["insert type"]
26732674
),
26742675
DiagnosticSpec(
26752676
locationMarker: "2️⃣",
26762677
// FIXME: "expected pattern and ':' in switch case"
2677-
message: "expected expression and ':' in switch case", fixIts: ["insert expression and ':'"]
2678+
message: "expected expression and ':' in switch case",
2679+
fixIts: ["insert expression and ':'"]
26782680
),
26792681
],
26802682
fixedSource: """

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,14 +3425,4 @@ final class RecoveryTests: ParserTestCase {
34253425
"""
34263426
)
34273427
}
3428-
3429-
func testTTT() {
3430-
assertParse(
3431-
"""
3432-
switch s {
3433-
@
3434-
}
3435-
"""
3436-
)
3437-
}
34383428
}

0 commit comments

Comments
 (0)