Skip to content

Commit c072938

Browse files
committed
tbs
1 parent 3f2abd2 commit c072938

File tree

6 files changed

+106
-75
lines changed

6 files changed

+106
-75
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -902,15 +902,19 @@ extension Parser {
902902
return .decls(RawMemberBlockItemListSyntax(elements: [member], arena: parser.arena))
903903
})
904904
decl = ifConfig.makeUnexpectedKeepingFirstNode(
905-
of: RawDeclSyntax.self, arena: self.arena,
905+
of: RawDeclSyntax.self,
906+
arena: self.arena,
906907
where: { !$0.is(RawIfConfigDeclSyntax.self) },
907908
makeMissing: {
908-
RawDeclSyntax(RawMissingDeclSyntax(
909-
attributes: self.emptyCollection(RawAttributeListSyntax.self),
910-
modifiers: self.emptyCollection(RawDeclModifierListSyntax.self),
911-
arena: self.arena)
909+
RawDeclSyntax(
910+
RawMissingDeclSyntax(
911+
attributes: self.emptyCollection(RawAttributeListSyntax.self),
912+
modifiers: self.emptyCollection(RawDeclModifierListSyntax.self),
913+
arena: self.arena
914+
)
912915
)
913-
})
916+
}
917+
)
914918
} else {
915919
decl = self.parseDeclaration(in: .argumentList)
916920
}

Sources/SwiftParser/Declarations.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,11 @@ extension Parser {
956956
decl = RawDeclSyntax(self.parsePoundSourceLocationDirective())
957957
attachSemi = false
958958
} else if self.at(.poundIf) && !self.withLookahead({ $0.consumeIfConfigOfAttributes() }) {
959-
decl = RawDeclSyntax(self.parsePoundIfDirective { parser in
960-
return .decls(parser.parseMemberDeclList(until: {$0.atEndOfIfConfigClauseBody()}))
961-
})
959+
decl = RawDeclSyntax(
960+
self.parsePoundIfDirective { parser in
961+
return .decls(parser.parseMemberDeclList(until: { $0.atEndOfIfConfigClauseBody() }))
962+
}
963+
)
962964
attachSemi = false
963965
} else if self.atStartOfDeclaration(allowInitDecl: true, requiresDecl: true) {
964966
decl = self.parseDeclaration(in: .memberDeclList)
@@ -1006,7 +1008,9 @@ extension Parser {
10061008
return result
10071009
}
10081010

1009-
mutating func parseMemberDeclList(until stopCondition: (inout Parser) -> Bool = { $0.at(.rightBrace) }) -> RawMemberBlockItemListSyntax {
1011+
mutating func parseMemberDeclList(
1012+
until stopCondition: (inout Parser) -> Bool = { $0.at(.rightBrace) }
1013+
) -> RawMemberBlockItemListSyntax {
10101014
var elements = [RawMemberBlockItemSyntax]()
10111015
do {
10121016
var loopProgress = LoopProgressCondition()
@@ -1038,15 +1042,7 @@ extension Parser {
10381042
/// If the left brace is missing, its indentation will be used to judge whether a following `}` was
10391043
/// indented to close this code block or a surrounding context. See `expectRightBrace`.
10401044
mutating func parseMemberBlock(introducer: RawTokenSyntax? = nil) -> RawMemberBlockSyntax {
1041-
guard let lBraceHandle = canRecoverTo(.leftBrace) else {
1042-
return RawMemberBlockSyntax(
1043-
leftBrace: self.missingToken(.leftBrace),
1044-
members: RawMemberBlockItemListSyntax(elements: [], arena: self.arena),
1045-
rightBrace: consume(if: TokenSpec(.rightBrace, allowAtStartOfLine: false)) ?? self.missingToken(.rightBrace),
1046-
arena: arena
1047-
)
1048-
}
1049-
let (unexpectedBeforeLBrace, lbrace) = self.eat(lBraceHandle)
1045+
let (unexpectedBeforeLBrace, lbrace) = self.expect(.leftBrace)
10501046
let members = parseMemberDeclList()
10511047
let (unexpectedBeforeRBrace, rbrace) = self.expectRightBrace(leftBrace: lbrace, introducer: introducer)
10521048

@@ -2341,7 +2337,9 @@ extension Parser {
23412337
) -> RawUnexpectedCodeDeclSyntax {
23422338
var unexpectedTokens = [RawSyntax]()
23432339
while !self.at(.endOfFile, .semicolon) && !stopCondition(&self) {
2344-
let numTokensToSkip = self.withLookahead({ $0.skipSingle(); return $0.tokensConsumed })
2340+
let numTokensToSkip = self.withLookahead({
2341+
$0.skipSingle(); return $0.tokensConsumed
2342+
})
23452343
for _ in 0..<numTokensToSkip {
23462344
unexpectedTokens.append(RawSyntax(self.consumeAnyTokenWithoutAdjustingNestingLevel()))
23472345
}

Sources/SwiftParser/TopLevel.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension Parser {
7878
break
7979
}
8080
if let lastItem = elements.last,
81-
lastItem.semicolon == nil && !newItemAtStartOfLine && !newElement.item.is(RawUnexpectedCodeDeclSyntax.self)
81+
lastItem.semicolon == nil && !newItemAtStartOfLine && !newElement.item.is(RawUnexpectedCodeDeclSyntax.self)
8282
{
8383
elements[elements.count - 1] = RawCodeBlockItemSyntax(
8484
lastItem.unexpectedBeforeItem,
@@ -159,7 +159,10 @@ extension Parser {
159159
/// closing braces while trying to recover to the next item.
160160
/// If we are not at the top level, such a closing brace should close the
161161
/// wrapping declaration instead of being consumed by lookahead.
162-
mutating func parseCodeBlockItem(allowInitDecl: Bool, until stopCondition: (inout Parser) -> Bool) -> RawCodeBlockItemSyntax? {
162+
mutating func parseCodeBlockItem(
163+
allowInitDecl: Bool,
164+
until stopCondition: (inout Parser) -> Bool
165+
) -> RawCodeBlockItemSyntax? {
163166
let startToken = self.currentToken
164167
if let syntax = self.loadCurrentSyntaxNodeFromCache(for: .codeBlockItem) {
165168
self.registerNodeForIncrementalParse(node: syntax.raw, startToken: startToken)
@@ -174,7 +177,7 @@ extension Parser {
174177
arena: self.arena
175178
)
176179
}
177-
180+
178181
if self.at(.keyword(.case), .keyword(.default)) {
179182
// 'case' and 'default' are invalid in code block items.
180183
// Parse them and put them in their own CodeBlockItem but as an unexpected node.
@@ -193,7 +196,10 @@ extension Parser {
193196
// If config of attributes is parsed as part of declaration parsing as it
194197
// doesn't constitute its own code block item.
195198
let directive = self.parsePoundIfDirective { parser in
196-
let items = parser.parseCodeBlockItemList(allowInitDecl: allowInitDecl, until: { $0.atEndOfIfConfigClauseBody() })
199+
let items = parser.parseCodeBlockItemList(
200+
allowInitDecl: allowInitDecl,
201+
until: { $0.atEndOfIfConfigClauseBody() }
202+
)
197203
return .statements(items)
198204
}
199205
item = .init(decl: directive)
@@ -215,7 +221,7 @@ extension Parser {
215221
// expression or statement starting with an attribute.
216222
item = .decl(self.parseDeclaration())
217223
attachSemi = true
218-
224+
219225
} else {
220226
item = .decl(
221227
RawDeclSyntax(
@@ -265,7 +271,7 @@ extension Parser {
265271
// or 'switch' as an expression when in statement position, but that
266272
// could result in less useful recovery behavior.
267273
if at(.keyword(.as)),
268-
let expr = stmt.as(RawExpressionStmtSyntax.self)?.expression
274+
let expr = stmt.as(RawExpressionStmtSyntax.self)?.expression
269275
{
270276
if expr.is(RawDoExprSyntax.self) || expr.is(RawIfExprSyntax.self) || expr.is(RawSwitchExprSyntax.self) {
271277
let (op, rhs) = parseUnresolvedAsExpr(

Tests/SwiftParserTest/AttributeTests.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -885,18 +885,28 @@ final class AttributeTests: ParserTestCase {
885885
),
886886
DiagnosticSpec(
887887
locationMarker: "2️⃣",
888-
message: "expected identifier and member block in class",
889-
fixIts: ["insert identifier and member block"]
888+
message: "expected identifier in class",
889+
fixIts: ["insert identifier"]
890890
),
891891
DiagnosticSpec(
892892
locationMarker: "2️⃣",
893-
message: "unexpected code '))' in source file"
893+
message: "expected '{' in class",
894+
fixIts: ["insert '{'"]
895+
),
896+
DiagnosticSpec(
897+
locationMarker: "2️⃣",
898+
message: "unexpected code '))' in class"
899+
),
900+
DiagnosticSpec(
901+
locationMarker: "3️⃣",
902+
message: "expected '}' to end class",
903+
fixIts: ["insert '}'"]
894904
),
895905
],
896906
fixedSource: """
897-
@attached(member, names: named(<#expression#>)) class <#identifier#> {
898-
}))
907+
@attached(member, names: named(<#expression#>)) class <#identifier#> {))
899908
macro m()
909+
}
900910
"""
901911
)
902912
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,8 +1651,8 @@ final class DeclarationTests: ParserTestCase {
16511651
diagnostics: [
16521652
DiagnosticSpec(
16531653
locationMarker: "1️⃣",
1654-
message: "expected member block in struct",
1655-
fixIts: ["insert member block"]
1654+
message: "expected '{' in struct",
1655+
fixIts: ["insert '{'"]
16561656
),
16571657
DiagnosticSpec(
16581658
locationMarker: "2️⃣",
@@ -1670,13 +1670,19 @@ final class DeclarationTests: ParserTestCase {
16701670
message: "expected '#endif' in conditional compilation block",
16711671
fixIts: ["insert '#endif'"]
16721672
),
1673+
DiagnosticSpec(
1674+
locationMarker: "3️⃣",
1675+
message: "expected '}' to end struct",
1676+
fixIts: ["insert '}'"]
1677+
),
1678+
16731679
],
16741680
fixedSource: """
16751681
struct n {
1676-
}
16771682
#if <#expression#>
16781683
@<#type#> <#declaration#>
16791684
#endif
1685+
}
16801686
"""
16811687
)
16821688
}

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -975,14 +975,16 @@ final class RecoveryTests: ParserTestCase {
975975
func testRecovery54() {
976976
assertParse(
977977
"""
978-
struct NoBracesStruct11️⃣()
978+
struct NoBracesStruct11️⃣()2️⃣
979979
""",
980980
diagnostics: [
981-
DiagnosticSpec(message: "expected member block in struct", fixIts: ["insert member block"])
981+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in struct", fixIts: ["insert '{'"]),
982+
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '()' in struct"),
983+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end struct", fixIts: ["insert '}'"]),
982984
],
983985
fixedSource: """
984-
struct NoBracesStruct1 {
985-
}()
986+
struct NoBracesStruct1 {()
987+
}
986988
"""
987989
)
988990
}
@@ -993,31 +995,31 @@ final class RecoveryTests: ParserTestCase {
993995
enum NoBracesUnion11️⃣()
994996
class NoBracesClass12️⃣()
995997
protocol NoBracesProtocol13️⃣()
996-
extension NoBracesStruct14️⃣()
998+
extension NoBracesStruct14️⃣()5️⃣
997999
""",
9981000
diagnostics: [
999-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected member block in enum", fixIts: ["insert member block"]),
1000-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected member block in class", fixIts: ["insert member block"]),
1001-
DiagnosticSpec(
1002-
locationMarker: "3️⃣",
1003-
message: "expected member block in protocol",
1004-
fixIts: ["insert member block"]
1005-
),
1006-
DiagnosticSpec(
1007-
locationMarker: "4️⃣",
1008-
message: "expected member block in extension",
1009-
fixIts: ["insert member block"]
1010-
),
1001+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in enum", fixIts: ["insert '{'"]),
1002+
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '()' in enum"),
1003+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '{' in class", fixIts: ["insert '{'"]),
1004+
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '()' in class"),
1005+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected '{' in protocol", fixIts: ["insert '{'"]),
1006+
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code '()' in protocol"),
1007+
DiagnosticSpec(locationMarker: "4️⃣", message: "expected '{' in extension", fixIts: ["insert '{'"]),
1008+
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code '()' in extension"),
1009+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected '}' to end extension", fixIts: ["insert '}'"]),
1010+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected '}' to end protocol", fixIts: ["insert '}'"]),
1011+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected '}' to end class", fixIts: ["insert '}'"]),
1012+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected '}' to end enum", fixIts: ["insert '}'"]),
10111013
],
10121014
fixedSource: """
1013-
enum NoBracesUnion1 {
1014-
}()
1015-
class NoBracesClass1 {
1016-
}()
1017-
protocol NoBracesProtocol1 {
1018-
}()
1019-
extension NoBracesStruct1 {
1020-
}()
1015+
enum NoBracesUnion1 {()
1016+
class NoBracesClass1 {()
1017+
protocol NoBracesProtocol1 {()
1018+
extension NoBracesStruct1 {()
1019+
}
1020+
}
1021+
}
1022+
}
10211023
"""
10221024
)
10231025
}
@@ -1032,35 +1034,31 @@ final class RecoveryTests: ParserTestCase {
10321034
extension NoBracesStruct25️⃣
10331035
""",
10341036
diagnostics: [
1035-
DiagnosticSpec(
1036-
locationMarker: "1️⃣",
1037-
message: "expected member block in struct",
1038-
fixIts: ["insert member block"]
1039-
),
1040-
DiagnosticSpec(locationMarker: "2️⃣", message: "expected member block in enum", fixIts: ["insert member block"]),
1041-
DiagnosticSpec(locationMarker: "3️⃣", message: "expected member block in class", fixIts: ["insert member block"]),
1042-
DiagnosticSpec(
1043-
locationMarker: "4️⃣",
1044-
message: "expected member block in protocol",
1045-
fixIts: ["insert member block"]
1046-
),
1037+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in struct", fixIts: ["insert '{'"]),
1038+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '{' in enum", fixIts: ["insert '{'"]),
1039+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected '{' in class", fixIts: ["insert '{'"]),
1040+
DiagnosticSpec(locationMarker: "4️⃣", message: "expected '{' in protocol", fixIts: ["insert '{'"]),
10471041
DiagnosticSpec(
10481042
locationMarker: "5️⃣",
10491043
message: "expected member block in extension",
10501044
fixIts: ["insert member block"]
10511045
),
1046+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected '}' to end protocol", fixIts: ["insert '}'"]),
1047+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected '}' to end class", fixIts: ["insert '}'"]),
1048+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected '}' to end enum", fixIts: ["insert '}'"]),
1049+
DiagnosticSpec(locationMarker: "5️⃣", message: "expected '}' to end struct", fixIts: ["insert '}'"]),
10521050
],
10531051
fixedSource: """
10541052
struct NoBracesStruct2 {
1055-
}
10561053
enum NoBracesUnion2 {
1057-
}
10581054
class NoBracesClass2 {
1059-
}
10601055
protocol NoBracesProtocol2 {
1061-
}
10621056
extension NoBracesStruct2 {
10631057
}
1058+
}
1059+
}
1060+
}
1061+
}
10641062
"""
10651063
)
10661064
}
@@ -3428,4 +3426,13 @@ final class RecoveryTests: ParserTestCase {
34283426
)
34293427
}
34303428

3429+
func testTTT() {
3430+
assertParse(
3431+
"""
3432+
struct S {
3433+
:
3434+
}
3435+
"""
3436+
)
3437+
}
34313438
}

0 commit comments

Comments
 (0)