-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsb.c
1535 lines (1391 loc) · 36.5 KB
/
jsb.c
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if RELEASE
#undef NDEBUG
#define NDEBUG
#undef DEBUG
#else
#include<string.h>
#endif
#include"jsb.h"
#include<sys/types.h>
#include<alloca.h>
#ifdef NDEBUG
#define assert(cond) ((void)0)
#else
#include<assert.h>
#include<stdarg.h>
#endif
#ifndef PRIVATE
#define PRIVATE static
#endif
#if RELEASE || !defined(DEBUG)
#define debug(args) do{}while(0)
#define GOTO(x) goto x
#else
#include<stdio.h>
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#define debug(args) do{ fprintf(stderr, "%d: ", __LINE__); dbg args; }while(0)
static void dbg(char *fmt, ...){
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
#define GOTO(x) do{ debug(("goto %s\n", #x)); goto x; }while(0)
#endif
#ifndef STATIC_ASSERT
#if defined( __STDC_VERSION__ ) && __STDC_VERSION__ >= 201112L
#define STATIC_ASSERT(e, m) _Static_assert((e), m)
#else
#define ASSERT_CONCAT_(a, b) a##b
#define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
#define STATIC_ASSERT(e, m) enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(int)(!!(e)) }
#endif
#endif
/* bytes 0xc0 and 0xc1 are both invalid in JSON as well as our binary format
* we'll borrow one to use internally as the EOF marker */
#define JSB_INT_EOF 0xc1
/*#define PICK(x, a, b) ((x) ? a : b)*/
/*#define PICK(x, a, b) ((a) ^ (!(x) * ((a)^(b))))*/
#define PICK(x, a, b) ((b) ^ (!!(x) * ((a)^(b))))
/*#define PICK(x, a, b) ((b) ^ ((x) * ((a)^(b))))*/
#define XND 8 /* xor to toggle JSB_ARR/JSB_OBJ to JSB_ARR_END/JSB_OBJ_END */
#define XAO 1 /* xor to toggle JSB_ARR/JSB_ARR_END to JSB_OBJ/JSB_OBJ_END */
STATIC_ASSERT(JSB_OBJ + 1 == JSB_ARR, "chk: obj + 1 == arr");
STATIC_ASSERT(JSB_OBJ_END + 1 == JSB_ARR_END, "chk: obj_end + 1 == arr_end");
STATIC_ASSERT((JSB_ARR ^ JSB_ARR_END) == XND, "chk: arr ^ arr_end == XND");
STATIC_ASSERT((JSB_OBJ ^ JSB_OBJ_END) == XND, "chk: obj ^ obj_end == XND");
STATIC_ASSERT((JSB_ARR & XAO) == 0, "chk: arr & XAO == 0");
STATIC_ASSERT((JSB_OBJ & XAO) == XAO, "chk: obj & XAO == XAO");
STATIC_ASSERT(('a' & 0x80) == 0, "assume 7-bit ascii");
STATIC_ASSERT(('l' & 0x80) == 0, "assume 7-bit ascii");
STATIC_ASSERT(('s' & 0x80) == 0, "assume 7-bit ascii");
STATIC_ASSERT(('e' & 0x80) == 0, "assume 7-bit ascii");
STATIC_ASSERT(('r' & 0x80) == 0, "assume 7-bit ascii");
STATIC_ASSERT(('u' & 0x80) == 0, "assume 7-bit ascii");
STATIC_ASSERT(('l' & 0x80) == 0, "assume 7-bit ascii");
STATIC_ASSERT((']' & 0x80) == 0, "assume 7-bit ascii");
STATIC_ASSERT(('}' & 0x80) == 0, "assume 7-bit ascii");
/* the _jsb_update() function is designed as a coroutine
* (see: https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html)
* but use the gcc/clang __COUNTER__ macro instead of ANSI C's __LINE__ */
/* ensure the __COUNTER__ macro provides a increasing numeric sequence: */
STATIC_ASSERT(__COUNTER__ + 1 == __COUNTER__, "check counter");
/* if it doesn't, you can a)
* in the build system, try something along the lines of:
* gcc -D__COUNTER__=__COUNTER__ -E jsb.c | perl -lnpe 's/\b__COUNTER__\b/$i++/ge' | gcc -O3 -xc -c -o jsb.o -
* or b)
* switch to using __LINE__, and
* use a larger size integer for jsb->state
*/
/*
* Utility functions
*/
static int mcmp(const void *s1, const void *s2, size_t n){
const uint8_t *a = s1, *b = s2;
int r;
while(n--)
if((r = *a++ - *b++))
return (r > 0) - (r < 0);
return 0;
}
static size_t strsz(const void *s){
const uint8_t *c = s;
size_t n;
assert(s);
while(*c)
c++;
n = c - (const uint8_t *)s;
#if !RELEASE
assert(strlen(s) == n);
#endif
return n;
}
/* ensure ASCII-based assumptions are valid */
/* for space() below */
STATIC_ASSERT('\t' < ' ', "tab");
STATIC_ASSERT('\r' < ' ', "cr");
STATIC_ASSERT('\n' < ' ', "lf");
/* for digit()/pdigit()/hex()/nibble() below */
STATIC_ASSERT('0' + 1 == '1', "01");
STATIC_ASSERT('1' + 1 == '2', "12");
STATIC_ASSERT('2' + 1 == '3', "23");
STATIC_ASSERT('3' + 1 == '4', "34");
STATIC_ASSERT('4' + 1 == '5', "45");
STATIC_ASSERT('5' + 1 == '6', "56");
STATIC_ASSERT('6' + 1 == '7', "67");
STATIC_ASSERT('7' + 1 == '8', "78");
STATIC_ASSERT('8' + 1 == '9', "89");
/* for hex()/nibble() below */
STATIC_ASSERT('a' + 1 == 'b', "ab");
STATIC_ASSERT('b' + 1 == 'c', "bc");
STATIC_ASSERT('c' + 1 == 'd', "cd");
STATIC_ASSERT('d' + 1 == 'e', "de");
STATIC_ASSERT('e' + 1 == 'f', "ef");
/* for hex() below */
STATIC_ASSERT('A' + 1 == 'B', "AB");
STATIC_ASSERT('B' + 1 == 'C', "BC");
STATIC_ASSERT('C' + 1 == 'D', "CD");
STATIC_ASSERT('D' + 1 == 'E', "DE");
STATIC_ASSERT('E' + 1 == 'F', "EF");
/* return 1 if supplied character is JSON-flavored whitespace */
PRIVATE uint8_t space(uint8_t ch){
if(ch > ' ') return 0;
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
}
/* digit */
PRIVATE uint8_t digit(uint8_t ch){
return ch >= '0' && ch <= '9';
}
/* positive digit */
PRIVATE uint8_t pdigit(uint8_t ch){
return ch >= '1' && ch <= '9';
}
/* map hexidecimal character to numeric value or -1 on error */
PRIVATE int hex(uint8_t ch){
if(ch >= '0' && ch <= '9')
return ch - '0';
if(ch >= 'a' && ch <= 'f')
return ch - ('a' - 0xa);
if(ch >= 'A' && ch <= 'F')
return ch - ('A' - 0xa);
return -1;
}
/* map lower 4 bits to hexidecimal character */
PRIVATE uint8_t nibble(uint8_t ch){
ch &= 0xf;
return ch + (ch < 0xa ? '0' : 'a' - 0xa);
}
/* generic heap routines */
typedef int heap_test(void *data, size_t a, size_t b);
typedef void heap_swap(void *data, size_t a, size_t b);
PRIVATE void heap_siftdown(void *data, size_t n, heap_test test, heap_swap swap, size_t i){
size_t t = i;
size_t a = 2 * i + 1;
size_t b = 2 * i + 2;
if (n > a && test(data, t, a))
t = a;
if (n > b && test(data, t, b))
t = b;
if(i != t){
swap(data, i, t);
heap_siftdown(data, n, test, swap, t);
}
}
PRIVATE void heap_ify(void *data, size_t n, heap_test test, heap_swap swap){
size_t i = n / 2;
while(i--)
heap_siftdown(data, n, test, swap, i);
}
PRIVATE void heap_sort(void *data, size_t n, heap_test test, heap_swap swap){
heap_ify(data, n, test, swap);
while(n-- > 1){
swap(data, 0, n);
heap_siftdown(data, n, test, swap, 0);
}
}
/* indexing support */
#define IDX_HDR 2
#define IDX_FTR 1
#define IDX_PAD (IDX_HDR + IDX_FTR)
typedef size_t node_t[3];
PRIVATE void swap_nodes(void *data, size_t a, size_t b){
node_t *nodes = data;
size_t i, t;
assert(a != b);
for(i = 0; i < 3; i++){
t = nodes[a][i];
nodes[a][i] = nodes[b][i];
nodes[b][i] = t;
}
}
/* used for deciding what to keep in the index - minheap by size/count */
PRIVATE int sz_a_gt_b(node_t a, node_t b){
assert(a != b);
return a[1] > b[1] || (a[1] == b[1] && a[2] > b[2]);
}
/* wrapper for the above */
PRIVATE int hs_sz_a_gt_b(void *data, size_t a, size_t b){
node_t *nodes = data;
return sz_a_gt_b(nodes[a], nodes[b]);
}
/* used for second pass - maxheap by offset */
PRIVATE int hs_off_a_lt_b(void *data, size_t a, size_t b){
node_t *nodes = data;
assert(a != b);
return nodes[a][0] < nodes[b][0];
}
PRIVATE void idx_insert(size_t n, size_t *idx, node_t v){
node_t *arr = (node_t *)(idx + IDX_HDR);
size_t j, i = idx[0];
if(i < n){
/* quick append if index isn't full */
for(j = 0; j < 3; j++)
arr[i][j] = v[j];
idx[0] = ++i;
return;
}
if(i == n){
/* heapify records if we've filled the array and have more to insert */
heap_ify(arr, n, hs_sz_a_gt_b, swap_nodes);
idx[0] = SIZE_MAX;
}
/* if current value is bigger than the smallest one in the index */
if(sz_a_gt_b(v, arr[0])){
/* first overwrite root node */
for(j = 0; j < 3; j++)
arr[0][j] = v[j];
/* then fix the heap */
heap_siftdown(arr, n, hs_sz_a_gt_b, swap_nodes, 0);
}
}
PRIVATE size_t idx_finish(size_t *idx, size_t n){
size_t i = (n < idx[0]) ? (idx[0] = n) : (n = idx[0]);
node_t *arr = (node_t *)(idx + IDX_HDR);
/* now heapsort on byte-offset column, low -> high */
heap_sort(arr, i, hs_off_a_lt_b, swap_nodes);
/* and cap it with an out-of-range offset */
arr[i][0] = SIZE_MAX;
return n;
}
/* return first index node >= supplied offset, possibly off=SIZE_MAX */
PRIVATE const size_t *idx_seek(const size_t *idx, size_t off){
size_t ret, mid, lo = 0, hi = ret = idx[0];
node_t *nodes = (node_t *)(idx + IDX_HDR);
while(lo < hi){
mid = (lo >> 1) + (hi >> 1) + (lo & hi & 1);
if(nodes[mid][0] < off)
ret = lo = mid + 1;
else
ret = hi = mid;
}
assert(nodes[ret][0] >= off);
return (size_t *)nodes[ret];
}
PRIVATE const size_t *idx_find(const size_t *idx, size_t off){
const size_t *r = NULL;
if(idx && idx[0]){
r = idx_seek(idx, off);
if(r[0] != off)
r = NULL;
}
return r;
}
PRIVATE size_t _jsb_str_count(const uint8_t *bin, size_t off, size_t *endpos){
size_t c = 0;
uint8_t b = bin[off];
while(b < 0xf5){
c += ((b & 0xc0) != 0x80);
b = bin[++off];
}
if(endpos)
*endpos = off;
return c;
}
PRIVATE size_t _jsb_size(const void *base, size_t offset, const size_t *meta);
PRIVATE size_t idx_load(const uint8_t *bin, size_t off, size_t *idx, const size_t n, const size_t m, size_t d){
uint8_t t = bin[off];
node_t v = { 0, 0, 0 };
v[0] = off++;
assert(t > 0xf4 && t < 0xfd);
switch(t){
default:
off = JSB_ERROR;
case JSB_NULL:
case JSB_FALSE:
case JSB_TRUE:
goto done;
case JSB_NUM:
/* we could store something else in v[2] for integers, as character count is size - 1.
Some options include:
* offset to first character that requires floating point parsing ('e' or '.'), else zero
* absolute value of an integer if it fit within the platform's size_t, else zero
* something trickier?
*/
case JSB_STR:
case JSB_KEY:
v[2] = _jsb_str_count(bin, off, &off);
break;
case JSB_OBJ:
case JSB_ARR:
t ^= XND;
d++;
if(bin[off] != t){
if(idx[1] < d)
idx[1] = d;
do{
if(JSB_OBJ_END == t){
assert(bin[off] == JSB_KEY);
off = idx_load(bin, off, idx, n, m, d);
if(JSB_ERROR == off) goto done;
}
assert(bin[off] != JSB_KEY);
assert(bin[off] != JSB_ARR_END);
assert(bin[off] != JSB_OBJ_END);
assert(bin[off] != JSB_DOC_END);
off = idx_load(bin, off, idx, n, m, d);
if(JSB_ERROR == off) goto done;
v[2]++;
}while(bin[off] != t);
}
assert(bin[off] == t);
off++;
d--;
}
assert(bin[off] > 0xf4);
v[1] = off - v[0];
#if CHECK
assert(_jsb_size(bin, v[0], NULL) == v[1]);
#endif
if(v[1] >= m)
idx_insert(n, idx, v);
done:
assert(off != JSB_ERROR);
return off;
}
/*
* Parser API
*/
/* return number of jsb_unit_t's required to back a jsb_t for a given maximum json depth */
PRIVATE size_t __attribute__((const)) _jsb_units(size_t maxdepth);
JSB_API size_t __attribute__((const)) jsb_units(size_t maxdepth){ return _jsb_units(maxdepth); }
PRIVATE size_t __attribute__((const)) _jsb_units(size_t maxdepth){
const size_t bc = ((maxdepth == (size_t)-1 ? JSB_DEFAULT_STACK_BYTES : maxdepth) + 7) / 8;
return (JSB_SIZE + bc + sizeof(jsb_unit_t) - 1) / sizeof(jsb_unit_t);
}
/* initialize jsb parser state */
PRIVATE size_t _jsb_init(jsb_t *jsb, uint32_t flags, size_t jsbsize);
JSB_API size_t jsb_init(jsb_t *jsb, uint32_t flags, size_t jsbsize){ return _jsb_init(jsb, flags, jsbsize); }
PRIVATE size_t _jsb_init(jsb_t *jsb, uint32_t flags, size_t jsbsize){
size_t stackbytes = (jsbsize < JSB_SIZE) ? JSB_DEFAULT_STACK_BYTES : (jsbsize - JSB_SIZE);
debug(("jsb_init(%p, %zx, %zu)\n", (void *)jsb, (size_t)flags, jsbsize));
/* don't modify next_in/avail_in/next_out/avail_out */
jsb->total_out = 0;
jsb->total_in = 0;
jsb->depth = 0;
*(size_t *)&jsb->maxdepth = stackbytes * 8;
jsb->flag_eof = !!(flags & JSB_EOF);
jsb->flag_reverse = !!(flags & JSB_REVERSE);
jsb->flag_ascii = !!(flags & JSB_ASCII);
jsb->flag_lines = !!(flags & JSB_LINES);
jsb->obj = 0;
jsb->key = 0;
jsb->misc = 0;
jsb->code = 0;
jsb->state = 0;
jsb->ch = 0;
jsb->outb = JSB_INT_EOF;
while(stackbytes--)
jsb->stack[stackbytes] = 0;
return jsb->maxdepth;
}
#define J(x) j_ ## x
#define JUMP(target) GOTO(J(target))
/* see: https://stackoverflow.com/questions/36932774/reset-counter-macro-to-zero */
enum { CB = __COUNTER__ };
#define YIELD(sz) do{ \
enum { ctr = __COUNTER__ - CB }; \
jsb->state = ctr; \
ret = sz; \
goto yield; \
case ctr: break; \
}while(0)
#define BEGIN(x) switch(x){ default: ERROR; case 0
#define END }
#define ADDCH APPEND(jsb->ch)
#define APPEND(x) do{ \
const uint8_t t = (x); \
debug(("append: %02x\n", t)); \
assert(JSB_INT_EOF != t); \
if(dstpos != dstlen){ \
dst[dstpos++] = t; \
}else{ \
jsb->outb = t; \
YIELD(JSB_OK); \
} \
}while(0)
/* tag labels with line number for use within a macro */
#define __tag(x, y) x ## y
#define _tag(x, y) __tag(x, y)
#define tag(x) _tag(x, __LINE__)
#define NEXT(sw) do{ \
debug(("next!\n")); \
tag(next): \
if(srcpos != srclen){ \
jsb->ch = src[srcpos++]; \
if(sw && space(jsb->ch)) \
goto tag(next); \
if(0xc0 == jsb->ch || 0xc1 == jsb->ch) \
ERROR; \
debug(("ch: %02x\n", jsb->ch)); \
}else if(jsb->flag_eof){ \
debug(("ch: EOF\n")); \
jsb->ch = JSB_INT_EOF; \
}else{ \
YIELD(JSB_OK); \
goto tag(next); \
} \
}while(0)
#if RELEASE
#define ERROR goto error
#else
#define ERROR do{ jsb->code = __LINE__; goto error; }while(0)
#endif
PRIVATE size_t _jsb_update(jsb_t *jsb){
size_t ret;
size_t srcpos = 0;
size_t dstpos = 0;
const size_t srclen = jsb->avail_in;
const size_t dstlen = jsb->avail_out;
const uint8_t * const src = jsb->next_in;
uint8_t * const dst = jsb->next_out;
if(0){ /* save state and suspend */
yield:
debug(("yield: %d\n", ret));
jsb->avail_in -= srcpos;
jsb->avail_out -= dstpos;
jsb->next_in += srcpos;
jsb->next_out += dstpos;
jsb->total_in += srcpos;
jsb->total_out += dstpos;
return ret;
}
if(jsb->outb != JSB_INT_EOF){
assert(jsb->outb != 0xc0);
if(dstlen)
dst[dstpos++] = jsb->outb;
else
return JSB_OK;
jsb->outb = JSB_INT_EOF;
}
debug(("enter: %d\n", jsb->state));
BEGIN(jsb->state):
if(jsb->flag_reverse)
goto reverse;
JUMP(value);
error:
debug(("error: line %d\n", jsb->code));
while(1) YIELD(JSB_ERROR);
J(escape):
NEXT(0);
switch(jsb->ch){
default: ERROR;
case 't': jsb->ch = '\t'; break;
case 'n': jsb->ch = '\n'; break;
case 'r': jsb->ch = '\r'; break;
case 'f': jsb->ch = '\f'; break;
case 'b': jsb->ch = '\b'; break;
case '"': case '/': case '\\':
break;
case 'u':
jsb->code = 0;
jsb->misc = 3;
JUMP(hex);
}
ADDCH;
JUMP(string2);
J(null):
jsb->code = 'u' | ('l'<<8) | ('l'<<16);
jsb->ch = JSB_NULL;
JUMP(const);
J(false):
jsb->code = 'a' | ('l'<<8) | ('s'<<16) | ('e'<<24);
jsb->ch = JSB_FALSE;
JUMP(const);
J(true):
jsb->code = 'r' | ('u'<<8) | ('e'<<16);
jsb->ch = JSB_TRUE;
JUMP(const);
J(const):
assert(jsb->code);
ADDCH;
do{
NEXT(0);
if((jsb->code & 0xff) != jsb->ch)
ERROR;
}while(jsb->code >>= 8);
JUMP(more);
J(pop):
APPEND(JSB_ARR_END - jsb->obj); /* JSB_ARR_END - 1 == JSB_OBJ_END */
if(!jsb->depth--) ERROR;
jsb->obj = (jsb->stack[jsb->depth >> 3] >> (jsb->depth & 7)) & 1;
assert(jsb->obj < 2);
jsb->key = 0;
JUMP(more);
J(endnum):
if(JSB_INT_EOF != jsb->ch){
debug(("srcpos--\n"));
srcpos--;
}
JUMP(more);
J(more):
if(!jsb->depth)
JUMP(done);
debug(("more: obj/key = %u/%u\n", jsb->obj, jsb->key));
NEXT(1);
if(PICK(jsb->key, ':', ',') == jsb->ch){
if(jsb->key ^= jsb->obj)
JUMP(key);
JUMP(value);
}else if(PICK(jsb->obj, '}', ']') == jsb->ch){
if(jsb->key)
ERROR;
JUMP(pop);
}
ERROR;
J(key):
NEXT(1);
J(key2):
if(jsb->ch != '"')
ERROR;
APPEND(JSB_KEY);
if(0)
J(string):
APPEND(JSB_STR);
J(string2):
NEXT(0);
if('"' == jsb->ch){
JUMP(more);
}else if('\\' == jsb->ch){
JUMP(escape);
}else if(jsb->ch >= 0x20 && jsb->ch < 0x80){
ADDCH;
}else if((jsb->ch & 0xe0) == 0xc0 && (jsb->ch & 0x1f)){
jsb->code = jsb->ch & 0x1f;
jsb->misc = 1;
JUMP(unicode);
}else if((jsb->ch & 0xf0) == 0xe0){
jsb->code = jsb->ch & 0xf;
jsb->misc = 2;
JUMP(unicode);
}else if((jsb->ch & 0xf8) == 0xf0){
jsb->code = jsb->ch & 0x7;
jsb->misc = 3;
JUMP(unicode);
}else ERROR;
JUMP(string2);
J(push):
APPEND(JSB_ARR - jsb->key); /* JSB_ARR - 1 == JSB_OBJ */
NEXT(1);
if(PICK(jsb->key, '}', ']') == jsb->ch){
APPEND(JSB_ARR_END - jsb->key); /* JSB_ARR_END - 1 == JSB_OBJ_END */
jsb->key = 0;
JUMP(more);
}
if(jsb->maxdepth == jsb->depth) ERROR;
{
const uint8_t tmp = 1 << (jsb->depth & 7);
if(jsb->obj)
jsb->stack[jsb->depth>>3] |= tmp;
else
jsb->stack[jsb->depth>>3] &= ~tmp;
}
jsb->depth++;
jsb->obj = jsb->key;
if(jsb->key)
JUMP(key2);
JUMP(value2);
J(value):
debug(("value!\n"));
NEXT(1);
if(0)
J(value2):
debug(("value2!\n"));
switch(jsb->ch){
case '"': JUMP(string);
case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8': case '9':
JUMP(number);
case '-': JUMP(sign);
case '0': JUMP(zero);
case 't': JUMP(true);
case 'f': JUMP(false);
case 'n': JUMP(null);
case '{':
case '[':
if(jsb->key)
default:
ERROR;
jsb->key = (jsb->ch == '{');
JUMP(push);
}
J(sign):
APPEND(JSB_NUM);
ADDCH;
NEXT(0);
if(pdigit(jsb->ch))
JUMP(number2);
if('0' == jsb->ch)
JUMP(zero2);
ERROR;
J(number):
APPEND(JSB_NUM);
J(number2):
ADDCH;
NEXT(0);
if(digit(jsb->ch))
JUMP(number2);
if('.' == jsb->ch)
JUMP(decimal);
JUMP(expchk);
J(zero):
APPEND(JSB_NUM);
J(zero2):
ADDCH;
NEXT(0);
if('.' == jsb->ch)
JUMP(decimal);
JUMP(expchk);
J(decimal):
ADDCH;
NEXT(0);
if(digit(jsb->ch))
JUMP(decimal_more);
ERROR;
J(decimal_more):
ADDCH;
NEXT(0);
if(digit(jsb->ch))
JUMP(decimal_more);
JUMP(expchk);
J(expchk):
if('e' == jsb->ch || 'E' == jsb->ch)
JUMP(exponent);
JUMP(endnum);
J(exponent):
APPEND('e');
NEXT(0);
jsb->misc = ('-' == jsb->ch);
if('-' == jsb->ch || '+' == jsb->ch)
NEXT(0);
if(pdigit(jsb->ch))
JUMP(exponent_more);
else if(jsb->ch == '0')
JUMP(exponent_zero);
ERROR;
J(exponent_zero):
NEXT(0);
if(jsb->ch == '0')
JUMP(exponent_zero);
if(digit(jsb->ch))
JUMP(exponent_more);
APPEND('0');
JUMP(endnum);
J(exponent_more):
if(jsb->misc)
APPEND('-');
J(exponent_more2):
ADDCH;
NEXT(0);
if(digit(jsb->ch))
JUMP(exponent_more2);
JUMP(endnum);
J(unicode):
ADDCH;
NEXT(0);
if((jsb->ch & 0xc0) != 0x80)
ERROR;
jsb->code = (jsb->code << 6) | (jsb->ch & 0x3f);
if(--jsb->misc)
JUMP(unicode);
if(jsb->code > 0x10ffff || (jsb->code >= 0xd800 && jsb->code < 0xe000))
ERROR;
ADDCH;
JUMP(string2);
J(hexb):
NEXT(0);
if('\\' != jsb->ch)
ERROR;
NEXT(0);
if('u' != jsb->ch)
ERROR;
jsb->code <<= 16;
jsb->misc = 3;
JUMP(hex);
J(hex):
NEXT(0);
{
uint32_t tmp = hex(jsb->ch);
if(tmp > 0xf)
ERROR;
jsb->code |= tmp << (4 * jsb->misc);
if(jsb->misc){
jsb->misc--;
JUMP(hex);
}
/* deal w/ UTF-16 surrogate pairs - https://en.wikipedia.org/wiki/UTF-16 */
tmp = jsb->code & 0xfc00;
if(jsb->code < 0x10000){
if(0xdc00 == tmp)
ERROR;
if(0xd800 == tmp)
JUMP(hexb);
}else{
if(0xdc00 != tmp)
ERROR;
jsb->code = 0x10000 + ((jsb->code & 0x3ff0000) >> 6) + (jsb->code & 0x3ff);
}
}
if(jsb->code < 0x80){
}else if(jsb->code < (0x1<<11)){
jsb->code = 0x80c0 | (jsb->code >> 6) | ((jsb->code & 0x3f)<<8);
}else if(jsb->code < (0x1<<16)){
jsb->code = 0x8080e0 | (jsb->code >>12) | ((jsb->code & 0xfc0) << 2) | ((jsb->code & 0x3f) << 16);
}else if(jsb->code < (0x11<<16)){
jsb->code = 0x808080f0 | (jsb->code >> 18) | ((jsb->code & 0x3f000) >> 4) | ((jsb->code & 0xfc0) << 10) | ((jsb->code & 0x3f)<<24);
}else ERROR;
do{
APPEND(jsb->code);
}while(jsb->code >>= 8);
JUMP(string2);
J(done):
APPEND(JSB_DOC_END);
NEXT(1);
if(jsb->flag_lines){
/* YIELD(JSB_OK); */
if(JSB_INT_EOF != jsb->ch)
JUMP(value2);
}else{
if(jsb->ch != JSB_INT_EOF)
srcpos--;
}
while(1)
YIELD(JSB_DONE);
#undef J
#define J(x) r_ ## x
reverse:
NEXT(0);
JUMP(start);
J(pop):
jsb->depth--;
jsb->misc = 0;
if(0)
J(push):
if(!++jsb->depth)
ERROR;
ADDCH;
J(nextch):
NEXT(0);
J(next):
if(!jsb->depth)
JUMP(done);
if(JSB_ARR_END == jsb->ch){
jsb->ch = ']';
JUMP(pop);
}else if(JSB_OBJ_END == jsb->ch){
jsb->ch = '}';
JUMP(pop);
}else if(JSB_KEY == jsb->misc){
APPEND(':');
}else if(JSB_ARR != jsb->misc && JSB_OBJ != jsb->misc){
APPEND(',');
}
J(start):
jsb->code = 0;
jsb->misc = jsb->ch;
if(JSB_NUM == jsb->ch){
while(1){
NEXT(0);
if(jsb->ch > 0xf4)
JUMP(next);
ADDCH;
}
}else if(JSB_ARR == jsb->ch){
jsb->ch = '[';
JUMP(push);
}else if(JSB_OBJ == jsb->ch){
jsb->ch = '{';
JUMP(push);
}else if(JSB_KEY == jsb->ch || JSB_STR == jsb->ch){
APPEND('"');
JUMP(string);
}else if(JSB_TRUE == jsb->ch){
jsb->ch = 't';
jsb->code = 'r' | ('u'<<8) | ('e'<<16);
}else if(JSB_FALSE == jsb->ch){
jsb->ch = 'f';
jsb->code = 'a' | ('l'<<8) | ('s'<<16) | ('e'<<24);
}else if(JSB_NULL == jsb->ch){
jsb->ch = 'n';
jsb->code = 'u' | ('l'<<8) | ('l'<<16);
}else{
ERROR;
}
ADDCH;
while(jsb->code){
APPEND(jsb->code);
jsb->code >>= 8;
}
JUMP(nextch);
J(string):
NEXT(0);
if(jsb->ch > 0xf4){
APPEND('"');
JUMP(next);
}else if(jsb->ch >= 0x20 && jsb->ch != '"' && jsb->ch != '\\' && (!jsb->flag_ascii || jsb->ch < 0x80)){
ADDCH;
JUMP(string);
}else if(jsb->ch < 0x80){
APPEND('\\');
if('"' == jsb->ch || '\\' == jsb->ch) (void)jsb->ch;
else if('\t' == jsb->ch) jsb->ch = 't';
else if('\n' == jsb->ch) jsb->ch = 'n';
else if('\r' == jsb->ch) jsb->ch = 'r';
else if('\f' == jsb->ch) jsb->ch = 'f';
else if('\b' == jsb->ch) jsb->ch = 'b';
else{
APPEND('u');
APPEND('0');
APPEND('0');
APPEND(nibble(jsb->ch>>4));
jsb->ch = nibble(jsb->ch);
}
}else{
if((jsb->ch & 0xf8) == 0xf0){
jsb->code = jsb->ch & 0x7;
goto _3;
}
if((jsb->ch & 0xf0) == 0xe0){
jsb->code = jsb->ch & 0xf;
goto _2;
}
assert((jsb->ch & 0xe0) == 0xc0);
jsb->code = jsb->ch & 0x1f;
goto _1;
_3: NEXT(0); assert((jsb->ch & 0xc0) == 0x80); jsb->code <<= 6; jsb->code |= jsb->ch ^ 0x80;
_2: NEXT(0); assert((jsb->ch & 0xc0) == 0x80); jsb->code <<= 6; jsb->code |= jsb->ch ^ 0x80;
_1: NEXT(0); assert((jsb->ch & 0xc0) == 0x80); jsb->code <<= 6; jsb->code |= jsb->ch ^ 0x80;
assert(jsb->code < 0x110000);
APPEND('\\');
APPEND('u');
if(jsb->code < 0x10000){
APPEND(nibble(jsb->code >> 12));
APPEND(nibble(jsb->code >> 8));
APPEND(nibble(jsb->code >> 4));
}else{
jsb->code -= 0x10000;
APPEND('d');
APPEND(nibble((jsb->code >> 18) | 0x8));
APPEND(nibble(jsb->code >> 14));
APPEND(nibble(jsb->code >> 10));
APPEND('\\');
APPEND('u');
APPEND('d');
APPEND(nibble((jsb->code >> 8) | 0xc));
APPEND(nibble(jsb->code >> 4));
}
jsb->ch = nibble(jsb->code);
}
ADDCH;
JUMP(string);
J(done):
/* parsing successful */
if(jsb->flag_lines)
APPEND('\n'); /* append a newline */
APPEND(0); /* and null terminate */
dstpos--; /* but don't include in output */
debug(("done!\n"));
if(jsb->flag_lines){
/* YIELD(JSB_OK); */
J(again):
NEXT(0);
switch(jsb->ch){
case JSB_DOC_END:
JUMP(again);
case JSB_NULL:
case JSB_FALSE:
case JSB_TRUE:
case JSB_NUM: