Skip to content

Improve diagnostic generated for string literal consisting of four quotes #670

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

/// Parse open quote.
let openQuote = self.parseStringLiteralQuote(
at: openDelimiter != nil ? .leadingRaw : .leading, text: text)
at: openDelimiter != nil ? .leadingRaw : .leading,
text: text
) ?? RawTokenSyntax(missing: .stringQuote, arena: arena)
text = text.dropFirst(openQuote.tokenText.count)

/// Parse segments.
Expand All @@ -915,7 +917,8 @@ extension Parser {
/// Parse close quote.
let closeQuote = self.parseStringLiteralQuote(
at: openDelimiter != nil ? .trailingRaw : .trailing,
text: text[closeStart...])
text: text[closeStart...]
) ?? RawTokenSyntax(missing: openQuote.tokenKind, arena: arena)
text = text.dropFirst(closeQuote.byteLength)

/// Parse closing raw string delimiter if exist.
Expand Down Expand Up @@ -1026,7 +1029,7 @@ extension Parser {
mutating func parseStringLiteralQuote(
at position: QuotePosition,
text: Slice<SyntaxText>
) -> RawTokenSyntax {
) -> RawTokenSyntax? {
// Single quote. We only support single line literal.
if let first = text.first, first == UInt8(ascii: "'") {
let index = text.index(after: text.startIndex)
Expand Down Expand Up @@ -1061,6 +1064,9 @@ extension Parser {
if position == .leadingRaw {
quoteCount = 1
index = text.index(text.startIndex, offsetBy: quoteCount)
} else if position == .leading {
quoteCount = 3
index = text.index(text.startIndex, offsetBy: quoteCount)
}
}

Expand All @@ -1075,7 +1081,7 @@ extension Parser {
.multilineStringQuote, text: text[..<index], at: position)
}
// Otherwise, this is not a literal quote.
return RawTokenSyntax(missing: .stringQuote, arena: arena)
return nil
}

/// Foo.
Expand Down
25 changes: 21 additions & 4 deletions Tests/SwiftParserTest/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,31 @@ final class ExpressionTests: XCTestCase {

AssertParse(
##"""
#^D0^#""""#^D1^#
""""#^DIAG^#
"""##,
diagnostics: [
// FIXME: Weird diagnostics doesn't recover code even if follow it
DiagnosticSpec(locationMarker: "D0", message: "Expected '\"' in expression"),
DiagnosticSpec(locationMarker: "D1", message: "Expected '\"' in expression")
// FIXME: This should say: Expected '"""' in string literal
DiagnosticSpec(message: #"Expected '"""' in expression"#)
]
)

AssertParse(
##"""
"""""#^DIAG^#
"""##,
diagnostics: [
// FIXME: This should say: Expected '"""' in string literal
DiagnosticSpec(message: #"Expected '"""' in expression"#)
]
)

// FIXME: We currently don't enforce that multiline string literal
// contents must start on a new line
AssertParse(
##"""
""""""#^DIAG^#
"""##
)
}

func testSingleQuoteStringLiteral() {
Expand Down