-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.cpp
More file actions
374 lines (330 loc) · 11.9 KB
/
json_parser.cpp
File metadata and controls
374 lines (330 loc) · 11.9 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
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
struct Json_object;
struct Json_array;
struct Json_value {
enum Type: u8 {
INVALID, OBJECT, ARRAY, STRING, INTEGER, NUMBER, BOOLEAN, UNDEFINED,
TYPE_COUNT
};
Array<u8> data;
u64 handle;
u8 type() { return handle >> 56; }
u64 offset() { return handle & ((1ull << 56) - 1); }
static Json_value make(Array<u8> data, u8 type, u64 offset=0) {
assert(0 < type and type < TYPE_COUNT);
assert(offset >> 56 == 0);
return {data, (u64)type << 56 | offset};
}
template <typename T>
Array<T> get_array() { return {(T*)&data[offset()+8], *(s64*)&data[offset()]}; }
Json_object* obj() { return type() == OBJECT ? (Json_object*)this : nullptr; }
Json_array* arr() { return type() == ARRAY ? (Json_array*) this : nullptr; }
Array<u8> str(bool* err=nullptr) {
if (type() != STRING) {
if (err) *err = true;
return {};
}
return get_array<u8>();
}
s64 integer(bool* err=nullptr) {
if (type() != INTEGER) {
if (err) *err = true;
return -1;
}
u64 val = offset();
if (val >> 57) val |= 0xffull << 56ull;
return (s64)val;
}
double number(bool* err=nullptr) {
if (type() != NUMBER) {
if (err) *err = true;
return -1;
}
return *(double*)&data[offset()+8];
}
double boolean(bool* err=nullptr) {
if (type() != BOOLEAN) {
if (err) *err = true;
return false;
}
return offset();
}
bool is_null() { return type() == UNDEFINED; }
};
struct Json_object_entry {
Json_value key, val;
};
struct Json_object_iter {
Array<u8> data;
u64 offset;
Json_object_entry operator* () { return {{data, *(u64*)&data[offset]}, {data, *(u64*)&data[offset+8]}}; }
Json_object_iter& operator++ () { offset += 16; return *this; }
bool operator!= (Json_object_iter o) { return data.data != o.data.data or offset != o.offset; }
};
struct Json_object: Json_value {
struct Entry {
u64 key, val;
};
Array<Entry> entries() { return get_array<Entry>(); }
s64 size() { return *(s64*)&data[offset()]; }
Json_object_iter begin() { return {data, offset()+8}; }
Json_object_iter end() { return {data, offset()+8 + 8*size()}; }
Json_value operator[] (char const* str) { return (*this)[array_from_str(str)]; }
Json_value operator[] (Array<u8> str) {
for (auto i: entries()) {
Json_value i_key {data, i.key};
Json_value i_val {data, i.val};
if (array_equal(i_key.str(), str)) return i_val;
}
return Json_value::make(data, Json_value::UNDEFINED);
}
bool contains(Array<u8> str) {
for (auto i: entries()) {
Json_value i_key {data, i.key};
if (array_equal(i_key.str(), str)) return true;
}
return false;
}
};
struct Json_array_iter {
Array<u8> data;
u64 offset;
Json_value operator* () { return {data, *(u64*)&data[offset]}; }
Json_array_iter& operator++ () { offset += 8; return *this; }
bool operator!= (Json_array_iter o) { return data.data != o.data.data or offset != o.offset; }
};
struct Json_array: Json_value {
s64 size() { return *(s64*)&data[offset()]; }
Json_array_iter begin() { return {data, offset()+8}; }
Json_array_iter end() { return {data, offset()+8 + 8*size()}; }
Json_value operator[] (s64 i) {
return {data, get_array<u64>()[i]};
}
};
struct Json_parser {
Array_dyn<u64> stack;
Array_dyn<u8> data;
Array<u8> input;
s64 index;
Status* status;
};
u8 json_parser_peek(Json_parser* parser) {
if (parser->status->bad()) return 0;
while (parser->index < parser->input.size) {
u8 c = parser->input[parser->index];
if (c == ' ' or c == '\t' or c == '\n' or c == '\r') {
++parser->index;
continue;
}
if (c == 0) break;
return c;
}
os_error_print(parser->status, 20250609001, "unexpected eof\n");
return 0;
}
void json_parser_match(Json_parser* parser, u8 str) {
u8 c = json_parser_peek(parser);
if (not c) return;
if (c != str) {
return os_error_print(parser->status, 20250609002, "unexpected character %C (expected %C)\n", c, str);
}
++parser->index;
}
void json_parser_match(Json_parser* parser, Array<u8> str) {
for (s64 i = 0; i < str.size; ++i) {
json_parser_match(parser, str[i]);
}
}
void json_parser_value(Json_parser* parser);
void json_parser_string(Json_parser* parser) {
json_parser_match(parser, '"');
if (parser->status->bad()) return;
u8 state = 0;
s64 offset = parser->data.size;
array_append_zero(&parser->data, 8);
while (true) {
if (parser->index >= parser->input.size) {
return os_error_print(parser->status, 20250609003, "unexpected eof while reading string literal\n");
}
u8 c = parser->input[parser->index++];
if (state == 0) {
if (c == '\\') {
state = 1;
} else if (c == '"') {
break;
} else {
array_push(&parser->data, c);
}
} else if (state == 1) {
if (c == '"' or c == '\\' or c == '/') {
array_push(&parser->data, c);
} else if (c == 'b') { array_push(&parser->data, '\b');
} else if (c == 'f') { array_push(&parser->data, '\f');
} else if (c == 'n') { array_push(&parser->data, '\n');
} else if (c == 'r') { array_push(&parser->data, '\r');
} else if (c == 't') { array_push(&parser->data, '\t');
} else if (c == 'u') {
if (parser->index+4 >= parser->data.size) {
return os_error_print(parser->status, 20250609004, "unexpected eof while reading unicode escape sequence\n");
}
u32 cp = 0;
for (s64 i = 0; i < 4; ++i) {
u8 cc = parser->input[parser->index++];
if ('0' <= cc and cc <= '9') {
cp = 16*cp + (cc - '0');
} else if ('a' <= cc and cc <= 'z') {
cp = 16*cp + (cc - 'a' + 10);
} else if ('A' <= cc and cc <= 'Z') {
cp = 16*cp + (cc - 'A' + 10);
} else {
return os_error_print(parser->status, 20250609005, "invalid character %C in unicode escape sequence\n", cc);
}
}
if (not utf8_encode(cp, &parser->data)) {
return os_error_print(parser->status, 20250609006, "invalid codepoint U+%x\n", cp);
}
} else {
return os_error_print(parser->status, 20250609007, "invalid escape character %C\n", c);
}
state = 0;
} else assert(false);
}
*(s64*)&parser->data[offset] = parser->data.size - (offset+8);
array_push(&parser->stack, offset | (u64)Json_value::STRING << 56);
}
void json_parser_object(Json_parser* parser) {
json_parser_match(parser, '{');
s64 index = parser->stack.size;
bool first = true;
while (true) {
u8 c = json_parser_peek(parser);
if (c == '}') {
++parser->index;
break;
} else if (first) {
first = false;
} else {
json_parser_match(parser, ',');
}
json_parser_string(parser);
json_parser_match(parser, ':');
json_parser_value(parser);
if (parser->status->bad()) return;
}
u64 offset = parser->data.size;
array_append(&parser->data, array_bytes_arr(array_subarray(parser->stack, index)));
parser->stack.size = index;
array_push(&parser->stack, offset | (u64)Json_value::OBJECT << 56);
}
void json_parser_array(Json_parser* parser) {
json_parser_match(parser, '[');
s64 index = parser->stack.size;
bool first = true;
while (true) {
u8 c = json_parser_peek(parser);
if (c == ']') {
++parser->index;
break;
} else if (first) {
first = false;
} else {
json_parser_match(parser, ',');
}
json_parser_value(parser);
if (parser->status->bad()) return;
}
u64 offset = parser->data.size;
array_append(&parser->data, array_bytes_arr(array_subarray(parser->stack, index)));
parser->stack.size = index;
array_push(&parser->stack, offset | (u64)Json_value::ARRAY << 56);
}
void json_parser_atom(Json_parser* parser, u8 c) {
if (c == 't') {
json_parser_match(parser, "true"_arr);
array_push(&parser->stack, 1 | (u64)Json_value::BOOLEAN << 56);
} else if (c == 'f') {
json_parser_match(parser, "false"_arr);
array_push(&parser->stack, 0 | (u64)Json_value::BOOLEAN << 56);
} else if (c == 'n') {
json_parser_match(parser, "null"_arr);
array_push(&parser->stack, 0 | (u64)Json_value::UNDEFINED << 56);
} else assert(false);
}
void json_parser_number(Json_parser* parser) {
if (parser->status->bad()) return;
s64 index = parser->index;
u8 state = 0;
s8 trans[9][6] = {
{2,3,1,-1,-1,-1},{2,3,-1,-1,-1,-1},{-1,-1,-1,-1,6,4},
{3,3,-1,-1,6,4},{5,5,-1,-1,-1,-1},{5,5,-1,-1,6,-1},
{-1,-1,7,7,-1,-1},{8,8,-1,-1,-1,-1},{8,8,-1,-1,-1,-1}
};
while (true) {
u8 c = parser->index < parser->input.size ? parser->input[parser->index] : 0;
u8 cc = -1;
if (c == '0') cc = 0;
else if ('1' <= c and c <= '9') cc = 1;
else if (c == '-') cc = 2;
else if (c == '+') cc = 3;
else if (c == 'e' or c == 'E') cc = 4;
else if (c == '.') cc = 5;
else break;
++parser->index;
state = trans[state][cc];
}
if (not (state == 2 or state == 3 or state == 5 or state == 8)) {
return os_error_print(parser->status, 20250609007, "invalid number format\n");
}
auto arr = array_subarray(parser->input, index, parser->index);
if (state == 2 or state == 3) {
s64 val = parse_int(arr);
array_push(&parser->stack, (val & ((1ull << 56)-1)) | (u64)Json_value::INTEGER << 56);
} else {
#ifdef PHILIB_NUMBER
double val = number_parse<double>(arr, parser->status));
if (parser->status->bad()) return;
u64 offset = parser->data.size;
array_append(&parser->data, array_bytes(&val));
array_push(&parser->stack, offset | (u64)Json_value::NUMBER << 56);
#else
assert(false);
#endif
}
}
void json_parser_value(Json_parser* parser) {
u8 c = json_parser_peek(parser);
if (c == 0) return;
if (c == '{') {
json_parser_object(parser);
} else if (c == '[') {
json_parser_array(parser);
} else if (c == '"') {
json_parser_string(parser);
} else if (c == 't' or c == 'f' or c == 'n') {
json_parser_atom(parser, c);
} else if ('0' <= c and c <= '9') {
json_parser_number(parser);
} else {
return os_error_print(parser->status, 20250609008, "invalid value starting with %c\n", c);
}
}
void json_parser_parse(Json_parser* parser, Array<u8> input, Status* status=nullptr) {
if (os_status_initp(&status)) return;
parser->input = input;
parser->index = 0;
parser->status = status;
json_parser_value(parser);
assert(parser->stack.size == 1);
}
Json_value json_parse(Array<u8> input, Status* status=nullptr) {
if (os_status_initp(&status)) return Json_value::make({}, Json_value::UNDEFINED);;
Json_parser parser;
json_parser_parse(&parser, input, status);
if (status->bad()) {
array_free(&parser.stack);
array_free(&parser.data);
return Json_value::make({}, Json_value::UNDEFINED);
}
Json_value val {parser.data, parser.stack[0]};
array_free(&parser.stack);
return val;
}