Skip to content

Parser: properly handle token receiver in back tracking scopes #23172

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 1 commit into from
Mar 8, 2019
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
33 changes: 31 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,38 @@ class Parser {
llvm::Optional<SyntaxParsingContext> SynContext;
bool Backtrack = true;

/// A token receiver used by the parser in the back tracking scope. This
/// receiver will save any consumed tokens during this back tracking scope.
/// After the scope ends, it either transfers the saved tokens to the old receiver
/// or discard them.
struct DelayedTokenReceiver: ConsumeTokenReceiver {
/// Keep track of the old token receiver in the parser so that we can recover
/// after the backtracking sope ends.
llvm::SaveAndRestore<ConsumeTokenReceiver*> savedConsumer;

// Whether the tokens should be transferred to the original receiver.
// When the back tracking scope will actually back track, this should be false;
// otherwise true.
bool shouldTransfer = false;
std::vector<Token> delayedTokens;
DelayedTokenReceiver(ConsumeTokenReceiver *&receiver):
savedConsumer(receiver, this) {}
void receive(Token tok) override {
delayedTokens.push_back(tok);
}
~DelayedTokenReceiver() {
if (!shouldTransfer)
return;
for (auto tok: delayedTokens) {
savedConsumer.get()->receive(tok);
}
}
} TempReceiver;

public:
BacktrackingScope(Parser &P)
: P(P), PP(P.getParserPosition()), DT(P.Diags) {
: P(P), PP(P.getParserPosition()), DT(P.Diags),
TempReceiver(P.TokReceiver) {
SynContext.emplace(P.SyntaxContext);
SynContext->setBackTracking();
}
Expand All @@ -440,8 +469,8 @@ class Parser {
SynContext->setTransparent();
SynContext.reset();
DT.commit();
TempReceiver.shouldTransfer = true;
}

};

/// RAII object that, when it is destructed, restores the parser and lexer to
Expand Down
11 changes: 11 additions & 0 deletions test/IDE/coloring_string_interpolation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s
// RUN: %target-swift-ide-test -syntax-coloring -typecheck -source-filename %s | %FileCheck %s

// CHECK: <kw>_</kw> = <str>"</str>\<anchor>(</anchor><anchor>)</anchor><str>"</str>
// CHECK: <kw>if</kw> <kw>true</kw> { <kw>_</kw> = <str>"</str>\<anchor>(</anchor><anchor>)</anchor><str>"</str> }

if true {
_ = "\()"
}

if true { _ = "\()" }