-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
201 lines (181 loc) · 6.2 KB
/
parse.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "parse.hpp"
#include <assert.h>
#include <stdexcept>
Statement* Parser::parse(Lexer& lexer) {
std::pair<std::string, Token> token = lexer.get_curr_token();
switch (token.second) {
case Token::CREATE:
return parse_create(lexer);
break;
case Token::INSERT:
return parse_insert(lexer);
break;
case Token::SELECT:
return parse_select(lexer);
break;
case Token::DROP:
return parse_drop(lexer);
break;
default:
return nullptr;
break;
}
}
CreateStatement* Parser::parse_create(Lexer& lexer) {
CreateStatement* stmt = new CreateStatement{};
std::pair<std::string, Token> token;
// grab table name.
while (lexer.has_next_token() &&
(token = lexer.get_curr_token()).second != Token::IDENTIFIER)
;
if (lexer.get_prev_token().second != Token::TABLE) {
throw std::invalid_argument(
"Malformed \"create\" query, expected \"table\" before table name.");
}
stmt->set_table_name(token.first);
// skip over "values" keyword and "("
token = lexer.get_curr_token();
if (token.second != Token::LPAREN) {
throw std::invalid_argument(
"Malformed \"create\" query, expected ( after \"values\".");
}
// parse values in the statement.
while (lexer.has_next_token()) {
std::vector<std::pair<std::string, Token>> value =
parse_until(lexer, Token::COMMA, Token::RPAREN);
if (value.size() != 2) {
throw std::invalid_argument(
"Malformed\"create\" query, too many keywords specified.");
}
stmt->add_mapping(value[0].first, value[1].first);
}
return stmt;
}
InsertStatement* Parser::parse_insert(Lexer& lexer) {
InsertStatement* stmt = new InsertStatement{};
std::pair<std::string, Token> token;
while (lexer.has_next_token() &&
(token = lexer.get_curr_token()).second != Token::IDENTIFIER)
;
if (lexer.get_prev_token().second != Token::INTO) {
throw std::invalid_argument(
"Malformed \"insert\" query, expected into before table name.");
}
stmt->set_table_name(token.first);
token = lexer.get_curr_token();
if (token.second != Token::VALUES) {
throw std::invalid_argument(
"Malformed query, expecting values after table name");
}
while (lexer.has_next_token() &&
(token = lexer.get_curr_token()).second != Token::LPAREN)
;
while (lexer.has_next_token()) {
std::vector<std::pair<std::string, Token>> tokens =
parse_until(lexer, Token::RPAREN, Token::RPAREN);
std::vector<std::string> tuple;
for (unsigned int token_idx = 0; token_idx < tokens.size(); token_idx++) {
std::pair<std::string, Token>& token = tokens[token_idx];
if (token_idx % 2 != 0 && token.second != Token::COMMA) {
throw std::invalid_argument(
"Malformed query, need commas between values to insert");
}
if (token.second != Token::COMMA) {
tuple.push_back(token.first);
}
}
stmt->add_tuple(std::move(tuple));
if (lexer.has_next_token()) {
token = lexer.get_curr_token();
if (token.second != Token::COMMA) {
throw std::invalid_argument(
"Malformed query, need commas separating sets of values");
}
token = lexer.get_curr_token();
if (token.second != Token::LPAREN) {
throw std::invalid_argument(
"Malformed query, need parens surrounding values that need to be "
"entered");
}
}
}
return stmt;
}
SelectStatement* Parser::parse_select(Lexer& lexer) {
SelectStatement* stmt = new SelectStatement{};
std::pair<std::string, Token> token;
std::vector<std::pair<std::string, Token>> tokens =
parse_until(lexer, Token::FROM, Token::FROM);
std::vector<std::string> attributes;
for (const auto& token : tokens) {
if (token.second == Token::ALL_ATTRIBUTES) {
stmt->set_select_all(true);
}
if (token.second != Token::COMMA && !stmt->get_select_all()) {
attributes.push_back(token.first);
}
}
if (!attributes.empty() && stmt->get_select_all()) {
throw std::invalid_argument(
"Malformed query: cannot select all and also specify other attributes");
}
for (const auto& attribute : attributes) {
stmt->add_attribute(std::move(attribute));
}
token = lexer.get_curr_token();
if (token.second != Token::IDENTIFIER) {
throw std::invalid_argument(
"expected table name at the end of the statement");
}
stmt->set_table_name(token.first);
// Parse the `where...` part if it exists.
if (lexer.has_next_token()) {
token = lexer.get_curr_token();
if (token.second != Token::WHERE) {
throw std::invalid_argument(
"Malformed query, expecting where or nothing after table name.");
}
token = lexer.get_curr_token();
if (token.second != Token::IDENTIFIER) {
throw std::invalid_argument(
"Malformed query, using restricted keywords in the where query");
}
std::string attribute = token.first;
token = lexer.get_curr_token();
stmt->set_where_operator(token.second);
if (!lexer.has_next_token()) {
throw std::invalid_argument(
"Malformed query, where condition is not properly specified.");
}
token = lexer.get_curr_token();
stmt->set_where_filter(attribute, token.first);
}
return stmt;
}
DropStatement* Parser::parse_drop(Lexer& lexer) {
DropStatement* stmt = new DropStatement{};
std::pair<std::string, Token> token;
token = lexer.get_curr_token();
if (token.second != Token::TABLE) {
throw std::invalid_argument(
"malformed query, expected table after drop keyword");
}
token = lexer.get_curr_token();
if (token.second != Token::IDENTIFIER) {
throw std::invalid_argument("table name cannot be a reserved keyword");
}
stmt->set_table_name(token.first);
return stmt;
}
std::vector<std::pair<std::string, Token>> Parser::parse_until(
Lexer& lexer, const Token& t1, const Token& t2) {
std::vector<std::pair<std::string, Token>> tokens;
while (lexer.has_next_token()) {
std::pair<std::string, Token> token = lexer.get_curr_token();
if (token.second == t1 || token.second == t2) {
return std::move(tokens);
}
tokens.push_back(std::move(token));
}
throw std::invalid_argument("Malformed query.");
}