Skip to content

Lookahead Through Balanced Brackets In Closure Capture Lists #858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2231,11 +2231,7 @@ extension Parser.Lookahead {

// Skip by a closure capture list if present.
if lookahead.consume(if: .leftSquareBracket) != nil {
var captureListProgress = LoopProgressCondition()
while !lookahead.at(any: [.eof, .rightSquareBracket]) && captureListProgress.evaluate(lookahead.currentToken) {
lookahead.consumeAnyToken()
}

lookahead.skipUntil(.rightSquareBracket, .rightSquareBracket)
if lookahead.consume(if: .rightSquareBracket) == nil {
return false
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/SwiftParserTest/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,21 @@ final class ExpressionTests: XCTestCase {
substructureAfterMarker: "PLUS"
)
}

func testBogusCaptureLists() {
// N.B. This test ensures that capture list lookahead doesn't try to pair
// the opening square bracket from the array literal with the closing
// square bracket from the capture list.
AssertParse(
"""
{
[
AboutItem(title: TextContent.legalAndMore, accessoryType: .disclosureIndicator, action: { [weak self] context in
self?.tracker.buttonPressed(.legal)
context.showSubmenu(title: TextContent.legalAndMore, configuration: LegalAndMoreSubmenuConfiguration())
}),
]
}()
""")
}
}