Skip to content
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

[Parse] multiline strings inside interpolations #9049

Merged
merged 6 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ ERROR(lex_illegal_multiline_string_end,none,
"invalid end of multi-line string literal", ())
ERROR(lex_ambiguous_string_indent,none,
"invalid mix of multi-line string literal indentation", ())
ERROR(lex_multiline_inside_interpolation,none,
"multi-line string literals are not allowed inside an interpolation", ())


ERROR(lex_invalid_character,none,
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Parse/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,6 @@ class Lexer {
/// Try to lex conflict markers by checking for the presence of the start and
/// end of the marker in diff3 or Perforce style respectively.
bool tryLexConflictMarker();

/// Check multiline string literal is indented correctly.
void validateMultilineIndents(const Token &Str);
};

} // end namespace swift
Expand Down
28 changes: 17 additions & 11 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,8 @@ unsigned Lexer::lexCharacter(const char *&CurPtr, char StopQuote,
/// outstanding delimiters as it scans the string.
static const char *skipToEndOfInterpolatedExpression(const char *CurPtr,
const char *EndPtr,
DiagnosticEngine *Diags) {
DiagnosticEngine *Diags,
bool MultilineString) {
llvm::SmallVector<char, 4> OpenDelimiters;
auto inStringLiteral = [&]() {
return !OpenDelimiters.empty() &&
Expand All @@ -1257,9 +1258,11 @@ static const char *skipToEndOfInterpolatedExpression(const char *CurPtr,
// issues with malformed tokens or other problems.
switch (*CurPtr++) {
// String literals in general cannot be split across multiple lines;
// interpolated ones are no exception.
// interpolated ones are no exception (unless multiline strings.)
Copy link
Collaborator

@xwu xwu Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: period after parenthesis; also, it's "unless multiline string literals" yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m never sure about that.. I’ll change it

case '\n':
case '\r':
if (MultilineString)
continue;
// Will be diagnosed as an unterminated string literal.
return CurPtr-1;

Expand All @@ -1272,12 +1275,12 @@ static const char *skipToEndOfInterpolatedExpression(const char *CurPtr,
}
// Otherwise it's an ordinary character; treat it normally.
} else {
if (*CurPtr == '"' && *(CurPtr + 1) == '"') {
Diags->diagnose(Lexer::getSourceLoc(CurPtr-1),
diag::lex_multiline_inside_interpolation);
}
OpenDelimiters.push_back(CurPtr[-1]);
}
if (*CurPtr == '"' && *(CurPtr + 1) == '"' && *(CurPtr - 1) == '"') {
MultilineString = true;
CurPtr += 2;
}
continue;
case '\\':
if (inStringLiteral()) {
Expand Down Expand Up @@ -1373,7 +1376,8 @@ static StringRef getMultilineTrailingIndent(const Token &Str,

/// validateMultilineIndents:
/// Diagnose contents of string literal that have inconsistent indentation.
void Lexer::validateMultilineIndents(const Token &Str) {
static void validateMultilineIndents(const Token &Str,
DiagnosticEngine *Diags) {
StringRef Indent = getMultilineTrailingIndent(Str, Diags);
if (Indent.empty())
return;
Expand All @@ -1385,7 +1389,8 @@ void Lexer::validateMultilineIndents(const Token &Str) {
size_t nextpos = pos + 1;
if (BytesPtr[nextpos] != '\n' && BytesPtr[nextpos] != '\r') {
if (Bytes.substr(nextpos, Indent.size()) != Indent)
diagnose(BytesPtr + nextpos, diag::lex_ambiguous_string_indent);
Diags->diagnose(Lexer::getSourceLoc(BytesPtr + nextpos),
diag::lex_ambiguous_string_indent);
}
pos = nextpos;
}
Expand Down Expand Up @@ -1415,7 +1420,8 @@ void Lexer::lexStringLiteral() {
// Consume tokens until we hit the corresponding ')'.
CurPtr += 2;
const char *EndPtr =
skipToEndOfInterpolatedExpression(CurPtr, BufferEnd, Diags);
skipToEndOfInterpolatedExpression(CurPtr, BufferEnd,
Diags, MultilineString);

if (*EndPtr == ')') {
// Successfully scanned the body of the expression literal.
Expand Down Expand Up @@ -1483,7 +1489,7 @@ void Lexer::lexStringLiteral() {
if (*CurPtr == '"' && *(CurPtr + 1) == '"' && *(CurPtr + 2) != '"') {
CurPtr += 2;
formToken(tok::string_literal, TokStart, MultilineString);
validateMultilineIndents(NextToken);
validateMultilineIndents(NextToken, Diags);
return;
}
else
Expand Down Expand Up @@ -1778,7 +1784,7 @@ void Lexer::getStringLiteralSegments(
// Find the closing ')'.
const char *End = skipToEndOfInterpolatedExpression(BytesPtr,
Str.getText().end(),
Diags);
Diags, MultilineString);
assert(*End == ')' && "invalid string literal interpolations should"
" not be returned as string literals");
++End;
Expand Down
7 changes: 0 additions & 7 deletions test/Parse/multiline_errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,3 @@ _ = """""" // expected-error@-0{{invalid start of multi-line string literal}}

_ = """ """ // expected-error@-0{{invalid start of multi-line string literal}}
// newline currently required after opening """

_ = "\("""
not valid
""")" // expected-error@-2{{multi-line string literals are not allowed inside an interpolation}}
// expected-error@-3{{unterminated string literal}}
// expected-error@-2{{invalid start of multi-line string literal}}
// expected-error@-3{{unterminated string literal}}
25 changes: 25 additions & 0 deletions test/Parse/multiline_string.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,28 @@ _ = """
_ = """
"""
// CHECK: ""

_ = "\("""
\("a" + """
valid
""")
""") literal"
// CHECK: "a"
// CHECK: " valid"
// CHECK: " literal"

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

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