-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathjimp.h
More file actions
448 lines (387 loc) · 12.3 KB
/
jimp.h
File metadata and controls
448 lines (387 loc) · 12.3 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
// Prototype of an Immediate Deserialization idea. Expect this API to change a lot.
#ifndef JIMP_H_
#define JIMP_H_
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
// TODO: move all diagnostics reporting outside of the library
// So the user has more options on how to report things
typedef enum {
JIMP_INVALID,
JIMP_EOF,
// Puncts
JIMP_OCURLY,
JIMP_CCURLY,
JIMP_OBRACKET,
JIMP_CBRACKET,
JIMP_COMMA,
JIMP_COLON,
// Symbols
JIMP_TRUE,
JIMP_FALSE,
JIMP_NULL,
// Values
JIMP_STRING,
JIMP_NUMBER,
} Jimp_Token;
typedef struct {
const char *file_path;
const char *start;
const char *end;
const char *point;
Jimp_Token token;
const char *token_start; // TODO: `token_start` is primarily used for diagnostics location. Rename it accordingly.
char *string;
size_t string_count;
size_t string_capacity;
double number;
bool boolean;
} Jimp;
// TODO: how do null-s fit into this entire system?
void jimp_begin(Jimp *jimp, const char *file_path, const char *input, size_t input_size);
/// If succeeds puts the freshly parsed boolean into jimp->boolean.
/// Any consequent calls to the jimp_* functions may invalidate jimp->boolean.
bool jimp_bool(Jimp *jimp);
/// If succeeds puts the freshly parsed number into jimp->number.
/// Any consequent calls to the jimp_* functions may invalidate jimp->number.
bool jimp_number(Jimp *jimp);
/// If succeeds puts the freshly parsed string into jimp->string as a NULL-terminated string.
/// Any consequent calls to the jimp_* functions may invalidate jimp->string.
/// strdup it if you don't wanna lose it (memory management is on you at that point).
bool jimp_string(Jimp *jimp);
/// Parses the beginning of the object `{`
bool jimp_object_begin(Jimp *jimp);
/// If succeeds puts the key of the member into jimp->string as a NULL-terminated string.
/// Any consequent calls to the jimp_* functions may invalidate jimp->string.
/// strdup it if you don't wanna lose it (memory management is on you at that point).
bool jimp_object_member(Jimp *jimp);
/// Parses the end of the object `}`
bool jimp_object_end(Jimp *jimp);
/// Reports jimp->string as an unknown member. jimp->string is expected to be populated by
/// jimp_object_member.
void jimp_unknown_member(Jimp *jimp);
/// Parses the beginning of the array `[`
bool jimp_array_begin(Jimp *jimp);
/// Checks whether there is any more items in the array.
bool jimp_array_item(Jimp *jimp);
/// Parses the end of the array `]`
bool jimp_array_end(Jimp *jimp);
/// Prints diagnostic at the current position of the parser.
void jimp_diagf(Jimp *jimp, const char *fmt, ...);
bool jimp_is_null_ahead(Jimp *jimp);
bool jimp_is_bool_ahead(Jimp *jimp);
bool jimp_is_number_ahead(Jimp *jimp);
bool jimp_is_string_ahead(Jimp *jimp);
bool jimp_is_array_ahead(Jimp *jimp);
bool jimp_is_object_ahead(Jimp *jimp);
#endif // JIMP_H_
#ifdef JIMP_IMPLEMENTATION
static bool jimp__expect_token(Jimp *jimp, Jimp_Token token);
static bool jimp__get_and_expect_token(Jimp *jimp, Jimp_Token token);
static const char *jimp__token_kind(Jimp_Token token);
static bool jimp__get_token(Jimp *jimp);
static void jimp__skip_whitespaces(Jimp *jimp);
static void jimp__append_to_string(Jimp *jimp, char x);
static void jimp__append_to_string(Jimp *jimp, char x)
{
if (jimp->string_count >= jimp->string_capacity) {
if (jimp->string_capacity == 0) jimp->string_capacity = 1024;
else jimp->string_capacity *= 2;
jimp->string = realloc(jimp->string, jimp->string_capacity);
}
jimp->string[jimp->string_count++] = x;
}
static void jimp__skip_whitespaces(Jimp *jimp)
{
while (jimp->point < jimp->end && isspace(*jimp->point)) {
jimp->point += 1;
}
}
static Jimp_Token jimp__puncts[256] = {
['{'] = JIMP_OCURLY,
['}'] = JIMP_CCURLY,
['['] = JIMP_OBRACKET,
[']'] = JIMP_CBRACKET,
[','] = JIMP_COMMA,
[':'] = JIMP_COLON,
};
static struct {
Jimp_Token token;
const char *symbol;
} jimp__symbols[] = {
{ .token = JIMP_TRUE, .symbol = "true" },
{ .token = JIMP_FALSE, .symbol = "false" },
{ .token = JIMP_NULL, .symbol = "null" },
};
#define jimp__symbols_count (sizeof(jimp__symbols)/sizeof(jimp__symbols[0]))
static bool jimp__get_token(Jimp *jimp)
{
jimp__skip_whitespaces(jimp);
jimp->token_start = jimp->point;
if (jimp->point >= jimp->end) {
jimp->token = JIMP_EOF;
return false;
}
jimp->token = jimp__puncts[(unsigned char)*jimp->point];
if (jimp->token) {
jimp->point += 1;
return true;
}
for (size_t i = 0; i < jimp__symbols_count; ++i) {
const char *symbol = jimp__symbols[i].symbol;
if (*symbol == *jimp->point) {
while (*symbol && jimp->point < jimp->end && *symbol++ == *jimp->point++) {}
if (*symbol) {
jimp->token = JIMP_INVALID;
jimp_diagf(jimp, "ERROR: invalid symbol\n");
return false;
} else {
jimp->token = jimp__symbols[i].token;
return true;
}
}
}
char *endptr = NULL;
jimp->number = strtod(jimp->point, &endptr); // TODO: This implies that jimp->end is a valid address and *jimp->end == 0
if (jimp->point != endptr) {
jimp->point = endptr;
jimp->token = JIMP_NUMBER;
return true;
}
if (*jimp->point == '"') {
jimp->point++;
jimp->string_count = 0;
while (jimp->point < jimp->end) {
// TODO: support all the JSON escape sequences defined in the spec
// Yes, including those dumb suroggate pairs. Spec is spec.
switch (*jimp->point) {
case '\\': {
jimp->point++;
if (jimp->point >= jimp->end) {
jimp->token_start = jimp->point;
jimp_diagf(jimp, "ERROR: unfinished escape sequence\n");
return false;
}
switch (*jimp->point) {
case 'r':
jimp->point++;
jimp__append_to_string(jimp, '\r');
break;
case 'n':
jimp->point++;
jimp__append_to_string(jimp, '\n');
break;
case 't':
jimp->point++;
jimp__append_to_string(jimp, '\t');
break;
case '\\':
jimp->point++;
jimp__append_to_string(jimp, '\\');
break;
case '"':
jimp->point++;
jimp__append_to_string(jimp, '"');
break;
default:
jimp->token_start = jimp->point;
jimp_diagf(jimp, "ERROR: invalid escape sequence\n");
return false;
}
break;
}
case '"': {
jimp->point++;
jimp__append_to_string(jimp, '\0');
jimp->token = JIMP_STRING;
return true;
}
default: {
char x = *jimp->point++;
jimp__append_to_string(jimp, x);
}
}
}
jimp->token = JIMP_INVALID;
jimp_diagf(jimp, "ERROR: unfinished string\n");
return false;
}
jimp->token = JIMP_INVALID;
jimp_diagf(jimp, "ERROR: invalid token\n");
return false;
}
void jimp_begin(Jimp *jimp, const char *file_path, const char *input, size_t input_size)
{
jimp->file_path = file_path;
jimp->start = input;
jimp->end = input + input_size;
jimp->point = input;
}
void jimp_diagf(Jimp *jimp, const char *fmt, ...)
{
long line_number = 0;
const char *line_start = jimp->start;
const char *point = jimp->start;
while (point < jimp->token_start) {
char x = *point++;
if (x == '\n') {
line_start = point;
line_number += 1;
}
}
fprintf(stderr, "%s:%ld:%ld: ", jimp->file_path, line_number + 1, point - line_start + 1);
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
static const char *jimp__token_kind(Jimp_Token token)
{
switch (token) {
case JIMP_EOF: return "end of input";
case JIMP_INVALID: return "invalid";
case JIMP_OCURLY: return "{";
case JIMP_CCURLY: return "}";
case JIMP_OBRACKET: return "[";
case JIMP_CBRACKET: return "]";
case JIMP_COMMA: return ",";
case JIMP_COLON: return ":";
case JIMP_TRUE: return "true";
case JIMP_FALSE: return "false";
case JIMP_NULL: return "null";
case JIMP_STRING: return "string";
case JIMP_NUMBER: return "number";
}
assert(0 && "unreachable");
return NULL;
}
bool jimp_array_begin(Jimp *jimp)
{
return jimp__get_and_expect_token(jimp, JIMP_OBRACKET);
}
bool jimp_array_end(Jimp *jimp)
{
return jimp__get_and_expect_token(jimp, JIMP_CBRACKET);
}
bool jimp_array_item(Jimp *jimp)
{
const char *point = jimp->point;
if (!jimp__get_token(jimp)) return false;
if (jimp->token == JIMP_COMMA) return true;
if (jimp->token == JIMP_CBRACKET) {
jimp->point = point;
return false;
}
jimp->point = point;
return true;
}
void jimp_unknown_member(Jimp *jimp)
{
jimp_diagf(jimp, "ERROR: unexpected object member `%s`\n", jimp->string);
}
bool jimp_object_begin(Jimp *jimp)
{
return jimp__get_and_expect_token(jimp, JIMP_OCURLY);
}
bool jimp_object_member(Jimp *jimp)
{
const char *point = jimp->point;
if (!jimp__get_token(jimp)) return false;
if (jimp->token == JIMP_COMMA) {
if (!jimp__get_and_expect_token(jimp, JIMP_STRING)) return false;
if (!jimp__get_and_expect_token(jimp, JIMP_COLON)) return false;
return true;
}
if (jimp->token == JIMP_CCURLY) {
jimp->point = point;
return false;
}
if (!jimp__expect_token(jimp, JIMP_STRING)) return false;
if (!jimp__get_and_expect_token(jimp, JIMP_COLON)) return false;
return true;
}
bool jimp_object_end(Jimp *jimp)
{
return jimp__get_and_expect_token(jimp, JIMP_CCURLY);
}
bool jimp_string(Jimp *jimp)
{
return jimp__get_and_expect_token(jimp, JIMP_STRING);
}
bool jimp_bool(Jimp *jimp)
{
jimp__get_token(jimp);
if (jimp->token == JIMP_TRUE) {
jimp->boolean = true;
} else if (jimp->token == JIMP_FALSE) {
jimp->boolean = false;
} else {
jimp_diagf(jimp, "ERROR: expected boolean, but got `%s`\n", jimp__token_kind(jimp->token));
return false;
}
return true;
}
bool jimp_number(Jimp *jimp)
{
return jimp__get_and_expect_token(jimp, JIMP_NUMBER);
}
bool jimp_is_null_ahead(Jimp *jimp)
{
const char *point = jimp->point;
if (!jimp__get_token(jimp)) return false;
jimp->point = point;
return jimp->token == JIMP_NULL;
}
bool jimp_is_bool_ahead(Jimp *jimp)
{
const char *point = jimp->point;
if (!jimp__get_token(jimp)) return false;
jimp->point = point;
return jimp->token == JIMP_TRUE || jimp->token == JIMP_FALSE;
}
bool jimp_is_number_ahead(Jimp *jimp)
{
const char *point = jimp->point;
if (!jimp__get_token(jimp)) return false;
jimp->point = point;
return jimp->token == JIMP_NUMBER;
}
bool jimp_is_string_ahead(Jimp *jimp)
{
const char *point = jimp->point;
if (!jimp__get_token(jimp)) return false;
jimp->point = point;
return jimp->token == JIMP_STRING;
}
bool jimp_is_array_ahead(Jimp *jimp)
{
const char *point = jimp->point;
if (!jimp__get_token(jimp)) return false;
jimp->point = point;
return jimp->token == JIMP_OBRACKET;
}
bool jimp_is_object_ahead(Jimp *jimp)
{
const char *point = jimp->point;
if (!jimp__get_token(jimp)) return false;
jimp->point = point;
return jimp->token == JIMP_OCURLY;
}
static bool jimp__get_and_expect_token(Jimp *jimp, Jimp_Token token)
{
if (!jimp__get_token(jimp)) return false;
return jimp__expect_token(jimp, token);
}
static bool jimp__expect_token(Jimp *jimp, Jimp_Token token)
{
if (jimp->token != token) {
jimp_diagf(jimp, "ERROR: expected %s, but got %s\n", jimp__token_kind(token), jimp__token_kind(jimp->token));
return false;
}
return true;
}
#endif // JIMP_IMPLEMENTATION