Skip to content
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
31 changes: 7 additions & 24 deletions Sources/SwiftParser/StringLiterals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,22 +340,6 @@ extension Parser {
unexpectedBeforeClosingQuote: [RawTokenSyntax],
closingQuote: RawTokenSyntax
) {
// -------------------------------------------------------------------------
// Precondition

precondition(
allSegments.allSatisfy {
if case .stringSegment(let segment) = $0 {
return segment.unexpectedBeforeContent == nil
&& segment.unexpectedAfterContent == nil
&& segment.content.leadingTriviaByteLength == 0
} else {
return true
}
},
"String segment produced by the lexer should not have unexpected text or trivia because we would drop it during post-processing"
)

// -------------------------------------------------------------------------
// Variables

Expand Down Expand Up @@ -395,6 +379,9 @@ extension Parser {
// Parse indentation of the closing quote

if let lastSegment,
lastSegment.unexpectedBeforeContent == nil,
lastSegment.unexpectedAfterContent == nil,
lastSegment.content.leadingTriviaByteLength == 0,
let parsedTrivia = parseIndentationTrivia(text: lastSegment.content.tokenText)
{
indentationTrivia = parsedTrivia
Expand All @@ -409,10 +396,9 @@ extension Parser {
arena: self.arena
)
} else {
if let lastSegment = lastSegment {
indentationTrivia = TriviaParser.parseTrivia(lastSegment.content.tokenText, position: .leading).prefix(while: {
$0.isIndentationWhitespace
})
if let lastSegment {
indentationTrivia = TriviaParser.parseTrivia(lastSegment.content.tokenText, position: .leading)
.prefix(while: \.isIndentationWhitespace)
let indentationByteLength = indentationTrivia.reduce(0, { $0 + $1.byteLength })
indentation = SyntaxText(rebasing: lastSegment.content.tokenText[0..<indentationByteLength])
middleSegments.append(.stringSegment(lastSegment))
Expand Down Expand Up @@ -703,10 +689,7 @@ extension Parser {
}
}

let (unexpectedBetweenSegmentAndCloseQuote, closeQuote) = self.expect(
anyIn: SimpleStringLiteralExprSyntax.ClosingQuoteOptions.self,
default: openQuote.closeTokenKind
)
let (unexpectedBetweenSegmentAndCloseQuote, closeQuote) = self.expect(openQuote.closeTokenKind.spec)
let closeDelimiter = self.consume(if: .rawStringPoundDelimiter)

if openQuote.tokenKind == .multilineStringQuote, !openQuote.isMissing, !closeQuote.isMissing {
Expand Down
33 changes: 33 additions & 0 deletions Tests/SwiftParserTest/translated/StringLiteralEofTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,37 @@ final class StringLiteralEofTests: ParserTestCase {
"""##
)
}

func testSimpleMultilineStringLiteralWith() {
assertParse(
#"""
#sourceLocation1️⃣(file: 2️⃣"""3️⃣\(4️⃣"5️⃣
"""#,
diagnostics: [
DiagnosticSpec(locationMarker: "3️⃣", message: "argument cannot be an interpolated string literal"),
DiagnosticSpec(
locationMarker: "4️⃣",
message: #"expected '"""' to end simple string literal"#,
notes: [NoteSpec(locationMarker: "2️⃣", message: #"to match this opening '"""'"#)],
fixIts: [#"insert '"""'"#]
),
DiagnosticSpec(
locationMarker: "4️⃣",
message: "expected ', line:' and line number in '#sourceLocation' arguments",
fixIts: ["insert ', line:' and line number"]
),
DiagnosticSpec(
locationMarker: "4️⃣",
message: "expected ')' in '#sourceLocation' directive",
notes: [NoteSpec(locationMarker: "1️⃣", message: "to match this opening '('")],
fixIts: ["insert ')'"]
),
DiagnosticSpec(locationMarker: "4️⃣", message: "extra tokens following the #sourceLocation directive"),
],
fixedSource: #"""
#sourceLocation(file: """\(
""", line: <#integer literal#>)"
"""#
)
}
}
Loading