Skip to content

Commit 62b7752

Browse files
committed
cleanup
1 parent 2ca55fe commit 62b7752

File tree

9 files changed

+28
-54
lines changed

9 files changed

+28
-54
lines changed

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ public let COMMON_NODES: [Node] = [
409409
children: [
410410
Child(
411411
name: "unexpectedCode",
412+
// NOTE: This is not .collection() on purpose. We don't need collection related functions for this.
412413
kind: .node(kind: .unexpectedNodes)
413414
)
414415
]

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.self.parseCodeBlockItem(allowInitDecl: true, until: { _ in false }) else {
71+
guard let node = 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(

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,12 @@ class ValidateSyntaxNodes: XCTestCase {
641641

642642
assertFailuresMatchXFails(
643643
failures,
644-
expectedFailures: []
644+
expectedFailures: [
645+
ValidationFailure(
646+
node: .unexpectedCodeDecl,
647+
message: "child 'unexpectedCode' is a SyntaxCollection but child is not marked as a collection"
648+
),
649+
]
645650
)
646651
}
647652

Sources/SwiftParser/Attributes.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ extension Parser {
3333
//
3434
// In such cases, the second `#if` is not `consumeIfConfigOfAttributes()`.
3535
return .ifConfigDecl(
36-
self.parsePoundIfDirective { parser in
37-
return .attributes(parser.parseAttributeList())
38-
}
36+
self.parsePoundIfDirective({ .attributes($0.parseAttributeList()) })
3937
)
4038
} else {
4139
return .attribute(self.parseAttribute())
@@ -896,6 +894,8 @@ extension Parser {
896894

897895
let decl: RawDeclSyntax
898896
if self.at(.poundIf) {
897+
// '#if' is not accepted in '@abi' attribute, but for recovery, parse it
898+
// parse it and wrap the first decl init with unexpected nodes.
899899
let ifConfig = self.parsePoundIfDirective({ parser in
900900
let decl = parser.parseDeclaration(in: .argumentList)
901901
let member = RawMemberBlockItemSyntax(decl: decl, semicolon: nil, arena: parser.arena)

Sources/SwiftParser/Declarations.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ extension TokenConsumer {
5252
}
5353
}
5454

55+
/// Check if the current token is at a start of any declaration.
56+
///
57+
///
58+
/// - Parameters
59+
/// - allowInitDecl: whether to consider 'init' a declaration in the context.
60+
/// Only initializer bodies should use `false` for this.
61+
/// - requiresDecl: Whether only declarations are expected in the context.
62+
/// For example, in member blocks.
63+
///
64+
/// - Note: this returns `false` for `#if` unless it's an attribute list.
5565
mutating func atStartOfDeclaration(
5666
allowInitDecl: Bool = true,
5767
requiresDecl: Bool = false
@@ -111,7 +121,7 @@ extension TokenConsumer {
111121
var lookahead = subparser.lookahead()
112122
repeat {
113123
lookahead.consumeAnyToken()
114-
} while lookahead.atStartOfDeclaration(allowInitDecl: allowInitDecl)
124+
} while lookahead.atStartOfDeclaration(allowInitDecl: allowInitDecl, requiresDecl: requiresDecl)
115125
return lookahead.at(.identifier)
116126
case .lhs(.case):
117127
// When 'case' appears inside a function, it's probably a switch
@@ -179,7 +189,7 @@ extension TokenConsumer {
179189
return true
180190
}
181191
}
182-
// Special recovery 'try let/var'.
192+
// Special recovery for 'try let/var'.
183193
if subparser.at(.keyword(.try)),
184194
subparser.peek(isAtAnyIn: VariableDeclSyntax.BindingSpecifierOptions.self) != nil
185195
{
@@ -199,10 +209,6 @@ extension Parser {
199209
self.attributes = attributes
200210
self.modifiers = modifiers
201211
}
202-
203-
var isEmpty: Bool {
204-
attributes.isEmpty && modifiers.isEmpty
205-
}
206212
}
207213

208214
/// Describes the context around a declaration in order to modify how it is parsed.
@@ -1010,7 +1016,7 @@ extension Parser {
10101016
var elements = [RawMemberBlockItemSyntax]()
10111017
do {
10121018
var loopProgress = LoopProgressCondition()
1013-
while !self.at(.endOfFile) && !stopCondition(&self) && self.hasProgressed(&loopProgress) {
1019+
while !stopCondition(&self), !self.at(.endOfFile), self.hasProgressed(&loopProgress) {
10141020
let newItemAtStartOfLine = self.atStartOfLine
10151021
guard let newElement = self.parseMemberBlockItem(until: stopCondition) else {
10161022
break

Sources/SwiftParser/Directives.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -180,33 +180,6 @@ extension Parser {
180180
// `#elif` or `#elif(…)` could be macro invocations.
181181
return lookahead.at(TokenSpec(.identifier, allowAtStartOfLine: false))
182182
}
183-
184-
private mutating func parseIfConfigClauseElements<Element: RawSyntaxNodeProtocol>(
185-
_ parseElement: (_ parser: inout Parser, _ isFirstElement: Bool) -> Element?,
186-
addSemicolonIfNeeded: (
187-
_ lastElement: Element, _ newItemAtStartOfLine: Bool, _ newItem: Element, _ parser: inout Parser
188-
) -> Element?
189-
) -> [Element] {
190-
var elements = [Element]()
191-
var elementsProgress = LoopProgressCondition()
192-
while !self.at(.endOfFile)
193-
&& !self.at(.poundElse, .poundElseif, .poundEndif)
194-
&& !self.atElifTypo()
195-
&& self.hasProgressed(&elementsProgress)
196-
{
197-
let newItemAtStartOfLine = self.atStartOfLine
198-
guard let element = parseElement(&self, elements.isEmpty), !element.isEmpty else {
199-
break
200-
}
201-
if let lastElement = elements.last,
202-
let fixedUpLastItem = addSemicolonIfNeeded(lastElement, newItemAtStartOfLine, element, &self)
203-
{
204-
elements[elements.count - 1] = fixedUpLastItem
205-
}
206-
elements.append(element)
207-
}
208-
return elements
209-
}
210183
}
211184

212185
extension Parser {

Sources/SwiftParser/Expressions.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ extension Parser {
23322332
var elements = [RawSwitchCaseListSyntax.Element]()
23332333
var elementsProgress = LoopProgressCondition()
23342334
while !self.at(.endOfFile, .rightBrace), !self.atEndOfIfConfigClauseBody(), self.hasProgressed(&elementsProgress) {
2335-
if self.withLookahead({ $0.atStartOfSwitchCase(allowRecovery: false) }) {
2335+
if self.withLookahead({ $0.atStartOfSwitchCase() }) {
23362336
elements.append(.switchCase(self.parseSwitchCase()))
23372337
} else if self.canRecoverTo(.poundIf) != nil {
23382338
// '#if' in 'case' position can enclose zero or more 'case' or 'default'
@@ -2392,13 +2392,6 @@ extension Parser {
23922392
if $0.at(.rightBrace, .keyword(.case), .keyword(.default)) || $0.atEndOfIfConfigClauseBody() {
23932393
return true
23942394
}
2395-
if $0.at(.atSign),
2396-
$0.withLookahead({
2397-
$0.consumeAnyAttribute(); return $0.at(.keyword(.case), .keyword(.default))
2398-
})
2399-
{
2400-
return true
2401-
}
24022395
if $0.withLookahead({ $0.atStartOfConditionalSwitchCases() }) {
24032396
return true
24042397
}

Sources/SwiftParser/Statements.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ extension Parser.Lookahead {
10771077

10781078
/// Returns whether the parser's current position is the start of a switch case,
10791079
/// given that we're in the middle of a switch already.
1080-
mutating func atStartOfSwitchCase(allowRecovery: Bool = false) -> Bool {
1080+
mutating func atStartOfSwitchCase() -> Bool {
10811081
// Check for and consume attributes. The only valid attribute is `@unknown`
10821082
// but that's a semantic restriction.
10831083
var lookahead = self.lookahead()
@@ -1092,11 +1092,7 @@ extension Parser.Lookahead {
10921092
return true
10931093
}
10941094

1095-
if allowRecovery {
1096-
return lookahead.canRecoverTo(anyIn: SwitchCaseStart.self) != nil
1097-
} else {
1098-
return lookahead.at(anyIn: SwitchCaseStart.self) != nil
1099-
}
1095+
return lookahead.at(anyIn: SwitchCaseStart.self) != nil
11001096
}
11011097

11021098
mutating func atStartOfConditionalSwitchCases() -> Bool {

Sources/SwiftParser/generated/LayoutNodes+Parsable.swift

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)