vkdb
A time series database engine in C++.
Loading...
Searching...
No Matches
lexer.h
1#ifndef QUERY_LEXER_H
2#define QUERY_LEXER_H
3
4#include <vkdb/token.h>
5#include <vector>
6#include <unordered_map>
7#include <concepts>
8
9namespace vkdb {
14static const std::unordered_map<Lexeme, TokenType> WORD_TO_TOKEN_TYPE{
15 {"SELECT", TokenType::SELECT},
16 {"PUT", TokenType::PUT},
17 {"DELETE", TokenType::DELETE},
18 {"CREATE", TokenType::CREATE},
19 {"DROP", TokenType::DROP},
20 {"ADD", TokenType::ADD},
21 {"REMOVE", TokenType::REMOVE},
22 {"TABLES", TokenType::TABLES},
23 {"DATA", TokenType::DATA},
24 {"AVG", TokenType::AVG},
25 {"SUM", TokenType::SUM},
26 {"COUNT", TokenType::COUNT},
27 {"MIN", TokenType::MIN},
28 {"MAX", TokenType::MAX},
29 {"TABLE", TokenType::TABLE},
30 {"TAGS", TokenType::TAGS},
31 {"ALL", TokenType::ALL},
32 {"BETWEEN", TokenType::BETWEEN},
33 {"AND", TokenType::AND},
34 {"AT", TokenType::AT},
35 {"WHERE", TokenType::WHERE},
36 {"FROM", TokenType::FROM},
37 {"INTO", TokenType::INTO},
38 {"TO", TokenType::TO}
39};
40
45class Lexer {
46public:
47 using size_type = uint64_t;
48
53 Lexer() = delete;
54
60 explicit Lexer(const std::string& input) noexcept;
61
66 Lexer(Lexer&&) noexcept = default;
67
72 Lexer& operator=(Lexer&&) noexcept = default;
73
78 Lexer(const Lexer&) = delete;
79
84 Lexer& operator=(const Lexer&) = delete;
85
90 ~Lexer() noexcept = default;
91
99 std::vector<Token> tokenize();
100
101private:
109 [[nodiscard]] bool is_whitespace(char ch) const noexcept;
110
120 [[nodiscard]] bool is_alpha(char ch) const noexcept;
121
129 [[nodiscard]] bool is_digit(char ch) const noexcept;
130
138 [[nodiscard]] bool is_alnum(char ch) const noexcept;
139
147 [[nodiscard]] bool chars_remaining() const noexcept;
148
156 [[nodiscard]] char peek() const;
157
165 [[nodiscard]] char peek_next() const;
166
174 char advance();
175
182 template <std::predicate<char> Pred>
183 void advance_while(const Pred& pred) noexcept;
184
190 void lex_whitespace() noexcept;
191
198 [[nodiscard]] Token lex_word() noexcept;
199
208 [[nodiscard]] Token lex_number() noexcept;
209
215 void lex_comment() noexcept;
216
222 [[nodiscard]] Token lex_equal() noexcept;
223
229 [[nodiscard]] Token lex_comma() noexcept;
230
236 [[nodiscard]] Token lex_semicolon() noexcept;
237
243 [[nodiscard]] Token lex_unknown() noexcept;
244
250 [[nodiscard]] Token lex_end_of_file() noexcept;
251
260 [[nodiscard]] Lexeme make_lexeme_from(size_type start) const;
261
269 [[nodiscard]] Token make_token(
270 TokenType type,
271 const Lexeme& lexeme
272 ) noexcept;
273
278 std::string input_;
279
284 size_type position_;
285
290 size_type line_;
291
296 size_type column_;
297};
298
299} // namespace vkdb
300
301#endif // QUERY_LEXER_H
Lexer for vq.
Definition lexer.h:45
std::vector< Token > tokenize()
Tokenize the input string.
Definition lexer.cpp:7
Lexer(Lexer &&) noexcept=default
Move-construct a new Lexer object.
Lexer()=delete
Deleted default constructor.
Represents a token.
Definition token.h:81
STL namespace.