Add convenience functions to inspect the kind of the next token.

This commit is contained in:
Richard Smith
2021-04-27 13:05:28 -07:00
parent 8924f5620b
commit 857a7e5cc6
3 changed files with 55 additions and 38 deletions
+14
View File
@@ -33,6 +33,20 @@ class ParseTree::Parser {
return tokens.GetKind(*position) == TokenKind::EndOfFile();
}
// Gets the kind of the next token to be consumed.
auto NextTokenKind() const -> TokenKind { return tokens.GetKind(*position); }
// Tests whether the next token to be consumed is of the specified kind.
auto NextTokenIs(TokenKind kind) const -> bool {
return NextTokenKind() == kind;
}
// Tests whether the next token to be consumed is of any of the specified
// kinds.
auto NextTokenIsOneOf(std::initializer_list<TokenKind> kinds) const -> bool {
return NextTokenKind().IsOneOf(kinds);
}
// Requires (and asserts) that the current position matches the provide
// `Kind`. Returns the current token and advances to the next position.
auto Consume(TokenKind kind) -> TokenizedBuffer::Token;