Skip to content

[4.0][Parse] Fix skipping string interpolation in Lexer #10699

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
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
40 changes: 31 additions & 9 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,9 @@ static const char *skipToEndOfInterpolatedExpression(const char *CurPtr,
DiagnosticEngine *Diags,
bool MultilineString) {
llvm::SmallVector<char, 4> OpenDelimiters;
llvm::SmallVector<bool, 4> AllowNewline;
AllowNewline.push_back(MultilineString);

auto inStringLiteral = [&]() {
return !OpenDelimiters.empty() &&
(OpenDelimiters.back() == '"' || OpenDelimiters.back() == '\'');
Expand All @@ -1262,27 +1265,46 @@ static const char *skipToEndOfInterpolatedExpression(const char *CurPtr,
// interpolated ones are no exception - unless multiline literals.
case '\n':
case '\r':
if (MultilineString)
if (AllowNewline.back())
continue;
// Will be diagnosed as an unterminated string literal.
return CurPtr-1;

case '"':
case '\'':
if (inStringLiteral()) {
// Is it the closing quote?
case '\'': {
if (!AllowNewline.back() && inStringLiteral()) {
if (OpenDelimiters.back() == CurPtr[-1]) {
// Closing single line string literal.
OpenDelimiters.pop_back();
AllowNewline.pop_back();
}
// Otherwise it's an ordinary character; treat it normally.
} else {
OpenDelimiters.push_back(CurPtr[-1]);
// Otherwise, it's just a quote in string literal. e.g. "foo's".
continue;
}
if (*CurPtr == '"' && *(CurPtr + 1) == '"' && *(CurPtr - 1) == '"') {
MultilineString = true;

bool isMultilineQuote = (
*CurPtr == '"' && *(CurPtr + 1) == '"' && *(CurPtr - 1) == '"');
if (isMultilineQuote)
CurPtr += 2;

if (!inStringLiteral()) {
// Open string literal
OpenDelimiters.push_back(CurPtr[-1]);
AllowNewline.push_back(isMultilineQuote);
continue;
}

// We are in multiline string literal.
assert(AllowNewline.back() && "other cases must be handled above");
if (isMultilineQuote) {
// Close multiline string literal.
OpenDelimiters.pop_back();
AllowNewline.pop_back();
}

// Otherwise, it's just a normal character in multiline string.
continue;
}
case '\\':
if (inStringLiteral()) {
char escapedChar = *CurPtr++;
Expand Down
16 changes: 15 additions & 1 deletion test/Parse/multiline_errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,18 @@ Three B
// expected-note@-15{{change indentation of these lines to match closing delimiter}} {{2-2= }} {{2-2= }}
// expected-error@-14{{unexpected space in indentation of next 4 lines in multi-line string literal}}
// expected-note@-7{{should match tab here}}
// expected-note@-16{{change indentation of these lines to match closing delimiter}} {{1-1= }} {{1-1= }} {{1-1= }} {{1-1= }}
// expected-note@-16{{change indentation of these lines to match closing delimiter}} {{1-1= }} {{1-1= }} {{1-1= }} {{1-1= }}

_ = "hello\("""
world
"""
)!"
// expected-error@-4 {{unterminated string literal}}
// expected-error@-2 {{unterminated string literal}}

_ = "hello\(
"""
world
""")!"
// expected-error@-4 {{unterminated string literal}}
// expected-error@-2 {{unterminated string literal}}
21 changes: 21 additions & 0 deletions test/Parse/multiline_string.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,24 @@ _ = """
// CHECK: "hello"
// CHECK: "world"
// CHECK: "\nabc"

_ = "hello\("""
"world'
""")abc"
// CHECK: "hello"
// CHECK: "\"world'"
// CHECK: "abc"

_ = """
welcome
\(
"to\("""
Swift
""")"
)
!
"""
// CHECK: "welcome\n"
// CHECK: "to"
// CHECK: "Swift"
// CHECK: "\n!"