forked from orangeduck/mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpc.c
2498 lines (1928 loc) · 63.5 KB
/
mpc.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
#include "mpc.h"
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
/*
** State Type
*/
typedef struct {
char last;
char next;
int pos;
int row;
int col;
} mpc_state_t;
/*
** Error Type
*/
struct mpc_err_t {
char* filename;
mpc_state_t state;
int expected_num;
char** expected;
};
static mpc_err_t* mpc_err_new(char* filename, mpc_state_t s, char* expected) {
mpc_err_t* x = malloc(sizeof(mpc_err_t));
x->filename = malloc(strlen(filename) + 1);
strcpy(x->filename, filename);
x->state = s;
x->expected_num = 1;
x->expected = malloc(sizeof(char*));
x->expected[0] = malloc(strlen(expected) + 1);
strcpy(x->expected[0], expected);
return x;
}
void mpc_err_delete(mpc_err_t* x) {
int i;
for (i = 0; i < x->expected_num; i++) {
free(x->expected[i]);
}
free(x->expected);
free(x->filename);
free(x);
}
static bool mpc_err_contains_expected(mpc_err_t* x, char* expected) {
int i;
for (i = 0; i < x->expected_num; i++) {
if (strcmp(x->expected[i], expected) == 0) { return true; }
}
return false;
}
static void mpc_err_add_expected(mpc_err_t* x, char* expected) {
x->expected_num++;
x->expected = realloc(x->expected, sizeof(char*) * x->expected_num);
x->expected[x->expected_num-1] = malloc(strlen(expected) + 1);
strcpy(x->expected[x->expected_num-1], expected);
}
static void mpc_err_clear_expected(mpc_err_t* x, char* expected) {
int i;
for (i = 0; i < x->expected_num; i++) {
free(x->expected[i]);
}
x->expected_num = 1;
x->expected = realloc(x->expected, sizeof(char*) * x->expected_num);
x->expected[0] = malloc(strlen(expected) + 1);
strcpy(x->expected[0], expected);
}
void mpc_err_print(mpc_err_t* x) {
mpc_err_print_to(x, stdout);
}
void mpc_err_print_to(mpc_err_t* x, FILE* f) {
fprintf(f, "%s:%i:%i: error: expected ", x->filename, x->state.row, x->state.col);
if (x->expected_num == 0) {
fprintf(f, "ERROR: NOTHING EXPECTED");
} else if (x->expected_num == 1) {
fprintf(f, "%s", x->expected[0]);
} else {
int i;
for (i = 0; i < x->expected_num-2; i++) {
fprintf(f, "%s, ", x->expected[i]);
}
fprintf(f, "%s or %s",
x->expected[x->expected_num-2],
x->expected[x->expected_num-1]);
}
printf(" at ");
if (x->state.next == '\a') { printf("bell"); }
else if (x->state.next == '\b') { printf("backspace"); }
else if (x->state.next == '\f') { printf("formfeed"); }
else if (x->state.next == '\r') { printf("carriage return"); }
else if (x->state.next == '\v') { printf("vertical tab"); }
else if (x->state.next == '\0') { printf("end of input"); }
else if (x->state.next == '\n') { printf("newline"); }
else if (x->state.next == '\t') { printf("tab"); }
else { printf("'%c'", x->state.next); }
printf("\n");
}
void mpc_err_msg(mpc_err_t* x, char* out, int* outn, int outmax) {
/* TODO: Implement */
}
static mpc_err_t* mpc_err_either(mpc_err_t* x, mpc_err_t* y) {
if (x->state.pos > y->state.pos) {
mpc_err_delete(y);
return x;
}
if (x->state.pos < y->state.pos) {
mpc_err_delete(x);
return y;
}
if (x->state.pos == y->state.pos) {
int i;
for (i = 0; i < y->expected_num; i++) {
if (mpc_err_contains_expected(x, y->expected[i])) { continue; }
else { mpc_err_add_expected(x, y->expected[i]); }
}
mpc_err_delete(y);
return x;
}
return NULL;
}
static mpc_err_t* mpc_err_or(mpc_err_t** x, int n) {
mpc_err_t* e = x[0];
int i;
for (i = 1; i < n; i++) {
e = mpc_err_either(e, x[i]);
}
return e;
}
static mpc_err_t* mpc_err_many1(mpc_err_t* x) {
char* expect = malloc(strlen("one or more of ") + 1);
strcpy(expect, "one or more of ");
int i;
for (i = 0; i < x->expected_num - 1; i++) {
expect = realloc(expect, strlen(expect) + strlen(x->expected[i]) + strlen(", ") + 1);
strcat(expect, x->expected[i]);
strcat(expect, ", ");
}
expect = realloc(expect, strlen(expect) + strlen(x->expected[x->expected_num-1]) + 1);
strcat(expect, x->expected[x->expected_num-1]);
mpc_err_clear_expected(x, expect);
free(expect);
return x;
}
static mpc_err_t* mpc_err_count(mpc_err_t* x, int n) {
int digits = n/10 + 1;
char* expect = malloc(digits + strlen(" of ") + 1);
sprintf(expect, "%i of ", n);
int i;
for (i = 0; i < x->expected_num - 1; i++) {
expect = realloc(expect, strlen(expect) + strlen(x->expected[i]) + strlen(", ") + 1);
strcat(expect, x->expected[i]);
strcat(expect, ", ");
}
expect = realloc(expect, strlen(expect) + strlen(x->expected[x->expected_num-1]) + 1);
strcat(expect, x->expected[x->expected_num-1]);
mpc_err_clear_expected(x, expect);
free(expect);
return x;
}
char* mpc_err_filename(mpc_err_t* x) {
return x->filename;
}
char** mpc_err_expected(mpc_err_t* x, int* num) {
*num = x->expected_num;
return x->expected;
}
int mpc_err_line(mpc_err_t* x) {
return x->state.row;
}
int mpc_err_column(mpc_err_t* x) {
return x->state.col;
}
char mpc_err_unexpected(mpc_err_t* x) {
return x->state.next;
}
/*
** Input Type
*/
typedef struct {
char* filename;
char* str;
mpc_state_t state;
int marks_num;
mpc_state_t* marks;
} mpc_input_t;
static mpc_input_t* mpc_input_new(const char* filename, const char* str) {
mpc_input_t* i = malloc(sizeof(mpc_input_t));
i->str = malloc(strlen(str) + 1);
strcpy(i->str, str);
i->filename = malloc(strlen(filename) + 1);
strcpy(i->filename, filename);
i->state.next = i->str[0];
i->state.last = '\0';
i->state.pos = 0;
i->state.row = 0;
i->state.col = 0;
i->marks_num = 0;
i->marks = NULL;
return i;
}
static void mpc_input_delete(mpc_input_t* i) {
free(i->filename);
free(i->str);
free(i->marks);
free(i);
}
static void mpc_input_mark(mpc_input_t* i) {
i->marks_num++;
i->marks = realloc(i->marks, sizeof(mpc_state_t) * i->marks_num);
i->marks[i->marks_num-1] = i->state;
}
static void mpc_input_unmark(mpc_input_t* i) {
i->marks_num--;
i->marks = realloc(i->marks, sizeof(mpc_state_t) * i->marks_num);
}
static void mpc_input_rewind(mpc_input_t* i) {
i->state = i->marks[i->marks_num-1];
mpc_input_unmark(i);
}
static bool mpc_input_next(mpc_input_t* i, char** o) {
i->state.last = i->str[i->state.pos];
i->state.pos++;
i->state.col++;
if (i->state.last == '\n') {
i->state.col = 0;
i->state.row++;
}
(*o) = malloc(2);
(*o)[0] = i->state.last;
(*o)[1] = '\0';
return true;
}
static bool mpc_input_any(mpc_input_t* i, char** o) {
if (i->state.pos >= strlen(i->str)) { i->state.next = '\0'; return false; }
if (i->str[i->state.pos] == '\0') {
i->state.next = i->str[i->state.pos];
return false;
}
return mpc_input_next(i, o);
}
static bool mpc_input_char(mpc_input_t* i, char c, char** o) {
if (i->state.pos >= strlen(i->str)) { i->state.next = '\0'; return false; }
if (i->str[i->state.pos] != c) {
i->state.next = i->str[i->state.pos];
return false;
}
return mpc_input_next(i, o);
}
static bool mpc_input_range(mpc_input_t* i, char c, char d, char** o) {
if (i->state.pos >= strlen(i->str)) { i->state.next = '\0'; return false; }
if (i->str[i->state.pos] < c ||
i->str[i->state.pos] > d) {
i->state.next = i->str[i->state.pos];
return false;
}
return mpc_input_next(i, o);
}
static bool char_in_string(char c, const char* x) {
while (*x) {
if (*x == c) { return true; }
x++;
}
return false;
}
static bool mpc_input_oneof(mpc_input_t* i, const char* c, char** o) {
if (i->state.pos >= strlen(i->str)) { i->state.next = '\0'; return false; }
if (!char_in_string(i->str[i->state.pos], c)) {
i->state.next = i->str[i->state.pos];
return false;
}
return mpc_input_next(i, o);
}
static bool mpc_input_noneof(mpc_input_t* i, const char* c, char** o) {
if (i->state.pos >= strlen(i->str)) { i->state.next = '\0'; return false; }
if (char_in_string(i->str[i->state.pos], c) || (i->str[i->state.pos] == '\0')) {
i->state.next = i->str[i->state.pos];
return false;
}
return mpc_input_next(i, o);
}
static bool mpc_input_satisfy(mpc_input_t* i, bool(*cond)(char), char** o) {
if (i->state.pos >= strlen(i->str)) { i->state.next = '\0'; return false; }
if (!cond(i->str[i->state.pos])) { i->state.next = i->str[i->state.pos]; return false; }
return mpc_input_next(i, o);
}
static bool mpc_input_eoi(mpc_input_t* i) {
return i->state.pos == strlen(i->str);
}
static bool mpc_input_soi(mpc_input_t* i) {
return i->state.pos == 0;
}
static bool mpc_input_string(mpc_input_t* i, const char* c, char** o) {
mpc_input_mark(i);
char* co = NULL;
const char* x = c;
while (*x) {
if (mpc_input_char(i, *x, &co)) {
free(co);
} else {
mpc_input_rewind(i);
return false;
}
x++;
}
mpc_input_unmark(i);
*o = malloc(strlen(c) + 1);
strcpy(*o, c);
return true;
}
/*
** Parser Type
*/
enum {
MPC_TYPE_UNDEFINED = 0,
MPC_TYPE_PASS = 1,
MPC_TYPE_FAIL = 2,
MPC_TYPE_LIFT = 3,
MPC_TYPE_LIFT_VAL = 4,
MPC_TYPE_EXPECT = 5,
MPC_TYPE_SOI = 6,
MPC_TYPE_EOI = 7,
MPC_TYPE_ANY = 8,
MPC_TYPE_SINGLE = 9,
MPC_TYPE_ONEOF = 10,
MPC_TYPE_NONEOF = 11,
MPC_TYPE_RANGE = 12,
MPC_TYPE_SATISFY = 13,
MPC_TYPE_STRING = 14,
MPC_TYPE_APPLY = 15,
MPC_TYPE_APPLY_TO = 16,
MPC_TYPE_NOT = 17,
MPC_TYPE_MAYBE = 18,
MPC_TYPE_MANY = 19,
MPC_TYPE_MANY1 = 20,
MPC_TYPE_COUNT = 21,
MPC_TYPE_ELSE = 22,
MPC_TYPE_ALSO = 23,
MPC_TYPE_OR = 24,
MPC_TYPE_AND = 25,
};
typedef struct { mpc_lift_t lf; void* x; } mpc_pdata_lift_t;
typedef struct { mpc_parser_t* x; char* m; } mpc_pdata_expect_t;
typedef struct { char x; } mpc_pdata_single_t;
typedef struct { char x; char y; } mpc_pdata_range_t;
typedef struct { bool(*f)(char); } mpc_pdata_satisfy_t;
typedef struct { char* x; } mpc_pdata_string_t;
typedef struct { mpc_parser_t* x; mpc_apply_t f; } mpc_pdata_apply_t;
typedef struct { mpc_parser_t* x; mpc_apply_to_t f; void* d; } mpc_pdata_apply_to_t;
typedef struct { mpc_parser_t* x; mpc_dtor_t dx; mpc_lift_t lf; } mpc_pdata_not_t;
typedef struct { mpc_parser_t* x; mpc_fold_t f; int n; mpc_dtor_t dx; mpc_lift_t lf; } mpc_pdata_repeat_t;
typedef struct { mpc_parser_t* x; mpc_parser_t* y; } mpc_pdata_else_t;
typedef struct { mpc_parser_t* x; mpc_parser_t* y; mpc_dtor_t dx; mpc_fold_t f; } mpc_pdata_also_t;
typedef struct { int n; mpc_parser_t** xs; } mpc_pdata_or_t;
typedef struct { int n; mpc_parser_t** xs; mpc_dtor_t* dxs; mpc_afold_t f; } mpc_pdata_and_t;
typedef union {
mpc_pdata_lift_t lift;
mpc_pdata_expect_t expect;
mpc_pdata_single_t single;
mpc_pdata_range_t range;
mpc_pdata_satisfy_t satisfy;
mpc_pdata_string_t string;
mpc_pdata_apply_t apply;
mpc_pdata_apply_to_t apply_to;
mpc_pdata_not_t not;
mpc_pdata_repeat_t repeat;
mpc_pdata_else_t orelse;
mpc_pdata_also_t also;
mpc_pdata_and_t and;
mpc_pdata_or_t or;
} mpc_pdata_t;
struct mpc_parser_t {
bool retained;
char* name;
uint8_t type;
mpc_pdata_t data;
};
/*
** This is rather pleasant. The core parsing routine
** is written in about 500 lines of C.
**
** I also love the way in which each parsing type
** concisely matches some construct or idiom in C.
** Particularly nice is are the `either` and `also`
** types which have a broken but mirrored structure
** with return value and error reflected.
**
*/
#define MPC_SUCCESS(x) r->output = x; return true;
#define MPC_FAILURE(x) r->error = x; return false;
#define MPC_TRY(x, f) if (f) { MPC_SUCCESS(x) } else { MPC_FAILURE(mpc_err_new(i->filename, i->state, "different character")); }
bool mpc_parse_input(mpc_input_t* i, mpc_parser_t* p, mpc_result_t* r) {
memset(r, 0, sizeof(mpc_result_t));
if (p->type == MPC_TYPE_UNDEFINED) { fprintf(stderr, "\nError: Parser Undefined!\n"); abort(); }
/* Trivial Parsers */
if (p->type == MPC_TYPE_PASS) { MPC_SUCCESS(NULL); }
if (p->type == MPC_TYPE_FAIL) { MPC_FAILURE(mpc_err_new(i->filename, i->state, "different character")); }
if (p->type == MPC_TYPE_LIFT) { MPC_SUCCESS(p->data.lift.lf()); }
if (p->type == MPC_TYPE_LIFT_VAL) { MPC_SUCCESS(p->data.lift.x); }
/* Basic Parsers */
char* s = NULL;
if (p->type == MPC_TYPE_SOI) { if (mpc_input_soi(i)) { MPC_SUCCESS(NULL) } else { MPC_FAILURE(mpc_err_new(i->filename, i->state, "start of input")); } }
if (p->type == MPC_TYPE_EOI) { if (mpc_input_eoi(i)) { MPC_SUCCESS(NULL) } else { MPC_FAILURE(mpc_err_new(i->filename, i->state, "end of input")); } }
if (p->type == MPC_TYPE_ANY) { MPC_TRY(s, mpc_input_any(i, &s)); }
if (p->type == MPC_TYPE_SINGLE) { MPC_TRY(s, mpc_input_char(i, p->data.single.x, &s)); }
if (p->type == MPC_TYPE_RANGE) { MPC_TRY(s, mpc_input_range(i, p->data.range.x, p->data.range.y, &s)); }
if (p->type == MPC_TYPE_ONEOF) { MPC_TRY(s, mpc_input_oneof(i, p->data.string.x, &s)); }
if (p->type == MPC_TYPE_NONEOF) { MPC_TRY(s, mpc_input_noneof(i, p->data.string.x, &s)); }
if (p->type == MPC_TYPE_SATISFY) { MPC_TRY(s, mpc_input_satisfy(i, p->data.satisfy.f, &s)); }
if (p->type == MPC_TYPE_STRING) { MPC_TRY(s, mpc_input_string(i, p->data.string.x, &s)); }
/* Advanced Parsers */
int c = 0;
mpc_val_t* t = NULL;
mpc_result_t x, y;
memset(&x, 0, sizeof(mpc_result_t));
memset(&y, 0, sizeof(mpc_result_t));
if (p->type == MPC_TYPE_EXPECT) {
if (mpc_parse_input(i, p->data.expect.x, &x)) {
MPC_SUCCESS(x.output);
} else {
mpc_err_delete(x.error);
MPC_FAILURE(mpc_err_new(i->filename, i->state, p->data.expect.m));
}
}
if (p->type == MPC_TYPE_APPLY) {
if (mpc_parse_input(i, p->data.apply.x, &x)) {
MPC_SUCCESS(p->data.apply.f(x.output));
} else {
MPC_FAILURE(x.error);
}
}
if (p->type == MPC_TYPE_APPLY_TO) {
if (mpc_parse_input(i, p->data.apply_to.x, &x)) {
MPC_SUCCESS(p->data.apply_to.f(x.output, p->data.apply_to.d));
} else {
MPC_FAILURE(x.error);
}
}
if (p->type == MPC_TYPE_NOT) {
if (mpc_parse_input(i, p->data.not.x, &x)) {
p->data.not.dx(x.output);
MPC_FAILURE(mpc_err_new(i->filename, i->state, "different character"));
} else {
mpc_err_delete(x.error);
MPC_SUCCESS(p->data.not.lf());
}
}
if (p->type == MPC_TYPE_MAYBE) {
if (mpc_parse_input(i, p->data.repeat.x, &x)) { MPC_SUCCESS(x.output); }
mpc_err_delete(x.error);
MPC_SUCCESS(p->data.repeat.lf());
}
if (p->type == MPC_TYPE_MANY) {
while (mpc_parse_input(i, p->data.repeat.x, &x)) { t = p->data.repeat.f(t, x.output); }
mpc_err_delete(x.error);
MPC_SUCCESS(t ? t : p->data.repeat.lf());
}
if (p->type == MPC_TYPE_MANY1) {
while (mpc_parse_input(i, p->data.repeat.x, &x)) { t = p->data.repeat.f(t, x.output); c++; }
if (c >= 1) {
mpc_err_delete(x.error);
MPC_SUCCESS(t);
} else {
MPC_FAILURE(mpc_err_many1(x.error));
}
}
if (p->type == MPC_TYPE_COUNT) {
mpc_input_mark(i);
while (mpc_parse_input(i, p->data.repeat.x, &x)) {
t = p->data.repeat.f(t, x.output);
c++;
if (c == p->data.repeat.n) { break; }
}
if (c == p->data.repeat.n) {
mpc_input_unmark(i);
MPC_SUCCESS(t ? t : p->data.repeat.lf());
} else {
p->data.repeat.dx(t);
mpc_input_rewind(i);
MPC_FAILURE(mpc_err_count(x.error, p->data.repeat.n));
}
}
/* Combinatory Parsers */
if (p->type == MPC_TYPE_ELSE) {
if (mpc_parse_input(i, p->data.orelse.x, &x)) { MPC_SUCCESS(x.output); }
if (mpc_parse_input(i, p->data.orelse.y, &y)) { mpc_err_delete(x.error); MPC_SUCCESS(y.output); }
MPC_FAILURE(mpc_err_either(x.error, y.error));
}
if (p->type == MPC_TYPE_ALSO) {
mpc_input_mark(i);
if (!mpc_parse_input(i, p->data.also.x, &x)) { mpc_input_rewind(i); MPC_FAILURE(x.error); }
if (!mpc_parse_input(i, p->data.also.y, &y)) { mpc_input_rewind(i); p->data.also.dx(x.output); MPC_FAILURE(y.error); }
mpc_input_unmark(i);
MPC_SUCCESS(p->data.also.f(x.output, y.output));
}
if (p->type == MPC_TYPE_OR) {
mpc_result_t* rs = malloc(sizeof(mpc_result_t) * p->data.or.n);
int ri, pri;
for (ri = 0; ri < p->data.or.n; ri++) {
if (mpc_parse_input(i, p->data.or.xs[ri], &rs[ri])) {
for (pri = 0; pri < ri; pri++) { mpc_err_delete(rs[pri].error); }
r->output = rs[ri].output;
free(rs);
return true;
}
}
mpc_err_t** vals = malloc(sizeof(mpc_err_t*) * p->data.or.n);
for (ri = 0; ri < p->data.and.n; ri++) {
vals[ri] = rs[ri].error;
}
r->error = mpc_err_or(vals, p->data.or.n);
free(vals);
free(rs);
return false;
}
if (p->type == MPC_TYPE_AND) {
mpc_input_mark(i);
mpc_result_t* rs = malloc(sizeof(mpc_result_t) * p->data.and.n);
int ri, pri;
for (ri = 0; ri < p->data.and.n; ri++) {
if (!mpc_parse_input(i, p->data.and.xs[ri], &rs[ri])) {
for (pri = 0; pri < ri; pri++) { p->data.and.dxs[pri](rs[pri].output); }
r->error = rs[ri].error;
free(rs);
mpc_input_rewind(i);
return false;
}
}
mpc_val_t** vals = malloc(sizeof(mpc_val_t*) * p->data.and.n);
for (ri = 0; ri < p->data.and.n; ri++) {
vals[ri] = rs[ri].output;
}
r->output = p->data.and.f(p->data.and.n, vals);
free(rs);
free(vals);
mpc_input_unmark(i);
return true;
}
fprintf(stderr, "\nError: Unknown Parser Type Id %i!\n", p->type);
abort();
}
#undef MPC_SUCCESS
#undef MPC_FAILURE
#undef MPC_TRY
bool mpc_parse(const char* filename, const char* s, mpc_parser_t* p, mpc_result_t* r) {
mpc_input_t* i = mpc_input_new(filename, s);
bool x = mpc_parse_input(i, p, r);
mpc_input_delete(i);
return x;
}
bool mpc_parse_file(const char* filename, FILE* f, mpc_parser_t* p, mpc_result_t* r) {
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
char* buff = malloc(len + 1);
fread(buff, 1, len, f);
buff[len] = '\0';
bool x = mpc_parse(filename, buff, p, r);
free(buff);
return x;
}
bool mpc_parse_filename(const char* filename, mpc_parser_t* p, mpc_result_t* r) {
FILE* f = fopen(filename, "r");
if (f == NULL) {
fprintf(stderr, "Error: Unable to open file '%s'\n", filename);
abort();
}
bool res = mpc_parse_file(filename, f, p, r);
fclose(f);
return res;
}
/*
** Building a Parser
*/
static void mpc_undefine_unretained(mpc_parser_t* p, bool force);
static void mpc_undefine_or(mpc_parser_t* p) {
int i;
for (i = 0; i < p->data.or.n; i++) {
mpc_undefine_unretained(p->data.or.xs[i], false);
}
free(p->data.or.xs);
}
static void mpc_undefine_and(mpc_parser_t* p) {
int i;
for (i = 0; i < p->data.and.n; i++) {
mpc_undefine_unretained(p->data.and.xs[i], false);
}
free(p->data.and.xs);
free(p->data.and.dxs);
}
static void mpc_undefine_unretained(mpc_parser_t* p, bool force) {
if (p->retained && !force) { return; }
switch (p->type) {
case MPC_TYPE_ONEOF:
case MPC_TYPE_NONEOF:
case MPC_TYPE_STRING:
free(p->data.string.x);
break;
case MPC_TYPE_APPLY:
mpc_undefine_unretained(p->data.apply.x, false);
break;
case MPC_TYPE_EXPECT:
mpc_undefine_unretained(p->data.expect.x, false);
free(p->data.expect.m);
break;
case MPC_TYPE_MAYBE:
case MPC_TYPE_MANY:
case MPC_TYPE_MANY1:
case MPC_TYPE_COUNT:
mpc_undefine_unretained(p->data.repeat.x, false);
break;
case MPC_TYPE_ELSE:
mpc_undefine_unretained(p->data.orelse.x, false);
mpc_undefine_unretained(p->data.orelse.y, false);
break;
case MPC_TYPE_ALSO:
mpc_undefine_unretained(p->data.also.x, false);
mpc_undefine_unretained(p->data.also.y, false);
break;
case MPC_TYPE_OR:
mpc_undefine_or(p);
break;
case MPC_TYPE_AND:
mpc_undefine_and(p);
break;
default: break;
}
if (!force) {
free(p->name);
free(p);
}
}
void mpc_delete(mpc_parser_t* p) {
if (p->retained) {
if (p->type != MPC_TYPE_UNDEFINED) {
fprintf(stderr, "\nError: Parser still Defined! Use `mpc_undefine` before delete!\n");
abort();
} else {
free(p->name);
free(p);
}
} else {
mpc_undefine_unretained(p, false);
}
}
static mpc_parser_t* mpc_undefined(void) {
mpc_parser_t* p = calloc(1, sizeof(mpc_parser_t));
p->retained = false;
p->type = MPC_TYPE_UNDEFINED;
p->name = NULL;
return p;
}
mpc_parser_t* mpc_new(const char* name) {
mpc_parser_t* p = mpc_undefined();
p->retained = true;
p->name = realloc(p->name, strlen(name) + 1);
strcpy(p->name, name);
return p;
}
mpc_parser_t* mpc_undefine(mpc_parser_t* p) {
mpc_undefine_unretained(p, true);
p->type = MPC_TYPE_UNDEFINED;
return p;
}
mpc_parser_t* mpc_define(mpc_parser_t* p, mpc_parser_t* a) {
p->type = a->type;
p->data = a->data;
free(a);
return p;
}
static void mpc_delete_all_va(int n, va_list va) {
int i;
for (i = 0; i < n; i++) {
mpc_delete(va_arg(va, mpc_parser_t*));
}
}
static void mpc_undefine_all_va(int n, va_list va) {
int i;
for (i = 0; i < n; i++) {
mpc_undefine(va_arg(va, mpc_parser_t*));
}
}
static void mpc_delete_all(int n, ...) {
va_list va;
va_start(va, n);
mpc_delete_all_va(n, va);
va_end(va);
}
static void mpc_undefine_all(int n, ...) {
va_list va;
va_start(va, n);
mpc_undefine_all_va(n, va);
va_end(va);
}
void mpc_cleanup(int n, ...) {
va_list va;
va_start(va, n);
mpc_cleanup_va(n, va);
va_end(va);
}
void mpc_cleanup_va(int n, va_list va) {
mpc_undefine_all_va(n, va);
mpc_delete_all_va(n, va);
}
mpc_parser_t* mpc_pass(void) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_PASS;
return p;
}
mpc_parser_t* mpc_fail(void) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_FAIL;
return p;
}
mpc_parser_t* mpc_lift_val(mpc_val_t* x) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_LIFT_VAL;
p->data.lift.x = x;
return p;
}
mpc_parser_t* mpc_lift(mpc_lift_t lf) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_LIFT;
p->data.lift.lf = lf;
return p;
}
mpc_parser_t* mpc_expect(mpc_parser_t* a, const char* expected) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_EXPECT;
p->data.expect.x = a;
p->data.expect.m = malloc(strlen(expected) + 1);
strcpy(p->data.expect.m, expected);
return p;
}
/*
** Basic Parsers
*/
mpc_parser_t* mpc_any(void) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_ANY;
return mpc_expect(p, "any character");
}
mpc_parser_t* mpc_char(char c) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_SINGLE;
p->data.single.x = c;
return mpc_expect(p, (char[]){ '\'', c, '\'', '\0' } );
}
mpc_parser_t* mpc_range(char s, char e) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_RANGE;
p->data.range.x = s;
p->data.range.y = e;
char expected[30];
strcpy(expected, "character between '");
strcat(expected, (char[]){ s, '\0' });
strcat(expected, "' and '");
strcat(expected, (char[]){ e, '\0' });
strcat(expected, "'");
return mpc_expect(p, expected);
}
mpc_parser_t* mpc_oneof(const char* s) {
mpc_parser_t* p = mpc_undefined();
p->type = MPC_TYPE_ONEOF;
p->data.string.x = malloc(strlen(s) + 1);
strcpy(p->data.string.x, s);
char* expected = malloc(strlen(s) + 10);