Skip to content

Commit 65fdc00

Browse files
omochirintaro
authored andcommitted
[Parse] Lexer build backtick trivia around espaced identifier token
1 parent d82f5c6 commit 65fdc00

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

include/swift/Parse/Lexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ class Lexer {
496496
}
497497

498498
void formToken(tok Kind, const char *TokStart, bool MultilineString = false);
499+
void formEscapedIdentifierToken(const char *TokStart);
499500

500501
/// Advance to the end of the line but leave the current buffer pointer
501502
/// at that newline character.

lib/Parse/Lexer.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <limits>
3535

3636
using namespace swift;
37+
using namespace swift::syntax;
3738

3839
// clang::isIdentifierHead and clang::isIdentifierBody are deliberately not in
3940
// this list as a reminder that they are using C rules for identifiers.
@@ -269,6 +270,19 @@ void Lexer::formToken(tok Kind, const char *TokStart, bool MultilineString) {
269270
NextToken.setToken(Kind, TokenText, CommentLength, MultilineString);
270271
}
271272

273+
void Lexer::formEscapedIdentifierToken(const char *TokStart) {
274+
assert(CurPtr - TokStart >= 3 && "escaped identifier must be longer than or equal 3 bytes");
275+
assert(TokStart[0] == '`' && "escaped identifier starts with backtick");
276+
assert(CurPtr[-1] == '`' && "escaped identifier ends with backtick");
277+
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
278+
LeadingTrivia.push_back(TriviaPiece::backtick());
279+
assert(TrailingTrivia.size() == 0 && "TrailingTrivia is empty here");
280+
TrailingTrivia.push_back(TriviaPiece::backtick());
281+
}
282+
formToken(tok::identifier, TokStart);
283+
NextToken.setEscapedIdentifier(true);
284+
}
285+
272286
Lexer::State Lexer::getStateForBeginningOfTokenLoc(SourceLoc Loc) const {
273287
const char *Ptr = getBufferPtrForSourceLoc(Loc);
274288
// Skip whitespace backwards until we hit a newline. This is needed to
@@ -1795,17 +1809,15 @@ void Lexer::lexEscapedIdentifier() {
17951809
// If we have the terminating "`", it's an escaped identifier.
17961810
if (*CurPtr == '`') {
17971811
++CurPtr;
1798-
formToken(tok::identifier, Quote);
1799-
NextToken.setEscapedIdentifier(true);
1812+
formEscapedIdentifierToken(Quote);
18001813
return;
18011814
}
18021815
}
18031816

18041817
// Special case; allow '`$`'.
18051818
if (Quote[1] == '$' && Quote[2] == '`') {
18061819
CurPtr = Quote + 3;
1807-
formToken(tok::identifier, Quote);
1808-
NextToken.setEscapedIdentifier(true);
1820+
formEscapedIdentifierToken(Quote);
18091821
return;
18101822
}
18111823

lib/Parse/Parser.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,6 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
286286
Trivia LeadingTrivia, TrailingTrivia;
287287
do {
288288
L.lex(Tok, LeadingTrivia, TrailingTrivia);
289-
if (Tok.isEscapedIdentifier()) {
290-
LeadingTrivia.push_back(TriviaPiece::backtick());
291-
TrailingTrivia.push_front(TriviaPiece::backtick());
292-
}
293289
auto ThisToken = RawTokenSyntax::make(Tok.getKind(), Tok.getText(),
294290
SourcePresence::Present, LeadingTrivia,
295291
TrailingTrivia);

lib/Syntax/SyntaxParsingContext.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ void SyntaxParsingContext::addToken(Token &Tok, Trivia &LeadingTrivia,
7777
if (!Enabled)
7878
return;
7979

80-
if (Tok.isEscapedIdentifier()) {
81-
LeadingTrivia.push_back(TriviaPiece::backtick());
82-
TrailingTrivia.push_front(TriviaPiece::backtick());
83-
}
8480
addRawSyntax(RawTokenSyntax::make(Tok.getKind(), Tok.getText(),
8581
SourcePresence::Present, LeadingTrivia,
8682
TrailingTrivia));

0 commit comments

Comments
 (0)