Skip to content

Commit d690b7b

Browse files
committed
Respect Trailing Commas Better In Collection Element Parsing
Match the C++ parser in its eagerness to process the next element after it sees a comma. This bug appears to have been masked by the rewrite of `atStartOfStatement`, which I reverted recently.
1 parent b233edb commit d690b7b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,13 @@ extension Parser {
16851685
}
16861686
}
16871687

1688+
// If we saw a comma, that's a strong indicator we have more elements
1689+
// to process. If that's not the case, we have to do some legwork to
1690+
// determine if we should bail out.
1691+
guard comma == nil || self.at(any: [.rightSquareBracket, .eof]) else {
1692+
continue
1693+
}
1694+
16881695
// If we found EOF or the closing square bracket, bailout.
16891696
if self.at(any: [.rightSquareBracket, .eof]) {
16901697
break

Tests/SwiftParserTest/Expressions.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,36 @@ final class ExpressionTests: XCTestCase {
189189
DiagnosticSpec(message: "expected ']' to end dictionary"),
190190
]
191191
)
192+
193+
AssertParse(
194+
"""
195+
[
196+
#line : Calendar(identifier: .gregorian),
197+
#^STRUCTURE^##line : Calendar(identifier: .buddhist),
198+
]
199+
""",
200+
substructure: Syntax(DictionaryElementSyntax.init(
201+
keyExpression: ExprSyntax(PoundLineExprSyntax(poundLine: .poundLineKeyword())),
202+
colon: .colonToken(),
203+
valueExpression: ExprSyntax(FunctionCallExprSyntax(
204+
calledExpression: ExprSyntax(IdentifierExprSyntax(identifier: .identifier("Calendar"), declNameArguments: nil)),
205+
leftParen: .leftParenToken(),
206+
argumentList: TupleExprElementListSyntax([
207+
TupleExprElementSyntax(
208+
label: .identifier("identifier"),
209+
colon: .colonToken(),
210+
expression: ExprSyntax(MemberAccessExprSyntax(
211+
base: nil,
212+
dot: .prefixPeriodToken(),
213+
name: .identifier("buddhist"),
214+
declNameArguments: nil)),
215+
trailingComma: nil)
216+
]),
217+
rightParen: .rightParenToken(),
218+
trailingClosure: nil,
219+
additionalTrailingClosures: nil)),
220+
trailingComma: .commaToken())),
221+
substructureAfterMarker: "STRUCTURE")
192222
}
193223

194224
func testInterpolatedStringLiterals() {

0 commit comments

Comments
 (0)