Skip to content

Commit 2ca55fe

Browse files
committed
tbs
1 parent 6268c80 commit 2ca55fe

File tree

8 files changed

+60
-14
lines changed

8 files changed

+60
-14
lines changed

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public let COMMON_NODES: [Node] = [
177177
kind: .decl,
178178
base: .syntax,
179179
nameForDiagnostics: "declaration",
180-
parserFunction: "parseDeclaration"
180+
parserFunction: "parseDeclarationOrIfConfig"
181181
),
182182

183183
Node(

CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/LayoutNodesParsableFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let layoutNodesParsableFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
6868
DeclSyntax(
6969
"""
7070
mutating func parseNonOptionalCodeBlockItem() -> RawCodeBlockItemSyntax {
71-
guard let node = self.parseCodeBlockItem(isAtTopLevel: false, allowInitDecl: true) else {
71+
guard let node = self.self.parseCodeBlockItem(allowInitDecl: true, until: { _ in false }) else {
7272
// The missing item is not necessary to be a declaration,
7373
// which is just a placeholder here
7474
return RawCodeBlockItemSyntax(

Sources/SwiftParser/Declarations.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,3 +2368,17 @@ extension Parser {
23682368
)
23692369
}
23702370
}
2371+
2372+
extension Parser {
2373+
mutating func parseDeclarationOrIfConfig() -> RawDeclSyntax {
2374+
if self.at(.poundIf) {
2375+
return RawDeclSyntax(
2376+
self.parsePoundIfDirective({
2377+
.decls($0.parseMemberDeclList(until: { $0.atEndOfIfConfigClauseBody() }))
2378+
})
2379+
)
2380+
} else {
2381+
return parseDeclaration(in: .memberDeclList)
2382+
}
2383+
}
2384+
}

Sources/SwiftParser/Statements.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,11 +1030,6 @@ extension Parser.Lookahead {
10301030
/// - Note: This function must be kept in sync with `parseStatement()`.
10311031
/// - Seealso: ``Parser/parseStatement()``
10321032
mutating func atStartOfStatement(preferExpr: Bool) -> Bool {
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-
// }
1037-
10381033
_ = self.consume(if: .identifier, followedBy: .colon)
10391034
switch self.at(anyIn: CanBeStatementStart.self)?.0 {
10401035
case .return?,

Sources/SwiftParser/generated/LayoutNodes+Parsable.swift

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesTUVWXYZ.swift

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/SwiftParserTest/DirectiveTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ final class DirectiveTests: ParserTestCase {
471471
locationMarker: "1️⃣",
472472
message: "expected '}' to end struct",
473473
notes: [NoteSpec(message: "to match this opening '{'")],
474-
fixIts: ["insert '}'"],
474+
fixIts: ["insert '}'"]
475475
),
476476
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code in source file"),
477477
],
@@ -496,7 +496,7 @@ final class DirectiveTests: ParserTestCase {
496496
locationMarker: "1️⃣",
497497
message: "expected '}' to end function",
498498
notes: [NoteSpec(message: "to match this opening '{'")],
499-
fixIts: ["insert '}'"],
499+
fixIts: ["insert '}'"]
500500
),
501501
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code in source file"),
502502
],
@@ -521,7 +521,7 @@ final class DirectiveTests: ParserTestCase {
521521
locationMarker: "1️⃣",
522522
message: "expected '}' to end 'switch' statement",
523523
notes: [NoteSpec(message: "to match this opening '{'")],
524-
fixIts: ["insert '}'"],
524+
fixIts: ["insert '}'"]
525525
),
526526
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code in source file"),
527527
],
@@ -548,7 +548,7 @@ final class DirectiveTests: ParserTestCase {
548548
locationMarker: "1️⃣",
549549
message: "expected '}' to end 'switch' statement",
550550
notes: [NoteSpec(message: "to match this opening '{'")],
551-
fixIts: ["insert '}'"],
551+
fixIts: ["insert '}'"]
552552
),
553553
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code in source file"),
554554
],
@@ -597,7 +597,7 @@ final class DirectiveTests: ParserTestCase {
597597
locationMarker: "1️⃣",
598598
message: "expected '}' to end function",
599599
notes: [NoteSpec(message: "to match this opening '{'")],
600-
fixIts: ["insert '}'"],
600+
fixIts: ["insert '}'"]
601601
),
602602
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected brace in source file"),
603603
],

Tests/SwiftParserTest/Parser+EntryTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,39 @@ class EntryTests: ParserTestCase {
3737
)
3838
}
3939

40+
func testDeclSyntaxParseIfConfig() throws {
41+
assertParse(
42+
"""
43+
#if FLAG
44+
func test() {}
45+
#endif
46+
""",
47+
{ DeclSyntax.parse(from: &$0) },
48+
substructure: IfConfigDeclSyntax(
49+
clauses: IfConfigClauseListSyntax([
50+
IfConfigClauseSyntax(
51+
poundKeyword: .poundIfToken(),
52+
condition: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("FLAG"))),
53+
elements: .init([
54+
MemberBlockItemSyntax(
55+
decl: DeclSyntax(
56+
FunctionDeclSyntax(
57+
funcKeyword: .keyword(.func),
58+
name: .identifier("test"),
59+
signature: FunctionSignatureSyntax(
60+
parameterClause: FunctionParameterClauseSyntax(parameters: [])
61+
),
62+
body: CodeBlockSyntax(statements: [])
63+
)
64+
)
65+
)
66+
])
67+
)
68+
])
69+
)
70+
)
71+
}
72+
4073
func testRemainderUnexpected() throws {
4174
assertParse(
4275
"func test() {} 1️⃣other tokens",

0 commit comments

Comments
 (0)