-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraon.c
More file actions
726 lines (625 loc) · 22.6 KB
/
raon.c
File metadata and controls
726 lines (625 loc) · 22.6 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
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
#include "raon.h"
#include <asm-generic/errno-base.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef raon_malloc
#define raon_malloc malloc
#endif
#ifndef raon_free
#define raon_free free
#endif
struct raon_lexer raon_lexer_init_from_str(char *str) {
const size_t str_len = strlen(str);
char *str_copy = raon_malloc(str_len * sizeof(char));
strncpy(str_copy, str, str_len);
return (struct raon_lexer) {
.str = str_copy,
.str_len = str_len,
};
}
// initialization failed if str field is NULL
struct raon_lexer raon_lexer_init_from_file(char *filepath) {
FILE *file = fopen(filepath, "r");
const struct raon_lexer error_val = { .str = NULL };
if (!file) {
return error_val;
}
fseek(file, 0, SEEK_END);
size_t len = ftell(file);
fseek(file, 0, SEEK_SET);
char *str = raon_malloc(len + 1);
if (!str) {
fclose(file);
return error_val;
}
fread(str, 1, len, file);
fclose(file);
str[len] = '\0';
return (struct raon_lexer) { .str = str, .str_len = strlen(str) };
}
void raon_lexer_free(struct raon_lexer *lex) {
raon_free(lex->str);
*lex = (struct raon_lexer) { 0 };
}
char raon_lexer_peek(struct raon_lexer *lex) {
size_t next_tok = lex->start ? lex->curr_idx + 1 : 0;
if (next_tok >= lex->str_len) {
return '\0';
}
return lex->str[next_tok];
}
void raon_lexer_eat(struct raon_lexer *lex) {
size_t next_idx;
if (lex->start) {
next_idx = lex->curr_idx + 1;
} else {
next_idx = 0;
lex->start = true;
}
if (next_idx >= lex->str_len) {
return;
}
lex->curr_idx = next_idx;
char next_tok = lex->str[next_idx];
if (next_tok == '\n') {
++lex->curr_y;
lex->curr_x = 0;
} else {
++lex->curr_x;
}
}
struct raon_token raon_lexer_lex_number(struct raon_lexer *lex) {
const struct raon_token error_val = { .type = raon_token_type_error };
const char first_char = raon_lexer_peek(lex);
if (!isdigit(first_char) && first_char != '-') {
return error_val;
}
raon_lexer_eat(lex);
struct raon_token tok = {
.from_x = lex->curr_x,
.from_y = lex->curr_y,
.type = raon_token_type_int,
};
size_t str_start_idx = lex->curr_idx;
size_t str_len = 1;
for (;;) {
char c = raon_lexer_peek(lex);
if (!isdigit(c)) {
break;
}
raon_lexer_eat(lex);
++str_len;
}
tok.to_x = lex->curr_x;
tok.to_y = lex->curr_y;
char *num = raon_malloc(str_len + 1);
if (!num) {
return (struct raon_token) { .type = raon_token_type_error };
}
strncpy(num, &lex->str[str_start_idx], str_len);
num[str_len] = '\0';
errno = 0;
tok.int_val = strtoll(num, NULL, 10);
if (errno == EINVAL || errno == ERANGE) {
return error_val;
}
raon_free(num);
return tok;
}
struct raon_token raon_lexer_lex_string(struct raon_lexer *lex) {
const struct raon_token error_val = (struct raon_token) { .type = raon_token_type_error };
if (raon_lexer_peek(lex) != '"') {
return (struct raon_token) { .type = raon_token_type_error };
}
raon_lexer_eat(lex);
raon_lexer_eat(lex);
size_t str_start_idx = lex->curr_idx;
struct raon_token tok = {
.from_x = lex->curr_x,
.from_y = lex->curr_y,
.type = raon_token_type_string,
};
size_t str_len = 0;
for (;;) {
char c = raon_lexer_peek(lex);
++str_len;
raon_lexer_eat(lex);
if (c == '"') {
break;
}
if (c == '\0') {
return error_val;
}
}
tok.string_val = raon_malloc(str_len + 1);
if (!tok.string_val) {
return error_val;
}
strncpy(tok.string_val, &lex->str[str_start_idx], str_len);
tok.string_val[str_len] = '\0';
return tok;
}
// lexes bools and fields
struct raon_token raon_lexer_lex_ident(struct raon_lexer *lex) {
const struct raon_token error_val = {
.type = raon_token_type_error,
};
if (!isalpha(raon_lexer_peek(lex))) {
return error_val;
}
raon_lexer_eat(lex);
struct raon_token tok = {
.from_x = lex->curr_x,
.from_y = lex->curr_y,
};
size_t str_start_idx = lex->curr_idx;
size_t str_len = 0;
for (;;) {
char c = raon_lexer_peek(lex);
++str_len;
raon_lexer_eat(lex);
if (c != '_' && c != '-' && !isalnum(c)) {
break;
}
}
tok.to_x = lex->curr_x;
tok.to_y = lex->curr_y;
const char *str = &lex->str[str_start_idx];
if (strncmp(str, "true", str_len) == 0) {
tok.type = raon_token_type_bool;
tok.bool_val = true;
return tok;
}
if (strncmp(str, "false", str_len) == 0) {
tok.type = raon_token_type_bool;
tok.bool_val = false;
return tok;
}
tok.type = raon_token_type_field;
tok.string_val = raon_malloc(str_len + 1);
if (!tok.string_val) {
return error_val;
}
strncpy(tok.string_val, &lex->str[str_start_idx], str_len);
tok.string_val[str_len] = '\0';
return tok;
}
void raon_lexer_ignore_comment(struct raon_lexer *lex) {
if (raon_lexer_peek(lex) != '#') {
return;
}
raon_lexer_eat(lex);
while (raon_lexer_peek(lex) != '\n') {
raon_lexer_eat(lex);
}
}
void raon_token_free(struct raon_token *token) {
if (token->type == raon_token_type_string || token->type == raon_token_type_field) {
raon_free(token->string_val);
}
*token = (struct raon_token) { 0 };
}
#define RAON_VEC(func_prefix, vec_item_type, vec_item_type_free) \
struct raon_##func_prefix##_vec { \
vec_item_type *items; \
size_t cap, len; \
}; \
\
struct raon_##func_prefix##_vec *raon_##func_prefix##_vec_init(void) { \
const size_t cap = 64; \
vec_item_type *items = raon_malloc(cap * sizeof(*items)); \
if (!items) { \
return NULL; \
} \
memset(items, 0, cap); \
\
struct raon_##func_prefix##_vec *vec = raon_malloc(sizeof(*vec)); \
if (!vec) { \
return NULL; \
} \
*vec = (struct raon_##func_prefix##_vec) { \
.items = items, \
.cap = cap, \
.len = 0, \
}; \
\
return vec; \
} \
\
void raon_##func_prefix##_vec_free(struct raon_##func_prefix##_vec *vec) { \
for (size_t i = 0; i < vec->len; i++) { \
vec_item_type_free(&vec->items[i]); \
vec->items[i] = (vec_item_type) { 0 }; \
} \
raon_free(vec->items); \
raon_free(vec); \
} \
\
bool raon_##func_prefix##_vec_push(struct raon_##func_prefix##_vec *vec, vec_item_type tok) { \
if (vec->len >= vec->cap) { \
vec->cap *= 2; \
vec_item_type *new_items = raon_malloc(vec->cap * sizeof(*new_items)); \
if (!new_items) { \
return false; \
} \
memcpy(new_items, vec->items, vec->len * sizeof(*new_items)); \
raon_free(vec->items); \
vec->items = new_items; \
} \
\
vec->items[vec->len] = tok; \
++vec->len; \
return true; \
}
static void raon_parser_value_free(struct raon_parser_value *value);
static void raon_parser_entry_free(struct raon_parser_entry *entry);
RAON_VEC(token, struct raon_token, raon_token_free)
RAON_VEC(value, struct raon_parser_value, raon_parser_value_free)
RAON_VEC(parser, struct raon_parser_entry, raon_parser_entry_free)
static void raon_parser_value_free(struct raon_parser_value *value) {
switch (value->type) {
case raon_value_type_string:
raon_free(value->string_val);
break;
case raon_value_type_block:
raon_parser_vec_free(value->block_val);
break;
case raon_value_type_array:
raon_value_vec_free(value->array_val);
break;
default:
// the other values don't allocate memory
break;
}
}
static void raon_parser_entry_free(struct raon_parser_entry *entry) {
if (entry->field_type == raon_field_type_string) {
raon_free(entry->string_field);
}
raon_parser_value_free(&entry->value);
}
struct raon_token_vec *raon_lexer_lex(struct raon_lexer *lex) {
#define RAON_ONE_CHAR_TOKEN(literal, enum_type) \
(struct raon_token) { \
.type = enum_type, .char_val = literal, .from_x = lex->curr_x, .from_y = lex->curr_y, \
.to_x = lex->curr_x, .to_y = lex->curr_y, \
}
struct raon_token_vec *token_vec = raon_token_vec_init();
if (!token_vec) {
return NULL;
}
while (lex->curr_idx < lex->str_len) {
raon_lexer_ignore_comment(lex);
char c = raon_lexer_peek(lex);
if (c == '\0') {
break;
}
struct raon_token curr_token = { 0 };
if (c == '\n') {
raon_lexer_eat(lex);
curr_token = RAON_ONE_CHAR_TOKEN('\n', raon_token_type_newline);
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
if (isspace(c)) {
raon_lexer_eat(lex);
continue;
}
curr_token = raon_lexer_lex_ident(lex);
if (curr_token.type != raon_token_type_error) {
if (!raon_token_vec_push(token_vec, curr_token)) {
raon_token_free(&curr_token);
return NULL;
}
continue;
}
curr_token = raon_lexer_lex_number(lex);
if (curr_token.type != raon_token_type_error) {
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
curr_token = raon_lexer_lex_string(lex);
if (curr_token.type != raon_token_type_error) {
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
if (c == '=') {
raon_lexer_eat(lex);
curr_token = RAON_ONE_CHAR_TOKEN('=', raon_token_type_equal);
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
if (c == '{') {
raon_lexer_eat(lex);
curr_token = RAON_ONE_CHAR_TOKEN('{', raon_token_type_block_open);
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
if (c == '}') {
raon_lexer_eat(lex);
curr_token = RAON_ONE_CHAR_TOKEN('}', raon_token_type_block_close);
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
if (c == '[') {
raon_lexer_eat(lex);
curr_token = RAON_ONE_CHAR_TOKEN('[', raon_token_type_array_open);
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
if (c == ']') {
raon_lexer_eat(lex);
curr_token = RAON_ONE_CHAR_TOKEN(']', raon_token_type_array_close);
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
if (c == ',') {
raon_lexer_eat(lex);
curr_token = RAON_ONE_CHAR_TOKEN(',', raon_token_type_comma);
if (!raon_token_vec_push(token_vec, curr_token)) {
return NULL;
}
continue;
}
raon_token_vec_free(token_vec);
token_vec = NULL;
break;
}
#undef RAON_ONE_CHAR_TOKEN
return token_vec;
}
struct raon_parser raon_parser_init(struct raon_token_vec *tokens) {
return (struct raon_parser) {
.tokens = tokens,
};
}
// returns error when there's nothing more to peek
struct raon_token raon_parser_peek(struct raon_parser *parser) {
const struct raon_token error_val = {
.type = raon_token_type_error,
};
if (parser->curr_idx >= parser->tokens->len) {
return error_val;
}
return parser->tokens->items[parser->curr_idx];
}
void raon_parser_eat(struct raon_parser *parser) {
if (parser->curr_idx >= parser->tokens->len) {
return;
}
++parser->curr_idx;
}
struct raon_parser_vec *raon_parser_parse_block(struct raon_parser *parser) {
struct raon_token tok = raon_parser_peek(parser);
if (tok.type != raon_token_type_block_open) {
return NULL;
}
raon_parser_eat(parser);
struct raon_parser_vec *entries = raon_parser_vec_init();
enum raon_field_type field_type = raon_field_type_error;
for (struct raon_token tok = raon_parser_peek(parser); tok.type != raon_token_type_error;
tok = raon_parser_peek(parser)) {
if (tok.type == raon_token_type_block_close) {
raon_parser_eat(parser);
break;
}
if (tok.type == raon_token_type_newline) {
raon_parser_eat(parser);
continue;
}
struct raon_parser_entry entry = raon_parser_parse_entry(parser);
if (entry.value.type == raon_value_type_error) {
raon_parser_vec_free(entries);
return NULL;
}
// TYPE CHECKING
if (field_type == raon_field_type_error) {
field_type = entry.field_type;
} else if (entry.field_type != field_type) {
raon_parser_vec_free(entries);
raon_parser_entry_free(&entry);
return NULL;
}
if (!raon_parser_vec_push(entries, entry)) {
raon_parser_vec_free(entries);
raon_parser_entry_free(&entry);
return NULL;
}
}
return entries;
}
struct raon_parser_value raon_parser_parse_value(struct raon_parser *parser) {
struct raon_parser_value error_val = { .type = raon_value_type_error };
struct raon_parser_value value = { 0 };
struct raon_token curr_token = raon_parser_peek(parser);
switch (curr_token.type) {
case raon_token_type_string: {
const size_t strsiz = strlen(curr_token.string_val);
value.string_val = raon_malloc(strsiz + 1);
if (!value.string_val) {
return error_val;
}
strncpy(value.string_val, curr_token.string_val, strsiz);
value.string_val[strsiz] = '\0';
value.type = raon_value_type_string;
break;
}
case raon_token_type_bool:
value.bool_val = curr_token.bool_val;
value.type = raon_value_type_bool;
break;
case raon_token_type_int:
value.int_val = curr_token.int_val;
value.type = raon_value_type_int;
break;
case raon_token_type_block_open: {
struct raon_parser_vec *block = raon_parser_parse_block(parser);
if (!block) {
return error_val;
}
value.block_val = block;
value.type = raon_value_type_block;
break;
}
case raon_token_type_array_open: {
struct raon_value_vec *array = raon_parser_parse_array(parser);
if (!array) {
return error_val;
}
value.array_val = array;
value.type = raon_value_type_array;
break;
}
default:
return error_val;
}
raon_parser_eat(parser);
curr_token = raon_parser_peek(parser);
if (curr_token.type == raon_token_type_comma || curr_token.type == raon_token_type_newline) {
raon_parser_eat(parser);
}
return value;
}
struct raon_value_vec *raon_parser_parse_array(struct raon_parser *parser) {
if (raon_parser_peek(parser).type != raon_token_type_array_open) {
return NULL;
}
raon_parser_eat(parser);
struct raon_value_vec *values = raon_value_vec_init();
// we start with an error as our "zero value"
// the first type we see in the array, we consider it as our array's type
enum raon_value_type array_type = raon_value_type_error;
for (struct raon_token tok = raon_parser_peek(parser); tok.type != raon_token_type_error;
tok = raon_parser_peek(parser)) {
if (tok.type == raon_token_type_newline) {
raon_parser_eat(parser);
continue;
}
if (tok.type == raon_token_type_array_close) {
raon_parser_eat(parser);
break;
}
struct raon_parser_value curr_value = raon_parser_parse_value(parser);
if (curr_value.type == raon_value_type_error) {
raon_value_vec_free(values);
return NULL;
}
// TYPE CHECKING
if (array_type == raon_value_type_error) {
array_type = curr_value.type;
} else if (curr_value.type != array_type) {
raon_value_vec_free(values);
raon_parser_value_free(&curr_value);
return NULL;
}
raon_value_vec_push(values, curr_value);
}
return values;
}
struct raon_parser_entry raon_parser_parse_entry(struct raon_parser *parser) {
struct raon_parser_entry error_val
= { .value.type = raon_value_type_error, .field_type = raon_field_type_error };
struct raon_parser_entry entry = { 0 };
struct raon_token curr_token = raon_parser_peek(parser);
if (curr_token.type == raon_token_type_string || curr_token.type == raon_token_type_field) {
const size_t strsiz = strlen(curr_token.string_val);
entry.string_field = raon_malloc(strsiz + 1);
if (!entry.string_field) {
return error_val;
}
strncpy(entry.string_field, curr_token.string_val, strsiz);
entry.string_field[strsiz] = '\0';
entry.field_type = raon_field_type_string;
} else if (curr_token.type == raon_token_type_int) {
entry.int_field = curr_token.int_val;
entry.field_type = raon_field_type_int;
} else {
return error_val;
}
raon_parser_eat(parser);
curr_token = raon_parser_peek(parser);
if (curr_token.type != raon_token_type_equal) {
if (entry.field_type == raon_field_type_string) {
raon_free(entry.string_field);
}
return error_val;
}
raon_parser_eat(parser);
entry.value = raon_parser_parse_value(parser);
if (entry.value.type == raon_value_type_error) {
if (entry.field_type == raon_field_type_string) {
raon_free(entry.string_field);
}
return error_val;
}
return entry;
}
struct raon_parser_vec *raon_parser_parse(struct raon_parser *parser) {
struct raon_parser_vec *entries = raon_parser_vec_init();
for (struct raon_token tok = raon_parser_peek(parser); tok.type != raon_token_type_error;
tok = raon_parser_peek(parser)) {
if (tok.type == raon_token_type_newline) {
raon_parser_eat(parser);
continue;
}
struct raon_parser_entry entry = raon_parser_parse_entry(parser);
if (entry.value.type == raon_value_type_error) {
raon_parser_vec_free(entries);
return NULL;
}
if (!raon_parser_vec_push(entries, entry)) {
raon_parser_vec_free(entries);
return NULL;
}
}
return entries;
}
int main(void) {
struct raon_lexer lex = raon_lexer_init_from_file("./example.raon");
if (!lex.str) {
perror("reading file failed");
goto cleanup;
}
puts(lex.str);
struct raon_token_vec *tokens = raon_lexer_lex(&lex);
if (!tokens) {
puts("error lexing");
goto cleanup;
}
struct raon_parser parser = raon_parser_init(tokens);
struct raon_parser_vec *entries = raon_parser_parse(&parser);
if (!entries) {
puts("error parsing");
goto cleanup;
}
puts("==> finished parsing with no errors");
cleanup:
if (entries)
raon_parser_vec_free(entries);
if (tokens)
raon_token_vec_free(tokens);
if (lex.str != NULL)
raon_lexer_free(&lex);
return 0;
}