Skip to content

[Parse] Lexer build backtick trivia around espaced identifier token #13631

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
merged 2 commits into from
Dec 29, 2017
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
1 change: 1 addition & 0 deletions include/swift/Parse/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ class Lexer {
}

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

/// Advance to the end of the line.
/// If EatNewLine is true, CurPtr will be at end of newline character.
Expand Down
19 changes: 15 additions & 4 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,19 @@ void Lexer::formToken(tok Kind, const char *TokStart, bool MultilineString) {
NextToken.setToken(Kind, TokenText, CommentLength, MultilineString);
}

void Lexer::formEscapedIdentifierToken(const char *TokStart) {
assert(CurPtr - TokStart >= 3 && "escaped identifier must be longer than or equal 3 bytes");
assert(TokStart[0] == '`' && "escaped identifier starts with backtick");
assert(CurPtr[-1] == '`' && "escaped identifier ends with backtick");
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
LeadingTrivia.push_back(TriviaPiece::backtick());
assert(TrailingTrivia.size() == 0 && "TrailingTrivia is empty here");
TrailingTrivia.push_back(TriviaPiece::backtick());
}
formToken(tok::identifier, TokStart);
NextToken.setEscapedIdentifier(true);
}

Lexer::State Lexer::getStateForBeginningOfTokenLoc(SourceLoc Loc) const {
const char *Ptr = getBufferPtrForSourceLoc(Loc);
// Skip whitespace backwards until we hit a newline. This is needed to
Expand Down Expand Up @@ -1770,17 +1783,15 @@ void Lexer::lexEscapedIdentifier() {
// If we have the terminating "`", it's an escaped identifier.
if (*CurPtr == '`') {
++CurPtr;
formToken(tok::identifier, Quote);
NextToken.setEscapedIdentifier(true);
formEscapedIdentifierToken(Quote);
return;
}
}

// Special case; allow '`$`'.
if (Quote[1] == '$' && Quote[2] == '`') {
CurPtr = Quote + 3;
formToken(tok::identifier, Quote);
NextToken.setEscapedIdentifier(true);
formEscapedIdentifierToken(Quote);
return;
}

Expand Down
4 changes: 0 additions & 4 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,6 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
Trivia LeadingTrivia, TrailingTrivia;
do {
L.lex(Tok, LeadingTrivia, TrailingTrivia);
if (Tok.isEscapedIdentifier()) {
LeadingTrivia.push_back(TriviaPiece::backtick());
TrailingTrivia.push_front(TriviaPiece::backtick());
}
auto ThisToken = RawTokenSyntax::make(Tok.getKind(), Tok.getText(),
SourcePresence::Present, LeadingTrivia,
TrailingTrivia);
Expand Down
4 changes: 0 additions & 4 deletions lib/Syntax/SyntaxParsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ void SyntaxParsingContext::addToken(Token &Tok, Trivia &LeadingTrivia,
if (!Enabled)
return;

if (Tok.isEscapedIdentifier()) {
LeadingTrivia.push_back(TriviaPiece::backtick());
TrailingTrivia.push_front(TriviaPiece::backtick());
}
addRawSyntax(RawTokenSyntax::make(Tok.getKind(), Tok.getText(),
SourcePresence::Present, LeadingTrivia,
TrailingTrivia));
Expand Down
21 changes: 21 additions & 0 deletions test/Syntax/tokens_escaped_identifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %swift-syntax-test -input-source-filename %s -dump-full-tokens | %FileCheck %s
let /*leading trivia*/ `if` = 3
print(/*leading trivia*/ `if` )

// CHECK-LABEL: 2:25
// CHECK-NEXT:(token identifier
// CHECK-NEXT: (trivia block_comment/*leading trivia*/)
// CHECK-NEXT: (trivia space 1)
// CHECK-NEXT: (trivia backtick 1)
// CHECK-NEXT: (text="if")
// CHECK-NEXT: (trivia backtick 1)
// CHECK-NEXT: (trivia space 1))

// CHECK-LABEL: 3:27
// CHECK-NEXT:(token identifier
// CHECK-NEXT: (trivia block_comment/*leading trivia*/)
// CHECK-NEXT: (trivia space 1)
// CHECK-NEXT: (trivia backtick 1)
// CHECK-NEXT: (text="if")
// CHECK-NEXT: (trivia backtick 1)
// CHECK-NEXT: (trivia space 1))