AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
token_stream.h
1#ifndef PARSER_TOKEN_STREAM_H
2#define PARSER_TOKEN_STREAM_H
3
4#include <string>
5#include <vector>
6
7namespace utils {
8class Context;
9}
10
11namespace downward::cli::parser {
12
13enum class TokenType {
14 OPENING_PARENTHESIS,
15 CLOSING_PARENTHESIS,
16 OPENING_BRACKET,
17 CLOSING_BRACKET,
18 COMMA,
19 EQUALS,
20 LET,
21 BOOLEAN,
22 STRING,
23 INTEGER,
24 FLOAT,
25 IDENTIFIER
26};
27
28struct Token {
29 std::string content;
30 TokenType type;
31
32 Token(const std::string& content, TokenType type);
33};
34
35class TokenStream {
36 std::vector<Token> tokens;
37 int pos;
38
39public:
40 explicit TokenStream(std::vector<Token>&& tokens);
41
42 bool has_tokens(int n) const;
43 Token peek(const utils::Context& context, int n = 0) const;
44 Token pop(const utils::Context& context);
45 Token pop(const utils::Context& context, TokenType expected_type);
46
47 int get_position() const;
48 int size() const;
49 std::string str(int from, int to) const;
50};
51
52extern std::string token_type_name(TokenType token_type);
53extern std::ostream& operator<<(std::ostream& out, TokenType token_type);
54extern std::ostream& operator<<(std::ostream& out, const Token& token);
55} // namespace downward::cli::parser
56namespace std {
57template <>
58struct hash<downward::cli::parser::TokenType> {
59 size_t operator()(const downward::cli::parser::TokenType& t) const
60 {
61 return size_t(t);
62 }
63};
64} // namespace std
65#endif
STL namespace.