Skip to content

Commit 0283e4d

Browse files
authored
Merge pull request #24718 from kitasuke/SR-10241_string_literal
SR-10241 Consolidate StringInterpolationExpr to StringLiteralExpr in Syntax
2 parents 9cb2e6e + e09a341 commit 0283e4d

File tree

7 files changed

+113
-69
lines changed

7 files changed

+113
-69
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,16 +2003,6 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20032003
SourceLoc Loc = Tok.getLoc();
20042004
SourceLoc EndLoc = Loc.getAdvancedLoc(Tok.getLength());
20052005

2006-
// The simple case: just a single literal segment.
2007-
if (Segments.size() == 1 &&
2008-
Segments.front().Kind == Lexer::StringSegment::Literal) {
2009-
consumeToken();
2010-
return makeParserResult(
2011-
createStringLiteralExprFromSegment(Context, L, Segments.front(), Loc));
2012-
}
2013-
2014-
// We are now sure this is a string interpolation expression.
2015-
LocalContext.setCreateSyntax(SyntaxKind::StringInterpolationExpr);
20162006
StringRef OpenQuoteStr, CloseQuoteStr;
20172007
tok QuoteKind;
20182008
std::tie(OpenQuoteStr, CloseQuoteStr, QuoteKind) = Tok.isMultilineString() ?
@@ -2032,6 +2022,35 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20322022
// Add the open quote to the context; the quote should have the leading trivia
20332023
// of the entire string token and a void trailing trivia.
20342024
SyntaxContext->addToken(OpenQuote, LeadingTrivia, EmptyTrivia);
2025+
2026+
// The simple case: just a single literal segment.
2027+
if (Segments.size() == 1 &&
2028+
Segments.front().Kind == Lexer::StringSegment::Literal) {
2029+
{
2030+
consumeExtraToken(Tok);
2031+
consumeTokenWithoutFeedingReceiver();
2032+
2033+
SyntaxParsingContext SegmentsCtx(SyntaxContext,
2034+
SyntaxKind::StringLiteralSegments);
2035+
2036+
SyntaxParsingContext StrSegContext(SyntaxContext,
2037+
SyntaxKind::StringSegment);
2038+
2039+
// Make an unknown token to encapsulate the entire string segment and add
2040+
// such token to the context.
2041+
auto Segment = Segments.front();
2042+
Token content(tok::string_segment,
2043+
CharSourceRange(Segment.Loc, Segment.Length).str());
2044+
SyntaxContext->addToken(content, EmptyTrivia, EmptyTrivia);
2045+
}
2046+
2047+
// Add the close quote the context; the quote should have a void leading trivia
2048+
// and the trailing trivia of the entire string token.
2049+
SyntaxContext->addToken(CloseQuote, EmptyTrivia, EntireTrailingTrivia);
2050+
2051+
return makeParserResult(
2052+
createStringLiteralExprFromSegment(Context, L, Segments.front(), Loc));
2053+
}
20352054

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

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

20762095
// Collect all string segments.
20772096
SyntaxParsingContext SegmentsCtx(SyntaxContext,
2078-
SyntaxKind::StringInterpolationSegments);
2097+
SyntaxKind::StringLiteralSegments);
20792098
Status = parseStringSegments(Segments, EntireTok, InterpolationVar,
20802099
Stmts, LiteralCapacity, InterpolationCount);
20812100

lib/Syntax/Status.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
* ForcedValueExpr
3232
* SuperRefExpr
3333
* ImplicitMemberExpr
34-
* InterpolatedStringLiteralExpr
3534
* KeyPathExpr
3635
* KeyPathDotExpr
3736
* InOutExpr

0 commit comments

Comments
 (0)