-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.cpp
86 lines (81 loc) · 2.42 KB
/
token.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
#include "token.h"
#include <stdexcept>
using namespace std;
vector<Token> Tokenize(istream& cl) {
vector<Token> tokens;
char c;
while (cl >> c) {
if (isdigit(c)) {
string date(1, c);
for (int i = 0; i < 3; ++i) {
while (isdigit(cl.peek())) {
date += cl.get();
}
if (i < 2) {
date += cl.get(); // Consume '-'
}
}
tokens.push_back({date, TokenType::DATE});
} else if (c == '"') {
string event;
getline(cl, event, '"');
tokens.push_back({event, TokenType::EVENT});
} else if (c == 'd') {
if (cl.get() == 'a' && cl.get() == 't' && cl.get() == 'e') {
tokens.push_back({"date", TokenType::COLUMN});
} else {
throw logic_error("Unknown token");
}
} else if (c == 'e') {
if (cl.get() == 'v' && cl.get() == 'e' && cl.get() == 'n' &&
cl.get() == 't') {
tokens.push_back({"event", TokenType::COLUMN});
} else {
throw logic_error("Unknown token");
}
} else if (c == 'A') {
if (cl.get() == 'N' && cl.get() == 'D') {
tokens.push_back({"AND", TokenType::LOGICAL_OP});
} else {
throw logic_error("Unknown token");
}
} else if (c == 'O') {
if (cl.get() == 'R') {
tokens.push_back({"OR", TokenType::LOGICAL_OP});
} else {
throw logic_error("Unknown token");
}
} else if (c == '(') {
tokens.push_back({"(", TokenType::PAREN_LEFT});
} else if (c == ')') {
tokens.push_back({")", TokenType::PAREN_RIGHT});
} else if (c == '<') {
if (cl.peek() == '=') {
cl.get();
tokens.push_back({"<=", TokenType::COMPARE_OP});
} else {
tokens.push_back({"<", TokenType::COMPARE_OP});
}
} else if (c == '>') {
if (cl.peek() == '=') {
cl.get();
tokens.push_back({">=", TokenType::COMPARE_OP});
} else {
tokens.push_back({">", TokenType::COMPARE_OP});
}
} else if (c == '=') {
if (cl.get() == '=') {
tokens.push_back({"==", TokenType::COMPARE_OP});
} else {
throw logic_error("Unknown token");
}
} else if (c == '!') {
if (cl.get() == '=') {
tokens.push_back({"!=", TokenType::COMPARE_OP});
} else {
throw logic_error("Unknown token");
}
}
}
return tokens;
}