-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcson.h
460 lines (414 loc) · 10.1 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
#ifndef CSON_H
#define CSON_H
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifndef CSON_ALLOC
#define CSON_ALLOC malloc
#endif
#ifndef CSON_FREE
#define CSON_FREE free
#endif
#ifndef CSON_PRINT
#define CSON_PRINT printf
#endif
typedef struct {
char *b;
size_t cap;
size_t size;
size_t cur;
} Cson;
typedef enum {
NULL_VALUE,
OBJECT,
ARRAY,
STRING,
NUMBER,
TRUE,
FALSE,
} TOKEN_TYPE;
typedef struct Token {
TOKEN_TYPE type;
char *text;
// If exists, the child is the 'value' of the current 'key' token
struct Token *child;
// If exists, holds the pointer to the next token at the same level
struct Token *next;
} Token;
Token *parse_json(Cson *c);
Token *parse_json_file(Cson *c, const char *path);
void free_tokens(Token *t);
void pretty_print(Token *root, int depth);
#ifdef CSON_IMPLEMENTATION
void log_error(Cson *c, char *message) {
char buf[256];
snprintf(buf, sizeof(buf), "ERROR: invalid token '%c'. %s", c->b[c->cur],
message);
fprintf(stderr, "%s", buf);
}
void init(Cson *c) {
c->cap = 1024 * 1024;
c->size = 0;
c->cur = 0;
c->b = CSON_ALLOC(c->cap);
assert(c->b);
}
void append_line(Cson *c, char *line, size_t len) {
if (c->size + len > c->cap) {
c->cap *= 2;
c->b = realloc(c->b, sizeof(char) * c->cap);
assert(c->b);
}
memcpy(&c->b[c->size], line, len);
c->size += len;
}
void open_file(Cson *c, const char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) {
exit(0);
}
char *line = NULL;
size_t len = 0;
ssize_t nread;
while ((nread = getline(&line, &len, fp)) != -1) {
append_line(c, line, nread);
}
free(line);
fclose(fp);
}
char advance(Cson *c) {
if (c->cur == c->size) {
return '\0';
}
return c->b[++c->cur];
}
char peek(Cson *c) {
if (c->cur == c->size) {
return '\0';
}
return c->b[c->cur];
}
char peek_next(Cson *c) {
if (c->cur + 1 >= c->size) {
return '\0';
}
return c->b[c->cur + 1];
}
const char *translate_tokentype(TOKEN_TYPE t) {
switch (t) {
case 0:
return "null";
case OBJECT:
return "object";
case ARRAY:
return "array";
case STRING:
return "string";
case NUMBER:
return "number";
case TRUE:
return "true";
case FALSE:
return "false";
default:
return NULL;
}
}
bool scan_number(Cson *c, Token *res) {
bool neg = false;
bool fraction = false;
bool exp = false;
int start = c->cur;
size_t len = 1;
while (true) {
char cur = peek(c);
if (cur == '-') {
if (neg)
return false;
char next = peek_next(c);
if (!isdigit(next))
return false;
neg = true;
advance(c);
len++;
} else if (isdigit(cur)) {
while (true) {
char next = advance(c);
if (isdigit(next)) {
len++;
} else if (next == '.') {
if (fraction || !isdigit(peek_next(c))) {
return false;
}
fraction = true;
len++;
} else if (next == 'e' || next == 'E') {
if (exp)
return false;
char next_next = peek_next(c);
if (next_next == '+' || next_next == '-') {
advance(c);
len++;
char next_next_next = peek_next(c);
if (!isdigit(next_next_next))
return false;
} else if (!isdigit(next_next))
return false;
exp = true;
len++;
} else {
char *text = CSON_ALLOC(len + 1);
memcpy(text, &c->b[start], len);
text[len] = '\0';
res->type = NUMBER;
res->text = text;
return true;
}
}
return false;
} else {
log_error(c, "Expected '-' or digit\n");
return false;
}
}
}
void skip_whitespace(Cson *c) {
while (true) {
char cur = peek(c);
switch (cur) {
case '\t':
case '\n':
case '\r':
case ' ':
advance(c);
continue;
case '\0':
return;
default:
return;
}
}
}
char scan(Cson *c) {
skip_whitespace(c);
return peek(c);
}
char scan_next(Cson *c) {
advance(c);
return scan(c);
}
bool scan_string(Cson *c, Token *res) {
char cur = peek(c);
if (cur != '"')
return false;
size_t len = 0;
size_t start = c->cur + 1;
while (true) {
char next = advance(c);
if (next == '\0') {
return false;
}
len++;
if (next == '"') {
if (len == 0) {
res->text = NULL;
} else {
char *text = CSON_ALLOC(len);
assert(text);
memcpy(text, &c->b[start], len - 1);
text[len - 1] = '\0';
res->text = text;
}
res->type = STRING;
advance(c);
return true;
} else if (next == '\\') {
char next_next = advance(c);
len++;
switch (next_next) {
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case 'u':
break;
default:
fprintf(stderr, "Invalid escape sequence '%c'\n", next_next);
return false;
}
}
}
}
bool scan_special(Cson *c, Token *res, TOKEN_TYPE special) {
const char *s = translate_tokentype(special);
size_t len = strlen(s);
if (c->cur + len > c->size)
return false;
char *temp = CSON_ALLOC(len + 1);
assert(temp);
memcpy(temp, &c->b[c->cur], len);
temp[len] = '\0';
if (strcmp(temp, s) == 0) {
res->type = special;
res->text = temp;
c->cur += len;
return true;
}
return false;
}
bool scan_token(Cson *c, Token *res);
bool scan_array(Cson *c, Token *res) {
assert(peek(c) == '[');
Token *root = res;
char cur = scan_next(c);
while (cur != ']') {
Token *next = CSON_ALLOC(sizeof(Token));
next->child = NULL;
next->next = NULL;
next->text = NULL;
next->type = 0;
if (scan_token(c, next)) {
if (root->child == NULL) {
root->child = next;
} else {
res->next = next;
}
res = next;
} else {
free(next);
return false;
}
cur = scan(c);
if (cur == ',') {
cur = scan_next(c);
}
}
root->type = ARRAY;
advance(c);
return true;
}
bool scan_object(Cson *c, Token *res) {
assert(peek(c) == '{');
Token *prev = res;
char cur = scan_next(c);
while (cur != '}') {
Token *next = CSON_ALLOC(sizeof(Token));
assert(next);
next->child = NULL;
next->next = NULL;
next->text = NULL;
next->type = 0;
if (!scan_string(c, next)) {
free(next);
return false;
}
cur = scan(c);
if (cur != ':') {
free(next);
fprintf(stderr, "Expected ':' after key\n");
return false;
}
advance(c);
Token *child = CSON_ALLOC(sizeof(Token));
assert(child);
child->child = NULL;
child->next = NULL;
child->text = NULL;
child->type = 0;
if (!scan_token(c, child)) {
free(child);
return false;
}
prev->next = next;
next->child = child;
prev = next;
cur = scan(c);
if (cur == ',') {
cur = scan_next(c);
}
}
advance(c);
res->type = OBJECT;
return true;
}
bool scan_token(Cson *c, Token *res) {
while (true) {
char cur = scan(c);
switch (cur) {
case '\0':
return false;
case '[':
return scan_array(c, res);
case '{':
return scan_object(c, res);
case '"':
return scan_string(c, res);
case 't':
return scan_special(c, res, TRUE);
case 'f':
return scan_special(c, res, FALSE);
case 'n':
return scan_special(c, res, 0);
default:
return scan_number(c, res);
}
}
return false;
}
void pretty_print(Token *root, int depth) {
if (root == NULL)
return;
while (root != NULL) {
for (int i = 0; i < depth; i++) {
CSON_PRINT("\t");
}
CSON_PRINT("type: %s, text: %s\n", translate_tokentype(root->type),
root->text);
if (root->child != NULL)
pretty_print(root->child, depth + 1);
root = root->next;
}
}
Token *parse_json(Cson *c) {
Token *t = CSON_ALLOC(sizeof(Token));
t->next = NULL;
t->child = NULL;
t->text = NULL;
t->type = 0;
if (!scan_token(c, t)) {
if (c->cur == c->size) {
CSON_PRINT("Reached EOF");
return NULL;
}
CSON_PRINT("An error while parsing char '%c' at position %zu",
c->b[c->cur], c->cur);
return NULL;
}
return t;
}
Token *parse_json_file(Cson *c, const char *path) {
init(c);
open_file(c, path);
return parse_json(c);
}
void free_tokens(Token *t) {
if (t == NULL)
return;
while (t != NULL) {
free_tokens(t->child);
Token *temp = t->next;
CSON_FREE(t->text);
CSON_FREE(t);
t = temp;
}
}
#endif // CSON_IMPLEMENTATION
#endif // ! CSON_H