Skip to content

Fix parsing of key paths that involve .<integer literal>. #840

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
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
10 changes: 6 additions & 4 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,12 @@ extension Parser {

// Check for a .foo suffix.
if self.at(any: [.period, .prefixPeriod]) {
// A key path is special, because it allows .[, unlike anywhere else. The
// period itself should be left in the token stream. (.? and .! end up
// being operators, and so aren't handled here.)
if periodHasKeyPathBehavior && self.peek().tokenKind == .leftSquareBracket {
// A key path is special, because it allows .[ and .<N>, unlike
// anywhere else. The period itself should be left in the token stream.
// (.? and .! end up being operators, and so aren't handled here.)
if periodHasKeyPathBehavior &&
(self.peek().tokenKind == .leftSquareBracket ||
self.peek().tokenKind == .integerLiteral) {
break
}

Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftParserTest/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ final class ExpressionTests: XCTestCase {
#"""
_ = \Lens<[Int]>.[0]
"""#)

AssertParse(
#"""
\(UnsafeRawPointer?, String).1
"""#
)
}

func testBasicLiterals() {
Expand Down