-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.hpp
66 lines (52 loc) · 1.57 KB
/
lexer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef LEXER
#define LEXER
#include <iterator>
#include <regex>
#include <string>
#include <vector>
#include "types.hpp"
class Lexer {
std::vector<std::pair<std::string, Token>> REGEX_TO_TOKENS = {
{"select", Token::SELECT},
{"create", Token::CREATE},
{"insert", Token::INSERT},
{"values", Token::VALUES},
{"from", Token::FROM},
{"into", Token::INTO},
{"table", Token::TABLE},
{"drop", Token::DROP},
{"int", Token::TYPE_INT},
{"=", Token::EQUAL},
{"where", Token::WHERE},
{"string", Token::TYPE_STRING},
{"\\*", Token::ALL_ATTRIBUTES},
{"[A-Za-z0-9]+", Token::IDENTIFIER},
{"\\(", Token::LPAREN},
{"\\)", Token::RPAREN},
{",", Token::COMMA}};
int prev_token_pos, curr_token_pos, next_token_pos;
std::vector<std::pair<std::string, Token>> tokens;
const std::string get_valid_regex() const {
std::string valid = "";
for (const auto& regex_to_token : REGEX_TO_TOKENS) {
valid += "(" + regex_to_token.first + ")|";
}
valid.pop_back();
return valid;
}
const Token& get_token_at_idx(const int& idx) const {
return REGEX_TO_TOKENS[idx].second;
}
public:
Lexer()
: prev_token_pos(-1),
curr_token_pos(0),
next_token_pos(0),
tokens(std::vector<std::pair<std::string, Token>>()){};
~Lexer() = default;
void lex(const std::string&);
const bool has_next_token() const noexcept;
const std::pair<std::string, Token> get_prev_token() const noexcept;
const std::pair<std::string, Token> get_curr_token() noexcept;
};
#endif