mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 22:38:10 +00:00
Add sentinel Eof token at the end of the tokenized buffer. (#388)
This has two goals: 1) It allows us to simplify and remove special cases from the parser: when we expect a particular token next, we can just check for it without needing a special case for end-of-file. 2) It gives us a token to use as a position when emitting diagnostics at the end of the file. Centralize all updating of `position` to `Consume` and `SkipTo`, so that we can in a single place ensure that we never go past the EOF token.
This commit is contained in:
1 parent
d1757d9979
commit
1e6c7e3963
7 files changed
+415
-284
No files matched your search
@@ -463,6 +463,12 @@ class TokenizedBuffer::Lexer {
|
||||
buffer.has_errors = true;
|
||||
return token;
|
||||
}
|
||||
|
||||
auto AddEndOfFileToken() -> void {
|
||||
buffer.AddToken({.kind = TokenKind::EndOfFile(),
|
||||
.token_line = current_line,
|
||||
.column = current_column});
|
||||
}
|
||||
};
|
||||
|
||||
auto TokenizedBuffer::Lex(SourceBuffer& source, DiagnosticConsumer& consumer)
|
||||
@@ -491,6 +497,7 @@ auto TokenizedBuffer::Lex(SourceBuffer& source, DiagnosticConsumer& consumer)
|
||||
}
|
||||
|
||||
lexer.CloseInvalidOpenGroups(TokenKind::Error());
|
||||
lexer.AddEndOfFileToken();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@@ -546,6 +553,10 @@ auto TokenizedBuffer::GetTokenText(Token token) const -> llvm::StringRef {
|
||||
return relexed_token->Text();
|
||||
}
|
||||
|
||||
if (token_info.kind == TokenKind::EndOfFile()) {
|
||||
return llvm::StringRef();
|
||||
}
|
||||
|
||||
assert(token_info.kind == TokenKind::Identifier() &&
|
||||
"Only identifiers have stored text!");
|
||||
return GetIdentifierText(token_info.id);
|
||||
|
||||
Reference in new issue
Block a user