Skip to content
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

Clean up injected token state. #914

Merged
merged 1 commit into from
Oct 21, 2023
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
20 changes: 18 additions & 2 deletions compiler/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,12 +1319,21 @@ int Lexer::lex() {
return current_token()->id;
}

if (!injected_token_stream_.empty())
return LexInjectedToken();
if (using_injected_tokens_) {
if (!injected_token_stream_.empty())
return LexInjectedToken();
return 0;
}

return LexNewToken();
}

bool Lexer::freading() const {
if (using_injected_tokens_)
return !injected_token_stream_.empty();
return freading_;
}

int Lexer::LexNewToken() {
full_token_t* tok = advance_token_ptr();
*tok = {};
Expand Down Expand Up @@ -2579,6 +2588,7 @@ void Lexer::AssertCleanState() {
assert(!in_string_continuation_);
assert(allow_tags_);
assert(injected_token_stream_.empty());
assert(!using_injected_tokens_);
}

TokenCache* Lexer::LexFunctionBody() {
Expand Down Expand Up @@ -2620,10 +2630,16 @@ void Lexer::InjectCachedTokens(TokenCache* cache) {
AssertCleanState();

injected_token_stream_ = std::move(cache->tokens);
using_injected_tokens_ = true;
token_caches_.remove(cache);
delete cache;

freading_ = true;
}

void Lexer::DiscardCachedTokens() {
using_injected_tokens_ = false;
injected_token_stream_.clear();
}

} // namespace sp
11 changes: 9 additions & 2 deletions compiler/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ class Lexer
// be replayed by lex().
void InjectCachedTokens(TokenCache* cache);

// Throw away tokens injected by InjectCachedTokens().
void DiscardCachedTokens();

full_token_t lex_tok() {
lex();
return *current_token();
Expand All @@ -344,7 +347,7 @@ class Lexer
bool& allow_tags() { return allow_tags_; }
bool& require_newdecls() { return state_.require_newdecls; }
bool& need_semicolon() { return state_.need_semicolon; }
bool freading() const { return freading_; }
bool freading() const;
int fcurrent() const { return state_.inpf->sources_index(); }
unsigned fline() const { return state_.fline; }
SourceFile* inpf() const { return state_.inpf.get(); }
Expand Down Expand Up @@ -513,9 +516,13 @@ class Lexer

LexerState state_;
tr::vector<LexerState> prev_state_;

// Set if tokens are being lexed into a new token cache.
bool caching_tokens_ = false;
ke::InlineList<TokenCache> token_caches_;

std::deque<full_token_t> injected_token_stream_;
bool caching_tokens_ = false;
bool using_injected_tokens_ = false;
};

std::string StringizePath(const std::filesystem::path& in_path);
Expand Down
12 changes: 10 additions & 2 deletions compiler/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,16 @@ Parser::Parse()
tokens->need_semicolon);

lexer_->InjectCachedTokens(tokens);
auto body = parse_stmt(false);
fun->set_body(BlockStmt::WrapStmt(body));

AutoCountErrors errors;
if (auto body = parse_stmt(false)) {
fun->set_body(BlockStmt::WrapStmt(body));

// If there were no errors, we should have consumed every token.
assert(!errors.ok() || !lexer_->freading());
}

lexer_->DiscardCachedTokens();
}

auto list = new StmtList(token_pos_t{}, stmts);
Expand Down