-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
537 lines (452 loc) · 10.9 KB
/
Copy pathparser.c
File metadata and controls
537 lines (452 loc) · 10.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"
#include "limits.h"
#include "stdarg.h"
#include "string.h"
#include "errno.h"
#include "nano/json.h"
/*
FALSE false
TRUE true
NULL null
SPACE '\032' | '\r' | '\n' | '\t'
number [-]? [0-9]+ ('.' [0-9]* ([Ee] [0-9]+)? )?
string '"' ( '\' ( ["\/bfnrt] | u[0-9A-Fa-f]{4} ) | [^\] ) * '"'
array '[' (json (',' json)*)? ']'
object '{' (string : json (',' string : json)*)? '}'
json object | array | TRUE | FALSE | NULL | number | string
*/
typedef
struct jsn_parser jsn_parser_t;
/* ------------------------------------------------------------------------ */
struct jsn_parser {
char *text; /* source text */
char *ptr; /* current parser position */
jsn_t *pool; /* array of json nodes */
size_t free_node_index; /* index of first free node */
size_t pool_size; /* total array size */
jsn_t *(* alloc)(jsn_parser_t *p);
};
/* ------------------------------------------------------------------------ */
static int is_alpha_char(int c)
{
return c <= 'Z' ? 'A' <= c : 'a' <= c && c <= 'z';
}
/* ------------------------------------------------------------------------ */
static int is_id_char(int c)
{
if (c < 'A')
return '0' <= c && c <= '9';
if (c >= 'a')
return c <= 'z' || c == '_';
return c <= 'Z';
}
/* ------------------------------------------------------------------------ */
static int after_space(char **p)
{
char *s = *p;
for (uint32_t ch; ch = *s, ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'; ++s)
;
return *(*p = s);
}
/* ------------------------------------------------------------------------ */
static int match_char(char **p, int ch)
{
return after_space(p) == ch ? (++*p, 1) : 0;
}
/* ------------------------------------------------------------------------ */
int match_number(char **p, jsn_t *obj)
{
char *s = *p;
char *c = s;
if (*c == '-')
++c;
#ifndef JSON_HEX_NUMBERS
if (c[0] == '0' && (c[1] == 'x' || c[1] == 'X'))
return 0;
#endif
while ('0' <= *c && *c <= '9')
++c;
#ifdef JSON_FLOATS
if (*c == '.' || *c == 'E' || *c == 'e') {
obj->data.floating = strtod(s, p);
if (s == *p)
return 0;
return obj->type = JS_FLOAT;
}
#endif
#ifdef JSON_64BITS_INTEGERS
#if LLONG_MAX == INT64_MAX
obj->data.number = strtoll(s, p, 0);
#else
#if LONG_MAX == INT64_MAX
obj->data.number = strtol(s, p, 0);
#else
#error "impossible to choose of 64 bits strtol function"
#endif
#endif
#else
#if LLONG_MAX == INT32_MAX
obj->data.number = strtoll(s, p, 0);
#else
#if LONG_MAX == INT32_MAX
obj->data.number = strtol(s, p, 0);
#else
#if LONG_MAX > INT32_MAX
long l = strtol(s, p, 0);
if (l > INT32_MAX) l = INT32_MAX;
else if (l < INT32_MIN) l = INT32_MIN;
obj->data.number = l;
#else
#error "impossible to choose of 32 bits strtol function"
#endif
#endif
#endif
#endif
return *p == s ? 0 : (obj->type = JS_NUMBER);
}
/* ------------------------------------------------------------------------ */
static int hextonibble(char digit)
{
switch (digit >> 6) {
case 1:;
int n = (digit & ~32) - 'A' + 10;
return 9 < n && n < 16 ? n : 256;
case 0:
digit -= '0';
if (0 <= digit && digit < 10)
return digit;
}
return 256;
}
/* ------------------------------------------------------------------------ */
int string_unescape(char *d, char *s)
{
for (; *s && *s != '"'; ++s) {
if (*s == '\\') {
switch (*++s) {
case '"': *d++ = '"'; continue;
case '/': *d++ = '/'; continue;
case '\\':*d++ = '\\'; continue;
case 'b': *d++ = '\b'; continue;
case 'f': *d++ = '\f'; continue;
case 'n': *d++ = '\n'; continue;
case 'r': *d++ = '\r'; continue;
case 't': *d++ = '\t'; continue;
case 'u': {
uint32_t uc = 0;
for (int i = 1; i < 5; ++i) {
uint32_t d = hextonibble(s[i]);
if (d > 15) {
--s;
goto _not_escape;
}
uc = uc * 16 + d;
}
s += 4;
if (!(uc & 0xFF80))
*d++ = uc;
else
if (!(uc & 0xF800)) {
*d++ = 0xC0 | (uc >> 6);
*d++ = 0x80 | (uc & 0x3F);
} else {
*d++ = 0xE0 | (uc >> 12);
*d++ = 0x80 | (0x3F & uc >> 6);
*d++ = 0x80 | (0x3F & uc);
}
} continue;
default:
if (!*s)
goto _fail;
}
}
_not_escape:
*d++ = *s;
}
_fail:
*d = 0;
return 0;
}
/* ------------------------------------------------------------------------ */
static int match_string(char **p, char **str)
{
char *s = *p;
if (*s != '"')
return 0;
for (++s; *s && *s != '"'; ++s) {
if (*s == '\\') {
switch (*++s) {
case '"':
case '/':
case '\\':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
break;
case 'u': {
for (int i = 1; i < 5; ++i) {
uint32_t d = hextonibble(s[i]);
if (d > 15) {
--s;
goto _not_escape;
}
}
s += 4;
} break;
default:
--s;
goto _not_escape;
}
} else
_not_escape:;
}
if (*s == '"') {
*str = *p + 1;
*p = s + 1;
return 1;
}
return 0;
}
/* ------------------------------------------------------------------------ */
static int match_json(jsn_parser_t *p, jsn_t *obj)
{
int open_char = after_space(&p->ptr);
if (open_char != '[' && open_char != '{' ) {
char *s = p->ptr;
switch (open_char) {
case '"':
if (!match_string(&p->ptr, &obj->data.string))
return errno = EINVAL, 0;
return obj->type = JS_STRING;
case 'n':
if (s[1] != 'u' || s[2] != 'l' || s[3] != 'l' || is_id_char(s[4]))
return errno = EINVAL, 0;
p->ptr = s + 4;
return obj->type = JS_NULL;
case 't':
if (s[1] != 'r' || s[2] != 'u' || s[3] != 'e' || is_id_char(s[4]))
return errno = EINVAL, 0;
p->ptr = s + 4;
obj->data.number = 1;
return obj->type = JS_BOOLEAN;
case 'f':
if (s[1] != 'a' || s[2] != 'l' || s[3] != 's' || s[4] != 'e' || is_id_char(s[5]))
return errno = EINVAL, 0;
p->ptr = s + 5;
obj->data.number = 0;
return obj->type = JS_BOOLEAN;
default:
return match_number(&p->ptr, obj) ?: ( errno = EINVAL, 0);
}
}
p->ptr += 1;
int is_object = open_char == '{';
int close_char = is_object ? '}' : ']';
int index = 0;
if (match_char(&p->ptr, close_char))
goto _empty;
int prev_ofs = obj - p->pool;
int obj_ofs = obj - p->pool;
do {
jsn_t *node = p->alloc(p);
if (!node)
return 0;
int node_ofs = (int)(node - p->pool);
if (obj_ofs != prev_ofs)
p->pool[prev_ofs].next = node_ofs - obj_ofs;
prev_ofs = node_ofs;
after_space(&p->ptr);
if (is_object) {
if (!match_string(&p->ptr, &node->id.string))
return errno = EINVAL, 0;
node->id_type = JS_STRING;
if (!match_char(&p->ptr, ':'))
return errno = EINVAL, 0;
} else {
node->id.number = index;
node->id_type = JS_NUMBER;
}
if (!match_json(p, node))
return 0;
++index;
} while (match_char(&p->ptr, ','));
if (!match_char(&p->ptr, close_char))
return errno = EINVAL, 0;
obj = p->pool + obj_ofs;
_empty:
obj->data.length = index;
return obj->type = (is_object ? JS_OBJECT : JS_ARRAY);
}
/* ------------------------------------------------------------------------ */
static int basic_parse(jsn_parser_t *p)
{
if (!match_json(p, p->alloc(p)))
return p->text - p->ptr; // return negative offset to error
if (after_space(&p->ptr))
return errno = EMSGSIZE, p->text - p->ptr;
for (int i = 0; i < p->free_node_index; ++i) {
jsn_t *node = p->pool + i;
if (node->type == JS_STRING)
string_unescape(node->data.string, node->data.string);
if (node->id_type == JS_STRING)
string_unescape(node->id.string, node->id.string);
}
return p->free_node_index; // return number of parsed js nodes (>0)
}
/* ------------------------------------------------------------------------ */
static jsn_t *jsn_alloc(jsn_parser_t *p)
{
if (p->free_node_index >= p->pool_size)
return errno = ENOMEM, NULL;
jsn_t *j = p->pool + p->free_node_index++;
j->next = 0;
j->type = 0;
return j;
}
/* ------------------------------------------------------------------------ */
int json_parse(jsn_t *pool, size_t size, char *text)
{
jsn_parser_t p = {
.text = text,
.ptr = text,
.pool = pool,
.free_node_index = 0,
.pool_size = size,
.alloc = jsn_alloc
};
return basic_parse(&p);
}
#ifdef JSON_AUTO_PARSE_FN
/* ------------------------------------------------------------------------ */
static jsn_t *jsn_realloc(jsn_parser_t *p)
{
if (p->free_node_index >= p->pool_size) {
jsn_t *pool = realloc(p->pool, sizeof(jsn_t) * JSON_AUTO_PARSE_POOL_INCREASE(p->pool_size));
if (!pool)
return NULL;
p->pool = pool;
p->pool_size = JSON_AUTO_PARSE_POOL_INCREASE(p->pool_size);
}
return jsn_alloc(p);
}
/* ------------------------------------------------------------------------ */
static int jsn_free_tail(jsn_parser_t *p)
{
if (p->free_node_index < p->pool_size) {
jsn_t *pool = realloc(p->pool, sizeof(jsn_t) * p->free_node_index);
if (!pool)
return -1;
p->pool = pool;
}
return 0;
}
/* ------------------------------------------------------------------------ */
jsn_t *json_auto_parse(char *text, char **end)
{
jsn_parser_t p = {
.text = text,
.ptr = text,
.free_node_index = 0,
.pool_size = JSON_AUTO_PARSE_POOL_START_SIZE,
.pool = malloc(JSON_AUTO_PARSE_POOL_START_SIZE * sizeof(jsn_t)),
.alloc = jsn_realloc
};
if (!p.pool)
return NULL;
int len = basic_parse(&p);
if (end)
*end = p.ptr;
if (len <= 0) {
free(p.pool);
return NULL;
} else
jsn_free_tail(&p);
return p.pool;
}
#endif /* JSON_AUTO_PARSE */
#ifdef JSON_GET_FN
static int match_id(char **p, char *id)
{
if (!is_alpha_char(**p))
return 0;
char *s = *p;
do {
++s;
} while (is_id_char(*s));
size_t len = (unsigned)(s - *p);
if (len > JSON_MAX_ID_LENGTH)
return 0;
memcpy(id, *p, len);
id[len] = 0;
*p = s;
return 1;
}
static int match_inum(char **p, int *num)
{
if (**p < '0' || '9' < **p)
return 0;
int v = 0;
char *s = *p;
do {
v = 10 * v + (*s - '0');
++s;
} while (is_id_char(*s));
*num = v;
*p = s;
return 1;
}
/* ------------------------------------------------------------------------ */
jsn_t *json_get(jsn_t *obj, char const *path)
{
/*
ID [a-zA-Z] [a-zA-Z0-9_]*
INUM [0-9]+
INDEX '[' ( INUM | string ) ']'
path ( '.' ID | INDEX ) )*
*/
char *p = (char *)path;
after_space(&p);
while (*p && obj) {
switch (*p) {
case '.': {
char id[JSON_MAX_ID_LENGTH];
++p;
if (!match_id(&p, id))
return errno = EINVAL, NULL;
obj = json_item(obj, id);
continue;
}
case '[': {
int i;
++p;
if (match_inum(&p, &i)) {
if (!match_char(&p, ']'))
return errno = EINVAL, NULL;
obj = json_cell(obj, i);
continue;
} else {
char *s;
if (match_string(&p, &s)) {
if (!match_char(&p, ']'))
return errno = EINVAL, NULL;
char *str = (char *)malloc(p - s);
string_unescape(str, s);
obj = json_item(obj, str);
free(str);
continue;
}
}
return errno = EINVAL, NULL;
}
default:
return errno = EINVAL, NULL;
}
after_space(&p);
}
return obj;
}
#endif /* JSON_GET_FN */