Skip to content

[Lexer] Allow multiline delimiter to be escaped in raw Strings #23115

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

Closed
wants to merge 12 commits into from
Closed
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
26 changes: 23 additions & 3 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1843,9 +1843,29 @@ void Lexer::lexStringLiteral(unsigned CustomDelimiterLen) {
assert((QuoteChar == '"' || QuoteChar == '\'') && "Unexpected start");

bool IsMultilineString = advanceIfMultilineDelimiter(CurPtr, Diags);
if (IsMultilineString && *CurPtr != '\n' && *CurPtr != '\r')
diagnose(CurPtr, diag::lex_illegal_multiline_string_start)
.fixItInsert(Lexer::getSourceLoc(CurPtr), "\n");
if (IsMultilineString && *CurPtr != '\n' && *CurPtr != '\r') {
// Test for single-line string literals that may resemble multiline delimiter.
if (CustomDelimiterLen > 0) {
for (const char *Ptr = CurPtr-1; Ptr <= BufferEnd-CustomDelimiterLen; Ptr++) {
if (*Ptr == '\r' || *Ptr == '\n') {
break;
}
if (*Ptr == '"') {
const char *TmpPtr = Ptr + 1;
if (delimiterMatches(CustomDelimiterLen, TmpPtr, nullptr)) {
// Undo effects from falsely detecting multiline delimiter.
CurPtr = CurPtr - 2;
IsMultilineString = false;
break;
}
}
}
}
if (IsMultilineString) {
diagnose(CurPtr, diag::lex_illegal_multiline_string_start)
.fixItInsert(Lexer::getSourceLoc(CurPtr), "\n");
}
}

bool wasErroneous = false;
while (true) {
Expand Down
12 changes: 12 additions & 0 deletions test/Parse/raw_string.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ _ = ##"""
"""##
// CHECK: "a raw string with \"\"\" in it"

_ = #"""#
// CHECK: "\""

_ = ##""" foo # "# "##
// CHECK: "\"\" foo # \"# "

_ = #"""""#
// CHECK: "\"\"\""

_ = ###"""##"###
// CHECK: "\"\"##"

let foo = "Interpolation"
_ = #"\b\b \#(foo)\#(foo) Kappa"#
// CHECK: "\\b\\b "
Expand Down
16 changes: 13 additions & 3 deletions test/Parse/raw_string_errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ let _ = #"\##("invalid")"#
let _ = ####"invalid"###
// expected-error@-1{{unterminated string literal}}

let _ = ###"""invalid"######
// expected-error@-1{{too many '#' characters in closing delimiter}}{{26-29=}}
// expected-error@-2{{consecutive statements on a line must be separated by ';'}}
// expected-error@-3{{expected expression}}

let _ = ###"invalid"######
// expected-error@-1{{too many '#' characters in closing delimiter}}{{24-27=}}
// expected-error@-2{{consecutive statements on a line must be separated by ';'}}
// expected-error@-3{{expected expression}}

let _ = ##"""##
let _ = ##"""a
foobar
##"""##
// expected-error@-3{{multi-line string literal content must begin on a new line}}{{14-14=\n}}
a"""##
// expected-error@-1{{multi-line string literal content must begin on a new line}}{{14-14=\n}}
// expected-error@-2{{multi-line string literal closing delimiter must begin on a new line}}{{5-5=\n}}

let _ = #""" foo "bar" #baz
"""#
// expected-error@-1{{multi-line string literal content must begin on a new line}}{{14-14=\n}}