-
Notifications
You must be signed in to change notification settings - Fork 3
/
json.h
402 lines (381 loc) · 11.8 KB
/
json.h
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
/**
* json.h - Simple JSON parser.
*
* Created on: Feb 27, 2017
* Author: asaparov
*/
#ifndef JSON_H_
#define JSON_H_
#include <core/array.h>
#include <core/lex.h>
#include <ctype.h>
using namespace core;
enum json_state {
JSON_KEY,
JSON_VALUE,
JSON_FIRST_KEY,
JSON_FIRST_VALUE,
JSON_COLON,
JSON_COMMA,
JSON_STRING,
JSON_KEYWORD,
JSON_NUMBER,
JSON_END
};
enum json_context {
JSON_CONTEXT_KEY,
JSON_CONTEXT_VALUE,
JSON_CONTEXT_LIST
};
template<typename Reader>
bool emit_keyword(const array<char>& token, const position& pos, Reader& reader) {
if (compare_strings(token, "true")) {
return emit_true(pos, reader);
} else if (compare_strings(token, "false")) {
return emit_false(pos, reader);
} else if (compare_strings(token, "null")) {
return emit_null(pos, reader);
} else {
read_error("Unrecognized keyword", pos);
return false;
}
}
template<typename Reader>
bool emit_number(const array<char>& token, const position& pos, Reader& reader) {
double value;
if (!parse_float(token, value)) {
read_error("Unable to interpret numerical value", pos);
return false;
}
return emit_number(value, pos, reader);
}
inline bool codepoint_to_utf8(array<char>& str, unsigned int codepoint)
{
if (!str.ensure_capacity(str.length + 3)) return false;
if (codepoint <= 0x7F) {
str[str.length] = codepoint;
str.length++;
} else if (codepoint <= 0x7FF) {
str[str.length] = 0xC0 | (codepoint >> 6);
str[str.length + 1] = 0x80 | (codepoint & 0x3F);
str.length += 2;
} else if (codepoint <= 0xFFFF) {
str[str.length] = 0xE0 | (codepoint >> 12);
str[str.length + 1] = 0x80 | ((codepoint >> 6) & 0x3F);
str[str.length + 2] = 0x80 | (codepoint & 0x3F);
str.length += 3;
} else if (codepoint <= 0x10FFFF) {
fprintf(stderr, "codepoint_to_utf8 ERROR: Codepoints larger than 0xFFFF are unsupported.\n");
return false;
/*str[str.length] = 0xF0 | (codepoint >> 18);
str[str.length + 1] = 0x80 | ((codepoint >> 12) & 0x3F);
str[str.length + 2] = 0x80 | ((codepoint >> 6) & 0x3F);
str[str.length + 3] = 0x80 | (codepoint & 0x3F);
str.length += 4;*/
}
return true;
}
template<typename Stream>
bool emit_escape(Stream& in, array<char>& token, int& next, position& current) {
if (next == -1) {
read_error("Unexpected end of input", current);
return false;
} else if (next == 'n') {
if (!token.add('\n')) return false;
} else if (next == 'b') {
if (!token.add('\b')) return false;
} else if (next == 'f') {
if (!token.add('\f')) return false;
} else if (next == 't') {
if (!token.add('\t')) return false;
} else if (next == 'r') {
if (!token.add('\r')) return false;
} else if (next == 'u') {
/* read the next four characters */
char escape[4];
for (unsigned int i = 0; i < 4; i++) {
next = fgetc(in);
current.column++;
if (next == -1) {
read_error("Unexpected end of input", current);
return false;
}
escape[i] = (char) next;
}
unsigned int codepoint;
if (!parse_uint(escape, codepoint, 16) || !codepoint_to_utf8(token, codepoint)) {
read_error("Unable to interpret Unicode codepoint", current);
return false;
}
} else if (!token.add(next)) return false;
return true;
}
template<typename Stream, typename Reader>
bool json_parse(Stream& in, Reader& reader, position& current)
{
array<char> token = array<char>(1024);
json_state state = JSON_VALUE;
int next = fgetc(in);
bool new_line = false;
array<json_context> contexts = array<json_context>(8);
while (next != -1) {
switch (state) {
case JSON_KEY:
case JSON_FIRST_KEY:
if (next == '"') {
state = JSON_STRING;
} else if (next == '}') {
if (state != JSON_FIRST_KEY || contexts.length == 0 || contexts.last() != JSON_CONTEXT_VALUE) {
read_error("Unexpected closing brace '}'", current);
return false;
}
end_object(current, reader);
contexts.pop();
if (contexts.length == 0) {
state = JSON_END;
} else { /* we don't check the previous context since the FSM doesn't allow JSON_CONTEXT_KEY */
state = JSON_COMMA;
}
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
new_line = (next == '\n');
} else {
read_error("Expected a key-value pair or closing brace '}' for object", current);
return false;
}
break;
case JSON_VALUE:
case JSON_FIRST_VALUE:
if (next == '{') {
state = JSON_FIRST_KEY;
if (!contexts.add(JSON_CONTEXT_KEY)) return false;
begin_object(current, reader);
} else if (next == '[') {
state = JSON_FIRST_VALUE;
if (!contexts.add(JSON_CONTEXT_LIST)) return false;
begin_list(current, reader);
} else if (next == '"') {
state = JSON_STRING;
} else if (isdigit(next) || next == '+' || next == '-' || next == '.') {
if (!token.add(next)) return false;
state = JSON_NUMBER;
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
new_line = (next == '\n');
} else if (next == ']') {
if (state != JSON_FIRST_VALUE || contexts.length == 0 || contexts.last() != JSON_CONTEXT_LIST) {
read_error("Unexpected closing bracket ']'", current);
return false;
}
end_list(current, reader);
contexts.pop();
if (contexts.length == 0) {
state = JSON_END;
} else { /* we don't check the previous context since the FSM doesn't allow JSON_CONTEXT_KEY */
state = JSON_COMMA;
}
} else {
if (!token.add(next)) return false;
state = JSON_KEYWORD;
}
break;
case JSON_COLON:
if (next == ':') {
state = JSON_VALUE;
contexts.last() = JSON_CONTEXT_VALUE;
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
new_line = (next == '\n');
} else {
read_error("Expected a colon ':'", current);
return false;
}
break;
case JSON_COMMA:
if (next == ',') {
if (contexts.length == 0) {
read_error("Unexpected comma ','", current);
return false;
}
if (contexts.last() == JSON_CONTEXT_VALUE) {
state = JSON_KEY;
contexts.last() = JSON_CONTEXT_KEY;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_LIST */
state = JSON_VALUE;
}
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
new_line = (next == '\n');
} else if (next == '}') {
if (contexts.length == 0 || contexts.last() != JSON_CONTEXT_VALUE) {
read_error("Unexpected closing brace '}'", current);
return false;
}
end_object(current, reader);
contexts.pop();
if (contexts.length == 0) {
state = JSON_END;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_VALUE or JSON_CONTEXT_LIST */
state = JSON_COMMA;
}
} else if (next == ']') {
if (contexts.length == 0 || contexts.last() != JSON_CONTEXT_LIST) {
read_error("Unexpected closing brace ']'", current);
return false;
}
end_list(current, reader);
contexts.pop();
if (contexts.length == 0) {
state = JSON_END;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_VALUE or JSON_CONTEXT_LIST */
state = JSON_COMMA;
}
} else {
read_error("Expected a comma ','", current);
return false;
}
break;
case JSON_STRING:
if (next == '"') {
if (contexts.length == 0) {
emit_string(token, current, reader);
state = JSON_END;
} else if (contexts.last() == JSON_CONTEXT_KEY) {
emit_key(token, current, reader);
state = JSON_COLON;
} else {
emit_string(token, current, reader);
state = JSON_COMMA;
}
token.clear();
} else if (next == '\\') {
next = fgetc(in);
current.column++;
emit_escape(in, token, next, current);
} else {
if (!token.add(next)) return false;
}
break;
case JSON_KEYWORD:
if (next == ',') {
emit_keyword(token, current, reader);
token.clear();
if (contexts.length == 0) {
state = JSON_END;
} else if (contexts.last() == JSON_CONTEXT_VALUE) {
state = JSON_KEY;
contexts.last() = JSON_CONTEXT_KEY;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_LIST */
state = JSON_VALUE;
}
} else if (next == '}') {
emit_keyword(token, current, reader);
token.clear();
if (contexts.length == 0 || contexts.last() != JSON_CONTEXT_VALUE) {
read_error("Unexpected closing brace '}'", current);
return false;
}
end_object(current, reader);
contexts.pop();
if (contexts.length == 0) {
state = JSON_END;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_VALUE or JSON_CONTEXT_LIST */
state = JSON_COMMA;
}
} else if (next == ']') {
emit_keyword(token, current, reader);
token.clear();
if (contexts.length == 0 || contexts.last() != JSON_CONTEXT_LIST) {
read_error("Unexpected closing brace ']'", current);
return false;
}
end_list(current, reader);
contexts.pop();
if (contexts.length == 0) {
state = JSON_END;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_VALUE or JSON_CONTEXT_LIST */
state = JSON_COMMA;
}
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
emit_keyword(token, current, reader);
token.clear();
if (contexts.length == 0) {
state = JSON_END;
} else if (contexts.last() == JSON_CONTEXT_LIST || contexts.last() == JSON_CONTEXT_VALUE) {
state = JSON_COMMA;
}
} else {
if (!token.add(next)) return false;
}
break;
case JSON_NUMBER:
if (isdigit(next) || next == '+' || next == '-' || next == '.' || next == 'e' || next == 'E') {
if (!token.add(next)) return false;
} else if (next == ',') {
emit_number(token, current, reader);
token.clear();
if (contexts.length == 0) {
state = JSON_END;
} else if (contexts.last() == JSON_CONTEXT_VALUE) {
state = JSON_KEY;
contexts.last() = JSON_CONTEXT_KEY;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_LIST */
state = JSON_VALUE;
}
} else if (next == '}') {
emit_number(token, current, reader);
token.clear();
if (contexts.length == 0 || contexts.last() != JSON_CONTEXT_VALUE) {
read_error("Unexpected closing brace '}'", current);
return false;
}
end_object(current, reader);
contexts.pop();
if (contexts.length == 0) {
state = JSON_END;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_VALUE or JSON_CONTEXT_LIST */
state = JSON_COMMA;
}
} else if (next == ']') {
emit_number(token, current, reader);
token.clear();
if (contexts.length == 0 || contexts.last() != JSON_CONTEXT_LIST) {
read_error("Unexpected closing brace ']'", current);
return false;
}
end_list(current, reader);
contexts.pop();
if (contexts.length == 0) {
state = JSON_END;
} else { /* we don't check the previous context since the FSM only allows for JSON_CONTEXT_VALUE or JSON_CONTEXT_LIST */
state = JSON_COMMA;
}
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
emit_number(token, current, reader);
token.clear();
if (contexts.length == 0) {
state = JSON_END;
} else if (contexts.last() == JSON_CONTEXT_LIST || contexts.last() == JSON_CONTEXT_VALUE) {
state = JSON_COMMA;
}
} else {
read_error("Unexpected symbol inside number", current);
return false;
}
break;
case JSON_END:
if (next != ' ' && next != '\t' && next != '\n' && next == '\r') {
read_error("Unexpected symbol. Expected end of input", current);
return false;
}
}
if (new_line) {
current.line++;
current.column = 1;
new_line = false;
} else current.column++;
next = fgetc(in);
}
if (state != JSON_END) {
read_error("Unexpected end of input", current);
return false;
}
return true;
}
#endif /* JSON_H_ */