Skip to content

Commit b1c437d

Browse files
authored
Merge pull request #872 from CodaFi/bond-comma-james
Respect Trailing Commas Better In Collection Element Parsing
2 parents 6944b43 + d690b7b commit b1c437d

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
@@ -1787,6 +1787,13 @@ extension Parser {
17871787
}
17881788
}
17891789

1790+
// If we saw a comma, that's a strong indicator we have more elements
1791+
// to process. If that's not the case, we have to do some legwork to
1792+
// determine if we should bail out.
1793+
guard comma == nil || self.at(any: [.rightSquareBracket, .eof]) else {
1794+
continue
1795+
}
1796+
17901797
// If we found EOF or the closing square bracket, bailout.
17911798
if self.at(any: [.rightSquareBracket, .eof]) {
17921799
break

Tests/SwiftParserTest/Expressions.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,36 @@ final class ExpressionTests: XCTestCase {
249249
DiagnosticSpec(message: "expected ']' to end dictionary"),
250250
]
251251
)
252+
253+
AssertParse(
254+
"""
255+
[
256+
#line : Calendar(identifier: .gregorian),
257+
#^STRUCTURE^##line : Calendar(identifier: .buddhist),
258+
]
259+
""",
260+
substructure: Syntax(DictionaryElementSyntax.init(
261+
keyExpression: ExprSyntax(PoundLineExprSyntax(poundLine: .poundLineKeyword())),
262+
colon: .colonToken(),
263+
valueExpression: ExprSyntax(FunctionCallExprSyntax(
264+
calledExpression: ExprSyntax(IdentifierExprSyntax(identifier: .identifier("Calendar"), declNameArguments: nil)),
265+
leftParen: .leftParenToken(),
266+
argumentList: TupleExprElementListSyntax([
267+
TupleExprElementSyntax(
268+
label: .identifier("identifier"),
269+
colon: .colonToken(),
270+
expression: ExprSyntax(MemberAccessExprSyntax(
271+
base: nil,
272+
dot: .prefixPeriodToken(),
273+
name: .identifier("buddhist"),
274+
declNameArguments: nil)),
275+
trailingComma: nil)
276+
]),
277+
rightParen: .rightParenToken(),
278+
trailingClosure: nil,
279+
additionalTrailingClosures: nil)),
280+
trailingComma: .commaToken())),
281+
substructureAfterMarker: "STRUCTURE")
252282
}
253283

254284
func testInterpolatedStringLiterals() {

0 commit comments

Comments
 (0)