-
Notifications
You must be signed in to change notification settings - Fork 0
/
cson.h
1372 lines (1253 loc) · 49 KB
/
cson.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
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
/* Automatically generated to be used as a single header replacement
* of the full library, do not edit. */
#ifndef JSON_JSON_H
#define JSON_JSON_H
#ifdef WANT_LIBC
#include<stdio.h>
#include<string.h>
#include <uchar.h>
#endif
#if (!(defined(_WIN32) || defined(_WIN64)) \
|| defined(__CYGWIN__) \
|| defined(__MINGW32__) \
|| defined(__MINGW64__))
#define HAS_VLA
#define va_(val) val
#else
#define va_(val)
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && !defined(FORCE_ANSI)
#define c99
#define res restrict
#else
#define res
#endif
#ifdef HAS_VLA
#define EXPORT extern
#else
#define EXPORT __declspec(dllexport)
#endif
#ifndef NULL /* No libC */
#define size_t unsigned long long
#define NULL ((void *)0)
#endif
#ifndef WANT_LIBC
/* from MuslC */
EXPORT size_t cisson_strlen(const unsigned char *s)
{
const unsigned char *a = s;
for (; *s; s++);
return s-a;
}
#define cs_strlen(s) cisson_strlen((const unsigned char*)(s))
EXPORT void * cisson_memset(void *dest, int c, size_t n)
{
unsigned char *s = dest;
for (; n; n--, s++) *s = c;
return dest;
}
#define cs_memset(dest, val, repeat) (cisson_memset((dest), (val), (repeat)))
EXPORT void *cisson_memcpy(void *restrict dest, const void *restrict src, size_t n)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (; n; n--) *d++ = *s++;
return dest;
}
#define cs_memcpy(dest, val, repeat) (cisson_memcpy((dest), (val), (repeat)))
EXPORT int cisson_memcmp(const void *vl, const void *vr, size_t n)
{
const unsigned char *l=vl, *r=vr;
for (; n && *l == *r; n--, l++, r++);
return n ? *l-*r : 0;
}
#define cs_memcmp(v1, v2, size) (cisson_memcmp((v1), (v2), (size)))
void *cisson_memmove(void *dest, const void *src, size_t n)
{
char *d = dest;
const char *s = src;
if (d==s) return d;
if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return cisson_memcpy(d, s, n);
if (d<s) {
for (; n; n--) *d++ = *s++;
} else {
while (n) n--, d[n] = s[n];
}
return dest;
}
#define cs_memmove(dest, src, size) (cisson_memmove((dest), (src), (size)))
#else
#define cs_strlen(s) (strlen((s)))
#define cs_memset(dest, val, repeat) (memset((dest), (val), (repeat)))
#define cs_memcpy(dest, val, repeat) (memcpy((dest), (val), (repeat)))
#define cs_memcmp(v1, v2, size) (memcmp((v1), (v2), (size)))
#define cs_memmove(dest, src, size) (memmove((dest), (src), (size)))
#endif /* WANT_LIBC */
/*****************************************************/
enum kind {
UNSET,
ROOT,
TRUE,
FALSE,
JSON_NULL,
STRING,
NUMBER,
ARRAY,
OBJECT,
};
struct token {
enum kind kind;
int root_index;
unsigned char * address;
};
#ifndef SELF_MANAGE_MEMORY
#endif
#ifndef STRING_POOL_SIZE
#define STRING_POOL_SIZE 0x2000
#endif
#ifndef MAX_TOKENS
#define MAX_TOKENS 0x200
#endif
/**
* Json structures are stored as a flat array of objects holding
* a link to their parent whose value is their index in that array, index zero being the root
* Json doesn't require json arrays elements to have an order so sibling data is not stored.
*/
#define WHITESPACE \
X(TABULATION, '\t') \
X(LINE_FEED, '\n') \
X(CARRIAGE_RETURN, '\r') \
X(SPACE, ' ')
#define ERRORS \
X(JSON_ERROR_NO_ERRORS, "No errors found.") \
X(JSON_ERROR_INVALID_CHARACTER, "Found an unknown token.") \
X(JSON_ERROR_TWO_OBJECTS_HAVE_SAME_PARENT, "Two values have the same parent.") \
X(JSON_ERROR_EMPTY, "A JSON document can't be empty.") \
X(JSON_ERROR_INVALID_ESCAPE_SEQUENCE, "Invalid escape sequence.") \
X(JSON_ERROR_INVALID_NUMBER, "Malformed number.") \
X(JSON_ERROR_INVALID_CHARACTER_IN_ARRAY, "Invalid character in array.") \
X(JSON_ERROR_ASSOC_EXPECT_STRING_A_KEY, "A JSON object key must be a quoted string.") \
X(JSON_ERROR_ASSOC_EXPECT_COLON, "Missing colon after object key.") \
X(JSON_ERROR_ASSOC_EXPECT_VALUE, "Missing value after JSON object key.") \
X(JSON_ERROR_NO_SIBLINGS, "Only Arrays and Objects can have sibling descendants.") \
X(JSON_ERROR_JSON1_ONLY_ASSOC_ROOT, "JSON1 only allows objects as root element.") \
X(JSON_ERROR_UTF16_NOT_SUPPORTED_YET, "Code points greater than 0x0800 not supported yet.") \
X(JSON_ERROR_INCOMPLETE_UNICODE_ESCAPE, "Incomplete unicode character sequence.") \
X(JSON_ERROR_UNESCAPED_CONTROL, "Control characters must be escaped.") \
X(JSON_ERROR_UNICODE_ERROR, "Failed to decode unicode codepoint.") \
#define X(a, b) a,
enum whitespace_tokens { WHITESPACE };
enum json_errors{ ERRORS };
#undef X
#ifdef c99
#define X(a, b) [a] = b,
#else
#define X(a, b) b,
#endif
static char whitespaces[] = {
WHITESPACE
'\0',
};
static char const * json_errors[] = {
ERRORS
};
#undef X
#undef WHITESPACE
#undef ERRORS
static char digit_starters[] = "-0123456789";
static char digits[] = "0123456789";
static char digits19[] = "123456789";
static char hexdigits[] = "0123456789abcdefABCDEF";
static char newline[] = "\r\n";
static char whitespace_wo_newline[] = "\t ";
enum states {
EXPECT_BOM,
EXPECT_VALUE,
AFTER_VALUE,
OPEN_ARRAY,
OPEN_ASSOC,
LITERAL_ESCAPE,
IN_STRING,
START_NUMBER,
NUMBER_AFTER_MINUS,
IN_NUMBER,
EXPECT_FRACTION,
EXPECT_EXPONENT,
IN_FRACTION,
IN_FRACTION_DIGIT,
EXPONENT_EXPECT_PLUS_MINUS,
EXPECT_EXPONENT_DIGIT,
IN_EXPONENT_DIGIT,
ARRAY_AFTER_VALUE,
ASSOC_EXPECT_KEY,
CLOSE_STRING,
ASSOC_EXPECT_COLON,
ASSOC_AFTER_INNER_VALUE,
IN_BARE_STRING,
IN_COMMENT,
IN_SQUOTED_STRING,
IN_VERBATIM_STRING,
LOOK_FOR_VERBATIM_CONT,
};
enum json_mode {
JSON2,
JSON1,
RELAXED,
CSON
};
static struct token static_stack[MAX_TOKENS] = { 0 };
static unsigned char static_pool[STRING_POOL_SIZE] = { 0 };
#define string_size_type_raw unsigned short int
#define string_size_type (string_size_type_raw)
#define tok_len(address) ((address) - sizeof string_size_type)
struct tree {
enum states cur_state;
int root_index;
struct tokens {
struct token *stack;
int max;
} tokens;
struct strings {
unsigned char *pool;
unsigned int cursor;
} strings;
enum json_mode mode;
};
/* cson exclusive */
EXPORT char *
cson_to_json(char* cson, unsigned char* sink, int sink_size);
/* State maintenance */
EXPORT void
start_state(struct tree * state, struct token *stack, size_t stack_size, unsigned char *pool, size_t pool_size);
EXPORT struct token *
query_(struct tree * state, size_t length, char query[va_(length)]);
#define query(state, string) query_((state), cs_strlen(string), (string))
EXPORT int
aindex(struct token* stack, struct token* which);
/* Parsing */
EXPORT enum json_errors
rjson_(
size_t len,
unsigned char *cursor,
struct tree * state);
#define rjson(text, state) rjson_(cs_strlen((text)), (unsigned char*)(text), (state))
EXPORT enum json_errors
inject_(size_t len,
unsigned char text[va_(len)],
struct tree * state,
struct token * where);
#define inject(text, state, where) inject_(cs_strlen((text)), (unsigned char*)(text), (state), (where))
/* Output */
#ifdef WANT_LIBC
EXPORT char* print_debug(struct tokens * );
#else
#define print_debug(_) ""
#endif
#define to_string(tokens_) (char * res)to_string_(tokens_, NULL, 2, NULL, -1)
#define to_string_compact(tokens_) (char * res)to_string_(tokens_, NULL, 0, NULL, -1)
#define to_string_pointer(state_, pointer_) (char * res)to_string_(&(state_)->tokens, pointer_, 0, NULL, -1)
#define to_string_sink(state_, sink, size) (char * res)to_string_(&(state_)->tokens, NULL, 2, sink, size)
EXPORT unsigned char * res
to_string_(struct tokens * res tokens, struct token * start, int compact, unsigned char* sink, int sink_size);
/* Building */
EXPORT void start_string(unsigned int *, const unsigned char [STRING_POOL_SIZE]);
EXPORT void push_string(const unsigned int * res cursor, unsigned char * res pool, char* res string, int length);
EXPORT void close_root(struct token * res, int * res);
EXPORT void push_root(int * res, const int * res);
EXPORT void push_token_kind(enum kind kind, void *res address
, struct tokens *tokens, int root_index);
EXPORT void
delete(struct token* which);
EXPORT void
move(struct tree* state, struct token* which, struct token* where);
EXPORT void
rename_string_(struct tree* state, struct token* which, int len, char* new_name);
#define rename_string(state, which, new_name) rename_string_(state, which, cs_strlen(new_name), new_name)
/* EZ JSON */
EXPORT void
insert_token(struct tree * state, char *token, struct token* root);
#define push_token(state, token) insert_token((state), (token), &(state)->tokens.stack[(state)->root_index])
EXPORT void
stream_tokens_(struct tree * state, struct token * where, char separator, char *stream, size_t length);
#define stream_tokens(state, sep, stream) stream_tokens_((state), &(state)->tokens.stack[(state)->root_index], (sep), (stream), cs_strlen(stream))
#define stream_into(state, where, sep, stream) stream_tokens_((state), (where), (sep), (stream), cs_strlen(stream))
#define START_STRING(state_) start_string(&(state_)->strings.cursor, (state_)->strings.pool)
#define PUSH_STRING(state_, string_, length_) \
push_string( \
&(state_)->strings.cursor, \
(state_)->strings.pool, \
(string_), \
(length_))
#define CLOSE_ROOT(state_) close_root((*(state_)).tokens.stack, &(*(state_)).root_index)
#define PUSH_ROOT(state_) push_root(&(state_)->root_index, &(state_)->tokens.max)
#define PUSH_TOKEN(kind_, address_, state_) \
push_token_kind( \
(kind_), \
(address_), \
&(state_)->tokens, \
(state_)->root_index)
#define INSERT_TOKEN(kind_, address_, state_, root) \
push_token_kind( \
(kind_), \
(address_), \
&(state_)->tokens, \
(root))
#define PUSH_STRING_TOKEN(kind_, state_) \
PUSH_TOKEN((kind_), (state_)->strings.pool + (state_)->strings.cursor, (state_))
#define INSERT_STRING_TOKEN(kind_, state_, root) \
INSERT_TOKEN((kind_), (state_)->strings.pool + (state_)->strings.cursor, (state_), (root))
#define START_AND_PUSH_TOKEN(state, kind, string) \
START_STRING(state); \
PUSH_STRING((state), string, (cs_strlen (string))); \
PUSH_STRING_TOKEN(kind, state)
#define START_AND_INSERT_TOKEN(state, kind, string, root) \
START_STRING(state); \
PUSH_STRING((state), (string), (cs_strlen (string))); \
INSERT_STRING_TOKEN((kind), (state), (root))
/* __/ */
/* Stripped header guard. */
#endif // JSON_JSON_H /* Automatically added header guard. */
#ifdef CSON_IMPLEMENTATION>
#include "json.h"
EXPORT char *
cson_to_json(char* cson, unsigned char* sink, int sink_size) {
struct tree tree = { 0 };
start_state(&tree, static_stack, sizeof static_stack,
static_pool, sizeof static_pool);
tree.mode = CSON;
enum json_errors err = rjson(cson, &tree);
if (err != JSON_ERROR_NO_ERRORS) {
return (char*)json_errors[err];
}
return to_string_sink(&tree, sink, sink_size);
}
static inline size_t
in(char* res hay, unsigned char needle) {
if (needle == '\0') return 0;
char * begin = hay;
for (;*hay; hay++) {
if(*hay == needle) return (hay - begin) + 1;
}
return 0;
}
static inline size_t
len_in(char* hay, unsigned char* cursor) {
int count = 0;
while(in(hay, cursor[count])) {
count++;
}
return count;
}
static inline size_t
len_whitespace(unsigned char * res string) {
return len_in(whitespaces, string);
}
static inline size_t
remaining(unsigned char * res max, unsigned char * res where) {
return max - where;
}
static inline size_t
unicode_codepoint(char* stream, size_t max_len, size_t *consumed) {
mbstate_t mb = { 0 };
char32_t codepoint;
size_t inlen;
while ((inlen = mbrtoc32(&codepoint, stream + *consumed, max_len - *consumed, &mb))) {
if((int)inlen ==-2) {
*consumed += 1;
} else {
*consumed += inlen;
break;
}
if(inlen == (size_t)-3 || inlen == (size_t)-1) {
return -1;
}
}
return codepoint;
}
static inline _Bool is_bare_name_starter(char32_t codepoint) {
return codepoint == 0x24 || /* $ */
codepoint == 0x2D || /* - */
codepoint >= 0x41 && codepoint <= 0x51 || /* A to Z */
codepoint == 0x5F || /* _ */
codepoint >= 0x61 && codepoint <= 0x7A || /* a to z */
codepoint == 0xAA || /* ª */
codepoint == 0xB5 || /* µ */
codepoint == 0xBA || /* º */
codepoint >= 0xC0 && codepoint <= 0xD6 || /* À to Ö */
codepoint >= 0xD8 && codepoint <= 0xF6 || /* Ø to ö */
codepoint >= 0xF8 && codepoint <= 0x2FF || /* ø to ˿ */
codepoint >= 0x370 && codepoint <= 0x37D || /* Ͱ to ͽ */
codepoint >= 0x37F && codepoint <= 0x1FFF || /* until sized spaces */
codepoint >= 0x200C && codepoint <= 0x200D || /* zero width joiner and non joiner */
codepoint >= 0x2070 && codepoint <= 0x218F || /* until arrows */
codepoint >= 0x2C00 && codepoint <= 0x2FEF ||
codepoint >= 0x3001 && codepoint <= 0xD7FF || /* until surrogates */
codepoint >= 0xF900 && codepoint <= 0xFDCF || /* except variations */
codepoint >= 0xFDF0 && codepoint <= 0xFFFD || /* exclude arabic controls */
codepoint >= 0x10000 && codepoint <= 0xEFFFF; /* all the rest of unicode */
}
static inline _Bool is_bare_name_middle(char32_t codepoint) {
return is_bare_name_starter(codepoint) ||
codepoint == 0x2E || /* . */
codepoint >= 0x30 && codepoint <= 0x39 || /* 0 to 9 */
codepoint == 0xB7 || /* · */
codepoint >= 0x300 && codepoint <= 0x036F || /* combining diacritic marks */
codepoint >= 0x203F && codepoint <= 0x2040; /* ‿ and ⁀ */
}
EXPORT void
start_string(unsigned int * res cursor,
const unsigned char pool[STRING_POOL_SIZE]) {
*cursor += pool[*cursor] + sizeof string_size_type;
}
EXPORT void
push_string(const unsigned int *cursor,
unsigned char *pool,
char* string,
int length) {
/* todo: memmove if we insert */
/* todo: support strings longer than 256 characters */
cs_memcpy(
pool + *cursor + sizeof string_size_type + pool[*cursor],
string,
length);
pool[*cursor] += length;
}
EXPORT void
close_root(struct token * tokens,
int * root_index) {
*root_index = tokens[*root_index].root_index;
}
EXPORT void
push_root(int * root_index, const int * token_cursor) {
*root_index = *token_cursor - 1;
}
EXPORT void
push_token_kind(
enum kind kind,
void * address,
struct tokens *tokens,
int root_index) {
tokens->stack[(tokens->max)++] = (struct token) {
.kind=kind, .root_index=root_index, .address=address
};
}
EXPORT void
insert_token(struct tree * state, char *token, struct token* root) {
if(token[0] == '>') {
int i = 0;
while (token[i] && token[i] == '>') {
CLOSE_ROOT(state);
i++;
}
return;
}
enum kind kind = (enum kind[]){
UNSET, ROOT, TRUE, FALSE, JSON_NULL, ARRAY, OBJECT,
NUMBER, NUMBER, NUMBER, NUMBER, NUMBER, NUMBER,
NUMBER, NUMBER, NUMBER, NUMBER, NUMBER, STRING}
[in("#tfn[{-0123456789\"", token[0])];
if(kind != UNSET) {
START_AND_INSERT_TOKEN(state, kind, token, root - state->tokens.stack);
}
if(in("{[:#", token[0])) PUSH_ROOT(state);
}
EXPORT void
stream_tokens_(struct tree * state, struct token * where, char separator, char *stream, size_t length) {
size_t i = 0;
int old_root = state->root_index;
state->root_index = (int)(where - state->tokens.stack);
while (i < length) {
size_t token_length = 0;
while (i + token_length < length && stream[i + token_length] != separator) {
token_length++;
}
stream[i + token_length] = '\0';
push_token(state, &stream[i]);
i = i + token_length + sizeof separator;
}
state->root_index = old_root;
}
EXPORT void
start_state(
struct tree * state,
struct token *stack,
size_t stack_size,
unsigned char *pool,
size_t pool_size) {
memset(state, 0, sizeof (struct tree));
memset(stack, 0, stack_size);
memset(pool, 0, pool_size);
state->tokens.stack = stack;
state->strings.pool = pool;
state->root_index = -1;
}
static struct token *
next_child(struct tokens * tokens, struct token * root, struct token * current) {
int root_idx = (int)(root - tokens->stack);
int start = (root_idx + 1) % tokens->max;
if (current == root) {
current = NULL;
}
if (current != NULL) {
while (current->root_index != root_idx) {
current = &tokens->stack[current->root_index];
}
int current_idx = (int)(current - tokens->stack);
start = (current_idx + 1) % tokens->max;
}
int i;
for(
i = start;
i != root_idx;
i++, i%=tokens->max) {
if (tokens->stack[i].root_index == root_idx) {
return &tokens->stack[i];
}
}
return NULL;
}
EXPORT struct token *
query_(struct tree * state, size_t length, char query[va_(length)]) {
/* todo : unescape ~0, ~1, ~2 */
size_t i = 0;
struct token * cursor = &state->tokens.stack[1];
while (i < length) {
if (query[i] == '/') {
if(cursor->kind != OBJECT && cursor->kind != ARRAY) {
return NULL;
}
i++;
size_t token_length = 0;
while (i + token_length < length && query[i + token_length] != '/') {
token_length++;
}
unsigned char buffer[0x80] = { '"' * (cursor->kind == OBJECT) };
cs_memcpy(&buffer[1 * (cursor->kind == OBJECT)], &query[i], token_length);
buffer[1 + token_length] = '"' * (cursor->kind == OBJECT);
if (cursor->kind == ARRAY) {
int index = 0;
int j;
for (j = 0; buffer[j]; j++) {
index *= 10;
index += buffer[j] - '0';
}
struct token * cur = NULL;
do {
cur = next_child(&state->tokens, cursor, cur);
} while (index--);
cursor = cur;
} else if (cursor->kind == OBJECT) {
int index = state->tokens.max;
struct token * cur = NULL;
do {
cur = next_child(&state->tokens, cursor, cur);
if (cs_memcmp(cur->address + sizeof string_size_type,
buffer,
token_length + 2) == 0) {
if((i + token_length + 1 < length &&
query[i+token_length] == '/' && query[i+token_length+1] == '<')) {
return cur;
}
cursor = next_child(
&state->tokens,
cur,
NULL);
break;
}
} while (index--);
}
i = i + token_length - 1;
}
i++;
}
return cursor;
}
EXPORT int
aindex(struct token* stack, struct token* which) {
return (int)(which - stack);
}
EXPORT void
delete(struct token* which) {
which->root_index = -2;
}
EXPORT void
move(struct tree* state, struct token* which, struct token* where) {
which->root_index = aindex(state->tokens.stack, where);
}
EXPORT void
rename_string_(struct tree* state, struct token* which, int len, char* new_name) {
START_STRING(state);
PUSH_STRING(state, "\"", 1);
PUSH_STRING(state, new_name, len);
PUSH_STRING(state, "\"", 1);
which->address = state->strings.pool + state->strings.cursor;
}
EXPORT enum json_errors
inject_(size_t len,
unsigned char text[va_(len)],
struct tree * state,
struct token * where) {
enum states old_state = state->cur_state;
int old_root = state->root_index;
state->root_index = (int)(where - state->tokens.stack);
state->cur_state = EXPECT_BOM;
enum json_errors error = rjson_(
cs_strlen((char *) text),
(unsigned char *) text, state);
state->cur_state = old_state;
state->root_index = old_root;
return error;
}
EXPORT enum json_errors
rjson_(size_t len,
unsigned char *cursor,
struct tree * state) {
#define peek_at(where) cursor[where]
#define SET_STATE_AND_ADVANCE_BY(which_, advance_) \
state->cur_state = which_; cursor += advance_
if (state->tokens.stack == NULL) {
start_state(
state,
static_stack,
sizeof static_stack,
static_pool,
sizeof static_pool);
}
if(state->tokens.stack->kind == UNSET) {
START_AND_PUSH_TOKEN(state, ROOT, "#root");
PUSH_ROOT(state);
}
enum json_errors error = JSON_ERROR_NO_ERRORS;
unsigned char * final = cursor + len;
// todo: fully test reentry
// todo: make ANSI/STDC compatible
// todo: complete unicode support?
// todo: add jasmine mode? aka not copy strings+numbers ?
// todo: pedantic mode? (forbid duplicate keys, enforce order, cap integer values to INT_MAX...)
// todo: test for overflows
// fixme: check for bounds
// todo: no memory move mode (simple tokenizer)
// todo: implement JSON pointer RFC6901
// todo: implement JSON schema
for(;;) {
switch (state->cur_state) {
case EXPECT_BOM: {
if(
(char)peek_at(0) == '\xEF'
&& (char)peek_at(1) == '\xBB'
&& (char)peek_at(2) == '\xBF'
) {
SET_STATE_AND_ADVANCE_BY(EXPECT_VALUE, 3);
} else {
SET_STATE_AND_ADVANCE_BY(EXPECT_VALUE, 0);
}
break;
}
case EXPECT_VALUE:
{
if (peek_at(len_whitespace(cursor)+0) == '"') {
START_STRING(state);
PUSH_STRING(state, (char[]) {peek_at(len_whitespace(cursor) + 0)}, 1);
SET_STATE_AND_ADVANCE_BY(IN_STRING, len_whitespace(cursor) + 1);
}
else if(state->mode == CSON && peek_at(len_whitespace(cursor)) == '\'') {
START_STRING(state);
PUSH_STRING(state, "\"", 1);
SET_STATE_AND_ADVANCE_BY(IN_SQUOTED_STRING,
len_whitespace(cursor) + 1);
}
else if (peek_at(len_whitespace(cursor) + 0) == '[') {
START_AND_PUSH_TOKEN(state, ARRAY, "[");
PUSH_ROOT(state);
SET_STATE_AND_ADVANCE_BY(OPEN_ARRAY, len_whitespace(cursor) + 1);
}
else if (peek_at(len_whitespace(cursor) + 0) == '{') {
START_AND_PUSH_TOKEN(state, OBJECT, "{");
PUSH_ROOT(state);
SET_STATE_AND_ADVANCE_BY(OPEN_ASSOC, len_whitespace(cursor) + 1);
}
else if (in(digit_starters, peek_at(len_whitespace(cursor) + 0))) {
START_STRING(state);
SET_STATE_AND_ADVANCE_BY(START_NUMBER, len_whitespace(cursor) + 0);
}
else if (
peek_at(len_whitespace(cursor) + 0) == 't'
&& peek_at(len_whitespace(cursor) + 1) == 'r'
&& peek_at(len_whitespace(cursor) + 2) == 'u'
&& peek_at(len_whitespace(cursor) + 3) == 'e' )
{
START_AND_PUSH_TOKEN(state, TRUE, "true");
SET_STATE_AND_ADVANCE_BY(
AFTER_VALUE,
len_whitespace(cursor)+sizeof("true") - 1)
;
}
else if (peek_at(len_whitespace(cursor) + 0) == 'f'
&& peek_at(len_whitespace(cursor) + 1) == 'a'
&& peek_at(len_whitespace(cursor) + 2) == 'l'
&& peek_at(len_whitespace(cursor) + 3) == 's'
&& peek_at(len_whitespace(cursor) + 4) == 'e') {
START_AND_PUSH_TOKEN(state, FALSE, "false");
SET_STATE_AND_ADVANCE_BY(
AFTER_VALUE,
len_whitespace(cursor)+sizeof("false") - 1
);
}
else if (peek_at(len_whitespace(cursor)+0) == 'n'
&& peek_at(len_whitespace(cursor)+1) == 'u'
&& peek_at(len_whitespace(cursor)+2) == 'l'
&& peek_at(len_whitespace(cursor)+3) == 'l') {
START_AND_PUSH_TOKEN(state, JSON_NULL, "null");
SET_STATE_AND_ADVANCE_BY(
AFTER_VALUE,
len_whitespace(cursor)+sizeof("null") - 1
);
}
else if(peek_at(len_whitespace(cursor)) == '#') {
SET_STATE_AND_ADVANCE_BY(IN_COMMENT, 1);
}
else if(state->mode == CSON && in("}]",
peek_at(len_whitespace(cursor)))) {
CLOSE_ROOT(state);
SET_STATE_AND_ADVANCE_BY(AFTER_VALUE, len_whitespace(cursor) + 1);
} else if(peek_at(len_whitespace(cursor)) == '|') {
START_STRING(state);
PUSH_STRING(state, "\"", 1);
SET_STATE_AND_ADVANCE_BY(IN_VERBATIM_STRING, len_whitespace(cursor) + 1);
}
else if (state->tokens.stack[state->root_index].kind != ROOT) {
error = JSON_ERROR_ASSOC_EXPECT_VALUE;
}
else if (remaining(final, cursor)) {
size_t inlen = 0;
char32_t codepoint = unicode_codepoint((char *)cursor,
remaining(final, cursor),
&inlen);
if(state->mode == CSON && is_bare_name_starter(codepoint)) {
if(state->tokens.max == 1) {
START_AND_PUSH_TOKEN(state, OBJECT, "{");
PUSH_ROOT(state);
SET_STATE_AND_ADVANCE_BY(ASSOC_EXPECT_KEY, 0);
}
} else
error = JSON_ERROR_INVALID_CHARACTER;
}
else {
error = JSON_ERROR_EMPTY;
}
break;
}
case AFTER_VALUE:
{
if (state->tokens.stack[state->root_index].kind == ROOT) {
if (remaining(final, cursor + len_whitespace(cursor))) {
error = JSON_ERROR_NO_SIBLINGS;
} else if(state->mode == JSON1 && state->tokens.stack[state->tokens.max-1].kind != OBJECT) {
error = JSON_ERROR_JSON1_ONLY_ASSOC_ROOT;
} else {
goto exit;
}
}
else if(state->tokens.stack[state->root_index].kind == ARRAY) {
SET_STATE_AND_ADVANCE_BY(ARRAY_AFTER_VALUE, 0);
}
else if(state->tokens.stack[state->root_index].kind == STRING) {
SET_STATE_AND_ADVANCE_BY(ASSOC_AFTER_INNER_VALUE, 0);
}
break;
}
case OPEN_ARRAY: {
if (peek_at(len_whitespace(cursor)) == ']') {
CLOSE_ROOT(state);
SET_STATE_AND_ADVANCE_BY(AFTER_VALUE, len_whitespace(cursor) + 1);
} else {
SET_STATE_AND_ADVANCE_BY(EXPECT_VALUE, 0);
}
break;
}
case OPEN_ASSOC: {
if (peek_at(len_whitespace(cursor)) == '}') {
CLOSE_ROOT(state);
SET_STATE_AND_ADVANCE_BY(AFTER_VALUE, len_whitespace(cursor) + 1);
} else {
SET_STATE_AND_ADVANCE_BY(ASSOC_EXPECT_KEY, len_whitespace(cursor));
}
break;
}
case LITERAL_ESCAPE: {
error = JSON_ERROR_INVALID_ESCAPE_SEQUENCE * !in("\\\"/u", peek_at(0)); /* u"\/ */
if (in("\\\"/", peek_at(0))) {
PUSH_STRING(state, (char[]) {peek_at(0)}, 1);
SET_STATE_AND_ADVANCE_BY(IN_STRING, 1);
}
else if (peek_at(0) == 'u') {
if(!(in(hexdigits, peek_at(1))
&& in(hexdigits, peek_at(2))
&& in(hexdigits, peek_at(3))
&& in(hexdigits, peek_at(4)))
) {
error = JSON_ERROR_INCOMPLETE_UNICODE_ESCAPE;
break;
}
PUSH_STRING(state, (char*)cursor, 5);
SET_STATE_AND_ADVANCE_BY(IN_STRING, 5);
break;
}
else {
error = JSON_ERROR_INVALID_ESCAPE_SEQUENCE;
}
break;
}
case IN_STRING: {
error = JSON_ERROR_UNESCAPED_CONTROL * (peek_at(0) < 0x20 && !in("\b\f\n\r\t", peek_at(0)));
PUSH_STRING(state, (char[]) {peek_at(0)}, error == JSON_ERROR_NO_ERRORS);
SET_STATE_AND_ADVANCE_BY(
(
(int[]){
IN_STRING,
LITERAL_ESCAPE,
CLOSE_STRING
}[in("\\\"", peek_at(0))]),
1);
break;
}
case START_NUMBER: {
PUSH_STRING(
state,
((char[]){peek_at(0)}),
((int[]){0, 1}[in("-", peek_at(0))])
);
SET_STATE_AND_ADVANCE_BY(
NUMBER_AFTER_MINUS,
(
(int[]){0, 1}[in("-", peek_at(0))])
);
break;
}
case NUMBER_AFTER_MINUS:
{
if (peek_at(0) == '0') {
PUSH_STRING(state, (char[]){peek_at(0)}, 1);
SET_STATE_AND_ADVANCE_BY(EXPECT_FRACTION, 1);
} else if (in(digits19, peek_at(0))) {
PUSH_STRING(state, (char[]){peek_at(0)}, 1);
SET_STATE_AND_ADVANCE_BY(IN_NUMBER, 1);
} else {
error = JSON_ERROR_INVALID_NUMBER;
}
break;
}
case IN_NUMBER: {
PUSH_STRING(
state,
(char[]){peek_at(0)},
(in(digits, peek_at(0)) != 0)
);
SET_STATE_AND_ADVANCE_BY(
((int[]){
EXPECT_FRACTION,
IN_NUMBER}[in(digits, peek_at(0)) != 0]),
(in(digits, peek_at(0)) != 0)
);
break;
}
case EXPECT_FRACTION: {
PUSH_STRING(
state,
(char[]){peek_at(0)},
(peek_at(0) == '.')
);
SET_STATE_AND_ADVANCE_BY(
((int[]){
EXPECT_EXPONENT,
IN_FRACTION}[peek_at(0) == '.']),
(peek_at(0) == '.')
);
break;
}
case EXPECT_EXPONENT: {
if (peek_at(0) == 'e' || peek_at(0) == 'E') {
PUSH_STRING(state, (char[]){peek_at(0)}, 1);
SET_STATE_AND_ADVANCE_BY(EXPONENT_EXPECT_PLUS_MINUS, 1);
} else {
PUSH_STRING_TOKEN(NUMBER, state);
SET_STATE_AND_ADVANCE_BY(AFTER_VALUE, 0);
}
break;
}
case IN_FRACTION: {
error = JSON_ERROR_INVALID_NUMBER * (in(digits, peek_at(0)) == 0);
PUSH_STRING(state,
(char[]){peek_at(0)},
error == JSON_ERROR_NO_ERRORS);
SET_STATE_AND_ADVANCE_BY(
((int[]){
IN_FRACTION,
IN_FRACTION_DIGIT
}[error == JSON_ERROR_NO_ERRORS]),
error == JSON_ERROR_NO_ERRORS);
break;
}
case IN_FRACTION_DIGIT: {
if (in(digits, peek_at(0))) {
PUSH_STRING(state, (char[]){peek_at(0)}, 1);
SET_STATE_AND_ADVANCE_BY(IN_FRACTION_DIGIT, 1);
} else {
SET_STATE_AND_ADVANCE_BY(EXPECT_EXPONENT, 0);
}
break;
}
case EXPONENT_EXPECT_PLUS_MINUS: {
if (peek_at(0) == '+' || peek_at(0) == '-') {
PUSH_STRING(state, (char[]){peek_at(0)}, 1);
SET_STATE_AND_ADVANCE_BY(EXPECT_EXPONENT_DIGIT, 1);
} else {
SET_STATE_AND_ADVANCE_BY(EXPECT_EXPONENT_DIGIT, 0);
}
break;