Skip to content

Commit 4025e68

Browse files
committed
Lookahead Through Balanced Brackets In Closure Capture Lists
Lookahead here was trying to eat its way to the nearest right bracket rather than the nearest *balanced* right bracket. rdar://100491747
1 parent 6068fc4 commit 4025e68

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,11 +2231,7 @@ extension Parser.Lookahead {
22312231

22322232
// Skip by a closure capture list if present.
22332233
if lookahead.consume(if: .leftSquareBracket) != nil {
2234-
var captureListProgress = LoopProgressCondition()
2235-
while !lookahead.at(any: [.eof, .rightSquareBracket]) && captureListProgress.evaluate(lookahead.currentToken) {
2236-
lookahead.consumeAnyToken()
2237-
}
2238-
2234+
lookahead.skipUntil(.rightSquareBracket, .rightSquareBracket)
22392235
if lookahead.consume(if: .rightSquareBracket) == nil {
22402236
return false
22412237
}

Tests/SwiftParserTest/Expressions.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,4 +593,21 @@ final class ExpressionTests: XCTestCase {
593593
substructureAfterMarker: "PLUS"
594594
)
595595
}
596+
597+
func testBogusCaptureLists() {
598+
// N.B. This test ensures that capture list lookahead doesn't try to pair
599+
// the opening square bracket from the array literal with the closing
600+
// square bracket from the capture list.
601+
AssertParse(
602+
"""
603+
{
604+
[
605+
AboutItem(title: TextContent.legalAndMore, accessoryType: .disclosureIndicator, action: { [weak self] context in
606+
self?.tracker.buttonPressed(.legal)
607+
context.showSubmenu(title: TextContent.legalAndMore, configuration: LegalAndMoreSubmenuConfiguration())
608+
}),
609+
]
610+
}()
611+
""")
612+
}
596613
}

0 commit comments

Comments
 (0)