-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
233 lines (198 loc) · 5.57 KB
/
parse.cpp
File metadata and controls
233 lines (198 loc) · 5.57 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <json/parse.h>
json::parser::parser() noexcept(true) { return; }
json::parser::parser(std::string data) noexcept(false) : input(data) { return; }
json::parser::~parser() noexcept(true) { return; }
json::object json::parser::load(std::string path) noexcept(false) {
std::stringstream buffer;
try {
std::ifstream file(path);
if (!file.is_open())
logger::error("File not found " + path,
"json::object json::parser::load(std::string path)");
buffer << file.rdbuf();
file.close();
} catch (std::exception &e) {
logger::error("Error reading file: " + path + " - " + e.what(),
"json::object json::parser::load(std::string path)");
} catch (...) {
logger::error("Error reading file " + path,
"json::object json::parser::load(std::string path)");
}
return this->loads(buffer.str());
}
json::object json::parser::loads(std::string data) noexcept(false) {
this->input = data;
this->index = 0;
this->output.reset();
this->output = this->parseValue();
return this->output;
}
json::object json::parser::parseValue() noexcept(false) {
this->skipWhitespace();
char firstChar = this->getCurrChar();
if (firstChar == 't' || firstChar == 'f' || firstChar == 'n' ||
firstChar == 'u')
return this->parseBooleanNullOrUndefined();
else if (std::isdigit(firstChar))
return this->parseNumber();
else if (firstChar == '"')
return this->parseString();
else if (firstChar == '[')
return this->parseArray();
else if (firstChar == '{')
return this->parseMap();
logger::error("Invalid JSON", "json::object json::parser::parseValue()");
return json::object();
}
json::object json::parser::parseBooleanNullOrUndefined() noexcept(false) {
std::string keyword = "";
char currChar = this->getCurrChar();
while (isalpha(currChar)) {
keyword += currChar;
this->index++;
currChar = this->getCurrChar();
}
if (keyword == "true")
return json::object(true);
else if (keyword == "false")
return json::object(false);
else if (keyword == "null")
return json::object(nullptr);
else if (keyword == "undefined")
return json::object();
logger::error("Invalid JSON",
"json::object json::parser::parseBooleanNullOrUndefined()");
return json::object();
}
json::object json::parser::parseNumber() noexcept(true) {
std::string number = "";
char currChar = this->getCurrChar();
bool isInt = true;
while (std::isdigit(currChar) || currChar == '.' || currChar == 'e' ||
currChar == 'E' || currChar == '+' || currChar == '-') {
number += currChar;
if (currChar == '.') {
isInt = false;
}
this->index++;
currChar = this->getCurrChar();
}
if (isInt)
return json::object(std::stoll(number));
else
return json::object(std::stod(number));
}
json::object json::parser::parseString() noexcept(true) {
std::string value = "";
this->index++;
char currChar = this->getCurrChar();
if (currChar != '"') {
while (true) {
value += currChar;
this->index++;
currChar = this->getCurrChar();
if (currChar == '"') {
if (this->shouldStringBeEscaped()) {
break;
}
}
}
}
this->index++;
return json::object(value);
}
json::object json::parser::parseArray() noexcept(false) {
std::vector<object> emptyVector;
json::object result(emptyVector);
this->index++;
char currChar;
while (true) {
this->skipWhitespace();
currChar = this->getCurrChar();
if (currChar == ']') {
this->index++;
break;
} else if (currChar == ',') {
this->index++;
continue;
}
json::object value = this->parseValue();
result.push_back(value);
}
return result;
}
json::object json::parser::parseMap() noexcept(false) {
std::map<std::string, json::object> emptyMap;
json::object result(emptyMap);
this->index++;
char currChar;
std::string key = "";
while (true) {
this->skipWhitespace();
currChar = this->getCurrChar();
if (currChar == '}') {
this->index++;
break;
} else if (currChar == ',') {
this->index++;
continue;
}
if (currChar != '"')
logger::error("Invalid JSON", "json::object json::parser::parseMap()");
this->index++;
currChar = this->getCurrChar();
if (currChar == '"')
logger::error("Invalid JSON", "json::object json::parser::parseMap()");
key = "";
while (true) {
key += currChar;
this->index++;
currChar = this->getCurrChar();
if (currChar == '"') {
if (this->shouldStringBeEscaped()) {
this->index++;
break;
}
}
}
this->skipWhitespace();
currChar = this->getCurrChar();
if (currChar != ':')
logger::error("Invalid JSON", "json::object json::parser::parseMap()");
this->index++;
result[key] = this->parseValue();
}
return result;
}
void json::parser::skipWhitespace() noexcept(true) {
while (std::isspace(this->input[this->index])) {
this->index++;
}
return;
}
bool json::parser::shouldStringBeEscaped() noexcept(true) {
int backslashes = 0;
for (int i = this->index - 1; i >= 0; i--) {
if (this->input[i] == '\\') {
backslashes++;
} else {
break;
}
}
if (backslashes % 2 == 0) {
return true;
}
return false;
}
char json::parser::getPrevChar() noexcept(true) {
if (this->index > 0) {
return this->input[this->index - 1];
}
return '\0';
}
char json::parser::getCurrChar() noexcept(true) {
if (this->index < this->input.size()) {
return this->input[this->index];
}
return '\0';
}