-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.cpp
More file actions
151 lines (140 loc) · 3.94 KB
/
Copy pathtokenizer.cpp
File metadata and controls
151 lines (140 loc) · 3.94 KB
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
#include "tokenizer.hpp"
#include <iostream>
namespace JsonDerulo {
std::string Token::to_str() {
switch (type) {
case TokenType::OPEN_BRACE:
return "{";
case TokenType::CLOSE_BRACE:
return "}";
case TokenType::STRING:
return "STRING: " + s_val;
case TokenType::DOUBLE:
return "NUMBER: " + std::to_string(int_val);
case TokenType::INT:
return "NUMBER: " + std::to_string(int_val);
case TokenType::COLON:
return ":";
case TokenType::COMMA:
return ",";
case TokenType::TRUE:
return "TRUE";
case TokenType::FALSE:
return "FALSE";
case TokenType::LEFT_BRACKET:
return "[";
case TokenType::RIGHT_BRACKET:
return "]";
}
}
Tokenizer::Tokenizer(std::string s) : s(s) { tokenize(); }
bool Tokenizer::succeeded() { return _succeeded; }
std::vector<Token> &Tokenizer::tokens() { return _tokens; }
bool compare(const std::string s1, int idx1, const std::string s2) {
if (idx1 + s2.length() > s1.length()) {
return false;
}
for (int i = 0; i < s2.length(); i++) {
if (s1[idx1 + i] != s2[i]) {
return false;
}
}
return true;
}
void Tokenizer::tokenize() {
int idx = 0;
int line = 1;
int col = 1;
while (idx < s.length()) {
if (s.substr(idx, 4) == "true") {
_tokens.push_back(Token(TokenType::TRUE, line, col));
idx += 4;
col += 4;
} else if (s.substr(idx, 5) == "false") {
_tokens.push_back(Token(TokenType::FALSE, line, col));
idx += 5;
col += 5;
} else if (s[idx] == '{') {
_tokens.push_back(Token(TokenType::OPEN_BRACE, line, col));
idx++;
col++;
} else if (s[idx] == '}') {
_tokens.push_back(Token(TokenType::CLOSE_BRACE, line, col));
idx++;
col++;
} else if (s[idx] == '[') {
_tokens.push_back(Token(TokenType::LEFT_BRACKET, line, col));
idx++;
col++;
} else if (s[idx] == ']') {
_tokens.push_back(Token(TokenType::RIGHT_BRACKET, line, col));
idx++;
col++;
} else if (s[idx] == '"') {
int start = idx + 1;
int end = start;
while (end < s.length() && s[end] != '"') {
end++;
col++;
}
if (end == s.length()) {
_succeeded = false;
return;
}
_tokens.push_back(
Token(TokenType::STRING, s.substr(start, end - start), line, col));
idx = end + 1;
col++;
} else if (s[idx] == ':') {
_tokens.push_back(Token(TokenType::COLON, line, col));
idx++;
col++;
} else if (s[idx] == ',') {
_tokens.push_back(Token(TokenType::COMMA, line, col));
idx++;
col++;
} else if ((s[idx] >= '0' && s[idx] <= '9') || s[idx] == '-') {
int start = idx;
int end = start;
bool found_decimal = false;
while (end < s.length() && ((s[end] >= '0' && s[end] <= '9') ||
(s[end] == '-' && end == start) ||
(s[end] == '.' && !found_decimal))) {
if (s[end] == '.') {
found_decimal = true;
}
end++;
col++;
}
if (found_decimal) {
_tokens.push_back(Token(TokenType::DOUBLE,
std::stod(s.substr(start, end - start)), line,
col));
} else {
_tokens.push_back(Token(TokenType::INT,
std::stoi(s.substr(start, end - start)), line,
col));
}
idx = end;
} else if (s[idx] == '\n') {
line++;
col = 1;
idx++;
} else if (s[idx] == ' ' || s[idx] == '\t') {
idx++;
} else {
_succeeded = false;
throw std::runtime_error(
"Error: unexpected character '" + std::to_string(s[idx]) +
"' at line " + std::to_string(line) + ", col " + std::to_string(col));
return;
}
}
_succeeded = true;
}
void Tokenizer::print_tokens() {
for (Token t : _tokens) {
std::cout << t.to_str() << std::endl;
}
}
} // namespace JsonDerulo