/
parser.hpp
418 lines (358 loc) · 11.6 KB
/
parser.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// The MIT License (MIT)
// Copyright (c) 2014 Rapptz
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef JSONPP_PARSER_HPP
#define JSONPP_PARSER_HPP
#include "error.hpp"
#include "value.hpp"
#include <cstring>
#include <iosfwd>
namespace json {
inline bool is_space(char ch) {
switch(ch) {
case 0x0D: // carriage return
case 0x09: // tab
case 0x0A: // line feed
case 0x20: // space
return true;
default:
return false;
}
}
struct parser {
private:
unsigned line = 1;
unsigned column = 1;
const char* str;
void skip_white_space() {
while(*str != '\0' && is_space(*str)) {
if(*str == 0x0A) {
++line;
column = 0;
}
++str;
++column;
}
}
void parse_null(value& v) {
static const char null_str[] = "null";
if(*str == '\0') {
throw parser_error("expected null, received EOF instead", line, column);
}
if(std::strncmp(str, null_str, sizeof(null_str) - 1) != 0) {
throw parser_error("expected null not found", line, column);
}
v = nullptr;
str = str + sizeof(null_str) - 1;
column += sizeof(null_str);
}
void parse_number(value& v) {
static const std::string lookup = "0123456789eE+-.";
const char* begin = str;
if(*begin == '\0') {
throw parser_error("expected number, received EOF instead", line, column);
}
while(lookup.find(*str) != std::string::npos) {
++str;
}
double val = 0.0;
try {
std::string temp(begin, str);
val = std::stod(temp);
column += temp.size() + 1;
}
catch(const std::exception& e) {
throw parser_error("number could not be parsed properly", line, column);
}
v = val;
}
int get_codepoint(const char*& copy) {
int codepoint = 0;
for(unsigned i = 0; i < 4; ++i) {
char hex = *copy;
if(hex <= 0x1F) {
throw parser_error("incomplete codepoint provided", line, column);
}
// convert the hex character to its integral representation
// e.g., 'F' -> 15
if(hex >= '0' && hex <= '9') {
hex -= '0';
}
else if(hex >= 'A' && hex <= 'F') {
hex -= 'A' - 0xA;
}
else if(hex >= 'a' && hex <= 'f') {
hex -= 'a' - 0xA;
}
else {
throw parser_error("invalid codepoint provided", line, column);
}
codepoint = codepoint * 16 + hex;
++column;
++copy;
}
return codepoint;
}
void parse_codepoint(const char*& copy, std::string& result) {
// parse the hex characters
// in order to do so, we have to increment by one to get these digits.
// ergo, *copy == 'u', ++copy = codepoint
++copy;
++column;
int codepoint = get_codepoint(copy);
// a regular ASCII code point
if(codepoint < 0x80) {
result.push_back(codepoint);
return;
}
// handle surrogate pairs
if(codepoint >= 0xD800 && codepoint <= 0xDFFF) {
if(codepoint >= 0xDC00) {
throw parser_error("low surrogate pair found but high surrogate pair expected", line, column);
}
// get the low surrogate pair
if(*(copy + 1) != '\\' && *(copy + 2) != 'u') {
throw parser_error("low surrogate pair expected but not found", line, column);
}
copy += 2;
int low_surrogate = get_codepoint(copy);
if(low_surrogate < 0xDC00 || low_surrogate > 0xDFFF) {
throw parser_error("low surrogate out of range [\\uDC000, \\uDFFF]", line, column);
}
codepoint = 0x10000 + (((codepoint - 0xD800) << 10) | ((low_surrogate - 0xDC00) & 0x3FF));
}
if(codepoint < 0x800) {
result.push_back(0xC0 | (codepoint >> 6));
}
else if(codepoint < 0x10000) {
result.push_back(0xE0 | (codepoint >> 12));
}
else {
result.push_back(0xF0 | (codepoint >> 18));
result.push_back(0x80 | ((codepoint >> 12) & 0x3F));
}
result.push_back(0x80 | ((codepoint >> 6) & 0x3F));
result.push_back(0x80 | (codepoint & 0x3F));
}
template<typename Value>
void parse_string(Value& v) {
const char* copy = str + 1;
if(*copy == '\0') {
throw parser_error("expected string, received EOF instead", line, column);
}
std::string result;
// begin parsing
while(true) {
++column;
bool increment_string = true;
auto byte = static_cast<unsigned char>(*copy);
if(byte <= 0x1F) {
throw parser_error("invalid characters found in string or string is incomplete", line, column);
}
// end of string found
if(*copy == '"') {
break;
}
// non-escape character is good to go
if(*copy != '\\') {
result.push_back(*copy++);
continue;
}
// at this point *copy == '\\'
// so increment it to check the next character
++copy;
switch(*copy) {
case '/':
result.push_back('/');
break;
case '\\':
result.push_back('\\');
break;
case '"':
result.push_back('\"');
break;
case 'f':
result.push_back('\f');
break;
case 'n':
result.push_back('\n');
break;
case 'r':
result.push_back('\r');
break;
case 't':
result.push_back('\t');
break;
case 'b':
result.push_back('\b');
break;
case 'u':
parse_codepoint(copy, result);
increment_string = false;
break;
default:
throw parser_error("improper or incomplete escape character found", line, column);
}
if(increment_string) {
++copy;
}
}
v = result;
++copy;
str = copy;
}
void parse_bool(value& v) {
if(*str == '\0') {
throw parser_error("expected boolean, received EOF instead", line, column);
}
bool expected_true = *str == 't';
const char* boolean = expected_true ? "true" : "false";
const size_t len = expected_true ? 4 : 5;
if(std::strncmp(str, boolean, len) != 0) {
throw parser_error("expected boolean not found", line, column);
}
v = expected_true;
str = str + len;
column += len;
}
void parse_array(value& v) {
++str;
array arr;
value elem;
skip_white_space();
if(*str == '\0') {
throw parser_error("expected value, received EOF instead", line, column);
}
while (*str && *str != ']') {
parse_value(elem);
if(*str != ',') {
if(*str != ']') {
throw parser_error("missing comma", line, column);
}
}
else if(*str == ',') {
++str;
// skip whitespace
skip_white_space();
// handle missing input
if(*str && *str == ']') {
throw parser_error("extraneous comma spotted", line, column);
}
}
arr.push_back(elem);
}
v = arr;
if(*str == ']') {
++str;
}
}
void parse_object(value& v) {
++str;
object obj;
std::string key;
value elem;
skip_white_space();
if(*str == '\0') {
throw parser_error("expected string key, received EOF instead", line, column);
}
while(*str) {
skip_white_space();
// empty object
if(*str == '}') {
break;
}
if(*str != '"') {
throw parser_error("expected string as key not found", line, column);
}
parse_string(key);
skip_white_space();
if(*str != ':') {
throw parser_error("missing semicolon", line, column);
}
++str;
parse_value(elem);
if(*str != ',') {
if(*str != '}') {
throw parser_error("missing comma", line, column);
}
}
else if(*str == ',') {
++str;
}
obj.emplace(key, elem);
}
v = obj;
if(*str == '}') {
++str;
}
}
void parse_value(value& v) {
skip_white_space();
if(*str == '\0') {
throw parser_error("unexpected EOF found", line, column);
}
if(isdigit(*str) || *str == '+' || *str == '-') {
parse_number(v);
}
else {
switch(*str) {
case 'n':
parse_null(v);
break;
case '"':
parse_string(v);
break;
case 't':
case 'f':
parse_bool(v);
break;
case '[':
parse_array(v);
break;
case '{':
parse_object(v);
break;
default:
throw parser_error("unexpected token found", line, column);
break;
}
}
skip_white_space();
}
public:
parser(const char* str) JSONPP_NOEXCEPT: str(str) {}
void parse(value& v) {
parse_value(v);
if(*str != '\0') {
throw parser_error("unexpected token found", line, column);
}
}
};
inline void parse(const std::string& str, value& v) {
parser js(str.c_str());
js.parse(v);
}
template<typename IStream, DisableIf<is_string<IStream>> = 0>
inline void parse(IStream& in, value& v) {
static_assert(std::is_base_of<std::istream, IStream>::value, "Input stream passed must inherit from std::istream");
if(in) {
std::ostringstream ss;
ss << in.rdbuf();
parse(ss.str(), v);
}
}
} // json
#endif // JSONPP_PARSER_HPP