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
24 changes: 20 additions & 4 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2471,14 +2471,30 @@ class UnresolvedDotExpr : public Expr {
: FunctionRefKind::Unapplied);
}

SourceLoc getLoc() const { return NameLoc.getBaseNameLoc(); }
SourceLoc getLoc() const {
if (NameLoc.isValid())
return NameLoc.getBaseNameLoc();
else if (DotLoc.isValid())
return DotLoc;
else
return SubExpr->getEndLoc();
}

SourceLoc getStartLoc() const {
return (DotLoc.isInvalid() ? NameLoc.getSourceRange().End
: SubExpr->getStartLoc());
if (SubExpr->getStartLoc().isValid())
return SubExpr->getStartLoc();
else if (DotLoc.isValid())
return DotLoc;
else
return NameLoc.getSourceRange().Start;
}
SourceLoc getEndLoc() const {
return NameLoc.getSourceRange().End;
if (NameLoc.isValid())
return NameLoc.getSourceRange().End;
else if (DotLoc.isValid())
return DotLoc;
else
return SubExpr->getEndLoc();
}

SourceLoc getDotLoc() const { return DotLoc; }
Expand Down
196 changes: 99 additions & 97 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1862,10 +1862,22 @@ parseStringSegments(SmallVectorImpl<Lexer::StringSegment> &Segments,
SyntaxParsingContext ExprContext(SyntaxContext,
SyntaxKind::ExpressionSegment);

unsigned DelimiterLen = EntireTok.getCustomDelimiterLen();
bool HasCustomDelimiter = DelimiterLen > 0;

// Backslash is part of an expression segment.
Token BackSlash(tok::backslash,
CharSourceRange(Segment.Loc.getAdvancedLoc(-1), 1).str());
SourceLoc BackSlashLoc = Segment.Loc.getAdvancedLoc(-1 - DelimiterLen);
Token BackSlash(tok::backslash, CharSourceRange(BackSlashLoc, 1).str());
ExprContext.addToken(BackSlash, EmptyTrivia, EmptyTrivia);

// Custom delimiter may be a part of an expression segment.
if (HasCustomDelimiter) {
SourceLoc DelimiterLoc = Segment.Loc.getAdvancedLoc(-DelimiterLen);
Token Delimiter(tok::raw_string_delimiter,
CharSourceRange(DelimiterLoc, DelimiterLen).str());
ExprContext.addToken(Delimiter, EmptyTrivia, EmptyTrivia);
}

// Create a temporary lexer that lexes from the body of the string.
LexerState BeginState =
L->getStateForBeginningOfTokenLoc(Segment.Loc);
Expand All @@ -1888,30 +1900,12 @@ parseStringSegments(SmallVectorImpl<Lexer::StringSegment> &Segments,
TokReceiver->registerTokenKindChange(Tok.getLoc(),
tok::string_interpolation_anchor);

SourceLoc lParen, rParen;
SmallVector<Expr *, 4> args;
SmallVector<Identifier, 4> argLabels;
SmallVector<SourceLoc, 4> argLabelLocs;
Expr *trailingClosureNeverPresent;
ParserStatus S =
parseExprList(tok::l_paren, tok::r_paren,
/*isPostfix=*/false, /*isExprBasic=*/true,
lParen, args, argLabels, argLabelLocs, rParen,
trailingClosureNeverPresent,
SyntaxKind::Unknown);
assert(!trailingClosureNeverPresent);

Status |= S;
// If there was an error parsing a parameter, add an ErrorExpr to
// represent it. Prevents spurious errors about a nonexistent
// appendInterpolation() overload.
if (S.isError() && args.size() == 0)
args.push_back(new (Context) ErrorExpr(SourceRange(lParen, rParen)));

while (argLabels.size() < args.size())
argLabels.push_back(Identifier());
while (argLabelLocs.size() < args.size())
argLabelLocs.push_back(SourceLoc());
auto callee = new (Context) UnresolvedDotExpr(InterpolationVarRef,
/*dotloc=*/BackSlashLoc,
appendInterpolation,
/*nameloc=*/DeclNameLoc(),
/*Implicit=*/true);
auto S = parseExprCallSuffix(makeParserResult(callee), true);

// If we stopped parsing the expression before the expression segment is
// over, eat the remaining tokens into a token list
Expand All @@ -1925,51 +1919,14 @@ parseStringSegments(SmallVectorImpl<Lexer::StringSegment> &Segments,
L->getLocForEndOfToken(SourceMgr, Tok.getLoc()));
}

Expr *call = S.getPtrOrNull();
if (!call)
call = new (Context) ErrorExpr(SourceRange(Segment.Loc,
Segment.getEndLoc()));

InterpolationCount += 1;

// In Swift 4.2 and earlier, a single argument with a label would ignore
// the label (at best), and multiple arguments would form a tuple.
if (!Context.isSwiftVersionAtLeast(5)) {
if (args.size() > 1) {
diagnose(args[1]->getLoc(), diag::string_interpolation_list_changing)
.highlightChars(args[1]->getLoc(), rParen);
diagnose(args[1]->getLoc(),
diag::string_interpolation_list_insert_parens)
.fixItInsertAfter(lParen, "(")
.fixItInsert(rParen, ")");

args = {
TupleExpr::create(Context,
lParen, args, argLabels, argLabelLocs, rParen,
/*hasTrailingClosure=*/false,
/*Implicit=*/false)
};
argLabels = { Identifier() };
argLabelLocs = { SourceLoc() };
}
else if (args.size() == 1 && argLabels[0] != Identifier()) {
diagnose(argLabelLocs[0], diag::string_interpolation_label_changing)
.highlightChars(argLabelLocs[0], args[0]->getStartLoc());
diagnose(argLabelLocs[0], diag::string_interpolation_remove_label,
argLabels[0])
.fixItRemoveChars(argLabelLocs[0], args[0]->getStartLoc());

argLabels[0] = Identifier();
}
}

auto AppendInterpolationRef =
new (Context) UnresolvedDotExpr(InterpolationVarRef,
/*dotloc=*/SourceLoc(),
appendInterpolation,
/*nameloc=*/DeclNameLoc(),
/*Implicit=*/true);
auto AppendInterpolationCall =
CallExpr::create(Context, AppendInterpolationRef,
lParen, args, argLabels, argLabelLocs, rParen,
/*trailingClosure=*/nullptr, /*implicit=*/false);

Stmts.push_back(AppendInterpolationCall);
Stmts.push_back(call);
Status |= S;

if (!Tok.is(tok::eof)) {
diagnose(Tok, diag::string_interpolation_extra);
Expand Down Expand Up @@ -2003,35 +1960,73 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
SourceLoc Loc = Tok.getLoc();
SourceLoc EndLoc = Loc.getAdvancedLoc(Tok.getLength());

// The simple case: just a single literal segment.
if (Segments.size() == 1 &&
Segments.front().Kind == Lexer::StringSegment::Literal) {
consumeToken();
return makeParserResult(
createStringLiteralExprFromSegment(Context, L, Segments.front(), Loc));
}

// We are now sure this is a string interpolation expression.
LocalContext.setCreateSyntax(SyntaxKind::StringInterpolationExpr);
StringRef OpenQuoteStr, CloseQuoteStr;
StringRef OpenDelimiterStr, OpenQuoteStr, CloseQuoteStr, CloseDelimiterStr;
unsigned DelimiterLength = Tok.getCustomDelimiterLen();
bool HasCustomDelimiter = DelimiterLength > 0;
unsigned QuoteLength;
tok QuoteKind;
std::tie(OpenQuoteStr, CloseQuoteStr, QuoteKind) = Tok.isMultilineString() ?
std::make_tuple(Tok.getRawText().take_front(3),
Tok.getRawText().take_back(3),
tok::multiline_string_quote) :
std::make_tuple(Tok.getRawText().take_front(1),
Tok.getRawText().take_back(1),
tok::string_quote);
std::tie(QuoteLength, QuoteKind) =
Tok.isMultilineString() ? std::make_tuple(3, tok::multiline_string_quote)
: std::make_tuple(1, tok::string_quote);
unsigned CloseQuoteBegin = Tok.getLength() - DelimiterLength - QuoteLength;

OpenDelimiterStr = Tok.getRawText().take_front(DelimiterLength);
OpenQuoteStr = Tok.getRawText().substr(DelimiterLength, QuoteLength);
CloseQuoteStr = Tok.getRawText().substr(CloseQuoteBegin, QuoteLength);
CloseDelimiterStr = Tok.getRawText().take_back(DelimiterLength);

// Make unknown tokens to represent the open and close quote.
Token OpenQuote(QuoteKind, OpenQuoteStr);
Token CloseQuote(QuoteKind, CloseQuoteStr);
ParsedTrivia EmptyTrivia;
ParsedTrivia EntireTrailingTrivia = TrailingTrivia;

// Add the open quote to the context; the quote should have the leading trivia
// of the entire string token and a void trailing trivia.
SyntaxContext->addToken(OpenQuote, LeadingTrivia, EmptyTrivia);
if (HasCustomDelimiter) {
Token OpenDelimiter(tok::raw_string_delimiter, OpenDelimiterStr);
// When a custom delimiter is present, it owns the leading trivia.
SyntaxContext->addToken(OpenDelimiter, LeadingTrivia, EmptyTrivia);

SyntaxContext->addToken(OpenQuote, EmptyTrivia, EmptyTrivia);
} else {
// Without custom delimiter the quote owns trailing trivia.
SyntaxContext->addToken(OpenQuote, LeadingTrivia, EmptyTrivia);
}

// The simple case: just a single literal segment.
if (Segments.size() == 1 &&
Segments.front().Kind == Lexer::StringSegment::Literal) {
{
consumeExtraToken(Tok);
consumeTokenWithoutFeedingReceiver();

SyntaxParsingContext SegmentsCtx(SyntaxContext,
SyntaxKind::StringLiteralSegments);

SyntaxParsingContext StrSegContext(SyntaxContext,
SyntaxKind::StringSegment);

// Make an unknown token to encapsulate the entire string segment and add
// such token to the context.
auto Segment = Segments.front();
Token content(tok::string_segment,
CharSourceRange(Segment.Loc, Segment.Length).str());
SyntaxContext->addToken(content, EmptyTrivia, EmptyTrivia);
}

if (HasCustomDelimiter) {
SyntaxContext->addToken(CloseQuote, EmptyTrivia, EmptyTrivia);

Token CloseDelimiter(tok::raw_string_delimiter, CloseDelimiterStr);
// When a custom delimiter is present it owns the trailing trivia.
SyntaxContext->addToken(CloseDelimiter, EmptyTrivia, EntireTrailingTrivia);
} else {
// Without custom delimiter the quote owns trailing trivia.
SyntaxContext->addToken(CloseQuote, EmptyTrivia, EntireTrailingTrivia);
}

return makeParserResult(
createStringLiteralExprFromSegment(Context, L, Segments.front(), Loc));
}

// We don't expose the entire interpolated string as one token. Instead, we
// should expose the tokens in each segment.
Expand All @@ -2044,8 +2039,8 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {

// We're not in a place where an interpolation would be valid.
if (!CurLocalContext) {
// Return an error, but include an empty InterpolatedStringLiteralExpr
// so that parseDeclPoundDiagnostic() can figure out why this string
// Return an error, but include an empty InterpolatedStringLiteralExpr
// so that parseDeclPoundDiagnostic() can figure out why this string
// literal was bad.
return makeParserErrorResult(
new (Context) InterpolatedStringLiteralExpr(Loc, 0, 0, nullptr));
Expand Down Expand Up @@ -2075,7 +2070,7 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {

// Collect all string segments.
SyntaxParsingContext SegmentsCtx(SyntaxContext,
SyntaxKind::StringInterpolationSegments);
SyntaxKind::StringLiteralSegments);
Status = parseStringSegments(Segments, EntireTok, InterpolationVar,
Stmts, LiteralCapacity, InterpolationCount);

Expand All @@ -2084,9 +2079,16 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
AppendingExpr = new (Context) TapExpr(nullptr, Body);
}

// Add the close quote the context; the quote should have a void leading trivia
// and the trailing trivia of the entire string token.
SyntaxContext->addToken(CloseQuote, EmptyTrivia, EntireTrailingTrivia);
if (HasCustomDelimiter) {
SyntaxContext->addToken(CloseQuote, EmptyTrivia, EmptyTrivia);

Token CloseDelimiter(tok::raw_string_delimiter, CloseDelimiterStr);
// When a custom delimiter is present it owns the trailing trivia.
SyntaxContext->addToken(CloseDelimiter, EmptyTrivia, EntireTrailingTrivia);
} else {
// Without custom delimiter the quote owns trailing trivia.
SyntaxContext->addToken(CloseQuote, EmptyTrivia, EntireTrailingTrivia);
}

if (AppendingExpr->getBody()->getNumElements() == 1) {
Status.setIsParseError();
Expand Down
Loading