forked from howerj/dbcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpc.c
4127 lines (3326 loc) · 107 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"
/*
** State Type
*/
static mpc_state_t mpc_state_invalid(void) {
mpc_state_t s;
s.pos = -1;
s.row = -1;
s.col = -1;
s.term = 0;
return s;
}
static mpc_state_t mpc_state_new(void) {
mpc_state_t s;
s.pos = 0;
s.row = 0;
s.col = 0;
s.term = 0;
return s;
}
/*
** Input Type
*/
/*
** In mpc the input type has three modes of
** operation: String, File and Pipe.
**
** String is easy. The whole contents are
** loaded into a buffer and scanned through.
** The cursor can jump around at will making
** backtracking easy.
**
** The second is a File which is also somewhat
** easy. The contents are never loaded into
** memory but backtracking can still be achieved
** by seeking in the file at different positions.
**
** The final mode is Pipe. This is the difficult
** one. As we assume pipes cannot be seeked - and
** only support a single character lookahead at
** any point, when the input is marked for a
** potential backtracking we start buffering any
** input.
**
** This means that if we are requested to seek
** back we can simply start reading from the
** buffer instead of the input.
**
** Of course using `mpc_predictive` will disable
** backtracking and make LL(1) grammars easy
** to parse for all input methods.
**
*/
enum {
MPC_INPUT_STRING = 0,
MPC_INPUT_FILE = 1,
MPC_INPUT_PIPE = 2
};
enum {
MPC_INPUT_MARKS_MIN = 32
};
enum {
MPC_INPUT_MEM_NUM = 512
};
typedef struct {
char mem[64];
} mpc_mem_t;
typedef struct {
int type;
char *filename;
mpc_state_t state;
char *string;
char *buffer;
FILE *file;
int suppress;
int backtrack;
int marks_slots;
int marks_num;
mpc_state_t *marks;
char *lasts;
char last;
size_t mem_index;
char mem_full[MPC_INPUT_MEM_NUM];
mpc_mem_t mem[MPC_INPUT_MEM_NUM];
} mpc_input_t;
static mpc_input_t *mpc_input_new_string(const char *filename, const char *string) {
mpc_input_t *i = malloc(sizeof(mpc_input_t));
i->filename = malloc(strlen(filename) + 1);
strcpy(i->filename, filename);
i->type = MPC_INPUT_STRING;
i->state = mpc_state_new();
i->string = malloc(strlen(string) + 1);
strcpy(i->string, string);
i->buffer = NULL;
i->file = NULL;
i->suppress = 0;
i->backtrack = 1;
i->marks_num = 0;
i->marks_slots = MPC_INPUT_MARKS_MIN;
i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots);
i->lasts = malloc(sizeof(char) * i->marks_slots);
i->last = '\0';
i->mem_index = 0;
memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM);
return i;
}
static mpc_input_t *mpc_input_new_nstring(const char *filename, const char *string, size_t length) {
mpc_input_t *i = malloc(sizeof(mpc_input_t));
i->filename = malloc(strlen(filename) + 1);
strcpy(i->filename, filename);
i->type = MPC_INPUT_STRING;
i->state = mpc_state_new();
i->string = malloc(length + 1);
strncpy(i->string, string, length);
i->string[length] = '\0';
i->buffer = NULL;
i->file = NULL;
i->suppress = 0;
i->backtrack = 1;
i->marks_num = 0;
i->marks_slots = MPC_INPUT_MARKS_MIN;
i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots);
i->lasts = malloc(sizeof(char) * i->marks_slots);
i->last = '\0';
i->mem_index = 0;
memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM);
return i;
}
static mpc_input_t *mpc_input_new_pipe(const char *filename, FILE *pipe) {
mpc_input_t *i = malloc(sizeof(mpc_input_t));
i->filename = malloc(strlen(filename) + 1);
strcpy(i->filename, filename);
i->type = MPC_INPUT_PIPE;
i->state = mpc_state_new();
i->string = NULL;
i->buffer = NULL;
i->file = pipe;
i->suppress = 0;
i->backtrack = 1;
i->marks_num = 0;
i->marks_slots = MPC_INPUT_MARKS_MIN;
i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots);
i->lasts = malloc(sizeof(char) * i->marks_slots);
i->last = '\0';
i->mem_index = 0;
memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM);
return i;
}
static mpc_input_t *mpc_input_new_file(const char *filename, FILE *file) {
mpc_input_t *i = malloc(sizeof(mpc_input_t));
i->filename = malloc(strlen(filename) + 1);
strcpy(i->filename, filename);
i->type = MPC_INPUT_FILE;
i->state = mpc_state_new();
i->string = NULL;
i->buffer = NULL;
i->file = file;
i->suppress = 0;
i->backtrack = 1;
i->marks_num = 0;
i->marks_slots = MPC_INPUT_MARKS_MIN;
i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots);
i->lasts = malloc(sizeof(char) * i->marks_slots);
i->last = '\0';
i->mem_index = 0;
memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM);
return i;
}
static void mpc_input_delete(mpc_input_t *i) {
free(i->filename);
if (i->type == MPC_INPUT_STRING) { free(i->string); }
if (i->type == MPC_INPUT_PIPE) { free(i->buffer); }
free(i->marks);
free(i->lasts);
free(i);
}
static int mpc_mem_ptr(mpc_input_t *i, void *p) {
return
(char*)p >= (char*)(i->mem) &&
(char*)p < (char*)(i->mem) + (MPC_INPUT_MEM_NUM * sizeof(mpc_mem_t));
}
static void *mpc_malloc(mpc_input_t *i, size_t n) {
size_t j;
char *p;
if (n > sizeof(mpc_mem_t)) { return malloc(n); }
j = i->mem_index;
do {
if (!i->mem_full[i->mem_index]) {
p = (void*)(i->mem + i->mem_index);
i->mem_full[i->mem_index] = 1;
i->mem_index = (i->mem_index+1) % MPC_INPUT_MEM_NUM;
return p;
}
i->mem_index = (i->mem_index+1) % MPC_INPUT_MEM_NUM;
} while (j != i->mem_index);
return malloc(n);
}
static void *mpc_calloc(mpc_input_t *i, size_t n, size_t m) {
char *x = mpc_malloc(i, n * m);
memset(x, 0, n * m);
return x;
}
static void mpc_free(mpc_input_t *i, void *p) {
size_t j;
if (!mpc_mem_ptr(i, p)) { free(p); return; }
j = ((size_t)(((char*)p) - ((char*)i->mem))) / sizeof(mpc_mem_t);
i->mem_full[j] = 0;
}
static void *mpc_realloc(mpc_input_t *i, void *p, size_t n) {
char *q = NULL;
if (!mpc_mem_ptr(i, p)) { return realloc(p, n); }
if (n > sizeof(mpc_mem_t)) {
q = malloc(n);
memcpy(q, p, sizeof(mpc_mem_t));
mpc_free(i, p);
return q;
}
return p;
}
static void *mpc_export(mpc_input_t *i, void *p) {
char *q = NULL;
if (!mpc_mem_ptr(i, p)) { return p; }
q = malloc(sizeof(mpc_mem_t));
memcpy(q, p, sizeof(mpc_mem_t));
mpc_free(i, p);
return q;
}
static void mpc_input_backtrack_disable(mpc_input_t *i) { i->backtrack--; }
static void mpc_input_backtrack_enable(mpc_input_t *i) { i->backtrack++; }
static void mpc_input_suppress_disable(mpc_input_t *i) { i->suppress--; }
static void mpc_input_suppress_enable(mpc_input_t *i) { i->suppress++; }
static void mpc_input_mark(mpc_input_t *i) {
if (i->backtrack < 1) { return; }
i->marks_num++;
if (i->marks_num > i->marks_slots) {
i->marks_slots = i->marks_num + i->marks_num / 2;
i->marks = realloc(i->marks, sizeof(mpc_state_t) * i->marks_slots);
i->lasts = realloc(i->lasts, sizeof(char) * i->marks_slots);
}
i->marks[i->marks_num-1] = i->state;
i->lasts[i->marks_num-1] = i->last;
if (i->type == MPC_INPUT_PIPE && i->marks_num == 1) {
i->buffer = calloc(1, 1);
}
}
static void mpc_input_unmark(mpc_input_t *i) {
int j;
if (i->backtrack < 1) { return; }
i->marks_num--;
if (i->marks_slots > i->marks_num + i->marks_num / 2
&& i->marks_slots > MPC_INPUT_MARKS_MIN) {
i->marks_slots =
i->marks_num > MPC_INPUT_MARKS_MIN ?
i->marks_num : MPC_INPUT_MARKS_MIN;
i->marks = realloc(i->marks, sizeof(mpc_state_t) * i->marks_slots);
i->lasts = realloc(i->lasts, sizeof(char) * i->marks_slots);
}
if (i->type == MPC_INPUT_PIPE && i->marks_num == 0) {
for (j = strlen(i->buffer) - 1; j >= 0; j--)
ungetc(i->buffer[j], i->file);
free(i->buffer);
i->buffer = NULL;
}
}
static void mpc_input_rewind(mpc_input_t *i) {
if (i->backtrack < 1) { return; }
i->state = i->marks[i->marks_num-1];
i->last = i->lasts[i->marks_num-1];
if (i->type == MPC_INPUT_FILE) {
fseek(i->file, i->state.pos, SEEK_SET);
}
mpc_input_unmark(i);
}
static int mpc_input_buffer_in_range(mpc_input_t *i) {
return i->state.pos < (long)(strlen(i->buffer) + i->marks[0].pos);
}
static char mpc_input_buffer_get(mpc_input_t *i) {
return i->buffer[i->state.pos - i->marks[0].pos];
}
static char mpc_input_getc(mpc_input_t *i) {
char c = '\0';
switch (i->type) {
case MPC_INPUT_STRING: return i->string[i->state.pos];
case MPC_INPUT_FILE: c = fgetc(i->file); return c;
case MPC_INPUT_PIPE:
if (!i->buffer) { c = getc(i->file); return c; }
if (i->buffer && mpc_input_buffer_in_range(i)) {
c = mpc_input_buffer_get(i);
return c;
} else {
c = getc(i->file);
return c;
}
default: return c;
}
}
static char mpc_input_peekc(mpc_input_t *i) {
char c = '\0';
switch (i->type) {
case MPC_INPUT_STRING: return i->string[i->state.pos];
case MPC_INPUT_FILE:
c = fgetc(i->file);
if (feof(i->file)) { return '\0'; }
fseek(i->file, -1, SEEK_CUR);
return c;
case MPC_INPUT_PIPE:
if (!i->buffer) {
c = getc(i->file);
if (feof(i->file)) { return '\0'; }
ungetc(c, i->file);
return c;
}
if (i->buffer && mpc_input_buffer_in_range(i)) {
return mpc_input_buffer_get(i);
} else {
c = getc(i->file);
if (feof(i->file)) { return '\0'; }
ungetc(c, i->file);
return c;
}
default: return c;
}
}
static int mpc_input_terminated(mpc_input_t *i) {
return mpc_input_peekc(i) == '\0';
}
static int mpc_input_failure(mpc_input_t *i, char c) {
switch (i->type) {
case MPC_INPUT_STRING: { break; }
case MPC_INPUT_FILE: fseek(i->file, -1, SEEK_CUR); { break; }
case MPC_INPUT_PIPE: {
if (!i->buffer) { ungetc(c, i->file); break; }
if (i->buffer && mpc_input_buffer_in_range(i)) {
break;
} else {
ungetc(c, i->file);
}
}
default: { break; }
}
return 0;
}
static int mpc_input_success(mpc_input_t *i, char c, char **o) {
if (i->type == MPC_INPUT_PIPE
&& i->buffer && !mpc_input_buffer_in_range(i)) {
i->buffer = realloc(i->buffer, strlen(i->buffer) + 2);
i->buffer[strlen(i->buffer) + 1] = '\0';
i->buffer[strlen(i->buffer) + 0] = c;
}
i->last = c;
i->state.pos++;
i->state.col++;
if (c == '\n') {
i->state.col = 0;
i->state.row++;
}
if (o) {
(*o) = mpc_malloc(i, 2);
(*o)[0] = c;
(*o)[1] = '\0';
}
return 1;
}
static int mpc_input_any(mpc_input_t *i, char **o) {
char x;
if (mpc_input_terminated(i)) { return 0; }
x = mpc_input_getc(i);
return mpc_input_success(i, x, o);
}
static int mpc_input_char(mpc_input_t *i, char c, char **o) {
char x;
if (mpc_input_terminated(i)) { return 0; }
x = mpc_input_getc(i);
return x == c ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
}
static int mpc_input_range(mpc_input_t *i, char c, char d, char **o) {
char x;
if (mpc_input_terminated(i)) { return 0; }
x = mpc_input_getc(i);
return x >= c && x <= d ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
}
static int mpc_input_oneof(mpc_input_t *i, const char *c, char **o) {
char x;
if (mpc_input_terminated(i)) { return 0; }
x = mpc_input_getc(i);
return strchr(c, x) != 0 ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
}
static int mpc_input_noneof(mpc_input_t *i, const char *c, char **o) {
char x;
if (mpc_input_terminated(i)) { return 0; }
x = mpc_input_getc(i);
return strchr(c, x) == 0 ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
}
static int mpc_input_satisfy(mpc_input_t *i, int(*cond)(char), char **o) {
char x;
if (mpc_input_terminated(i)) { return 0; }
x = mpc_input_getc(i);
return cond(x) ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
}
static int mpc_input_string(mpc_input_t *i, const char *c, char **o) {
const char *x = c;
mpc_input_mark(i);
while (*x) {
if (!mpc_input_char(i, *x, NULL)) {
mpc_input_rewind(i);
return 0;
}
x++;
}
mpc_input_unmark(i);
*o = mpc_malloc(i, strlen(c) + 1);
strcpy(*o, c);
return 1;
}
static int mpc_input_anchor(mpc_input_t* i, int(*f)(char,char), char **o) {
*o = NULL;
return f(i->last, mpc_input_peekc(i));
}
static int mpc_input_soi(mpc_input_t* i, char **o) {
*o = NULL;
return i->last == '\0';
}
static int mpc_input_eoi(mpc_input_t* i, char **o) {
*o = NULL;
if (i->state.term) {
return 0;
} else if (mpc_input_terminated(i)) {
i->state.term = 1;
return 1;
} else {
return 0;
}
}
static mpc_state_t *mpc_input_state_copy(mpc_input_t *i) {
mpc_state_t *r = mpc_malloc(i, sizeof(mpc_state_t));
memcpy(r, &i->state, sizeof(mpc_state_t));
return r;
}
/*
** Error Type
*/
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->failure);
free(x);
}
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) {
char *str = mpc_err_string(x);
fprintf(f, "%s", str);
free(str);
}
static void mpc_err_string_cat(char *buffer, int *pos, int *max, char const *fmt, ...) {
/* TODO: Error Checking on Length */
int left = ((*max) - (*pos));
va_list va;
va_start(va, fmt);
if (left < 0) { left = 0;}
(*pos) += vsprintf(buffer + (*pos), fmt, va);
va_end(va);
}
static const char *mpc_err_char_unescape(char c, char char_unescape_buffer[4]) {
char_unescape_buffer[0] = '\'';
char_unescape_buffer[1] = ' ';
char_unescape_buffer[2] = '\'';
char_unescape_buffer[3] = '\0';
switch (c) {
case '\a': return "bell";
case '\b': return "backspace";
case '\f': return "formfeed";
case '\r': return "carriage return";
case '\v': return "vertical tab";
case '\0': return "end of input";
case '\n': return "newline";
case '\t': return "tab";
case ' ' : return "space";
default:
char_unescape_buffer[1] = c;
return char_unescape_buffer;
}
}
char *mpc_err_string(mpc_err_t *x) {
int i;
int pos = 0;
int max = 1023;
char *buffer = calloc(1, 1024);
char char_unescape_buffer[4];
if (x->failure) {
mpc_err_string_cat(buffer, &pos, &max,
"%s: error: %s\n", x->filename, x->failure);
return buffer;
}
mpc_err_string_cat(buffer, &pos, &max,
"%s:%li:%li: error: expected ", x->filename, x->state.row+1, x->state.col+1);
if (x->expected_num == 0) { mpc_err_string_cat(buffer, &pos, &max, "ERROR: NOTHING EXPECTED"); }
if (x->expected_num == 1) { mpc_err_string_cat(buffer, &pos, &max, "%s", x->expected[0]); }
if (x->expected_num >= 2) {
for (i = 0; i < x->expected_num-2; i++) {
mpc_err_string_cat(buffer, &pos, &max, "%s, ", x->expected[i]);
}
mpc_err_string_cat(buffer, &pos, &max, "%s or %s",
x->expected[x->expected_num-2],
x->expected[x->expected_num-1]);
}
mpc_err_string_cat(buffer, &pos, &max, " at ");
mpc_err_string_cat(buffer, &pos, &max, mpc_err_char_unescape(x->received, char_unescape_buffer));
mpc_err_string_cat(buffer, &pos, &max, "\n");
return realloc(buffer, strlen(buffer) + 1);
}
static mpc_err_t *mpc_err_new(mpc_input_t *i, const char *expected) {
mpc_err_t *x;
if (i->suppress) { return NULL; }
x = mpc_malloc(i, sizeof(mpc_err_t));
x->filename = mpc_malloc(i, strlen(i->filename) + 1);
strcpy(x->filename, i->filename);
x->state = i->state;
x->expected_num = 1;
x->expected = mpc_malloc(i, sizeof(char*));
x->expected[0] = mpc_malloc(i, strlen(expected) + 1);
strcpy(x->expected[0], expected);
x->failure = NULL;
x->received = mpc_input_peekc(i);
return x;
}
static mpc_err_t *mpc_err_fail(mpc_input_t *i, const char *failure) {
mpc_err_t *x;
if (i->suppress) { return NULL; }
x = mpc_malloc(i, sizeof(mpc_err_t));
x->filename = mpc_malloc(i, strlen(i->filename) + 1);
strcpy(x->filename, i->filename);
x->state = i->state;
x->expected_num = 0;
x->expected = NULL;
x->failure = mpc_malloc(i, strlen(failure) + 1);
strcpy(x->failure, failure);
x->received = ' ';
return x;
}
static mpc_err_t *mpc_err_file(const char *filename, const char *failure) {
mpc_err_t *x;
x = malloc(sizeof(mpc_err_t));
x->filename = malloc(strlen(filename) + 1);
strcpy(x->filename, filename);
x->state = mpc_state_new();
x->expected_num = 0;
x->expected = NULL;
x->failure = malloc(strlen(failure) + 1);
strcpy(x->failure, failure);
x->received = ' ';
return x;
}
static void mpc_err_delete_internal(mpc_input_t *i, mpc_err_t *x) {
int j;
if (x == NULL) { return; }
for (j = 0; j < x->expected_num; j++) { mpc_free(i, x->expected[j]); }
mpc_free(i, x->expected);
mpc_free(i, x->filename);
mpc_free(i, x->failure);
mpc_free(i, x);
}
static mpc_err_t *mpc_err_export(mpc_input_t *i, mpc_err_t *x) {
int j;
for (j = 0; j < x->expected_num; j++) {
x->expected[j] = mpc_export(i, x->expected[j]);
}
x->expected = mpc_export(i, x->expected);
x->filename = mpc_export(i, x->filename);
x->failure = mpc_export(i, x->failure);
return mpc_export(i, x);
}
static int mpc_err_contains_expected(mpc_input_t *i, mpc_err_t *x, char *expected) {
int j;
(void)i;
for (j = 0; j < x->expected_num; j++) {
if (strcmp(x->expected[j], expected) == 0) { return 1; }
}
return 0;
}
static void mpc_err_add_expected(mpc_input_t *i, mpc_err_t *x, char *expected) {
(void)i;
x->expected_num++;
x->expected = mpc_realloc(i, x->expected, sizeof(char*) * x->expected_num);
x->expected[x->expected_num-1] = mpc_malloc(i, strlen(expected) + 1);
strcpy(x->expected[x->expected_num-1], expected);
}
static mpc_err_t *mpc_err_or(mpc_input_t *i, mpc_err_t** x, int n) {
int j, k, fst;
mpc_err_t *e;
fst = -1;
for (j = 0; j < n; j++) {
if (x[j] != NULL) { fst = j; }
}
if (fst == -1) { return NULL; }
e = mpc_malloc(i, sizeof(mpc_err_t));
e->state = mpc_state_invalid();
e->expected_num = 0;
e->expected = NULL;
e->failure = NULL;
e->filename = mpc_malloc(i, strlen(x[fst]->filename)+1);
strcpy(e->filename, x[fst]->filename);
for (j = 0; j < n; j++) {
if (x[j] == NULL) { continue; }
if (x[j]->state.pos > e->state.pos) { e->state = x[j]->state; }
}
for (j = 0; j < n; j++) {
if (x[j] == NULL) { continue; }
if (x[j]->state.pos < e->state.pos) { continue; }
if (x[j]->failure) {
e->failure = mpc_malloc(i, strlen(x[j]->failure)+1);
strcpy(e->failure, x[j]->failure);
break;
}
e->received = x[j]->received;
for (k = 0; k < x[j]->expected_num; k++) {
if (!mpc_err_contains_expected(i, e, x[j]->expected[k])) {
mpc_err_add_expected(i, e, x[j]->expected[k]);
}
}
}
for (j = 0; j < n; j++) {
if (x[j] == NULL) { continue; }
mpc_err_delete_internal(i, x[j]);
}
return e;
}
static mpc_err_t *mpc_err_repeat(mpc_input_t *i, mpc_err_t *x, const char *prefix) {
int j = 0;
size_t l = 0;
char *expect = NULL;
if (x == NULL) { return NULL; }
if (x->expected_num == 0) {
expect = mpc_calloc(i, 1, 1);
x->expected_num = 1;
x->expected = mpc_realloc(i, x->expected, sizeof(char*) * x->expected_num);
x->expected[0] = expect;
return x;
}
else if (x->expected_num == 1) {
expect = mpc_malloc(i, strlen(prefix) + strlen(x->expected[0]) + 1);
strcpy(expect, prefix);
strcat(expect, x->expected[0]);
mpc_free(i, x->expected[0]);
x->expected[0] = expect;
return x;
}
else if (x->expected_num > 1) {
l += strlen(prefix);
for (j = 0; j < x->expected_num-2; j++) {
l += strlen(x->expected[j]) + strlen(", ");
}
l += strlen(x->expected[x->expected_num-2]);
l += strlen(" or ");
l += strlen(x->expected[x->expected_num-1]);
expect = mpc_malloc(i, l + 1);
strcpy(expect, prefix);
for (j = 0; j < x->expected_num-2; j++) {
strcat(expect, x->expected[j]); strcat(expect, ", ");
}
strcat(expect, x->expected[x->expected_num-2]);
strcat(expect, " or ");
strcat(expect, x->expected[x->expected_num-1]);
for (j = 0; j < x->expected_num; j++) { mpc_free(i, x->expected[j]); }
x->expected_num = 1;
x->expected = mpc_realloc(i, x->expected, sizeof(char*) * x->expected_num);
x->expected[0] = expect;
return x;
}
return NULL;
}
static mpc_err_t *mpc_err_many1(mpc_input_t *i, mpc_err_t *x) {
return mpc_err_repeat(i, x, "one or more of ");
}
static mpc_err_t *mpc_err_count(mpc_input_t *i, mpc_err_t *x, int n) {
mpc_err_t *y;
int digits = n/10 + 1;
char *prefix;
prefix = mpc_malloc(i, digits + strlen(" of ") + 1);
if (!prefix) {
return NULL;
}
sprintf(prefix, "%i of ", n);
y = mpc_err_repeat(i, x, prefix);
mpc_free(i, prefix);
return y;
}
static mpc_err_t *mpc_err_merge(mpc_input_t *i, mpc_err_t *x, mpc_err_t *y) {
mpc_err_t *errs[2];
errs[0] = x;
errs[1] = y;
return mpc_err_or(i, errs, 2);
}
/*
** 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_ANCHOR = 6,
MPC_TYPE_STATE = 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_PREDICT = 17,
MPC_TYPE_NOT = 18,
MPC_TYPE_MAYBE = 19,
MPC_TYPE_MANY = 20,
MPC_TYPE_MANY1 = 21,
MPC_TYPE_COUNT = 22,
MPC_TYPE_OR = 23,
MPC_TYPE_AND = 24,
MPC_TYPE_CHECK = 25,
MPC_TYPE_CHECK_WITH = 26,
MPC_TYPE_SOI = 27,
MPC_TYPE_EOI = 28,
MPC_TYPE_SEPBY1 = 29
};
typedef struct { char *m; } mpc_pdata_fail_t;
typedef struct { mpc_ctor_t lf; void *x; } mpc_pdata_lift_t;
typedef struct { mpc_parser_t *x; char *m; } mpc_pdata_expect_t;
typedef struct { int(*f)(char,char); } mpc_pdata_anchor_t;
typedef struct { char x; } mpc_pdata_single_t;
typedef struct { char x; char y; } mpc_pdata_range_t;
typedef struct { int(*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_check_t f; char *e; } mpc_pdata_check_t;
typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_check_with_t f; void *d; char *e; } mpc_pdata_check_with_t;
typedef struct { mpc_parser_t *x; } mpc_pdata_predict_t;
typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_ctor_t lf; } mpc_pdata_not_t;
typedef struct { int n; mpc_fold_t f; mpc_parser_t *x; mpc_dtor_t dx; } mpc_pdata_repeat_t;
typedef struct { int n; mpc_parser_t **xs; } mpc_pdata_or_t;
typedef struct { int n; mpc_fold_t f; mpc_parser_t **xs; mpc_dtor_t *dxs; } mpc_pdata_and_t;
typedef struct { int n; mpc_fold_t f; mpc_parser_t *x; mpc_parser_t *sep; } mpc_pdata_sepby1;
typedef union {
mpc_pdata_fail_t fail;
mpc_pdata_lift_t lift;
mpc_pdata_expect_t expect;
mpc_pdata_anchor_t anchor;
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_check_t check;
mpc_pdata_check_with_t check_with;
mpc_pdata_predict_t predict;
mpc_pdata_not_t not;
mpc_pdata_repeat_t repeat;
mpc_pdata_and_t and;
mpc_pdata_or_t or;
mpc_pdata_sepby1 sepby1;
} mpc_pdata_t;
struct mpc_parser_t {
char *name;
mpc_pdata_t data;
char type;
char retained;
};
static mpc_val_t *mpcf_input_nth_free(mpc_input_t *i, int n, mpc_val_t **xs, int x) {
int j;
for (j = 0; j < n; j++) { if (j != x) { mpc_free(i, xs[j]); } }
return xs[x];
}
static mpc_val_t *mpcf_input_fst_free(mpc_input_t *i, int n, mpc_val_t **xs) { return mpcf_input_nth_free(i, n, xs, 0); }
static mpc_val_t *mpcf_input_snd_free(mpc_input_t *i, int n, mpc_val_t **xs) { return mpcf_input_nth_free(i, n, xs, 1); }
static mpc_val_t *mpcf_input_trd_free(mpc_input_t *i, int n, mpc_val_t **xs) { return mpcf_input_nth_free(i, n, xs, 2); }
static mpc_val_t *mpcf_input_strfold(mpc_input_t *i, int n, mpc_val_t **xs) {
int j;
size_t l = 0;
if (n == 0) { return mpc_calloc(i, 1, 1); }
for (j = 0; j < n; j++) { l += strlen(xs[j]); }
xs[0] = mpc_realloc(i, xs[0], l + 1);
for (j = 1; j < n; j++) { strcat(xs[0], xs[j]); mpc_free(i, xs[j]); }
return xs[0];
}
static mpc_val_t *mpcf_input_state_ast(mpc_input_t *i, int n, mpc_val_t **xs) {
mpc_state_t *s = ((mpc_state_t**)xs)[0];
mpc_ast_t *a = ((mpc_ast_t**)xs)[1];
a = mpc_ast_state(a, *s);
mpc_free(i, s);
(void) n;
return a;
}
static mpc_val_t *mpc_parse_fold(mpc_input_t *i, mpc_fold_t f, int n, mpc_val_t **xs) {