Rizin
unix-like reverse engineering framework and cli tools
mpc.c
Go to the documentation of this file.
1 #include "mpc.h"
2 
3 /*
4 ** State Type
5 */
6 
9  s.pos = -1;
10  s.row = -1;
11  s.col = -1;
12  s.term = 0;
13  return s;
14 }
15 
16 static mpc_state_t mpc_state_new(void) {
17  mpc_state_t s;
18  s.pos = 0;
19  s.row = 0;
20  s.col = 0;
21  s.term = 0;
22  return s;
23 }
24 
25 /*
26 ** Input Type
27 */
28 
29 /*
30 ** In mpc the input type has three modes of
31 ** operation: String, File and Pipe.
32 **
33 ** String is easy. The whole contents are
34 ** loaded into a buffer and scanned through.
35 ** The cursor can jump around at will making
36 ** backtracking easy.
37 **
38 ** The second is a File which is also somewhat
39 ** easy. The contents are never loaded into
40 ** memory but backtracking can still be achieved
41 ** by seeking in the file at different positions.
42 **
43 ** The final mode is Pipe. This is the difficult
44 ** one. As we assume pipes cannot be seeked - and
45 ** only support a single character lookahead at
46 ** any point, when the input is marked for a
47 ** potential backtracking we start buffering any
48 ** input.
49 **
50 ** This means that if we are requested to seek
51 ** back we can simply start reading from the
52 ** buffer instead of the input.
53 **
54 ** Of course using `mpc_predictive` will disable
55 ** backtracking and make LL(1) grammars easy
56 ** to parse for all input methods.
57 **
58 */
59 
60 enum {
63  MPC_INPUT_PIPE = 2
64 };
65 
66 enum {
68 };
69 
70 enum {
71  MPC_INPUT_MEM_NUM = 512
72 };
73 
74 typedef struct {
75  char mem[64];
76 } mpc_mem_t;
77 
78 typedef struct {
79 
80  int type;
81  char *filename;
83 
84  char *string;
85  char *buffer;
87 
88  int suppress;
89  int backtrack;
91  int marks_num;
93 
94  char *lasts;
95  char last;
96 
97  size_t mem_index;
98  char mem_full[MPC_INPUT_MEM_NUM];
100 
101 } mpc_input_t;
102 
103 static mpc_input_t *mpc_input_new_string(const char *filename, const char *string) {
104 
105  mpc_input_t *i = malloc(sizeof(mpc_input_t));
106 
107  i->filename = malloc(strlen(filename) + 1);
108  strcpy(i->filename, filename);
109  i->type = MPC_INPUT_STRING;
110 
111  i->state = mpc_state_new();
112 
113  i->string = malloc(strlen(string) + 1);
114  strcpy(i->string, string);
115  i->buffer = NULL;
116  i->file = NULL;
117 
118  i->suppress = 0;
119  i->backtrack = 1;
120  i->marks_num = 0;
121  i->marks_slots = MPC_INPUT_MARKS_MIN;
122  i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots);
123  i->lasts = malloc(sizeof(char) * i->marks_slots);
124  i->last = '\0';
125 
126  i->mem_index = 0;
127  memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM);
128 
129  return i;
130 }
131 
132 static mpc_input_t *mpc_input_new_nstring(const char *filename, const char *string, size_t length) {
133 
134  mpc_input_t *i = malloc(sizeof(mpc_input_t));
135 
136  i->filename = malloc(strlen(filename) + 1);
137  strcpy(i->filename, filename);
138  i->type = MPC_INPUT_STRING;
139 
140  i->state = mpc_state_new();
141 
142  i->string = malloc(length + 1);
143  strncpy(i->string, string, length);
144  i->string[length] = '\0';
145  i->buffer = NULL;
146  i->file = NULL;
147 
148  i->suppress = 0;
149  i->backtrack = 1;
150  i->marks_num = 0;
151  i->marks_slots = MPC_INPUT_MARKS_MIN;
152  i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots);
153  i->lasts = malloc(sizeof(char) * i->marks_slots);
154  i->last = '\0';
155 
156  i->mem_index = 0;
157  memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM);
158 
159  return i;
160 
161 }
162 
164 
165  mpc_input_t *i = malloc(sizeof(mpc_input_t));
166 
167  i->filename = malloc(strlen(filename) + 1);
168  strcpy(i->filename, filename);
169 
170  i->type = MPC_INPUT_PIPE;
171  i->state = mpc_state_new();
172 
173  i->string = NULL;
174  i->buffer = NULL;
175  i->file = pipe;
176 
177  i->suppress = 0;
178  i->backtrack = 1;
179  i->marks_num = 0;
180  i->marks_slots = MPC_INPUT_MARKS_MIN;
181  i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots);
182  i->lasts = malloc(sizeof(char) * i->marks_slots);
183  i->last = '\0';
184 
185  i->mem_index = 0;
186  memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM);
187 
188  return i;
189 
190 }
191 
193 
194  mpc_input_t *i = malloc(sizeof(mpc_input_t));
195 
196  i->filename = malloc(strlen(filename) + 1);
197  strcpy(i->filename, filename);
198  i->type = MPC_INPUT_FILE;
199  i->state = mpc_state_new();
200 
201  i->string = NULL;
202  i->buffer = NULL;
203  i->file = file;
204 
205  i->suppress = 0;
206  i->backtrack = 1;
207  i->marks_num = 0;
208  i->marks_slots = MPC_INPUT_MARKS_MIN;
209  i->marks = malloc(sizeof(mpc_state_t) * i->marks_slots);
210  i->lasts = malloc(sizeof(char) * i->marks_slots);
211  i->last = '\0';
212 
213  i->mem_index = 0;
214  memset(i->mem_full, 0, sizeof(char) * MPC_INPUT_MEM_NUM);
215 
216  return i;
217 }
218 
220 
221  free(i->filename);
222 
223  if (i->type == MPC_INPUT_STRING) { free(i->string); }
224  if (i->type == MPC_INPUT_PIPE) { free(i->buffer); }
225 
226  free(i->marks);
227  free(i->lasts);
228  free(i);
229 }
230 
231 static int mpc_mem_ptr(mpc_input_t *i, void *p) {
232  return
233  (char*)p >= (char*)(i->mem) &&
234  (char*)p < (char*)(i->mem) + (MPC_INPUT_MEM_NUM * sizeof(mpc_mem_t));
235 }
236 
237 static void *mpc_malloc(mpc_input_t *i, size_t n) {
238  size_t j;
239  char *p;
240 
241  if (n > sizeof(mpc_mem_t)) { return malloc(n); }
242 
243  j = i->mem_index;
244  do {
245  if (!i->mem_full[i->mem_index]) {
246  p = (void*)(i->mem + i->mem_index);
247  i->mem_full[i->mem_index] = 1;
248  i->mem_index = (i->mem_index+1) % MPC_INPUT_MEM_NUM;
249  return p;
250  }
251  i->mem_index = (i->mem_index+1) % MPC_INPUT_MEM_NUM;
252  } while (j != i->mem_index);
253 
254  return malloc(n);
255 }
256 
257 static void *mpc_calloc(mpc_input_t *i, size_t n, size_t m) {
258  char *x = mpc_malloc(i, n * m);
259  memset(x, 0, n * m);
260  return x;
261 }
262 
263 static void mpc_free(mpc_input_t *i, void *p) {
264  size_t j;
265  if (!mpc_mem_ptr(i, p)) { free(p); return; }
266  j = ((size_t)(((char*)p) - ((char*)i->mem))) / sizeof(mpc_mem_t);
267  i->mem_full[j] = 0;
268 }
269 
270 static void *mpc_realloc(mpc_input_t *i, void *p, size_t n) {
271 
272  char *q = NULL;
273 
274  if (!mpc_mem_ptr(i, p)) { return realloc(p, n); }
275 
276  if (n > sizeof(mpc_mem_t)) {
277  q = malloc(n);
278  memcpy(q, p, sizeof(mpc_mem_t));
279  mpc_free(i, p);
280  return q;
281  }
282 
283  return p;
284 }
285 
286 static void *mpc_export(mpc_input_t *i, void *p) {
287  char *q = NULL;
288  if (!mpc_mem_ptr(i, p)) { return p; }
289  q = malloc(sizeof(mpc_mem_t));
290  memcpy(q, p, sizeof(mpc_mem_t));
291  mpc_free(i, p);
292  return q;
293 }
294 
295 static void mpc_input_backtrack_disable(mpc_input_t *i) { i->backtrack--; }
296 static void mpc_input_backtrack_enable(mpc_input_t *i) { i->backtrack++; }
297 
298 static void mpc_input_suppress_disable(mpc_input_t *i) { i->suppress--; }
299 static void mpc_input_suppress_enable(mpc_input_t *i) { i->suppress++; }
300 
301 static void mpc_input_mark(mpc_input_t *i) {
302 
303  if (i->backtrack < 1) { return; }
304 
305  i->marks_num++;
306 
307  if (i->marks_num > i->marks_slots) {
308  i->marks_slots = i->marks_num + i->marks_num / 2;
309  i->marks = realloc(i->marks, sizeof(mpc_state_t) * i->marks_slots);
310  i->lasts = realloc(i->lasts, sizeof(char) * i->marks_slots);
311  }
312 
313  i->marks[i->marks_num-1] = i->state;
314  i->lasts[i->marks_num-1] = i->last;
315 
316  if (i->type == MPC_INPUT_PIPE && i->marks_num == 1) {
317  i->buffer = calloc(1, 1);
318  }
319 
320 }
321 
323  int j;
324 
325  if (i->backtrack < 1) { return; }
326 
327  i->marks_num--;
328 
329  if (i->marks_slots > i->marks_num + i->marks_num / 2
330  && i->marks_slots > MPC_INPUT_MARKS_MIN) {
331  i->marks_slots =
332  i->marks_num > MPC_INPUT_MARKS_MIN ?
333  i->marks_num : MPC_INPUT_MARKS_MIN;
334  i->marks = realloc(i->marks, sizeof(mpc_state_t) * i->marks_slots);
335  i->lasts = realloc(i->lasts, sizeof(char) * i->marks_slots);
336  }
337 
338  if (i->type == MPC_INPUT_PIPE && i->marks_num == 0) {
339  for (j = strlen(i->buffer) - 1; j >= 0; j--)
340  ungetc(i->buffer[j], i->file);
341 
342  free(i->buffer);
343  i->buffer = NULL;
344  }
345 
346 }
347 
349 
350  if (i->backtrack < 1) { return; }
351 
352  i->state = i->marks[i->marks_num-1];
353  i->last = i->lasts[i->marks_num-1];
354 
355  if (i->type == MPC_INPUT_FILE) {
356  fseek(i->file, i->state.pos, SEEK_SET);
357  }
358 
360 }
361 
363  return i->state.pos < (long)(strlen(i->buffer) + i->marks[0].pos);
364 }
365 
367  return i->buffer[i->state.pos - i->marks[0].pos];
368 }
369 
370 static char mpc_input_getc(mpc_input_t *i) {
371 
372  char c = '\0';
373 
374  switch (i->type) {
375 
376  case MPC_INPUT_STRING: return i->string[i->state.pos];
377  case MPC_INPUT_FILE: c = fgetc(i->file); return c;
378  case MPC_INPUT_PIPE:
379 
380  if (!i->buffer) { c = getc(i->file); return c; }
381 
382  if (i->buffer && mpc_input_buffer_in_range(i)) {
384  return c;
385  } else {
386  c = getc(i->file);
387  return c;
388  }
389 
390  default: return c;
391  }
392 }
393 
395 
396  char c = '\0';
397 
398  switch (i->type) {
399  case MPC_INPUT_STRING: return i->string[i->state.pos];
400  case MPC_INPUT_FILE:
401 
402  c = fgetc(i->file);
403  if (feof(i->file)) { return '\0'; }
404 
405  fseek(i->file, -1, SEEK_CUR);
406  return c;
407 
408  case MPC_INPUT_PIPE:
409 
410  if (!i->buffer) {
411  c = getc(i->file);
412  if (feof(i->file)) { return '\0'; }
413  ungetc(c, i->file);
414  return c;
415  }
416 
417  if (i->buffer && mpc_input_buffer_in_range(i)) {
418  return mpc_input_buffer_get(i);
419  } else {
420  c = getc(i->file);
421  if (feof(i->file)) { return '\0'; }
422  ungetc(c, i->file);
423  return c;
424  }
425 
426  default: return c;
427  }
428 
429 }
430 
432  return mpc_input_peekc(i) == '\0';
433 }
434 
435 static int mpc_input_failure(mpc_input_t *i, char c) {
436 
437  switch (i->type) {
438  case MPC_INPUT_STRING: { break; }
439  case MPC_INPUT_FILE: fseek(i->file, -1, SEEK_CUR); { break; }
440  case MPC_INPUT_PIPE: {
441 
442  if (!i->buffer) { ungetc(c, i->file); break; }
443 
444  if (i->buffer && mpc_input_buffer_in_range(i)) {
445  break;
446  } else {
447  ungetc(c, i->file);
448  }
449  }
450  default: { break; }
451  }
452  return 0;
453 }
454 
455 static int mpc_input_success(mpc_input_t *i, char c, char **o) {
456 
457  if (i->type == MPC_INPUT_PIPE
458  && i->buffer && !mpc_input_buffer_in_range(i)) {
459  i->buffer = realloc(i->buffer, strlen(i->buffer) + 2);
460  i->buffer[strlen(i->buffer) + 1] = '\0';
461  i->buffer[strlen(i->buffer) + 0] = c;
462  }
463 
464  i->last = c;
465  i->state.pos++;
466  i->state.col++;
467 
468  if (c == '\n') {
469  i->state.col = 0;
470  i->state.row++;
471  }
472 
473  if (o) {
474  (*o) = mpc_malloc(i, 2);
475  (*o)[0] = c;
476  (*o)[1] = '\0';
477  }
478 
479  return 1;
480 }
481 
482 static int mpc_input_any(mpc_input_t *i, char **o) {
483  char x;
484  if (mpc_input_terminated(i)) { return 0; }
485  x = mpc_input_getc(i);
486  return mpc_input_success(i, x, o);
487 }
488 
489 static int mpc_input_char(mpc_input_t *i, char c, char **o) {
490  char x;
491  if (mpc_input_terminated(i)) { return 0; }
492  x = mpc_input_getc(i);
493  return x == c ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
494 }
495 
496 static int mpc_input_range(mpc_input_t *i, char c, char d, char **o) {
497  char x;
498  if (mpc_input_terminated(i)) { return 0; }
499  x = mpc_input_getc(i);
500  return x >= c && x <= d ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
501 }
502 
503 static int mpc_input_oneof(mpc_input_t *i, const char *c, char **o) {
504  char x;
505  if (mpc_input_terminated(i)) { return 0; }
506  x = mpc_input_getc(i);
507  return strchr(c, x) != 0 ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
508 }
509 
510 static int mpc_input_noneof(mpc_input_t *i, const char *c, char **o) {
511  char x;
512  if (mpc_input_terminated(i)) { return 0; }
513  x = mpc_input_getc(i);
514  return strchr(c, x) == 0 ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
515 }
516 
517 static int mpc_input_satisfy(mpc_input_t *i, int(*cond)(char), char **o) {
518  char x;
519  if (mpc_input_terminated(i)) { return 0; }
520  x = mpc_input_getc(i);
521  return cond(x) ? mpc_input_success(i, x, o) : mpc_input_failure(i, x);
522 }
523 
524 static int mpc_input_string(mpc_input_t *i, const char *c, char **o) {
525 
526  const char *x = c;
527 
528  mpc_input_mark(i);
529  while (*x) {
530  if (!mpc_input_char(i, *x, NULL)) {
532  return 0;
533  }
534  x++;
535  }
537 
538  *o = mpc_malloc(i, strlen(c) + 1);
539  strcpy(*o, c);
540  return 1;
541 }
542 
543 static int mpc_input_anchor(mpc_input_t* i, int(*f)(char,char), char **o) {
544  *o = NULL;
545  return f(i->last, mpc_input_peekc(i));
546 }
547 
548 static int mpc_input_soi(mpc_input_t* i, char **o) {
549  *o = NULL;
550  return i->last == '\0';
551 }
552 
553 static int mpc_input_eoi(mpc_input_t* i, char **o) {
554  *o = NULL;
555  if (i->state.term) {
556  return 0;
557  } else if (mpc_input_terminated(i)) {
558  i->state.term = 1;
559  return 1;
560  } else {
561  return 0;
562  }
563 }
564 
566  mpc_state_t *r = mpc_malloc(i, sizeof(mpc_state_t));
567  memcpy(r, &i->state, sizeof(mpc_state_t));
568  return r;
569 }
570 
571 /*
572 ** Error Type
573 */
574 
576  int i;
577  for (i = 0; i < x->expected_num; i++) { free(x->expected[i]); }
578  free(x->expected);
579  free(x->filename);
580  free(x->failure);
581  free(x);
582 }
583 
585  mpc_err_print_to(x, stdout);
586 }
587 
589  char *str = mpc_err_string(x);
590  fprintf(f, "%s", str);
591  free(str);
592 }
593 
594 static void mpc_err_string_cat(char *buffer, int *pos, int *max, char const *fmt, ...) {
595  /* TODO: Error Checking on Length */
596  int left = ((*max) - (*pos));
597  va_list va;
598  va_start(va, fmt);
599  if (left < 0) { left = 0;}
600  (*pos) += vsprintf(buffer + (*pos), fmt, va);
601  va_end(va);
602 }
603 
604 static char char_unescape_buffer[4];
605 
606 static const char *mpc_err_char_unescape(char c) {
607 
608  char_unescape_buffer[0] = '\'';
609  char_unescape_buffer[1] = ' ';
610  char_unescape_buffer[2] = '\'';
611  char_unescape_buffer[3] = '\0';
612 
613  switch (c) {
614  case '\a': return "bell";
615  case '\b': return "backspace";
616  case '\f': return "formfeed";
617  case '\r': return "carriage return";
618  case '\v': return "vertical tab";
619  case '\0': return "end of input";
620  case '\n': return "newline";
621  case '\t': return "tab";
622  case ' ' : return "space";
623  default:
624  char_unescape_buffer[1] = c;
625  return char_unescape_buffer;
626  }
627 
628 }
629 
631 
632  int i;
633  int pos = 0;
634  int max = 1023;
635  char *buffer = calloc(1, 1024);
636 
637  if (x->failure) {
639  "%s: error: %s\n", x->filename, x->failure);
640  return buffer;
641  }
642 
644  "%s:%li:%li: error: expected ", x->filename, x->state.row+1, x->state.col+1);
645 
646  if (x->expected_num == 0) { mpc_err_string_cat(buffer, &pos, &max, "ERROR: NOTHING EXPECTED"); }
647  if (x->expected_num == 1) { mpc_err_string_cat(buffer, &pos, &max, "%s", x->expected[0]); }
648  if (x->expected_num >= 2) {
649 
650  for (i = 0; i < x->expected_num-2; i++) {
651  mpc_err_string_cat(buffer, &pos, &max, "%s, ", x->expected[i]);
652  }
653 
654  mpc_err_string_cat(buffer, &pos, &max, "%s or %s",
655  x->expected[x->expected_num-2],
656  x->expected[x->expected_num-1]);
657  }
658 
659  mpc_err_string_cat(buffer, &pos, &max, " at ");
661  mpc_err_string_cat(buffer, &pos, &max, "\n");
662 
663  return realloc(buffer, strlen(buffer) + 1);
664 }
665 
666 static mpc_err_t *mpc_err_new(mpc_input_t *i, const char *expected) {
667  mpc_err_t *x;
668  if (i->suppress) { return NULL; }
669  x = mpc_malloc(i, sizeof(mpc_err_t));
670  x->filename = mpc_malloc(i, strlen(i->filename) + 1);
671  strcpy(x->filename, i->filename);
672  x->state = i->state;
673  x->expected_num = 1;
674  x->expected = mpc_malloc(i, sizeof(char*));
675  x->expected[0] = mpc_malloc(i, strlen(expected) + 1);
676  strcpy(x->expected[0], expected);
677  x->failure = NULL;
678  x->received = mpc_input_peekc(i);
679  return x;
680 }
681 
682 static mpc_err_t *mpc_err_fail(mpc_input_t *i, const char *failure) {
683  mpc_err_t *x;
684  if (i->suppress) { return NULL; }
685  x = mpc_malloc(i, sizeof(mpc_err_t));
686  x->filename = mpc_malloc(i, strlen(i->filename) + 1);
687  strcpy(x->filename, i->filename);
688  x->state = i->state;
689  x->expected_num = 0;
690  x->expected = NULL;
691  x->failure = mpc_malloc(i, strlen(failure) + 1);
692  strcpy(x->failure, failure);
693  x->received = ' ';
694  return x;
695 }
696 
697 static mpc_err_t *mpc_err_file(const char *filename, const char *failure) {
698  mpc_err_t *x;
699  x = malloc(sizeof(mpc_err_t));
700  x->filename = malloc(strlen(filename) + 1);
701  strcpy(x->filename, filename);
702  x->state = mpc_state_new();
703  x->expected_num = 0;
704  x->expected = NULL;
705  x->failure = malloc(strlen(failure) + 1);
706  strcpy(x->failure, failure);
707  x->received = ' ';
708  return x;
709 }
710 
712  int j;
713  if (x == NULL) { return; }
714  for (j = 0; j < x->expected_num; j++) { mpc_free(i, x->expected[j]); }
715  mpc_free(i, x->expected);
716  mpc_free(i, x->filename);
717  mpc_free(i, x->failure);
718  mpc_free(i, x);
719 }
720 
722  int j;
723  for (j = 0; j < x->expected_num; j++) {
724  x->expected[j] = mpc_export(i, x->expected[j]);
725  }
726  x->expected = mpc_export(i, x->expected);
727  x->filename = mpc_export(i, x->filename);
728  x->failure = mpc_export(i, x->failure);
729  return mpc_export(i, x);
730 }
731 
732 static int mpc_err_contains_expected(mpc_input_t *i, mpc_err_t *x, char *expected) {
733  int j;
734  (void)i;
735  for (j = 0; j < x->expected_num; j++) {
736  if (strcmp(x->expected[j], expected) == 0) { return 1; }
737  }
738  return 0;
739 }
740 
741 static void mpc_err_add_expected(mpc_input_t *i, mpc_err_t *x, char *expected) {
742  (void)i;
743  x->expected_num++;
744  x->expected = mpc_realloc(i, x->expected, sizeof(char*) * x->expected_num);
745  x->expected[x->expected_num-1] = mpc_malloc(i, strlen(expected) + 1);
746  strcpy(x->expected[x->expected_num-1], expected);
747 }
748 
750 
751  int j, k, fst;
752  mpc_err_t *e;
753 
754  fst = -1;
755  for (j = 0; j < n; j++) {
756  if (x[j] != NULL) { fst = j; }
757  }
758 
759  if (fst == -1) { return NULL; }
760 
761  e = mpc_malloc(i, sizeof(mpc_err_t));
762  e->state = mpc_state_invalid();
763  e->expected_num = 0;
764  e->expected = NULL;
765  e->failure = NULL;
766  e->filename = mpc_malloc(i, strlen(x[fst]->filename)+1);
767  strcpy(e->filename, x[fst]->filename);
768 
769  for (j = 0; j < n; j++) {
770  if (x[j] == NULL) { continue; }
771  if (x[j]->state.pos > e->state.pos) { e->state = x[j]->state; }
772  }
773 
774  for (j = 0; j < n; j++) {
775  if (x[j] == NULL) { continue; }
776  if (x[j]->state.pos < e->state.pos) { continue; }
777 
778  if (x[j]->failure) {
779  e->failure = mpc_malloc(i, strlen(x[j]->failure)+1);
780  strcpy(e->failure, x[j]->failure);
781  break;
782  }
783 
784  e->received = x[j]->received;
785 
786  for (k = 0; k < x[j]->expected_num; k++) {
787  if (!mpc_err_contains_expected(i, e, x[j]->expected[k])) {
788  mpc_err_add_expected(i, e, x[j]->expected[k]);
789  }
790  }
791  }
792 
793  for (j = 0; j < n; j++) {
794  if (x[j] == NULL) { continue; }
796  }
797 
798  return e;
799 }
800 
802 
803  int j = 0;
804  size_t l = 0;
805  char *expect = NULL;
806 
807  if (x == NULL) { return NULL; }
808 
809  if (x->expected_num == 0) {
810  expect = mpc_calloc(i, 1, 1);
811  x->expected_num = 1;
812  x->expected = mpc_realloc(i, x->expected, sizeof(char*) * x->expected_num);
813  x->expected[0] = expect;
814  return x;
815  }
816 
817  else if (x->expected_num == 1) {
818  expect = mpc_malloc(i, strlen(prefix) + strlen(x->expected[0]) + 1);
819  strcpy(expect, prefix);
820  strcat(expect, x->expected[0]);
821  mpc_free(i, x->expected[0]);
822  x->expected[0] = expect;
823  return x;
824  }
825 
826  else if (x->expected_num > 1) {
827 
828  l += strlen(prefix);
829  for (j = 0; j < x->expected_num-2; j++) {
830  l += strlen(x->expected[j]) + strlen(", ");
831  }
832  l += strlen(x->expected[x->expected_num-2]);
833  l += strlen(" or ");
834  l += strlen(x->expected[x->expected_num-1]);
835 
836  expect = mpc_malloc(i, l + 1);
837 
838  strcpy(expect, prefix);
839  for (j = 0; j < x->expected_num-2; j++) {
840  strcat(expect, x->expected[j]); strcat(expect, ", ");
841  }
842  strcat(expect, x->expected[x->expected_num-2]);
843  strcat(expect, " or ");
844  strcat(expect, x->expected[x->expected_num-1]);
845 
846  for (j = 0; j < x->expected_num; j++) { mpc_free(i, x->expected[j]); }
847 
848  x->expected_num = 1;
849  x->expected = mpc_realloc(i, x->expected, sizeof(char*) * x->expected_num);
850  x->expected[0] = expect;
851  return x;
852  }
853 
854  return NULL;
855 }
856 
858  return mpc_err_repeat(i, x, "one or more of ");
859 }
860 
862  mpc_err_t *y;
863  int digits = n/10 + 1;
864  char *prefix;
865  prefix = mpc_malloc(i, digits + strlen(" of ") + 1);
866  sprintf(prefix, "%i of ", n);
867  y = mpc_err_repeat(i, x, prefix);
868  mpc_free(i, prefix);
869  return y;
870 }
871 
873  mpc_err_t *errs[2];
874  errs[0] = x;
875  errs[1] = y;
876  return mpc_err_or(i, errs, 2);
877 }
878 
879 /*
880 ** Parser Type
881 */
882 
883 enum {
892 
900 
909 
912 
915 
917  MPC_TYPE_EOI = 28
918 };
919 
920 typedef struct { char *m; } mpc_pdata_fail_t;
921 typedef struct { mpc_ctor_t lf; void *x; } mpc_pdata_lift_t;
922 typedef struct { mpc_parser_t *x; char *m; } mpc_pdata_expect_t;
923 typedef struct { int(*f)(char,char); } mpc_pdata_anchor_t;
924 typedef struct { char x; } mpc_pdata_single_t;
925 typedef struct { char x; char y; } mpc_pdata_range_t;
926 typedef struct { int(*f)(char); } mpc_pdata_satisfy_t;
927 typedef struct { char *x; } mpc_pdata_string_t;
929 typedef struct { mpc_parser_t *x; mpc_apply_to_t f; void *d; } mpc_pdata_apply_to_t;
930 typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_check_t f; char *e; } mpc_pdata_check_t;
931 typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_check_with_t f; void *d; char *e; } mpc_pdata_check_with_t;
932 typedef struct { mpc_parser_t *x; } mpc_pdata_predict_t;
935 typedef struct { int n; mpc_parser_t **xs; } mpc_pdata_or_t;
936 typedef struct { int n; mpc_fold_t f; mpc_parser_t **xs; mpc_dtor_t *dxs; } mpc_pdata_and_t;
937 
938 typedef union {
956 } mpc_pdata_t;
957 
958 struct mpc_parser_t {
959  char *name;
961  char type;
962  char retained;
963 };
964 
966  int j;
967  for (j = 0; j < n; j++) { if (j != x) { mpc_free(i, xs[j]); } }
968  return xs[x];
969 }
970 
971 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); }
972 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); }
973 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); }
974 
976  int j;
977  size_t l = 0;
978  if (n == 0) { return mpc_calloc(i, 1, 1); }
979  for (j = 0; j < n; j++) { l += strlen(xs[j]); }
980  xs[0] = mpc_realloc(i, xs[0], l + 1);
981  for (j = 1; j < n; j++) { strcat(xs[0], xs[j]); mpc_free(i, xs[j]); }
982  return xs[0];
983 }
984 
986  mpc_state_t *s = ((mpc_state_t**)xs)[0];
987  mpc_ast_t *a = ((mpc_ast_t**)xs)[1];
988  a = mpc_ast_state(a, *s);
989  mpc_free(i, s);
990  (void) n;
991  return a;
992 }
993 
995  int j;
996  if (f == mpcf_null) { return mpcf_null(n, xs); }
997  if (f == mpcf_fst) { return mpcf_fst(n, xs); }
998  if (f == mpcf_snd) { return mpcf_snd(n, xs); }
999  if (f == mpcf_trd) { return mpcf_trd(n, xs); }
1000  if (f == mpcf_fst_free) { return mpcf_input_fst_free(i, n, xs); }
1001  if (f == mpcf_snd_free) { return mpcf_input_snd_free(i, n, xs); }
1002  if (f == mpcf_trd_free) { return mpcf_input_trd_free(i, n, xs); }
1003  if (f == mpcf_strfold) { return mpcf_input_strfold(i, n, xs); }
1004  if (f == mpcf_state_ast) { return mpcf_input_state_ast(i, n, xs); }
1005  for (j = 0; j < n; j++) { xs[j] = mpc_export(i, xs[j]); }
1006  return f(j, xs);
1007 }
1008 
1010  mpc_free(i, x);
1011  return NULL;
1012 }
1013 
1015  mpc_ast_t *a = mpc_ast_new("", c);
1016  mpc_free(i, c);
1017  return a;
1018 }
1019 
1021  if (f == mpcf_free) { return mpcf_input_free(i, x); }
1022  if (f == mpcf_str_ast) { return mpcf_input_str_ast(i, x); }
1023  return f(mpc_export(i, x));
1024 }
1025 
1027  return f(mpc_export(i, x), d);
1028 }
1029 
1031  if (d == free) { mpc_free(i, x); return; }
1032  d(mpc_export(i, x));
1033 }
1034 
1035 enum {
1037 };
1038 
1039 #define MPC_SUCCESS(x) r->output = x; return 1
1040 #define MPC_FAILURE(x) r->error = x; return 0
1041 #define MPC_PRIMITIVE(x) \
1042  if (x) { MPC_SUCCESS(r->output); } \
1043  else { MPC_FAILURE(NULL); }
1044 
1045 #define MPC_MAX_RECURSION_DEPTH 1000
1046 
1048 
1049  int j = 0, k = 0;
1050  mpc_result_t results_stk[MPC_PARSE_STACK_MIN];
1051  mpc_result_t *results;
1052  int results_slots = MPC_PARSE_STACK_MIN;
1053 
1054  if (depth == MPC_MAX_RECURSION_DEPTH)
1055  {
1056  MPC_FAILURE(mpc_err_fail(i, "Maximum recursion depth exceeded!"));
1057  }
1058 
1059  switch (p->type) {
1060 
1061  /* Basic Parsers */
1062 
1063  case MPC_TYPE_ANY: MPC_PRIMITIVE(mpc_input_any(i, (char**)&r->output));
1064  case MPC_TYPE_SINGLE: MPC_PRIMITIVE(mpc_input_char(i, p->data.single.x, (char**)&r->output));
1065  case MPC_TYPE_RANGE: MPC_PRIMITIVE(mpc_input_range(i, p->data.range.x, p->data.range.y, (char**)&r->output));
1066  case MPC_TYPE_ONEOF: MPC_PRIMITIVE(mpc_input_oneof(i, p->data.string.x, (char**)&r->output));
1067  case MPC_TYPE_NONEOF: MPC_PRIMITIVE(mpc_input_noneof(i, p->data.string.x, (char**)&r->output));
1068  case MPC_TYPE_SATISFY: MPC_PRIMITIVE(mpc_input_satisfy(i, p->data.satisfy.f, (char**)&r->output));
1069  case MPC_TYPE_STRING: MPC_PRIMITIVE(mpc_input_string(i, p->data.string.x, (char**)&r->output));
1070  case MPC_TYPE_ANCHOR: MPC_PRIMITIVE(mpc_input_anchor(i, p->data.anchor.f, (char**)&r->output));
1071  case MPC_TYPE_SOI: MPC_PRIMITIVE(mpc_input_soi(i, (char**)&r->output));
1072  case MPC_TYPE_EOI: MPC_PRIMITIVE(mpc_input_eoi(i, (char**)&r->output));
1073 
1074  /* Other parsers */
1075 
1076  case MPC_TYPE_UNDEFINED: MPC_FAILURE(mpc_err_fail(i, "Parser Undefined!"));
1078  case MPC_TYPE_FAIL: MPC_FAILURE(mpc_err_fail(i, p->data.fail.m));
1079  case MPC_TYPE_LIFT: MPC_SUCCESS(p->data.lift.lf());
1080  case MPC_TYPE_LIFT_VAL: MPC_SUCCESS(p->data.lift.x);
1082 
1083  /* Application Parsers */
1084 
1085  case MPC_TYPE_APPLY:
1086  if (mpc_parse_run(i, p->data.apply.x, r, e, depth+1)) {
1087  MPC_SUCCESS(mpc_parse_apply(i, p->data.apply.f, r->output));
1088  } else {
1089  MPC_FAILURE(r->output);
1090  }
1091 
1092  case MPC_TYPE_APPLY_TO:
1093  if (mpc_parse_run(i, p->data.apply_to.x, r, e, depth+1)) {
1094  MPC_SUCCESS(mpc_parse_apply_to(i, p->data.apply_to.f, r->output, p->data.apply_to.d));
1095  } else {
1096  MPC_FAILURE(r->error);
1097  }
1098 
1099  case MPC_TYPE_CHECK:
1100  if (mpc_parse_run(i, p->data.check.x, r, e, depth+1)) {
1101  if (p->data.check.f(&r->output)) {
1102  MPC_SUCCESS(r->output);
1103  } else {
1104  mpc_parse_dtor(i, p->data.check.dx, r->output);
1105  MPC_FAILURE(mpc_err_fail(i, p->data.check.e));
1106  }
1107  } else {
1108  MPC_FAILURE(r->error);
1109  }
1110 
1111  case MPC_TYPE_CHECK_WITH:
1112  if (mpc_parse_run(i, p->data.check_with.x, r, e, depth+1)) {
1113  if (p->data.check_with.f(&r->output, p->data.check_with.d)) {
1114  MPC_SUCCESS(r->output);
1115  } else {
1116  mpc_parse_dtor(i, p->data.check.dx, r->output);
1117  MPC_FAILURE(mpc_err_fail(i, p->data.check_with.e));
1118  }
1119  } else {
1120  MPC_FAILURE(r->error);
1121  }
1122 
1123  case MPC_TYPE_EXPECT:
1125  if (mpc_parse_run(i, p->data.expect.x, r, e, depth+1)) {
1127  MPC_SUCCESS(r->output);
1128  } else {
1130  MPC_FAILURE(mpc_err_new(i, p->data.expect.m));
1131  }
1132 
1133  case MPC_TYPE_PREDICT:
1135  if (mpc_parse_run(i, p->data.predict.x, r, e, depth+1)) {
1137  MPC_SUCCESS(r->output);
1138  } else {
1140  MPC_FAILURE(r->error);
1141  }
1142 
1143  /* Optional Parsers */
1144 
1145  /* TODO: Update Not Error Message */
1146 
1147  case MPC_TYPE_NOT:
1148  mpc_input_mark(i);
1150  if (mpc_parse_run(i, p->data.not.x, r, e, depth+1)) {
1153  mpc_parse_dtor(i, p->data.not.dx, r->output);
1154  MPC_FAILURE(mpc_err_new(i, "opposite"));
1155  } else {
1158  MPC_SUCCESS(p->data.not.lf());
1159  }
1160 
1161  case MPC_TYPE_MAYBE:
1162  if (mpc_parse_run(i, p->data.not.x, r, e, depth+1)) {
1163  MPC_SUCCESS(r->output);
1164  } else {
1165  *e = mpc_err_merge(i, *e, r->error);
1166  MPC_SUCCESS(p->data.not.lf());
1167  }
1168 
1169  /* Repeat Parsers */
1170 
1171  case MPC_TYPE_MANY:
1172 
1173  results = results_stk;
1174 
1175  while (mpc_parse_run(i, p->data.repeat.x, &results[j], e, depth+1)) {
1176  j++;
1177  if (j == MPC_PARSE_STACK_MIN) {
1178  results_slots = j + j / 2;
1179  results = mpc_malloc(i, sizeof(mpc_result_t) * results_slots);
1180  memcpy(results, results_stk, sizeof(mpc_result_t) * MPC_PARSE_STACK_MIN);
1181  } else if (j >= results_slots) {
1182  results_slots = j + j / 2;
1183  results = mpc_realloc(i, results, sizeof(mpc_result_t) * results_slots);
1184  }
1185  }
1186 
1187  *e = mpc_err_merge(i, *e, results[j].error);
1188 
1189  MPC_SUCCESS(
1190  mpc_parse_fold(i, p->data.repeat.f, j, (mpc_val_t**)results);
1191  if (j >= MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1192 
1193  case MPC_TYPE_MANY1:
1194 
1195  results = results_stk;
1196 
1197  while (mpc_parse_run(i, p->data.repeat.x, &results[j], e, depth+1)) {
1198  j++;
1199  if (j == MPC_PARSE_STACK_MIN) {
1200  results_slots = j + j / 2;
1201  results = mpc_malloc(i, sizeof(mpc_result_t) * results_slots);
1202  memcpy(results, results_stk, sizeof(mpc_result_t) * MPC_PARSE_STACK_MIN);
1203  } else if (j >= results_slots) {
1204  results_slots = j + j / 2;
1205  results = mpc_realloc(i, results, sizeof(mpc_result_t) * results_slots);
1206  }
1207  }
1208 
1209  if (j == 0) {
1210  MPC_FAILURE(
1211  mpc_err_many1(i, results[j].error);
1212  if (j >= MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1213  } else {
1214 
1215  *e = mpc_err_merge(i, *e, results[j].error);
1216 
1217  MPC_SUCCESS(
1218  mpc_parse_fold(i, p->data.repeat.f, j, (mpc_val_t**)results);
1219  if (j >= MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1220  }
1221 
1222  case MPC_TYPE_COUNT:
1223 
1224  results = p->data.repeat.n > MPC_PARSE_STACK_MIN
1225  ? mpc_malloc(i, sizeof(mpc_result_t) * p->data.repeat.n)
1226  : results_stk;
1227 
1228  while (mpc_parse_run(i, p->data.repeat.x, &results[j], e, depth+1)) {
1229  j++;
1230  if (j == p->data.repeat.n) { break; }
1231  }
1232 
1233  if (j == p->data.repeat.n) {
1234  MPC_SUCCESS(
1235  mpc_parse_fold(i, p->data.repeat.f, j, (mpc_val_t**)results);
1236  if (p->data.repeat.n > MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1237  } else {
1238  for (k = 0; k < j; k++) {
1239  mpc_parse_dtor(i, p->data.repeat.dx, results[k].output);
1240  }
1241  MPC_FAILURE(
1242  mpc_err_count(i, results[j].error, p->data.repeat.n);
1243  if (p->data.repeat.n > MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1244  }
1245 
1246  /* Combinatory Parsers */
1247 
1248  case MPC_TYPE_OR:
1249 
1250  if (p->data.or.n == 0) { MPC_SUCCESS(NULL); }
1251 
1252  results = p->data.or.n > MPC_PARSE_STACK_MIN
1253  ? mpc_malloc(i, sizeof(mpc_result_t) * p->data.or.n)
1254  : results_stk;
1255 
1256  for (j = 0; j < p->data.or.n; j++) {
1257  if (mpc_parse_run(i, p->data.or.xs[j], &results[j], e, depth+1)) {
1258  MPC_SUCCESS(results[j].output;
1259  if (p->data.or.n > MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1260  } else {
1261  *e = mpc_err_merge(i, *e, results[j].error);
1262  }
1263  }
1264 
1265  MPC_FAILURE(NULL;
1266  if (p->data.or.n > MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1267 
1268  case MPC_TYPE_AND:
1269 
1270  if (p->data.and.n == 0) { MPC_SUCCESS(NULL); }
1271 
1272  results = p->data.or.n > MPC_PARSE_STACK_MIN
1273  ? mpc_malloc(i, sizeof(mpc_result_t) * p->data.or.n)
1274  : results_stk;
1275 
1276  mpc_input_mark(i);
1277  for (j = 0; j < p->data.and.n; j++) {
1278  if (!mpc_parse_run(i, p->data.and.xs[j], &results[j], e, depth+1)) {
1280  for (k = 0; k < j; k++) {
1281  mpc_parse_dtor(i, p->data.and.dxs[k], results[k].output);
1282  }
1283  MPC_FAILURE(results[j].error;
1284  if (p->data.or.n > MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1285  }
1286  }
1288  MPC_SUCCESS(
1289  mpc_parse_fold(i, p->data.and.f, j, (mpc_val_t**)results);
1290  if (p->data.or.n > MPC_PARSE_STACK_MIN) { mpc_free(i, results); });
1291 
1292  /* End */
1293 
1294  default:
1295 
1296  MPC_FAILURE(mpc_err_fail(i, "Unknown Parser Type Id!"));
1297  }
1298 
1299  return 0;
1300 
1301 }
1302 
1303 #undef MPC_SUCCESS
1304 #undef MPC_FAILURE
1305 #undef MPC_PRIMITIVE
1306 
1308  int x;
1309  mpc_err_t *e = mpc_err_fail(i, "Unknown Error");
1310  e->state = mpc_state_invalid();
1311  x = mpc_parse_run(i, p, r, &e, 0);
1312  if (x) {
1314  r->output = mpc_export(i, r->output);
1315  } else {
1316  r->error = mpc_err_export(i, mpc_err_merge(i, e, r->error));
1317  }
1318  return x;
1319 }
1320 
1321 int mpc_parse(const char *filename, const char *string, mpc_parser_t *p, mpc_result_t *r) {
1322  int x;
1324  x = mpc_parse_input(i, p, r);
1326  return x;
1327 }
1328 
1329 int mpc_nparse(const char *filename, const char *string, size_t length, mpc_parser_t *p, mpc_result_t *r) {
1330  int x;
1332  x = mpc_parse_input(i, p, r);
1334  return x;
1335 }
1336 
1338  int x;
1340  x = mpc_parse_input(i, p, r);
1342  return x;
1343 }
1344 
1346  int x;
1348  x = mpc_parse_input(i, p, r);
1350  return x;
1351 }
1352 
1354 
1355  FILE *f = fopen(filename, "rb");
1356  int res;
1357 
1358  if (f == NULL) {
1359  r->output = NULL;
1360  r->error = mpc_err_file(filename, "Unable to open file!");
1361  return 0;
1362  }
1363 
1364  res = mpc_parse_file(filename, f, p, r);
1365  fclose(f);
1366  return res;
1367 }
1368 
1369 /*
1370 ** Building a Parser
1371 */
1372 
1373 static void mpc_undefine_unretained(mpc_parser_t *p, int force);
1374 
1376 
1377  int i;
1378  for (i = 0; i < p->data.or.n; i++) {
1379  mpc_undefine_unretained(p->data.or.xs[i], 0);
1380  }
1381  free(p->data.or.xs);
1382 
1383 }
1384 
1386 
1387  int i;
1388  for (i = 0; i < p->data.and.n; i++) {
1389  mpc_undefine_unretained(p->data.and.xs[i], 0);
1390  }
1391  free(p->data.and.xs);
1392  free(p->data.and.dxs);
1393 
1394 }
1395 
1396 static void mpc_undefine_unretained(mpc_parser_t *p, int force) {
1397 
1398  if (p->retained && !force) { return; }
1399 
1400  switch (p->type) {
1401 
1402  case MPC_TYPE_FAIL: free(p->data.fail.m); break;
1403 
1404  case MPC_TYPE_ONEOF:
1405  case MPC_TYPE_NONEOF:
1406  case MPC_TYPE_STRING:
1407  free(p->data.string.x);
1408  break;
1409 
1410  case MPC_TYPE_APPLY: mpc_undefine_unretained(p->data.apply.x, 0); break;
1411  case MPC_TYPE_APPLY_TO: mpc_undefine_unretained(p->data.apply_to.x, 0); break;
1412  case MPC_TYPE_PREDICT: mpc_undefine_unretained(p->data.predict.x, 0); break;
1413 
1414  case MPC_TYPE_MAYBE:
1415  case MPC_TYPE_NOT:
1416  mpc_undefine_unretained(p->data.not.x, 0);
1417  break;
1418 
1419  case MPC_TYPE_EXPECT:
1420  mpc_undefine_unretained(p->data.expect.x, 0);
1421  free(p->data.expect.m);
1422  break;
1423 
1424  case MPC_TYPE_MANY:
1425  case MPC_TYPE_MANY1:
1426  case MPC_TYPE_COUNT:
1427  mpc_undefine_unretained(p->data.repeat.x, 0);
1428  break;
1429 
1430  case MPC_TYPE_OR: mpc_undefine_or(p); break;
1431  case MPC_TYPE_AND: mpc_undefine_and(p); break;
1432 
1433  case MPC_TYPE_CHECK:
1434  mpc_undefine_unretained(p->data.check.x, 0);
1435  free(p->data.check.e);
1436  break;
1437 
1438  case MPC_TYPE_CHECK_WITH:
1439  mpc_undefine_unretained(p->data.check_with.x, 0);
1440  free(p->data.check_with.e);
1441  break;
1442 
1443  default: break;
1444  }
1445 
1446  if (!force) {
1447  free(p->name);
1448  free(p);
1449  }
1450 
1451 }
1452 
1454  if (p->retained) {
1455 
1456  if (p->type != MPC_TYPE_UNDEFINED) {
1458  }
1459 
1460  free(p->name);
1461  free(p);
1462 
1463  } else {
1465  }
1466 }
1467 
1468 static void mpc_soft_delete(mpc_val_t *x) {
1470 }
1471 
1473  mpc_parser_t *p = calloc(1, sizeof(mpc_parser_t));
1474  p->retained = 0;
1475  p->type = MPC_TYPE_UNDEFINED;
1476  p->name = NULL;
1477  return p;
1478 }
1479 
1480 mpc_parser_t *mpc_new(const char *name) {
1482  p->retained = 1;
1483  p->name = realloc(p->name, strlen(name) + 1);
1484  strcpy(p->name, name);
1485  return p;
1486 }
1487 
1489  int i = 0;
1490  mpc_parser_t *p;
1491 
1492  if (a->retained) { return a; }
1493 
1494  p = mpc_undefined();
1495  p->retained = a->retained;
1496  p->type = a->type;
1497  p->data = a->data;
1498 
1499  if (a->name) {
1500  p->name = malloc(strlen(a->name)+1);
1501  strcpy(p->name, a->name);
1502  }
1503 
1504  switch (a->type) {
1505 
1506  case MPC_TYPE_FAIL:
1507  p->data.fail.m = malloc(strlen(a->data.fail.m)+1);
1508  strcpy(p->data.fail.m, a->data.fail.m);
1509  break;
1510 
1511  case MPC_TYPE_ONEOF:
1512  case MPC_TYPE_NONEOF:
1513  case MPC_TYPE_STRING:
1514  p->data.string.x = malloc(strlen(a->data.string.x)+1);
1515  strcpy(p->data.string.x, a->data.string.x);
1516  break;
1517 
1518  case MPC_TYPE_APPLY: p->data.apply.x = mpc_copy(a->data.apply.x); break;
1519  case MPC_TYPE_APPLY_TO: p->data.apply_to.x = mpc_copy(a->data.apply_to.x); break;
1520  case MPC_TYPE_PREDICT: p->data.predict.x = mpc_copy(a->data.predict.x); break;
1521 
1522  case MPC_TYPE_MAYBE:
1523  case MPC_TYPE_NOT:
1524  p->data.not.x = mpc_copy(a->data.not.x);
1525  break;
1526 
1527  case MPC_TYPE_EXPECT:
1528  p->data.expect.x = mpc_copy(a->data.expect.x);
1529  p->data.expect.m = malloc(strlen(a->data.expect.m)+1);
1530  strcpy(p->data.expect.m, a->data.expect.m);
1531  break;
1532 
1533  case MPC_TYPE_MANY:
1534  case MPC_TYPE_MANY1:
1535  case MPC_TYPE_COUNT:
1536  p->data.repeat.x = mpc_copy(a->data.repeat.x);
1537  break;
1538 
1539  case MPC_TYPE_OR:
1540  p->data.or.xs = malloc(a->data.or.n * sizeof(mpc_parser_t*));
1541  for (i = 0; i < a->data.or.n; i++) {
1542  p->data.or.xs[i] = mpc_copy(a->data.or.xs[i]);
1543  }
1544  break;
1545  case MPC_TYPE_AND:
1546  p->data.and.xs = malloc(a->data.and.n * sizeof(mpc_parser_t*));
1547  for (i = 0; i < a->data.and.n; i++) {
1548  p->data.and.xs[i] = mpc_copy(a->data.and.xs[i]);
1549  }
1550  p->data.and.dxs = malloc((a->data.and.n-1) * sizeof(mpc_dtor_t));
1551  for (i = 0; i < a->data.and.n-1; i++) {
1552  p->data.and.dxs[i] = a->data.and.dxs[i];
1553  }
1554  break;
1555 
1556  case MPC_TYPE_CHECK:
1557  p->data.check.x = mpc_copy(a->data.check.x);
1558  p->data.check.e = malloc(strlen(a->data.check.e)+1);
1559  strcpy(p->data.check.e, a->data.check.e);
1560  break;
1561  case MPC_TYPE_CHECK_WITH:
1562  p->data.check_with.x = mpc_copy(a->data.check_with.x);
1563  p->data.check_with.e = malloc(strlen(a->data.check_with.e)+1);
1564  strcpy(p->data.check_with.e, a->data.check_with.e);
1565  break;
1566 
1567  default: break;
1568  }
1569 
1570 
1571  return p;
1572 }
1573 
1576  p->type = MPC_TYPE_UNDEFINED;
1577  return p;
1578 }
1579 
1581 
1582  if (p->retained) {
1583  p->type = a->type;
1584  p->data = a->data;
1585  } else {
1586  mpc_parser_t *a2 = mpc_failf("Attempt to assign to Unretained Parser!");
1587  p->type = a2->type;
1588  p->data = a2->data;
1589  free(a2);
1590  }
1591 
1592  free(a);
1593  return p;
1594 }
1595 
1596 void mpc_cleanup(int n, ...) {
1597  int i;
1598  mpc_parser_t **list = malloc(sizeof(mpc_parser_t*) * n);
1599 
1600  va_list va;
1601  va_start(va, n);
1602  for (i = 0; i < n; i++) { list[i] = va_arg(va, mpc_parser_t*); }
1603  for (i = 0; i < n; i++) { mpc_undefine(list[i]); }
1604  for (i = 0; i < n; i++) { mpc_delete(list[i]); }
1605  va_end(va);
1606 
1607  free(list);
1608 }
1609 
1612  p->type = MPC_TYPE_PASS;
1613  return p;
1614 }
1615 
1616 mpc_parser_t *mpc_fail(const char *m) {
1618  p->type = MPC_TYPE_FAIL;
1619  p->data.fail.m = malloc(strlen(m) + 1);
1620  strcpy(p->data.fail.m, m);
1621  return p;
1622 }
1623 
1624 /*
1625 ** As `snprintf` is not ANSI standard this
1626 ** function `mpc_failf` should be considered
1627 ** unsafe.
1628 **
1629 ** You have a few options if this is going to be
1630 ** trouble.
1631 **
1632 ** - Ensure the format string does not exceed
1633 ** the buffer length using precision specifiers
1634 ** such as `%.512s`.
1635 **
1636 ** - Patch this function in your code base to
1637 ** use `snprintf` or whatever variant your
1638 ** system supports.
1639 **
1640 ** - Avoid it altogether.
1641 **
1642 */
1643 
1644 mpc_parser_t *mpc_failf(const char *fmt, ...) {
1645 
1646  va_list va;
1647  char *buffer;
1648 
1650  p->type = MPC_TYPE_FAIL;
1651 
1652  va_start(va, fmt);
1653  buffer = malloc(2048);
1654  vsprintf(buffer, fmt, va);
1655  va_end(va);
1656 
1657  buffer = realloc(buffer, strlen(buffer) + 1);
1658  p->data.fail.m = buffer;
1659  return p;
1660 
1661 }
1662 
1665  p->type = MPC_TYPE_LIFT_VAL;
1666  p->data.lift.x = x;
1667  return p;
1668 }
1669 
1672  p->type = MPC_TYPE_LIFT;
1673  p->data.lift.lf = lf;
1674  return p;
1675 }
1676 
1677 mpc_parser_t *mpc_anchor(int(*f)(char,char)) {
1679  p->type = MPC_TYPE_ANCHOR;
1680  p->data.anchor.f = f;
1681  return mpc_expect(p, "anchor");
1682 }
1683 
1686  p->type = MPC_TYPE_STATE;
1687  return p;
1688 }
1689 
1690 mpc_parser_t *mpc_expect(mpc_parser_t *a, const char *expected) {
1692  p->type = MPC_TYPE_EXPECT;
1693  p->data.expect.x = a;
1694  p->data.expect.m = malloc(strlen(expected) + 1);
1695  strcpy(p->data.expect.m, expected);
1696  return p;
1697 }
1698 
1699 /*
1700 ** As `snprintf` is not ANSI standard this
1701 ** function `mpc_expectf` should be considered
1702 ** unsafe.
1703 **
1704 ** You have a few options if this is going to be
1705 ** trouble.
1706 **
1707 ** - Ensure the format string does not exceed
1708 ** the buffer length using precision specifiers
1709 ** such as `%.512s`.
1710 **
1711 ** - Patch this function in your code base to
1712 ** use `snprintf` or whatever variant your
1713 ** system supports.
1714 **
1715 ** - Avoid it altogether.
1716 **
1717 */
1718 
1719 mpc_parser_t *mpc_expectf(mpc_parser_t *a, const char *fmt, ...) {
1720  va_list va;
1721  char *buffer;
1722 
1724  p->type = MPC_TYPE_EXPECT;
1725 
1726  va_start(va, fmt);
1727  buffer = malloc(2048);
1728  vsprintf(buffer, fmt, va);
1729  va_end(va);
1730 
1731  buffer = realloc(buffer, strlen(buffer) + 1);
1732  p->data.expect.x = a;
1733  p->data.expect.m = buffer;
1734  return p;
1735 }
1736 
1737 /*
1738 ** Basic Parsers
1739 */
1740 
1743  p->type = MPC_TYPE_ANY;
1744  return mpc_expect(p, "any character");
1745 }
1746 
1749  p->type = MPC_TYPE_SINGLE;
1750  p->data.single.x = c;
1751  return mpc_expectf(p, "'%c'", c);
1752 }
1753 
1754 mpc_parser_t *mpc_range(char s, char e) {
1756  p->type = MPC_TYPE_RANGE;
1757  p->data.range.x = s;
1758  p->data.range.y = e;
1759  return mpc_expectf(p, "character between '%c' and '%c'", s, e);
1760 }
1761 
1762 mpc_parser_t *mpc_oneof(const char *s) {
1764  p->type = MPC_TYPE_ONEOF;
1765  p->data.string.x = malloc(strlen(s) + 1);
1766  strcpy(p->data.string.x, s);
1767  return mpc_expectf(p, "one of '%s'", s);
1768 }
1769 
1770 mpc_parser_t *mpc_noneof(const char *s) {
1772  p->type = MPC_TYPE_NONEOF;
1773  p->data.string.x = malloc(strlen(s) + 1);
1774  strcpy(p->data.string.x, s);
1775  return mpc_expectf(p, "none of '%s'", s);
1776 
1777 }
1778 
1779 mpc_parser_t *mpc_satisfy(int(*f)(char)) {
1781  p->type = MPC_TYPE_SATISFY;
1782  p->data.satisfy.f = f;
1783  return mpc_expectf(p, "character satisfying function %p", f);
1784 }
1785 
1786 mpc_parser_t *mpc_string(const char *s) {
1788  p->type = MPC_TYPE_STRING;
1789  p->data.string.x = malloc(strlen(s) + 1);
1790  strcpy(p->data.string.x, s);
1791  return mpc_expectf(p, "\"%s\"", s);
1792 }
1793 
1794 /*
1795 ** Core Parsers
1796 */
1797 
1800  p->type = MPC_TYPE_APPLY;
1801  p->data.apply.x = a;
1802  p->data.apply.f = f;
1803  return p;
1804 }
1805 
1808  p->type = MPC_TYPE_APPLY_TO;
1809  p->data.apply_to.x = a;
1810  p->data.apply_to.f = f;
1811  p->data.apply_to.d = x;
1812  return p;
1813 }
1814 
1817  p->type = MPC_TYPE_CHECK;
1818  p->data.check.x = a;
1819  p->data.check.dx = da;
1820  p->data.check.f = f;
1821  p->data.check.e = malloc(strlen(e) + 1);
1822  strcpy(p->data.check.e, e);
1823  return p;
1824 }
1825 
1828  p->type = MPC_TYPE_CHECK_WITH;
1829  p->data.check_with.x = a;
1830  p->data.check_with.dx = da;
1831  p->data.check_with.f = f;
1832  p->data.check_with.d = x;
1833  p->data.check_with.e = malloc(strlen(e) + 1);
1834  strcpy(p->data.check_with.e, e);
1835  return p;
1836 }
1837 
1839  va_list va;
1840  char *buffer;
1841  mpc_parser_t *p;
1842 
1843  va_start(va, fmt);
1844  buffer = malloc(2048);
1845  vsprintf(buffer, fmt, va);
1846  va_end(va);
1847 
1848  p = mpc_check(a, da, f, buffer);
1849  free(buffer);
1850 
1851  return p;
1852 }
1853 
1855  va_list va;
1856  char *buffer;
1857  mpc_parser_t *p;
1858 
1859  va_start(va, fmt);
1860  buffer = malloc(2048);
1861  vsprintf(buffer, fmt, va);
1862  va_end(va);
1863 
1864  p = mpc_check_with(a, da, f, x, buffer);
1865  free(buffer);
1866 
1867  return p;
1868 }
1869 
1872  p->type = MPC_TYPE_PREDICT;
1873  p->data.predict.x = a;
1874  return p;
1875 }
1876 
1879  p->type = MPC_TYPE_NOT;
1880  p->data.not.x = a;
1881  p->data.not.dx = da;
1882  p->data.not.lf = lf;
1883  return p;
1884 }
1885 
1887  return mpc_not_lift(a, da, mpcf_ctor_null);
1888 }
1889 
1892  p->type = MPC_TYPE_MAYBE;
1893  p->data.not.x = a;
1894  p->data.not.lf = lf;
1895  return p;
1896 }
1897 
1899  return mpc_maybe_lift(a, mpcf_ctor_null);
1900 }
1901 
1904  p->type = MPC_TYPE_MANY;
1905  p->data.repeat.x = a;
1906  p->data.repeat.f = f;
1907  return p;
1908 }
1909 
1912  p->type = MPC_TYPE_MANY1;
1913  p->data.repeat.x = a;
1914  p->data.repeat.f = f;
1915  return p;
1916 }
1917 
1920  p->type = MPC_TYPE_COUNT;
1921  p->data.repeat.n = n;
1922  p->data.repeat.f = f;
1923  p->data.repeat.x = a;
1924  p->data.repeat.dx = da;
1925  return p;
1926 }
1927 
1928 mpc_parser_t *mpc_or(int n, ...) {
1929 
1930  int i;
1931  va_list va;
1932 
1934 
1935  p->type = MPC_TYPE_OR;
1936  p->data.or.n = n;
1937  p->data.or.xs = malloc(sizeof(mpc_parser_t*) * n);
1938 
1939  va_start(va, n);
1940  for (i = 0; i < n; i++) {
1941  p->data.or.xs[i] = va_arg(va, mpc_parser_t*);
1942  }
1943  va_end(va);
1944 
1945  return p;
1946 }
1947 
1949 
1950  int i;
1951  va_list va;
1952 
1954 
1955  p->type = MPC_TYPE_AND;
1956  p->data.and.n = n;
1957  p->data.and.f = f;
1958  p->data.and.xs = malloc(sizeof(mpc_parser_t*) * n);
1959  p->data.and.dxs = malloc(sizeof(mpc_dtor_t) * (n-1));
1960 
1961  va_start(va, f);
1962  for (i = 0; i < n; i++) {
1963  p->data.and.xs[i] = va_arg(va, mpc_parser_t*);
1964  }
1965  for (i = 0; i < (n-1); i++) {
1966  p->data.and.dxs[i] = va_arg(va, mpc_dtor_t);
1967  }
1968  va_end(va);
1969 
1970  return p;
1971 }
1972 
1973 /*
1974 ** Common Parsers
1975 */
1976 
1979  p->type = MPC_TYPE_SOI;
1980  return mpc_expect(p, "start of input");
1981 }
1982 
1985  p->type = MPC_TYPE_EOI;
1986  return mpc_expect(p, "end of input");
1987 }
1988 
1989 static int mpc_boundary_anchor(char prev, char next) {
1990  const char* word = "abcdefghijklmnopqrstuvwxyz"
1991  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1992  "0123456789_";
1993  if ( strchr(word, next) && prev == '\0') { return 1; }
1994  if ( strchr(word, prev) && next == '\0') { return 1; }
1995  if ( strchr(word, next) && !strchr(word, prev)) { return 1; }
1996  if (!strchr(word, next) && strchr(word, prev)) { return 1; }
1997  return 0;
1998 }
1999 
2000 static int mpc_boundary_newline_anchor(char prev, char next) {
2001  (void)next;
2002  return prev == '\n';
2003 }
2004 
2007 
2008 mpc_parser_t *mpc_whitespace(void) { return mpc_expect(mpc_oneof(" \f\n\r\t\v"), "whitespace"); }
2011 
2012 mpc_parser_t *mpc_newline(void) { return mpc_expect(mpc_char('\n'), "newline"); }
2013 mpc_parser_t *mpc_tab(void) { return mpc_expect(mpc_char('\t'), "tab"); }
2015 
2016 mpc_parser_t *mpc_digit(void) { return mpc_expect(mpc_oneof("0123456789"), "digit"); }
2017 mpc_parser_t *mpc_hexdigit(void) { return mpc_expect(mpc_oneof("0123456789ABCDEFabcdef"), "hex digit"); }
2018 mpc_parser_t *mpc_octdigit(void) { return mpc_expect(mpc_oneof("01234567"), "oct digit"); }
2022 
2023 mpc_parser_t *mpc_lower(void) { return mpc_expect(mpc_oneof("abcdefghijklmnopqrstuvwxyz"), "lowercase letter"); }
2024 mpc_parser_t *mpc_upper(void) { return mpc_expect(mpc_oneof("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), "uppercase letter"); }
2025 mpc_parser_t *mpc_alpha(void) { return mpc_expect(mpc_oneof("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), "letter"); }
2026 mpc_parser_t *mpc_underscore(void) { return mpc_expect(mpc_char('_'), "underscore"); }
2027 mpc_parser_t *mpc_alphanum(void) { return mpc_expect(mpc_or(3, mpc_alpha(), mpc_digit(), mpc_underscore()), "alphanumeric"); }
2028 
2029 mpc_parser_t *mpc_int(void) { return mpc_expect(mpc_apply(mpc_digits(), mpcf_int), "integer"); }
2030 mpc_parser_t *mpc_hex(void) { return mpc_expect(mpc_apply(mpc_hexdigits(), mpcf_hex), "hexadecimal"); }
2031 mpc_parser_t *mpc_oct(void) { return mpc_expect(mpc_apply(mpc_octdigits(), mpcf_oct), "octadecimal"); }
2032 mpc_parser_t *mpc_number(void) { return mpc_expect(mpc_or(3, mpc_int(), mpc_hex(), mpc_oct()), "number"); }
2033 
2035 
2036  /* [+-]?\d+(\.\d+)?([eE][+-]?[0-9]+)? */
2037 
2038  mpc_parser_t *p0, *p1, *p2, *p30, *p31, *p32, *p3;
2039 
2040  p0 = mpc_maybe_lift(mpc_oneof("+-"), mpcf_ctor_str);
2041  p1 = mpc_digits();
2043  p30 = mpc_oneof("eE");
2044  p31 = mpc_maybe_lift(mpc_oneof("+-"), mpcf_ctor_str);
2045  p32 = mpc_digits();
2046  p3 = mpc_maybe_lift(mpc_and(3, mpcf_strfold, p30, p31, p32, free, free), mpcf_ctor_str);
2047 
2048  return mpc_expect(mpc_and(4, mpcf_strfold, p0, p1, p2, p3, free, free, free), "real");
2049 
2050 }
2051 
2053  return mpc_expect(mpc_apply(mpc_real(), mpcf_float), "float");
2054 }
2055 
2057  return mpc_expect(mpc_between(mpc_or(2, mpc_escape(), mpc_any()), free, "'", "'"), "char");
2058 }
2059 
2061  mpc_parser_t *strchar = mpc_or(2, mpc_escape(), mpc_noneof("\""));
2062  return mpc_expect(mpc_between(mpc_many(mpcf_strfold, strchar), free, "\"", "\""), "string");
2063 }
2064 
2066  mpc_parser_t *regexchar = mpc_or(2, mpc_escape(), mpc_noneof("/"));
2067  return mpc_expect(mpc_between(mpc_many(mpcf_strfold, regexchar), free, "/", "/"), "regex");
2068 }
2069 
2071  mpc_parser_t *p0, *p1;
2072  p0 = mpc_or(2, mpc_alpha(), mpc_underscore());
2074  return mpc_and(2, mpcf_strfold, p0, p1, free);
2075 }
2076 
2077 /*
2078 ** Useful Parsers
2079 */
2080 
2084 
2089 mpc_parser_t *mpc_sym(const char *s) { return mpc_tok(mpc_string(s)); }
2090 
2092 
2093 mpc_parser_t *mpc_between(mpc_parser_t *a, mpc_dtor_t ad, const char *o, const char *c) {
2094  return mpc_and(3, mpcf_snd_free,
2095  mpc_string(o), a, mpc_string(c),
2096  free, ad);
2097 }
2098 
2099 mpc_parser_t *mpc_parens(mpc_parser_t *a, mpc_dtor_t ad) { return mpc_between(a, ad, "(", ")"); }
2100 mpc_parser_t *mpc_braces(mpc_parser_t *a, mpc_dtor_t ad) { return mpc_between(a, ad, "<", ">"); }
2102 mpc_parser_t *mpc_squares(mpc_parser_t *a, mpc_dtor_t ad) { return mpc_between(a, ad, "[", "]"); }
2103 
2104 mpc_parser_t *mpc_tok_between(mpc_parser_t *a, mpc_dtor_t ad, const char *o, const char *c) {
2105  return mpc_and(3, mpcf_snd_free,
2106  mpc_sym(o), mpc_tok(a), mpc_sym(c),
2107  free, ad);
2108 }
2109 
2114 
2115 /*
2116 ** Regular Expression Parsers
2117 */
2118 
2119 /*
2120 ** So here is a cute bootstrapping.
2121 **
2122 ** I'm using the previously defined
2123 ** mpc constructs and functions to
2124 ** parse the user regex string and
2125 ** construct a parser from it.
2126 **
2127 ** As it turns out lots of the standard
2128 ** mpc functions look a lot like `fold`
2129 ** functions and so can be used indirectly
2130 ** by many of the parsing functions to build
2131 ** a parser directly - as we are parsing.
2132 **
2133 ** This is certainly something that
2134 ** would be less elegant/interesting
2135 ** in a two-phase parser which first
2136 ** builds an AST and then traverses it
2137 ** to generate the object.
2138 **
2139 ** This whole thing acts as a great
2140 ** case study for how trivial it can be
2141 ** to write a great parser in a few
2142 ** lines of code using mpc.
2143 */
2144 
2145 /*
2146 **
2147 ** ### Regular Expression Grammar
2148 **
2149 ** <regex> : <term> | (<term> "|" <regex>)
2150 **
2151 ** <term> : <factor>*
2152 **
2153 ** <factor> : <base>
2154 ** | <base> "*"
2155 ** | <base> "+"
2156 ** | <base> "?"
2157 ** | <base> "{" <digits> "}"
2158 **
2159 ** <base> : <char>
2160 ** | "\" <char>
2161 ** | "(" <regex> ")"
2162 ** | "[" <range> "]"
2163 */
2164 
2165 static mpc_val_t *mpcf_re_or(int n, mpc_val_t **xs) {
2166  (void) n;
2167  if (xs[1] == NULL) { return xs[0]; }
2168  else { return mpc_or(2, xs[0], xs[1]); }
2169 }
2170 
2171 static mpc_val_t *mpcf_re_and(int n, mpc_val_t **xs) {
2172  int i;
2174  for (i = 0; i < n; i++) {
2175  p = mpc_and(2, mpcf_strfold, p, xs[i], free);
2176  }
2177  return p;
2178 }
2179 
2180 static mpc_val_t *mpcf_re_repeat(int n, mpc_val_t **xs) {
2181  int num;
2182  (void) n;
2183  if (xs[1] == NULL) { return xs[0]; }
2184  switch(((char*)xs[1])[0])
2185  {
2186  case '*': { free(xs[1]); return mpc_many(mpcf_strfold, xs[0]); }; break;
2187  case '+': { free(xs[1]); return mpc_many1(mpcf_strfold, xs[0]); }; break;
2188  case '?': { free(xs[1]); return mpc_maybe_lift(xs[0], mpcf_ctor_str); }; break;
2189  default:
2190  num = *(int*)xs[1];
2191  free(xs[1]);
2192  }
2193 
2194  return mpc_count(num, mpcf_strfold, xs[0], free);
2195 }
2196 
2198  switch (c) {
2199  case 'a': return mpc_char('\a');
2200  case 'f': return mpc_char('\f');
2201  case 'n': return mpc_char('\n');
2202  case 'r': return mpc_char('\r');
2203  case 't': return mpc_char('\t');
2204  case 'v': return mpc_char('\v');
2205  case 'b': return mpc_and(2, mpcf_snd, mpc_boundary(), mpc_lift(mpcf_ctor_str), free);
2206  case 'B': return mpc_not_lift(mpc_boundary(), free, mpcf_ctor_str);
2207  case 'A': return mpc_and(2, mpcf_snd, mpc_soi(), mpc_lift(mpcf_ctor_str), free);
2208  case 'Z': return mpc_and(2, mpcf_snd, mpc_eoi(), mpc_lift(mpcf_ctor_str), free);
2209  case 'd': return mpc_digit();
2210  case 'D': return mpc_not_lift(mpc_digit(), free, mpcf_ctor_str);
2211  case 's': return mpc_whitespace();
2212  case 'S': return mpc_not_lift(mpc_whitespace(), free, mpcf_ctor_str);
2213  case 'w': return mpc_alphanum();
2214  case 'W': return mpc_not_lift(mpc_alphanum(), free, mpcf_ctor_str);
2215  default: return NULL;
2216  }
2217 }
2218 
2219 static mpc_val_t *mpcf_re_escape(mpc_val_t *x, void* data) {
2220 
2221  int mode = *((int*)data);
2222  char *s = x;
2223  mpc_parser_t *p;
2224 
2225  /* Any Character */
2226  if (s[0] == '.') {
2227  free(s);
2228  if (mode & MPC_RE_DOTALL) {
2229  return mpc_any();
2230  } else {
2231  return mpc_expect(mpc_noneof("\n"), "any character except a newline");
2232  }
2233  }
2234 
2235  /* Start of Input */
2236  if (s[0] == '^') {
2237  free(s);
2238  if (mode & MPC_RE_MULTILINE) {
2240  } else {
2242  }
2243  }
2244 
2245  /* End of Input */
2246  if (s[0] == '$') {
2247  free(s);
2248  if (mode & MPC_RE_MULTILINE) {
2249  return mpc_or(2,
2250  mpc_newline(),
2252  } else {
2253  return mpc_or(2,
2256  }
2257  }
2258 
2259  /* Regex Escape */
2260  if (s[0] == '\\') {
2261  p = mpc_re_escape_char(s[1]);
2262  p = (p == NULL) ? mpc_char(s[1]) : p;
2263  free(s);
2264  return p;
2265  }
2266 
2267  /* Regex Standard */
2268  p = mpc_char(s[0]);
2269  free(s);
2270  return p;
2271 }
2272 
2273 static const char *mpc_re_range_escape_char(char c) {
2274  switch (c) {
2275  case '-': return "-";
2276  case 'a': return "\a";
2277  case 'f': return "\f";
2278  case 'n': return "\n";
2279  case 'r': return "\r";
2280  case 't': return "\t";
2281  case 'v': return "\v";
2282  case 'b': return "\b";
2283  case 'd': return "0123456789";
2284  case 's': return " \f\n\r\t\v";
2285  case 'w': return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
2286  default: return NULL;
2287  }
2288 }
2289 
2291 
2292  mpc_parser_t *out;
2293  size_t i, j;
2294  size_t start, end;
2295  const char *tmp = NULL;
2296  const char *s = x;
2297  int comp = s[0] == '^' ? 1 : 0;
2298  char *range = calloc(1,1);
2299 
2300  if (s[0] == '\0') { free(range); free(x); return mpc_fail("Invalid Regex Range Expression"); }
2301  if (s[0] == '^' &&
2302  s[1] == '\0') { free(range); free(x); return mpc_fail("Invalid Regex Range Expression"); }
2303 
2304  for (i = comp; i < strlen(s); i++){
2305 
2306  /* Regex Range Escape */
2307  if (s[i] == '\\') {
2309  if (tmp != NULL) {
2310  range = realloc(range, strlen(range) + strlen(tmp) + 1);
2311  strcat(range, tmp);
2312  } else {
2313  range = realloc(range, strlen(range) + 1 + 1);
2314  range[strlen(range) + 1] = '\0';
2315  range[strlen(range) + 0] = s[i+1];
2316  }
2317  i++;
2318  }
2319 
2320  /* Regex Range...Range */
2321  else if (s[i] == '-') {
2322  if (s[i+1] == '\0' || i == 0) {
2323  range = realloc(range, strlen(range) + strlen("-") + 1);
2324  strcat(range, "-");
2325  } else {
2326  start = s[i-1]+1;
2327  end = s[i+1]-1;
2328  for (j = start; j <= end; j++) {
2329  range = realloc(range, strlen(range) + 1 + 1 + 1);
2330  range[strlen(range) + 1] = '\0';
2331  range[strlen(range) + 0] = (char)j;
2332  }
2333  }
2334  }
2335 
2336  /* Regex Range Normal */
2337  else {
2338  range = realloc(range, strlen(range) + 1 + 1);
2339  range[strlen(range) + 1] = '\0';
2340  range[strlen(range) + 0] = s[i];
2341  }
2342 
2343  }
2344 
2345  out = comp == 1 ? mpc_noneof(range) : mpc_oneof(range);
2346 
2347  free(x);
2348  free(range);
2349 
2350  return out;
2351 }
2352 
2353 mpc_parser_t *mpc_re(const char *re) {
2354  return mpc_re_mode(re, MPC_RE_DEFAULT);
2355 }
2356 
2357 mpc_parser_t *mpc_re_mode(const char *re, int mode) {
2358 
2359  char *err_msg;
2360  mpc_parser_t *err_out;
2361  mpc_result_t r;
2362  mpc_parser_t *Regex, *Term, *Factor, *Base, *Range, *RegexEnclose;
2363 
2364  Regex = mpc_new("regex");
2365  Term = mpc_new("term");
2366  Factor = mpc_new("factor");
2367  Base = mpc_new("base");
2368  Range = mpc_new("range");
2369 
2370  mpc_define(Regex, mpc_and(2, mpcf_re_or,
2371  Term,
2372  mpc_maybe(mpc_and(2, mpcf_snd_free, mpc_char('|'), Regex, free)),
2374  ));
2375 
2376  mpc_define(Term, mpc_many(mpcf_re_and, Factor));
2377 
2378  mpc_define(Factor, mpc_and(2, mpcf_re_repeat,
2379  Base,
2380  mpc_or(5,
2381  mpc_char('*'), mpc_char('+'), mpc_char('?'),
2383  mpc_pass()),
2385  ));
2386 
2387  mpc_define(Base, mpc_or(4,
2388  mpc_parens(Regex, (mpc_dtor_t)mpc_delete),
2392  ));
2393 
2394  mpc_define(Range, mpc_apply(
2397  ));
2398 
2399  RegexEnclose = mpc_whole(mpc_predictive(Regex), (mpc_dtor_t)mpc_delete);
2400 
2401  mpc_optimise(RegexEnclose);
2402  mpc_optimise(Regex);
2403  mpc_optimise(Term);
2404  mpc_optimise(Factor);
2405  mpc_optimise(Base);
2406  mpc_optimise(Range);
2407 
2408  if(!mpc_parse("<mpc_re_compiler>", re, RegexEnclose, &r)) {
2409  err_msg = mpc_err_string(r.error);
2410  err_out = mpc_failf("Invalid Regex: %s", err_msg);
2411  mpc_err_delete(r.error);
2412  free(err_msg);
2413  r.output = err_out;
2414  }
2415 
2416  mpc_cleanup(6, RegexEnclose, Regex, Term, Factor, Base, Range);
2417 
2418  mpc_optimise(r.output);
2419 
2420  return r.output;
2421 
2422 }
2423 
2424 /*
2425 ** Common Fold Functions
2426 */
2427 
2428 void mpcf_dtor_null(mpc_val_t *x) { (void) x; return; }
2429 
2430 mpc_val_t *mpcf_ctor_null(void) { return NULL; }
2431 mpc_val_t *mpcf_ctor_str(void) { return calloc(1, 1); }
2433 
2435  int *y = malloc(sizeof(int));
2436  *y = strtol(x, NULL, 10);
2437  free(x);
2438  return y;
2439 }
2440 
2442  int *y = malloc(sizeof(int));
2443  *y = strtol(x, NULL, 16);
2444  free(x);
2445  return y;
2446 }
2447 
2449  int *y = malloc(sizeof(int));
2450  *y = strtol(x, NULL, 8);
2451  free(x);
2452  return y;
2453 }
2454 
2456  float *y = malloc(sizeof(float));
2457  *y = strtod(x, NULL);
2458  free(x);
2459  return y;
2460 }
2461 
2463  char *s = x;
2464  while (isspace((unsigned char)*s)) {
2465  memmove(s, s+1, strlen(s));
2466  }
2467  return s;
2468 }
2469 
2471  char *s = x;
2472  size_t l = strlen(s);
2473  while (l > 0 && isspace((unsigned char)s[l-1])) {
2474  s[l-1] = '\0'; l--;
2475  }
2476  return s;
2477 }
2478 
2480  return mpcf_strtriml(mpcf_strtrimr(x));
2481 }
2482 
2483 static const char mpc_escape_input_c[] = {
2484  '\a', '\b', '\f', '\n', '\r',
2485  '\t', '\v', '\\', '\'', '\"', '\0'};
2486 
2487 static const char *mpc_escape_output_c[] = {
2488  "\\a", "\\b", "\\f", "\\n", "\\r", "\\t",
2489  "\\v", "\\\\", "\\'", "\\\"", "\\0", NULL};
2490 
2491 static const char mpc_escape_input_raw_re[] = { '/' };
2492 static const char *mpc_escape_output_raw_re[] = { "\\/", NULL };
2493 
2494 static const char mpc_escape_input_raw_cstr[] = { '"' };
2495 static const char *mpc_escape_output_raw_cstr[] = { "\\\"", NULL };
2496 
2497 static const char mpc_escape_input_raw_cchar[] = { '\'' };
2498 static const char *mpc_escape_output_raw_cchar[] = { "\\'", NULL };
2499 
2500 static mpc_val_t *mpcf_escape_new(mpc_val_t *x, const char *input, const char **output) {
2501 
2502  int i;
2503  int found;
2504  char buff[2];
2505  char *s = x;
2506  char *y = calloc(1, 1);
2507 
2508  while (*s) {
2509 
2510  i = 0;
2511  found = 0;
2512 
2513  while (output[i]) {
2514  if (*s == input[i]) {
2515  y = realloc(y, strlen(y) + strlen(output[i]) + 1);
2516  strcat(y, output[i]);
2517  found = 1;
2518  break;
2519  }
2520  i++;
2521  }
2522 
2523  if (!found) {
2524  y = realloc(y, strlen(y) + 2);
2525  buff[0] = *s; buff[1] = '\0';
2526  strcat(y, buff);
2527  }
2528 
2529  s++;
2530  }
2531 
2532 
2533  return y;
2534 }
2535 
2536 static mpc_val_t *mpcf_unescape_new(mpc_val_t *x, const char *input, const char **output) {
2537 
2538  int i;
2539  int found = 0;
2540  char buff[2];
2541  char *s = x;
2542  char *y = calloc(1, 1);
2543 
2544  while (*s) {
2545 
2546  i = 0;
2547  found = 0;
2548 
2549  while (output[i]) {
2550  if ((*(s+0)) == output[i][0] &&
2551  (*(s+1)) == output[i][1]) {
2552  y = realloc(y, strlen(y) + 1 + 1);
2553  buff[0] = input[i]; buff[1] = '\0';
2554  strcat(y, buff);
2555  found = 1;
2556  s++;
2557  break;
2558  }
2559  i++;
2560  }
2561 
2562  if (!found) {
2563  y = realloc(y, strlen(y) + 1 + 1);
2564  buff[0] = *s; buff[1] = '\0';
2565  strcat(y, buff);
2566  }
2567 
2568  if (*s == '\0') { break; }
2569  else { s++; }
2570  }
2571 
2572  return y;
2573 
2574 }
2575 
2578  free(x);
2579  return y;
2580 }
2581 
2584  free(x);
2585  return y;
2586 }
2587 
2590  free(x);
2591  return y;
2592 }
2593 
2596  free(x);
2597  return y;
2598 }
2599 
2602  free(x);
2603  return y;
2604 }
2605 
2608  free(x);
2609  return y;
2610 }
2611 
2614  free(x);
2615  return y;
2616 }
2617 
2620  free(x);
2621  return y;
2622 }
2623 
2624 mpc_val_t *mpcf_null(int n, mpc_val_t** xs) { (void) n; (void) xs; return NULL; }
2625 mpc_val_t *mpcf_fst(int n, mpc_val_t **xs) { (void) n; return xs[0]; }
2626 mpc_val_t *mpcf_snd(int n, mpc_val_t **xs) { (void) n; return xs[1]; }
2627 mpc_val_t *mpcf_trd(int n, mpc_val_t **xs) { (void) n; return xs[2]; }
2628 
2629 static mpc_val_t *mpcf_nth_free(int n, mpc_val_t **xs, int x) {
2630  int i;
2631  for (i = 0; i < n; i++) {
2632  if (i != x) { free(xs[i]); }
2633  }
2634  return xs[x];
2635 }
2636 
2637 mpc_val_t *mpcf_fst_free(int n, mpc_val_t **xs) { return mpcf_nth_free(n, xs, 0); }
2638 mpc_val_t *mpcf_snd_free(int n, mpc_val_t **xs) { return mpcf_nth_free(n, xs, 1); }
2639 mpc_val_t *mpcf_trd_free(int n, mpc_val_t **xs) { return mpcf_nth_free(n, xs, 2); }
2641  int i;
2642  for (i = 0; i < n; i++) {
2643  free(xs[i]);
2644  }
2645  return NULL;
2646 }
2647 
2649  int i;
2650  size_t l = 0;
2651 
2652  if (n == 0) { return calloc(1, 1); }
2653 
2654  for (i = 0; i < n; i++) { l += strlen(xs[i]); }
2655 
2656  xs[0] = realloc(xs[0], l + 1);
2657 
2658  for (i = 1; i < n; i++) {
2659  strcat(xs[0], xs[i]); free(xs[i]);
2660  }
2661 
2662  return xs[0];
2663 }
2664 
2666  int **vs = (int**)xs;
2667  (void) n;
2668 
2669  switch(((char*)xs[1])[0])
2670  {
2671  case '*': { *vs[0] *= *vs[2]; }; break;
2672  case '/': { *vs[0] /= *vs[2]; }; break;
2673  case '%': { *vs[0] %= *vs[2]; }; break;
2674  case '+': { *vs[0] += *vs[2]; }; break;
2675  case '-': { *vs[0] -= *vs[2]; }; break;
2676  default: break;
2677  }
2678 
2679  free(xs[1]); free(xs[2]);
2680 
2681  return xs[0];
2682 }
2683 
2684 /*
2685 ** Printing
2686 */
2687 
2688 static void mpc_print_unretained(mpc_parser_t *p, int force) {
2689 
2690  /* TODO: Print Everything Escaped */
2691 
2692  int i;
2693  char *s, *e;
2694  char buff[2];
2695 
2696  if (p->retained && !force) {;
2697  if (p->name) { printf("<%s>", p->name); }
2698  else { printf("<anon>"); }
2699  return;
2700  }
2701 
2702  if (p->type == MPC_TYPE_UNDEFINED) { printf("<?>"); }
2703  if (p->type == MPC_TYPE_PASS) { printf("<:>"); }
2704  if (p->type == MPC_TYPE_FAIL) { printf("<!>"); }
2705  if (p->type == MPC_TYPE_LIFT) { printf("<#>"); }
2706  if (p->type == MPC_TYPE_STATE) { printf("<S>"); }
2707  if (p->type == MPC_TYPE_ANCHOR) { printf("<@>"); }
2708  if (p->type == MPC_TYPE_EXPECT) {
2709  printf("%s", p->data.expect.m);
2710  /*mpc_print_unretained(p->data.expect.x, 0);*/
2711  }
2712 
2713  if (p->type == MPC_TYPE_ANY) { printf("<.>"); }
2714  if (p->type == MPC_TYPE_SATISFY) { printf("<f>"); }
2715 
2716  if (p->type == MPC_TYPE_SINGLE) {
2717  buff[0] = p->data.single.x; buff[1] = '\0';
2718  s = mpcf_escape_new(
2719  buff,
2722  printf("'%s'", s);
2723  free(s);
2724  }
2725 
2726  if (p->type == MPC_TYPE_RANGE) {
2727  buff[0] = p->data.range.x; buff[1] = '\0';
2728  s = mpcf_escape_new(
2729  buff,
2732  buff[0] = p->data.range.y; buff[1] = '\0';
2733  e = mpcf_escape_new(
2734  buff,
2737  printf("[%s-%s]", s, e);
2738  free(s);
2739  free(e);
2740  }
2741 
2742  if (p->type == MPC_TYPE_ONEOF) {
2743  s = mpcf_escape_new(
2744  p->data.string.x,
2747  printf("[%s]", s);
2748  free(s);
2749  }
2750 
2751  if (p->type == MPC_TYPE_NONEOF) {
2752  s = mpcf_escape_new(
2753  p->data.string.x,
2756  printf("[^%s]", s);
2757  free(s);
2758  }
2759 
2760  if (p->type == MPC_TYPE_STRING) {
2761  s = mpcf_escape_new(
2762  p->data.string.x,
2765  printf("\"%s\"", s);
2766  free(s);
2767  }
2768 
2769  if (p->type == MPC_TYPE_APPLY) { mpc_print_unretained(p->data.apply.x, 0); }
2770  if (p->type == MPC_TYPE_APPLY_TO) { mpc_print_unretained(p->data.apply_to.x, 0); }
2771  if (p->type == MPC_TYPE_PREDICT) { mpc_print_unretained(p->data.predict.x, 0); }
2772 
2773  if (p->type == MPC_TYPE_NOT) { mpc_print_unretained(p->data.not.x, 0); printf("!"); }
2774  if (p->type == MPC_TYPE_MAYBE) { mpc_print_unretained(p->data.not.x, 0); printf("?"); }
2775 
2776  if (p->type == MPC_TYPE_MANY) { mpc_print_unretained(p->data.repeat.x, 0); printf("*"); }
2777  if (p->type == MPC_TYPE_MANY1) { mpc_print_unretained(p->data.repeat.x, 0); printf("+"); }
2778  if (p->type == MPC_TYPE_COUNT) { mpc_print_unretained(p->data.repeat.x, 0); printf("{%i}", p->data.repeat.n); }
2779 
2780  if (p->type == MPC_TYPE_OR) {
2781  printf("(");
2782  for(i = 0; i < p->data.or.n-1; i++) {
2783  mpc_print_unretained(p->data.or.xs[i], 0);
2784  printf(" | ");
2785  }
2786  mpc_print_unretained(p->data.or.xs[p->data.or.n-1], 0);
2787  printf(")");
2788  }
2789 
2790  if (p->type == MPC_TYPE_AND) {
2791  printf("(");
2792  for(i = 0; i < p->data.and.n-1; i++) {
2793  mpc_print_unretained(p->data.and.xs[i], 0);
2794  printf(" ");
2795  }
2796  mpc_print_unretained(p->data.and.xs[p->data.and.n-1], 0);
2797  printf(")");
2798  }
2799 
2800  if (p->type == MPC_TYPE_CHECK) {
2801  mpc_print_unretained(p->data.check.x, 0);
2802  printf("->?");
2803  }
2804  if (p->type == MPC_TYPE_CHECK_WITH) {
2805  mpc_print_unretained(p->data.check_with.x, 0);
2806  printf("->?");
2807  }
2808 
2809 }
2810 
2812  mpc_print_unretained(p, 1);
2813  printf("\n");
2814 }
2815 
2816 /*
2817 ** Testing
2818 */
2819 
2820 /*
2821 ** These functions are slightly unwieldy and
2822 ** also the whole of the testing suite for mpc
2823 ** mpc is pretty shaky.
2824 **
2825 ** It could do with a lot more tests and more
2826 ** precision. Currently I am only really testing
2827 ** changes off of the examples.
2828 **
2829 */
2830 
2831 int mpc_test_fail(mpc_parser_t *p, const char *s, const void *d,
2832  int(*tester)(const void*, const void*),
2833  mpc_dtor_t destructor,
2834  void(*printer)(const void*)) {
2835  mpc_result_t r;
2836  (void) printer;
2837  if (mpc_parse("<test>", s, p, &r)) {
2838 
2839  if (tester(r.output, d)) {
2840  destructor(r.output);
2841  return 0;
2842  } else {
2843  destructor(r.output);
2844  return 1;
2845  }
2846 
2847  } else {
2848  mpc_err_delete(r.error);
2849  return 1;
2850  }
2851 
2852 }
2853 
2854 int mpc_test_pass(mpc_parser_t *p, const char *s, const void *d,
2855  int(*tester)(const void*, const void*),
2856  mpc_dtor_t destructor,
2857  void(*printer)(const void*)) {
2858 
2859  mpc_result_t r;
2860  if (mpc_parse("<test>", s, p, &r)) {
2861 
2862  if (tester(r.output, d)) {
2863  destructor(r.output);
2864  return 1;
2865  } else {
2866  printf("Got "); printer(r.output); printf("\n");
2867  printf("Expected "); printer(d); printf("\n");
2868  destructor(r.output);
2869  return 0;
2870  }
2871 
2872  } else {
2873  mpc_err_print(r.error);
2874  mpc_err_delete(r.error);
2875  return 0;
2876 
2877  }
2878 
2879 }
2880 
2881 
2882 /*
2883 ** AST
2884 */
2885 
2887 
2888  int i;
2889 
2890  if (a == NULL) { return; }
2891 
2892  for (i = 0; i < a->children_num; i++) {
2893  mpc_ast_delete(a->children[i]);
2894  }
2895 
2896  free(a->children);
2897  free(a->tag);
2898  free(a->contents);
2899  free(a);
2900 
2901 }
2902 
2904  free(a->children);
2905  free(a->tag);
2906  free(a->contents);
2907  free(a);
2908 }
2909 
2910 mpc_ast_t *mpc_ast_new(const char *tag, const char *contents) {
2911 
2912  mpc_ast_t *a = malloc(sizeof(mpc_ast_t));
2913 
2914  a->tag = malloc(strlen(tag) + 1);
2915  strcpy(a->tag, tag);
2916 
2917  a->contents = malloc(strlen(contents) + 1);
2918  strcpy(a->contents, contents);
2919 
2920  a->state = mpc_state_new();
2921 
2922  a->children_num = 0;
2923  a->children = NULL;
2924  return a;
2925 
2926 }
2927 
2928 mpc_ast_t *mpc_ast_build(int n, const char *tag, ...) {
2929 
2930  mpc_ast_t *a = mpc_ast_new(tag, "");
2931 
2932  int i;
2933  va_list va;
2934  va_start(va, tag);
2935 
2936  for (i = 0; i < n; i++) {
2937  mpc_ast_add_child(a, va_arg(va, mpc_ast_t*));
2938  }
2939 
2940  va_end(va);
2941 
2942  return a;
2943 
2944 }
2945 
2947 
2948  mpc_ast_t *r;
2949 
2950  if (a == NULL) { return a; }
2951  if (a->children_num == 0) { return a; }
2952  if (a->children_num == 1) { return a; }
2953 
2954  r = mpc_ast_new(">", "");
2955  mpc_ast_add_child(r, a);
2956  return r;
2957 }
2958 
2960 
2961  int i;
2962 
2963  if (strcmp(a->tag, b->tag) != 0) { return 0; }
2964  if (strcmp(a->contents, b->contents) != 0) { return 0; }
2965  if (a->children_num != b->children_num) { return 0; }
2966 
2967  for (i = 0; i < a->children_num; i++) {
2968  if (!mpc_ast_eq(a->children[i], b->children[i])) { return 0; }
2969  }
2970 
2971  return 1;
2972 }
2973 
2975  r->children_num++;
2976  r->children = realloc(r->children, sizeof(mpc_ast_t*) * r->children_num);
2977  r->children[r->children_num-1] = a;
2978  return r;
2979 }
2980 
2982  if (a == NULL) { return a; }
2983  a->tag = realloc(a->tag, strlen(t) + 1 + strlen(a->tag) + 1);
2984  memmove(a->tag + strlen(t) + 1, a->tag, strlen(a->tag)+1);
2985  memmove(a->tag, t, strlen(t));
2986  memmove(a->tag + strlen(t), "|", 1);
2987  return a;
2988 }
2989 
2991  if (a == NULL) { return a; }
2992  a->tag = realloc(a->tag, (strlen(t)-1) + strlen(a->tag) + 1);
2993  memmove(a->tag + (strlen(t)-1), a->tag, strlen(a->tag)+1);
2994  memmove(a->tag, t, (strlen(t)-1));
2995  return a;
2996 }
2997 
2998 mpc_ast_t *mpc_ast_tag(mpc_ast_t *a, const char *t) {
2999  a->tag = realloc(a->tag, strlen(t) + 1);
3000  strcpy(a->tag, t);
3001  return a;
3002 }
3003 
3005  if (a == NULL) { return a; }
3006  a->state = s;
3007  return a;
3008 }
3009 
3010 static void mpc_ast_print_depth(mpc_ast_t *a, int d, FILE *fp) {
3011 
3012  int i;
3013 
3014  if (a == NULL) {
3015  fprintf(fp, "NULL\n");
3016  return;
3017  }
3018 
3019  for (i = 0; i < d; i++) { fprintf(fp, " "); }
3020 
3021  if (strlen(a->contents)) {
3022  fprintf(fp, "%s:%lu:%lu '%s'\n", a->tag,
3023  (long unsigned int)(a->state.row+1),
3024  (long unsigned int)(a->state.col+1),
3025  a->contents);
3026  } else {
3027  fprintf(fp, "%s \n", a->tag);
3028  }
3029 
3030  for (i = 0; i < a->children_num; i++) {
3031  mpc_ast_print_depth(a->children[i], d+1, fp);
3032  }
3033 
3034 }
3035 
3037  mpc_ast_print_depth(a, 0, stdout);
3038 }
3039 
3041  mpc_ast_print_depth(a, 0, fp);
3042 }
3043 
3044 int mpc_ast_get_index(mpc_ast_t *ast, const char *tag) {
3045  return mpc_ast_get_index_lb(ast, tag, 0);
3046 }
3047 
3048 int mpc_ast_get_index_lb(mpc_ast_t *ast, const char *tag, int lb) {
3049  int i;
3050 
3051  for(i=lb; i<ast->children_num; i++) {
3052  if(strcmp(ast->children[i]->tag, tag) == 0) {
3053  return i;
3054  }
3055  }
3056 
3057  return -1;
3058 }
3059 
3061  return mpc_ast_get_child_lb(ast, tag, 0);
3062 }
3063 
3064 mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, const char *tag, int lb) {
3065  int i;
3066 
3067  for(i=lb; i<ast->children_num; i++) {
3068  if(strcmp(ast->children[i]->tag, tag) == 0) {
3069  return ast->children[i];
3070  }
3071  }
3072 
3073  return NULL;
3074 }
3075 
3077  mpc_ast_trav_order_t order)
3078 {
3079  mpc_ast_trav_t *trav, *n_trav;
3080  mpc_ast_t *cnode = ast;
3081 
3082  /* Create the traversal structure */
3083  trav = malloc(sizeof(mpc_ast_trav_t));
3084  trav->curr_node = cnode;
3085  trav->parent = NULL;
3086  trav->curr_child = 0;
3087  trav->order = order;
3088 
3089  /* Get start node */
3090  switch(order) {
3092  /* Nothing else is needed for pre order start */
3093  break;
3094 
3096  while(cnode->children_num > 0) {
3097  cnode = cnode->children[0];
3098 
3099  n_trav = malloc(sizeof(mpc_ast_trav_t));
3100  n_trav->curr_node = cnode;
3101  n_trav->parent = trav;
3102  n_trav->curr_child = 0;
3103  n_trav->order = order;
3104 
3105  trav = n_trav;
3106  }
3107 
3108  break;
3109 
3110  default:
3111  /* Unreachable, but compiler complaints */
3112  break;
3113  }
3114 
3115  return trav;
3116 }
3117 
3119  mpc_ast_trav_t *n_trav, *to_free;
3120  mpc_ast_t *ret = NULL;
3121  int cchild;
3122 
3123  /* The end of traversal was reached */
3124  if(*trav == NULL) return NULL;
3125 
3126  switch((*trav)->order) {
3128  ret = (*trav)->curr_node;
3129 
3130  /* If there aren't any more children, go up */
3131  while(*trav != NULL &&
3132  (*trav)->curr_child >= (*trav)->curr_node->children_num)
3133  {
3134  to_free = *trav;
3135  *trav = (*trav)->parent;
3136  free(to_free);
3137  }
3138 
3139  /* If trav is NULL, the end was reached */
3140  if(*trav == NULL) {
3141  break;
3142  }
3143 
3144  /* Go to next child */
3145  n_trav = malloc(sizeof(mpc_ast_trav_t));
3146 
3147  cchild = (*trav)->curr_child;
3148  n_trav->curr_node = (*trav)->curr_node->children[cchild];
3149  n_trav->parent = *trav;
3150  n_trav->curr_child = 0;
3151  n_trav->order = (*trav)->order;
3152 
3153  (*trav)->curr_child++;
3154  *trav = n_trav;
3155 
3156  break;
3157 
3159  ret = (*trav)->curr_node;
3160 
3161  /* Move up tree to the parent If the parent doesn't have any more nodes,
3162  * then this is the current node. If it does, move down to its left most
3163  * child. Also, free the previous traversal node */
3164  to_free = *trav;
3165  *trav = (*trav)->parent;
3166  free(to_free);
3167 
3168  if(*trav == NULL)
3169  break;
3170 
3171  /* Next child */
3172  (*trav)->curr_child++;
3173 
3174  /* If there aren't any more children, this is the next node */
3175  if((*trav)->curr_child >= (*trav)->curr_node->children_num) {
3176  break;
3177  }
3178 
3179  /* If there are still more children, find the leftmost child from this
3180  * node */
3181  while((*trav)->curr_node->children_num > 0) {
3182  n_trav = malloc(sizeof(mpc_ast_trav_t));
3183 
3184  cchild = (*trav)->curr_child;
3185  n_trav->curr_node = (*trav)->curr_node->children[cchild];
3186  n_trav->parent = *trav;
3187  n_trav->curr_child = 0;
3188  n_trav->order = (*trav)->order;
3189 
3190  *trav = n_trav;
3191  }
3192 
3193  default:
3194  /* Unreachable, but compiler complaints */
3195  break;
3196  }
3197 
3198  return ret;
3199 }
3200 
3202  mpc_ast_trav_t *n_trav;
3203 
3204  /* Go through parents until all are free */
3205  while(*trav != NULL) {
3206  n_trav = (*trav)->parent;
3207  free(*trav);
3208  *trav = n_trav;
3209  }
3210 }
3211 
3213 
3214  int i, j;
3215  mpc_ast_t** as = (mpc_ast_t**)xs;
3216  mpc_ast_t *r;
3217 
3218  if (n == 0) { return NULL; }
3219  if (n == 1) { return xs[0]; }
3220  if (n == 2 && xs[1] == NULL) { return xs[0]; }
3221  if (n == 2 && xs[0] == NULL) { return xs[1]; }
3222 
3223  r = mpc_ast_new(">", "");
3224 
3225  for (i = 0; i < n; i++) {
3226 
3227  if (as[i] == NULL) { continue; }
3228 
3229  if (as[i] && as[i]->children_num == 0) {
3230  mpc_ast_add_child(r, as[i]);
3231  } else if (as[i] && as[i]->children_num == 1) {
3232  mpc_ast_add_child(r, mpc_ast_add_root_tag(as[i]->children[0], as[i]->tag));
3234  } else if (as[i] && as[i]->children_num >= 2) {
3235  for (j = 0; j < as[i]->children_num; j++) {
3236  mpc_ast_add_child(r, as[i]->children[j]);
3237  }
3239  }
3240 
3241  }
3242 
3243  if (r->children_num) {
3244  r->state = r->children[0]->state;
3245  }
3246 
3247  return r;
3248 }
3249 
3251  mpc_ast_t *a = mpc_ast_new("", c);
3252  free(c);
3253  return a;
3254 }
3255 
3257  mpc_state_t *s = ((mpc_state_t**)xs)[0];
3258  mpc_ast_t *a = ((mpc_ast_t**)xs)[1];
3259  (void)n;
3260  a = mpc_ast_state(a, *s);
3261  free(s);
3262  return a;
3263 }
3264 
3266  return mpc_and(2, mpcf_state_ast, mpc_state(), a, free);
3267 }
3268 
3270  return mpc_apply_to(a, (mpc_apply_to_t)mpc_ast_tag, (void*)t);
3271 }
3272 
3274  return mpc_apply_to(a, (mpc_apply_to_t)mpc_ast_add_tag, (void*)t);
3275 }
3276 
3279 }
3280 
3286 
3287 mpc_parser_t *mpca_or(int n, ...) {
3288 
3289  int i;
3290  va_list va;
3291 
3293 
3294  p->type = MPC_TYPE_OR;
3295  p->data.or.n = n;
3296  p->data.or.xs = malloc(sizeof(mpc_parser_t*) * n);
3297 
3298  va_start(va, n);
3299  for (i = 0; i < n; i++) {
3300  p->data.or.xs[i] = va_arg(va, mpc_parser_t*);
3301  }
3302  va_end(va);
3303 
3304  return p;
3305 
3306 }
3307 
3309 
3310  int i;
3311  va_list va;
3312 
3314 
3315  p->type = MPC_TYPE_AND;
3316  p->data.and.n = n;
3317  p->data.and.f = mpcf_fold_ast;
3318  p->data.and.xs = malloc(sizeof(mpc_parser_t*) * n);
3319  p->data.and.dxs = malloc(sizeof(mpc_dtor_t) * (n-1));
3320 
3321  va_start(va, n);
3322  for (i = 0; i < n; i++) {
3323  p->data.and.xs[i] = va_arg(va, mpc_parser_t*);
3324  }
3325  for (i = 0; i < (n-1); i++) {
3326  p->data.and.dxs[i] = (mpc_dtor_t)mpc_ast_delete;
3327  }
3328  va_end(va);
3329 
3330  return p;
3331 }
3332 
3334 
3335 /*
3336 ** Grammar Parser
3337 */
3338 
3339 /*
3340 ** This is another interesting bootstrapping.
3341 **
3342 ** Having a general purpose AST type allows
3343 ** users to specify the grammar alone and
3344 ** let all fold rules be automatically taken
3345 ** care of by existing functions.
3346 **
3347 ** You don't get to control the type spat
3348 ** out but this means you can make a nice
3349 ** parser to take in some grammar in nice
3350 ** syntax and spit out a parser that works.
3351 **
3352 ** The grammar for this looks surprisingly
3353 ** like regex but the main difference is that
3354 ** it is now whitespace insensitive and the
3355 ** base type takes literals of some form.
3356 */
3357 
3358 /*
3359 **
3360 ** ### Grammar Grammar
3361 **
3362 ** <grammar> : (<term> "|" <grammar>) | <term>
3363 **
3364 ** <term> : <factor>*
3365 **
3366 ** <factor> : <base>
3367 ** | <base> "*"
3368 ** | <base> "+"
3369 ** | <base> "?"
3370 ** | <base> "{" <digits> "}"
3371 **
3372 ** <base> : "<" (<digits> | <ident>) ">"
3373 ** | <string_lit>
3374 ** | <char_lit>
3375 ** | <regex_lit> <regex_mode>
3376 ** | "(" <grammar> ")"
3377 */
3378 
3379 typedef struct {
3380  va_list *va;
3383  int flags;
3385 
3387  (void) n;
3388  if (xs[1] == NULL) { return xs[0]; }
3389  else { return mpca_or(2, xs[0], xs[1]); }
3390 }
3391 
3393  int i;
3394  mpc_parser_t *p = mpc_pass();
3395  for (i = 0; i < n; i++) {
3396  if (xs[i] != NULL) { p = mpca_and(2, p, xs[i]); }
3397  }
3398  return p;
3399 }
3400 
3402  int num;
3403  (void) n;
3404  if (xs[1] == NULL) { return xs[0]; }
3405  switch(((char*)xs[1])[0])
3406  {
3407  case '*': { free(xs[1]); return mpca_many(xs[0]); }; break;
3408  case '+': { free(xs[1]); return mpca_many1(xs[0]); }; break;
3409  case '?': { free(xs[1]); return mpca_maybe(xs[0]); }; break;
3410  case '!': { free(xs[1]); return mpca_not(xs[0]); }; break;
3411  default:
3412  num = *((int*)xs[1]);
3413  free(xs[1]);
3414  }
3415  return mpca_count(num, xs[0]);
3416 }
3417 
3419  mpca_grammar_st_t *st = s;
3420  char *y = mpcf_unescape(x);
3422  free(y);
3423  return mpca_state(mpca_tag(mpc_apply(p, mpcf_str_ast), "string"));
3424 }
3425 
3427  mpca_grammar_st_t *st = s;
3428  char *y = mpcf_unescape(x);
3430  free(y);
3431  return mpca_state(mpca_tag(mpc_apply(p, mpcf_str_ast), "char"));
3432 }
3433 
3435  char *y = xs[0];
3436  char *m = xs[1];
3437  mpca_grammar_st_t *st = xs[2];
3438  mpc_parser_t *p;
3439  int mode = MPC_RE_DEFAULT;
3440 
3441  (void)n;
3442  if (strchr(m, 'm')) { mode |= MPC_RE_MULTILINE; }
3443  if (strchr(m, 's')) { mode |= MPC_RE_DOTALL; }
3444  y = mpcf_unescape_regex(y);
3446  free(y);
3447  free(m);
3448 
3449  return mpca_state(mpca_tag(mpc_apply(p, mpcf_str_ast), "regex"));
3450 }
3451 
3452 /* Should this just use `isdigit` instead? */
3453 static int is_number(const char* s) {
3454  size_t i;
3455  for (i = 0; i < strlen(s); i++) { if (!strchr("0123456789", s[i])) { return 0; } }
3456  return 1;
3457 }
3458 
3460 
3461  int i;
3462  mpc_parser_t *p;
3463 
3464  /* Case of Number */
3465  if (is_number(x)) {
3466 
3467  i = strtol(x, NULL, 10);
3468 
3469  while (st->parsers_num <= i) {
3470  st->parsers_num++;
3471  st->parsers = realloc(st->parsers, sizeof(mpc_parser_t*) * st->parsers_num);
3472  st->parsers[st->parsers_num-1] = va_arg(*st->va, mpc_parser_t*);
3473  if (st->parsers[st->parsers_num-1] == NULL) {
3474  return mpc_failf("No Parser in position %i! Only supplied %i Parsers!", i, st->parsers_num);
3475  }
3476  }
3477 
3478  return st->parsers[st->parsers_num-1];
3479 
3480  /* Case of Identifier */
3481  } else {
3482 
3483  /* Search Existing Parsers */
3484  for (i = 0; i < st->parsers_num; i++) {
3485  mpc_parser_t *q = st->parsers[i];
3486  if (q == NULL) { return mpc_failf("Unknown Parser '%s'!", x); }
3487  if (q->name && strcmp(q->name, x) == 0) { return q; }
3488  }
3489 
3490  /* Search New Parsers */
3491  while (1) {
3492 
3493  p = va_arg(*st->va, mpc_parser_t*);
3494 
3495  st->parsers_num++;
3496  st->parsers = realloc(st->parsers, sizeof(mpc_parser_t*) * st->parsers_num);
3497  st->parsers[st->parsers_num-1] = p;
3498 
3499  if (p == NULL || p->name == NULL) { return mpc_failf("Unknown Parser '%s'!", x); }
3500  if (p->name && strcmp(p->name, x) == 0) { return p; }
3501 
3502  }
3503 
3504  }
3505 
3506 }
3507 
3509 
3510  mpca_grammar_st_t *st = s;
3512  free(x);
3513 
3514  if (p->name) {
3515  return mpca_state(mpca_root(mpca_add_tag(p, p->name)));
3516  } else {
3517  return mpca_state(mpca_root(p));
3518  }
3519 }
3520 
3521 mpc_parser_t *mpca_grammar_st(const char *grammar, mpca_grammar_st_t *st) {
3522 
3523  char *err_msg;
3524  mpc_parser_t *err_out;
3525  mpc_result_t r;
3526  mpc_parser_t *GrammarTotal, *Grammar, *Term, *Factor, *Base;
3527 
3528  GrammarTotal = mpc_new("grammar_total");
3529  Grammar = mpc_new("grammar");
3530  Term = mpc_new("term");
3531  Factor = mpc_new("factor");
3532  Base = mpc_new("base");
3533 
3534  mpc_define(GrammarTotal,
3536  );
3537 
3538  mpc_define(Grammar, mpc_and(2, mpcaf_grammar_or,
3539  Term,
3540  mpc_maybe(mpc_and(2, mpcf_snd_free, mpc_sym("|"), Grammar, free)),
3542  ));
3543 
3544  mpc_define(Term, mpc_many1(mpcaf_grammar_and, Factor));
3545 
3547  Base,
3548  mpc_or(6,
3549  mpc_sym("*"),
3550  mpc_sym("+"),
3551  mpc_sym("?"),
3552  mpc_sym("!"),
3554  mpc_pass()),
3556  ));
3557 
3558  mpc_define(Base, mpc_or(5,
3564  ));
3565 
3566  mpc_optimise(GrammarTotal);
3567  mpc_optimise(Grammar);
3568  mpc_optimise(Factor);
3569  mpc_optimise(Term);
3570  mpc_optimise(Base);
3571 
3572  if(!mpc_parse("<mpc_grammar_compiler>", grammar, GrammarTotal, &r)) {
3573  err_msg = mpc_err_string(r.error);
3574  err_out = mpc_failf("Invalid Grammar: %s", err_msg);
3575  mpc_err_delete(r.error);
3576  free(err_msg);
3577  r.output = err_out;
3578  }
3579 
3580  mpc_cleanup(5, GrammarTotal, Grammar, Term, Factor, Base);
3581 
3582  mpc_optimise(r.output);
3583 
3584  return (st->flags & MPCA_LANG_PREDICTIVE) ? mpc_predictive(r.output) : r.output;
3585 
3586 }
3587 
3588 mpc_parser_t *mpca_grammar(int flags, const char *grammar, ...) {
3589  mpca_grammar_st_t st;
3590  mpc_parser_t *res;
3591  va_list va;
3592  va_start(va, grammar);
3593 
3594  st.va = &va;
3595  st.parsers_num = 0;
3596  st.parsers = NULL;
3597  st.flags = flags;
3598 
3599  res = mpca_grammar_st(grammar, &st);
3600  free(st.parsers);
3601  va_end(va);
3602  return res;
3603 }
3604 
3605 typedef struct {
3606  char *ident;
3607  char *name;
3609 } mpca_stmt_t;
3610 
3611 static mpc_val_t *mpca_stmt_afold(int n, mpc_val_t **xs) {
3612  mpca_stmt_t *stmt = malloc(sizeof(mpca_stmt_t));
3613  stmt->ident = ((char**)xs)[0];
3614  stmt->name = ((char**)xs)[1];
3615  stmt->grammar = ((mpc_parser_t**)xs)[3];
3616  (void) n;
3617  free(((char**)xs)[2]);
3618  free(((char**)xs)[4]);
3619 
3620  return stmt;
3621 }
3622 
3623 static mpc_val_t *mpca_stmt_fold(int n, mpc_val_t **xs) {
3624 
3625  int i;
3626  mpca_stmt_t **stmts = malloc(sizeof(mpca_stmt_t*) * (n+1));
3627 
3628  for (i = 0; i < n; i++) {
3629  stmts[i] = xs[i];
3630  }
3631  stmts[n] = NULL;
3632 
3633  return stmts;
3634 }
3635 
3637 
3638  mpca_stmt_t **stmts = x;
3639 
3640  while(*stmts) {
3641  mpca_stmt_t *stmt = *stmts;
3642  free(stmt->ident);
3643  free(stmt->name);
3644  mpc_soft_delete(stmt->grammar);
3645  free(stmt);
3646  stmts++;
3647  }
3648  free(x);
3649 
3650 }
3651 
3653 
3654  mpca_grammar_st_t *st = s;
3655  mpca_stmt_t *stmt;
3656  mpca_stmt_t **stmts = x;
3657  mpc_parser_t *left;
3658 
3659  while(*stmts) {
3660  stmt = *stmts;
3661  left = mpca_grammar_find_parser(stmt->ident, st);
3662  if (st->flags & MPCA_LANG_PREDICTIVE) { stmt->grammar = mpc_predictive(stmt->grammar); }
3663  if (stmt->name) { stmt->grammar = mpc_expect(stmt->grammar, stmt->name); }
3664  mpc_optimise(stmt->grammar);
3665  mpc_define(left, stmt->grammar);
3666  free(stmt->ident);
3667  free(stmt->name);
3668  free(stmt);
3669  stmts++;
3670  }
3671 
3672  free(x);
3673 
3674  return NULL;
3675 }
3676 
3678 
3679  mpc_result_t r;
3680  mpc_err_t *e;
3681  mpc_parser_t *Lang, *Stmt, *Grammar, *Term, *Factor, *Base;
3682 
3683  Lang = mpc_new("lang");
3684  Stmt = mpc_new("stmt");
3685  Grammar = mpc_new("grammar");
3686  Term = mpc_new("term");
3687  Factor = mpc_new("factor");
3688  Base = mpc_new("base");
3689 
3690  mpc_define(Lang, mpc_apply_to(
3693  ));
3694 
3696  mpc_tok(mpc_ident()), mpc_maybe(mpc_tok(mpc_string_lit())), mpc_sym(":"), Grammar, mpc_sym(";"),
3698  ));
3699 
3700  mpc_define(Grammar, mpc_and(2, mpcaf_grammar_or,
3701  Term,
3702  mpc_maybe(mpc_and(2, mpcf_snd_free, mpc_sym("|"), Grammar, free)),
3704  ));
3705 
3706  mpc_define(Term, mpc_many1(mpcaf_grammar_and, Factor));
3707 
3709  Base,
3710  mpc_or(6,
3711  mpc_sym("*"),
3712  mpc_sym("+"),
3713  mpc_sym("?"),
3714  mpc_sym("!"),
3716  mpc_pass()),
3718  ));
3719 
3720  mpc_define(Base, mpc_or(5,
3726  ));
3727 
3728  mpc_optimise(Lang);
3729  mpc_optimise(Stmt);
3730  mpc_optimise(Grammar);
3731  mpc_optimise(Term);
3732  mpc_optimise(Factor);
3733  mpc_optimise(Base);
3734 
3735  if (!mpc_parse_input(i, Lang, &r)) {
3736  e = r.error;
3737  } else {
3738  e = NULL;
3739  }
3740 
3741  mpc_cleanup(6, Lang, Stmt, Grammar, Term, Factor, Base);
3742 
3743  return e;
3744 }
3745 
3747  mpca_grammar_st_t st;
3748  mpc_input_t *i;
3749  mpc_err_t *err;
3750 
3751  va_list va;
3752  va_start(va, f);
3753 
3754  st.va = &va;
3755  st.parsers_num = 0;
3756  st.parsers = NULL;
3757  st.flags = flags;
3758 
3759  i = mpc_input_new_file("<mpca_lang_file>", f);
3760  err = mpca_lang_st(i, &st);
3762 
3763  free(st.parsers);
3764  va_end(va);
3765  return err;
3766 }
3767 
3769  mpca_grammar_st_t st;
3770  mpc_input_t *i;
3771  mpc_err_t *err;
3772 
3773  va_list va;
3774  va_start(va, p);
3775 
3776  st.va = &va;
3777  st.parsers_num = 0;
3778  st.parsers = NULL;
3779  st.flags = flags;
3780 
3781  i = mpc_input_new_pipe("<mpca_lang_pipe>", p);
3782  err = mpca_lang_st(i, &st);
3784 
3785  free(st.parsers);
3786  va_end(va);
3787  return err;
3788 }
3789 
3790 mpc_err_t *mpca_lang(int flags, const char *language, ...) {
3791 
3792  mpca_grammar_st_t st;
3793  mpc_input_t *i;
3794  mpc_err_t *err;
3795 
3796  va_list va;
3797  va_start(va, language);
3798 
3799  st.va = &va;
3800  st.parsers_num = 0;
3801  st.parsers = NULL;
3802  st.flags = flags;
3803 
3804  i = mpc_input_new_string("<mpca_lang>", language);
3805  err = mpca_lang_st(i, &st);
3807 
3808  free(st.parsers);
3809  va_end(va);
3810  return err;
3811 }
3812 
3813 mpc_err_t *mpca_lang_contents(int flags, const char *filename, ...) {
3814 
3815  mpca_grammar_st_t st;
3816  mpc_input_t *i;
3817  mpc_err_t *err;
3818 
3819  va_list va;
3820 
3821  FILE *f = fopen(filename, "rb");
3822 
3823  if (f == NULL) {
3824  err = mpc_err_file(filename, "Unable to open file!");
3825  return err;
3826  }
3827 
3828  va_start(va, filename);
3829 
3830  st.va = &va;
3831  st.parsers_num = 0;
3832  st.parsers = NULL;
3833  st.flags = flags;
3834 
3836  err = mpca_lang_st(i, &st);
3838 
3839  free(st.parsers);
3840  va_end(va);
3841 
3842  fclose(f);
3843 
3844  return err;
3845 }
3846 
3847 static int mpc_nodecount_unretained(mpc_parser_t* p, int force) {
3848 
3849  int i, total;
3850 
3851  if (p->retained && !force) { return 0; }
3852 
3853  if (p->type == MPC_TYPE_EXPECT) { return 1 + mpc_nodecount_unretained(p->data.expect.x, 0); }
3854 
3855  if (p->type == MPC_TYPE_APPLY) { return 1 + mpc_nodecount_unretained(p->data.apply.x, 0); }
3856  if (p->type == MPC_TYPE_APPLY_TO) { return 1 + mpc_nodecount_unretained(p->data.apply_to.x, 0); }
3857  if (p->type == MPC_TYPE_PREDICT) { return 1 + mpc_nodecount_unretained(p->data.predict.x, 0); }
3858 
3859  if (p->type == MPC_TYPE_CHECK) { return 1 + mpc_nodecount_unretained(p->data.check.x, 0); }
3860  if (p->type == MPC_TYPE_CHECK_WITH) { return 1 + mpc_nodecount_unretained(p->data.check_with.x, 0); }
3861 
3862  if (p->type == MPC_TYPE_NOT) { return 1 + mpc_nodecount_unretained(p->data.not.x, 0); }
3863  if (p->type == MPC_TYPE_MAYBE) { return 1 + mpc_nodecount_unretained(p->data.not.x, 0); }
3864 
3865  if (p->type == MPC_TYPE_MANY) { return 1 + mpc_nodecount_unretained(p->data.repeat.x, 0); }
3866  if (p->type == MPC_TYPE_MANY1) { return 1 + mpc_nodecount_unretained(p->data.repeat.x, 0); }
3867  if (p->type == MPC_TYPE_COUNT) { return 1 + mpc_nodecount_unretained(p->data.repeat.x, 0); }
3868 
3869  if (p->type == MPC_TYPE_OR) {
3870  total = 1;
3871  for(i = 0; i < p->data.or.n; i++) {
3872  total += mpc_nodecount_unretained(p->data.or.xs[i], 0);
3873  }
3874  return total;
3875  }
3876 
3877  if (p->type == MPC_TYPE_AND) {
3878  total = 1;
3879  for(i = 0; i < p->data.and.n; i++) {
3880  total += mpc_nodecount_unretained(p->data.and.xs[i], 0);
3881  }
3882  return total;
3883  }
3884 
3885  return 1;
3886 
3887 }
3888 
3890  printf("Stats\n");
3891  printf("=====\n");
3892  printf("Node Count: %i\n", mpc_nodecount_unretained(p, 1));
3893 }
3894 
3895 static void mpc_optimise_unretained(mpc_parser_t *p, int force) {
3896 
3897  int i, n, m;
3898  mpc_parser_t *t;
3899 
3900  if (p->retained && !force) { return; }
3901 
3902  /* Optimise Subexpressions */
3903 
3904  if (p->type == MPC_TYPE_EXPECT) { mpc_optimise_unretained(p->data.expect.x, 0); }
3905  if (p->type == MPC_TYPE_APPLY) { mpc_optimise_unretained(p->data.apply.x, 0); }
3906  if (p->type == MPC_TYPE_APPLY_TO) { mpc_optimise_unretained(p->data.apply_to.x, 0); }
3907  if (p->type == MPC_TYPE_CHECK) { mpc_optimise_unretained(p->data.check.x, 0); }
3908  if (p->type == MPC_TYPE_CHECK_WITH) { mpc_optimise_unretained(p->data.check_with.x, 0); }
3909  if (p->type == MPC_TYPE_PREDICT) { mpc_optimise_unretained(p->data.predict.x, 0); }
3910  if (p->type == MPC_TYPE_NOT) { mpc_optimise_unretained(p->data.not.x, 0); }
3911  if (p->type == MPC_TYPE_MAYBE) { mpc_optimise_unretained(p->data.not.x, 0); }
3912  if (p->type == MPC_TYPE_MANY) { mpc_optimise_unretained(p->data.repeat.x, 0); }
3913  if (p->type == MPC_TYPE_MANY1) { mpc_optimise_unretained(p->data.repeat.x, 0); }
3914  if (p->type == MPC_TYPE_COUNT) { mpc_optimise_unretained(p->data.repeat.x, 0); }
3915 
3916  if (p->type == MPC_TYPE_OR) {
3917  for(i = 0; i < p->data.or.n; i++) {
3918  mpc_optimise_unretained(p->data.or.xs[i], 0);
3919  }
3920  }
3921 
3922  if (p->type == MPC_TYPE_AND) {
3923  for(i = 0; i < p->data.and.n; i++) {
3924  mpc_optimise_unretained(p->data.and.xs[i], 0);
3925  }
3926  }
3927 
3928  /* Perform optimisations */
3929 
3930  while (1) {
3931 
3932  /* Merge rhs `or` */
3933  if (p->type == MPC_TYPE_OR
3934  && p->data.or.xs[p->data.or.n-1]->type == MPC_TYPE_OR
3935  && !p->data.or.xs[p->data.or.n-1]->retained) {
3936  t = p->data.or.xs[p->data.or.n-1];
3937  n = p->data.or.n; m = t->data.or.n;
3938  p->data.or.n = n + m - 1;
3939  p->data.or.xs = realloc(p->data.or.xs, sizeof(mpc_parser_t*) * (n + m -1));
3940  memmove(p->data.or.xs + n - 1, t->data.or.xs, m * sizeof(mpc_parser_t*));
3941  free(t->data.or.xs); free(t->name); free(t);
3942  continue;
3943  }
3944 
3945  /* Merge lhs `or` */
3946  if (p->type == MPC_TYPE_OR
3947  && p->data.or.xs[0]->type == MPC_TYPE_OR
3948  && !p->data.or.xs[0]->retained) {
3949  t = p->data.or.xs[0];
3950  n = p->data.or.n; m = t->data.or.n;
3951  p->data.or.n = n + m - 1;
3952  p->data.or.xs = realloc(p->data.or.xs, sizeof(mpc_parser_t*) * (n + m -1));
3953  memmove(p->data.or.xs + m, p->data.or.xs + 1, (n - 1) * sizeof(mpc_parser_t*));
3954  memmove(p->data.or.xs, t->data.or.xs, m * sizeof(mpc_parser_t*));
3955  free(t->data.or.xs); free(t->name); free(t);
3956  continue;
3957  }
3958 
3959  /* Remove ast `pass` */
3960  if (p->type == MPC_TYPE_AND
3961  && p->data.and.n == 2
3962  && p->data.and.xs[0]->type == MPC_TYPE_PASS
3963  && !p->data.and.xs[0]->retained
3964  && p->data.and.f == mpcf_fold_ast) {
3965  t = p->data.and.xs[1];
3966  mpc_delete(p->data.and.xs[0]);
3967  free(p->data.and.xs); free(p->data.and.dxs); free(p->name);
3968  memcpy(p, t, sizeof(mpc_parser_t));
3969  free(t);
3970  continue;
3971  }
3972 
3973  /* Merge ast lhs `and` */
3974  if (p->type == MPC_TYPE_AND
3975  && p->data.and.f == mpcf_fold_ast
3976  && p->data.and.xs[0]->type == MPC_TYPE_AND
3977  && !p->data.and.xs[0]->retained
3978  && p->data.and.xs[0]->data.and.f == mpcf_fold_ast) {
3979  t = p->data.and.xs[0];
3980  n = p->data.and.n; m = t->data.and.n;
3981  p->data.and.n = n + m - 1;
3982  p->data.and.xs = realloc(p->data.and.xs, sizeof(mpc_parser_t*) * (n + m - 1));
3983  p->data.and.dxs = realloc(p->data.and.dxs, sizeof(mpc_dtor_t) * (n + m - 1 - 1));
3984  memmove(p->data.and.xs + m, p->data.and.xs + 1, (n - 1) * sizeof(mpc_parser_t*));
3985  memmove(p->data.and.xs, t->data.and.xs, m * sizeof(mpc_parser_t*));
3986  for (i = 0; i < p->data.and.n-1; i++) { p->data.and.dxs[i] = (mpc_dtor_t)mpc_ast_delete; }
3987  free(t->data.and.xs); free(t->data.and.dxs); free(t->name); free(t);
3988  continue;
3989  }
3990 
3991  /* Merge ast rhs `and` */
3992  if (p->type == MPC_TYPE_AND
3993  && p->data.and.f == mpcf_fold_ast
3994  && p->data.and.xs[p->data.and.n-1]->type == MPC_TYPE_AND
3995  && !p->data.and.xs[p->data.and.n-1]->retained
3996  && p->data.and.xs[p->data.and.n-1]->data.and.f == mpcf_fold_ast) {
3997  t = p->data.and.xs[p->data.and.n-1];
3998  n = p->data.and.n; m = t->data.and.n;
3999  p->data.and.n = n + m - 1;
4000  p->data.and.xs = realloc(p->data.and.xs, sizeof(mpc_parser_t*) * (n + m -1));
4001  p->data.and.dxs = realloc(p->data.and.dxs, sizeof(mpc_dtor_t) * (n + m - 1 - 1));
4002  memmove(p->data.and.xs + n - 1, t->data.and.xs, m * sizeof(mpc_parser_t*));
4003  for (i = 0; i < p->data.and.n-1; i++) { p->data.and.dxs[i] = (mpc_dtor_t)mpc_ast_delete; }
4004  free(t->data.and.xs); free(t->data.and.dxs); free(t->name); free(t);
4005  continue;
4006  }
4007 
4008  /* Remove re `lift` */
4009  if (p->type == MPC_TYPE_AND
4010  && p->data.and.n == 2
4011  && p->data.and.xs[0]->type == MPC_TYPE_LIFT
4012  && p->data.and.xs[0]->data.lift.lf == mpcf_ctor_str
4013  && !p->data.and.xs[0]->retained
4014  && p->data.and.f == mpcf_strfold) {
4015  t = p->data.and.xs[1];
4016  mpc_delete(p->data.and.xs[0]);
4017  free(p->data.and.xs); free(p->data.and.dxs); free(p->name);
4018  memcpy(p, t, sizeof(mpc_parser_t));
4019  free(t);
4020  continue;
4021  }
4022 
4023  /* Merge re lhs `and` */
4024  if (p->type == MPC_TYPE_AND
4025  && p->data.and.f == mpcf_strfold
4026  && p->data.and.xs[0]->type == MPC_TYPE_AND
4027  && !p->data.and.xs[0]->retained
4028  && p->data.and.xs[0]->data.and.f == mpcf_strfold) {
4029  t = p->data.and.xs[0];
4030  n = p->data.and.n; m = t->data.and.n;
4031  p->data.and.n = n + m - 1;
4032  p->data.and.xs = realloc(p->data.and.xs, sizeof(mpc_parser_t*) * (n + m - 1));
4033  p->data.and.dxs = realloc(p->data.and.dxs, sizeof(mpc_dtor_t) * (n + m - 1 - 1));
4034  memmove(p->data.and.xs + m, p->data.and.xs + 1, (n - 1) * sizeof(mpc_parser_t*));
4035  memmove(p->data.and.xs, t->data.and.xs, m * sizeof(mpc_parser_t*));
4036  for (i = 0; i < p->data.and.n-1; i++) { p->data.and.dxs[i] = free; }
4037  free(t->data.and.xs); free(t->data.and.dxs); free(t->name); free(t);
4038  continue;
4039  }
4040 
4041  /* Merge re rhs `and` */
4042  if (p->type == MPC_TYPE_AND
4043  && p->data.and.f == mpcf_strfold
4044  && p->data.and.xs[p->data.and.n-1]->type == MPC_TYPE_AND
4045  && !p->data.and.xs[p->data.and.n-1]->retained
4046  && p->data.and.xs[p->data.and.n-1]->data.and.f == mpcf_strfold) {
4047  t = p->data.and.xs[p->data.and.n-1];
4048  n = p->data.and.n; m = t->data.and.n;
4049  p->data.and.n = n + m - 1;
4050  p->data.and.xs = realloc(p->data.and.xs, sizeof(mpc_parser_t*) * (n + m -1));
4051  p->data.and.dxs = realloc(p->data.and.dxs, sizeof(mpc_dtor_t) * (n + m - 1 - 1));
4052  memmove(p->data.and.xs + n - 1, t->data.and.xs, m * sizeof(mpc_parser_t*));
4053  for (i = 0; i < p->data.and.n-1; i++) { p->data.and.dxs[i] = free; }
4054  free(t->data.and.xs); free(t->data.and.dxs); free(t->name); free(t);
4055  continue;
4056  }
4057 
4058  return;
4059 
4060  }
4061 
4062 }
4063 
4066 }
4067 
#define e(frag)
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
struct buffer buffer
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags long
Definition: sflib.h:79
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
const char * k
Definition: dsignal.c:11
int max
Definition: enough.c:225
unsigned short prefix[65536]
Definition: gun.c:163
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
const char * filename
Definition: ioapi.h:137
const char int mode
Definition: ioapi.h:137
vsprintf
Definition: kernel.h:367
sprintf
Definition: kernel.h:365
return memset(p, 0, total)
void * p
Definition: libc.cpp:67
void * mem
Definition: libc.cpp:91
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void list(RzEgg *egg)
Definition: rz-gg.c:52
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname pipe
Definition: sflib.h:73
#define expect(expr, value)
Definition: lz4.c:170
int x
Definition: mipsasm.c:20
int n
Definition: mipsasm.c:19
static int mpc_input_terminated(mpc_input_t *i)
Definition: mpc.c:431
char * mpc_err_string(mpc_err_t *x)
Definition: mpc.c:630
mpc_parser_t * mpc_whitespace(void)
Definition: mpc.c:2008
mpc_parser_t * mpc_define(mpc_parser_t *p, mpc_parser_t *a)
Definition: mpc.c:1580
mpc_parser_t * mpca_not(mpc_parser_t *a)
Definition: mpc.c:3281
mpc_parser_t * mpc_lift_val(mpc_val_t *x)
Definition: mpc.c:1663
static mpc_err_t * mpc_err_merge(mpc_input_t *i, mpc_err_t *x, mpc_err_t *y)
Definition: mpc.c:872
void mpc_optimise(mpc_parser_t *p)
Definition: mpc.c:4064
mpc_parser_t * mpc_real(void)
Definition: mpc.c:2034
mpc_parser_t * mpc_char(char c)
Definition: mpc.c:1747
mpc_ast_t * mpc_ast_get_child(mpc_ast_t *ast, const char *tag)
Definition: mpc.c:3060
static mpc_err_t * mpc_err_file(const char *filename, const char *failure)
Definition: mpc.c:697
mpc_parser_t * mpc_re(const char *re)
Definition: mpc.c:2353
static mpc_val_t * mpcf_input_fst_free(mpc_input_t *i, int n, mpc_val_t **xs)
Definition: mpc.c:971
static mpc_parser_t * mpc_re_escape_char(char c)
Definition: mpc.c:2197
mpc_parser_t * mpca_add_tag(mpc_parser_t *a, const char *t)
Definition: mpc.c:3273
mpc_err_t * mpca_lang(int flags, const char *language,...)
Definition: mpc.c:3790
mpc_parser_t * mpc_noneof(const char *s)
Definition: mpc.c:1770
mpc_parser_t * mpc_escape(void)
Definition: mpc.c:2014
#define MPC_MAX_RECURSION_DEPTH
Definition: mpc.c:1045
static void mpc_soft_delete(mpc_val_t *x)
Definition: mpc.c:1468
mpc_val_t * mpcf_trd(int n, mpc_val_t **xs)
Definition: mpc.c:2627
mpc_parser_t * mpc_fail(const char *m)
Definition: mpc.c:1616
static mpc_val_t * mpcaf_grammar_string(mpc_val_t *x, void *s)
Definition: mpc.c:3418
#define MPC_SUCCESS(x)
Definition: mpc.c:1039
static int mpc_input_soi(mpc_input_t *i, char **o)
Definition: mpc.c:548
mpc_parser_t * mpc_undefine(mpc_parser_t *p)
Definition: mpc.c:1574
static void mpc_input_suppress_disable(mpc_input_t *i)
Definition: mpc.c:298
@ MPC_INPUT_MEM_NUM
Definition: mpc.c:71
void mpcf_dtor_null(mpc_val_t *x)
Definition: mpc.c:2428
static mpc_val_t * mpcf_re_or(int n, mpc_val_t **xs)
Definition: mpc.c:2165
mpc_parser_t * mpc_check_withf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *fmt,...)
Definition: mpc.c:1854
mpc_ast_t * mpc_ast_get_child_lb(mpc_ast_t *ast, const char *tag, int lb)
Definition: mpc.c:3064
mpc_parser_t * mpc_lift(mpc_ctor_t lf)
Definition: mpc.c:1670
mpc_val_t * mpcf_float(mpc_val_t *x)
Definition: mpc.c:2455
mpc_parser_t * mpc_brackets(mpc_parser_t *a, mpc_dtor_t ad)
Definition: mpc.c:2101
mpc_val_t * mpcf_hex(mpc_val_t *x)
Definition: mpc.c:2441
static void mpc_err_delete_internal(mpc_input_t *i, mpc_err_t *x)
Definition: mpc.c:711
static void mpc_input_backtrack_enable(mpc_input_t *i)
Definition: mpc.c:296
mpc_parser_t * mpc_check_with(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *e)
Definition: mpc.c:1826
static void mpc_input_mark(mpc_input_t *i)
Definition: mpc.c:301
static int mpc_input_eoi(mpc_input_t *i, char **o)
Definition: mpc.c:553
static mpc_input_t * mpc_input_new_pipe(const char *filename, FILE *pipe)
Definition: mpc.c:163
static mpc_val_t * mpc_parse_apply(mpc_input_t *i, mpc_apply_t f, mpc_val_t *x)
Definition: mpc.c:1020
static int mpc_err_contains_expected(mpc_input_t *i, mpc_err_t *x, char *expected)
Definition: mpc.c:732
mpc_val_t * mpcf_ctor_str(void)
Definition: mpc.c:2431
mpc_parser_t * mpc_upper(void)
Definition: mpc.c:2024
static void mpc_parse_dtor(mpc_input_t *i, mpc_dtor_t d, mpc_val_t *x)
Definition: mpc.c:1030
mpc_parser_t * mpc_string(const char *s)
Definition: mpc.c:1786
static const char * mpc_escape_output_raw_cchar[]
Definition: mpc.c:2498
static void * mpc_realloc(mpc_input_t *i, void *p, size_t n)
Definition: mpc.c:270
mpc_parser_t * mpca_and(int n,...)
Definition: mpc.c:3308
mpc_parser_t * mpc_hex(void)
Definition: mpc.c:2030
mpc_val_t * mpcf_ctor_null(void)
Definition: mpc.c:2430
static mpc_val_t * mpcf_re_escape(mpc_val_t *x, void *data)
Definition: mpc.c:2219
mpc_parser_t * mpc_braces(mpc_parser_t *a, mpc_dtor_t ad)
Definition: mpc.c:2100
static const char * mpc_err_char_unescape(char c)
Definition: mpc.c:606
mpc_parser_t * mpc_parens(mpc_parser_t *a, mpc_dtor_t ad)
Definition: mpc.c:2099
mpc_parser_t * mpc_tok_parens(mpc_parser_t *a, mpc_dtor_t ad)
Definition: mpc.c:2110
static int mpc_input_string(mpc_input_t *i, const char *c, char **o)
Definition: mpc.c:524
static mpc_val_t * mpcaf_grammar_and(int n, mpc_val_t **xs)
Definition: mpc.c:3392
static mpc_input_t * mpc_input_new_string(const char *filename, const char *string)
Definition: mpc.c:103
static mpc_val_t * mpcf_input_snd_free(mpc_input_t *i, int n, mpc_val_t **xs)
Definition: mpc.c:972
static void * mpc_export(mpc_input_t *i, void *p)
Definition: mpc.c:286
mpc_parser_t * mpc_string_lit(void)
Definition: mpc.c:2060
static int mpc_input_anchor(mpc_input_t *i, int(*f)(char, char), char **o)
Definition: mpc.c:543
void mpc_ast_delete(mpc_ast_t *a)
Definition: mpc.c:2886
static void mpc_print_unretained(mpc_parser_t *p, int force)
Definition: mpc.c:2688
mpc_parser_t * mpc_failf(const char *fmt,...)
Definition: mpc.c:1644
mpc_parser_t * mpc_copy(mpc_parser_t *a)
Definition: mpc.c:1488
static const char * mpc_escape_output_raw_cstr[]
Definition: mpc.c:2495
mpc_parser_t * mpca_root(mpc_parser_t *a)
Definition: mpc.c:3277
int mpc_parse_pipe(const char *filename, FILE *pipe, mpc_parser_t *p, mpc_result_t *r)
Definition: mpc.c:1345
mpc_parser_t * mpc_digit(void)
Definition: mpc.c:2016
mpc_parser_t * mpc_oneof(const char *s)
Definition: mpc.c:1762
mpc_ast_t * mpc_ast_new(const char *tag, const char *contents)
Definition: mpc.c:2910
void mpc_err_print(mpc_err_t *x)
Definition: mpc.c:584
mpc_parser_t * mpc_blank(void)
Definition: mpc.c:2010
void mpc_ast_print(mpc_ast_t *a)
Definition: mpc.c:3036
static mpc_val_t * mpc_parse_apply_to(mpc_input_t *i, mpc_apply_to_t f, mpc_val_t *x, mpc_val_t *d)
Definition: mpc.c:1026
static int mpc_input_char(mpc_input_t *i, char c, char **o)
Definition: mpc.c:489
mpc_ast_t * mpc_ast_tag(mpc_ast_t *a, const char *t)
Definition: mpc.c:2998
mpc_val_t * mpcf_unescape(mpc_val_t *x)
Definition: mpc.c:2582
mpc_val_t * mpcf_strtrimr(mpc_val_t *x)
Definition: mpc.c:2470
mpc_parser_t * mpc_ident(void)
Definition: mpc.c:2070
mpc_parser_t * mpc_boundary_newline(void)
Definition: mpc.c:2006
static int mpc_parse_run(mpc_input_t *i, mpc_parser_t *p, mpc_result_t *r, mpc_err_t **e, int depth)
Definition: mpc.c:1047
static void mpc_input_rewind(mpc_input_t *i)
Definition: mpc.c:348
static void mpc_undefine_or(mpc_parser_t *p)
Definition: mpc.c:1375
mpc_parser_t * mpc_strip(mpc_parser_t *a)
Definition: mpc.c:2087
mpc_val_t * mpcf_maths(int n, mpc_val_t **xs)
Definition: mpc.c:2665
static mpc_val_t * mpcf_nth_free(int n, mpc_val_t **xs, int x)
Definition: mpc.c:2629
mpc_parser_t * mpc_apply(mpc_parser_t *a, mpc_apply_t f)
Definition: mpc.c:1798
mpc_parser_t * mpc_eoi(void)
Definition: mpc.c:1983
mpc_err_t * mpca_lang_contents(int flags, const char *filename,...)
Definition: mpc.c:3813
mpc_parser_t * mpc_int(void)
Definition: mpc.c:2029
mpc_parser_t * mpca_many(mpc_parser_t *a)
Definition: mpc.c:3283
mpc_val_t * mpcf_escape_char_raw(mpc_val_t *x)
Definition: mpc.c:2612
mpc_parser_t * mpca_maybe(mpc_parser_t *a)
Definition: mpc.c:3282
mpc_parser_t * mpc_satisfy(int(*f)(char))
Definition: mpc.c:1779
mpc_val_t * mpcf_all_free(int n, mpc_val_t **xs)
Definition: mpc.c:2640
static void mpc_input_delete(mpc_input_t *i)
Definition: mpc.c:219
mpc_parser_t * mpc_not(mpc_parser_t *a, mpc_dtor_t da)
Definition: mpc.c:1886
static mpc_parser_t * mpca_grammar_find_parser(char *x, mpca_grammar_st_t *st)
Definition: mpc.c:3459
static mpc_input_t * mpc_input_new_nstring(const char *filename, const char *string, size_t length)
Definition: mpc.c:132
mpc_val_t * mpcf_oct(mpc_val_t *x)
Definition: mpc.c:2448
mpc_ast_trav_t * mpc_ast_traverse_start(mpc_ast_t *ast, mpc_ast_trav_order_t order)
Definition: mpc.c:3076
int mpc_ast_get_index_lb(mpc_ast_t *ast, const char *tag, int lb)
Definition: mpc.c:3048
@ MPC_INPUT_MARKS_MIN
Definition: mpc.c:67
void mpc_stats(mpc_parser_t *p)
Definition: mpc.c:3889
int mpc_parse_contents(const char *filename, mpc_parser_t *p, mpc_result_t *r)
Definition: mpc.c:1353
static mpc_parser_t * mpc_undefined(void)
Definition: mpc.c:1472
mpc_parser_t * mpc_digits(void)
Definition: mpc.c:2019
mpc_parser_t * mpc_predictive(mpc_parser_t *a)
Definition: mpc.c:1870
static char mpc_input_getc(mpc_input_t *i)
Definition: mpc.c:370
mpc_parser_t * mpc_many(mpc_fold_t f, mpc_parser_t *a)
Definition: mpc.c:1902
mpc_parser_t * mpc_alpha(void)
Definition: mpc.c:2025
static int mpc_input_failure(mpc_input_t *i, char c)
Definition: mpc.c:435
static int mpc_input_success(mpc_input_t *i, char c, char **o)
Definition: mpc.c:455
static int mpc_boundary_anchor(char prev, char next)
Definition: mpc.c:1989
int mpc_nparse(const char *filename, const char *string, size_t length, mpc_parser_t *p, mpc_result_t *r)
Definition: mpc.c:1329
static mpc_err_t * mpc_err_or(mpc_input_t *i, mpc_err_t **x, int n)
Definition: mpc.c:749
mpc_parser_t * mpc_maybe_lift(mpc_parser_t *a, mpc_ctor_t lf)
Definition: mpc.c:1890
mpc_val_t * mpcf_escape_string_raw(mpc_val_t *x)
Definition: mpc.c:2600
mpc_parser_t * mpc_total(mpc_parser_t *a, mpc_dtor_t da)
Definition: mpc.c:2091
mpc_parser_t * mpca_count(int n, mpc_parser_t *a)
Definition: mpc.c:3285
mpc_parser_t * mpc_number(void)
Definition: mpc.c:2032
int mpc_parse(const char *filename, const char *string, mpc_parser_t *p, mpc_result_t *r)
Definition: mpc.c:1321
static void mpc_ast_delete_no_children(mpc_ast_t *a)
Definition: mpc.c:2903
static mpc_val_t * mpcf_re_range(mpc_val_t *x)
Definition: mpc.c:2290
static void mpc_err_string_cat(char *buffer, int *pos, int *max, char const *fmt,...)
Definition: mpc.c:594
mpc_parser_t * mpc_new(const char *name)
Definition: mpc.c:1480
mpc_parser_t * mpc_tab(void)
Definition: mpc.c:2013
mpc_parser_t * mpc_octdigit(void)
Definition: mpc.c:2018
static mpc_err_t * mpc_err_count(mpc_input_t *i, mpc_err_t *x, int n)
Definition: mpc.c:861
static mpc_val_t * mpcaf_grammar_id(mpc_val_t *x, void *s)
Definition: mpc.c:3508
mpc_parser_t * mpc_any(void)
Definition: mpc.c:1741
static int mpc_boundary_newline_anchor(char prev, char next)
Definition: mpc.c:2000
static mpc_state_t mpc_state_new(void)
Definition: mpc.c:16
mpc_parser_t * mpc_re_mode(const char *re, int mode)
Definition: mpc.c:2357
static mpc_err_t * mpc_err_many1(mpc_input_t *i, mpc_err_t *x)
Definition: mpc.c:857
mpc_parser_t * mpc_newline(void)
Definition: mpc.c:2012
mpc_val_t * mpcf_snd(int n, mpc_val_t **xs)
Definition: mpc.c:2626
static const char mpc_escape_input_raw_cstr[]
Definition: mpc.c:2494
static mpc_err_t * mpca_lang_st(mpc_input_t *i, mpca_grammar_st_t *st)
Definition: mpc.c:3677
mpc_parser_t * mpc_between(mpc_parser_t *a, mpc_dtor_t ad, const char *o, const char *c)
Definition: mpc.c:2093
mpc_val_t * mpcf_free(mpc_val_t *x)
Definition: mpc.c:2432
void mpc_delete(mpc_parser_t *p)
Definition: mpc.c:1453
mpc_parser_t * mpc_tok_between(mpc_parser_t *a, mpc_dtor_t ad, const char *o, const char *c)
Definition: mpc.c:2104
mpc_parser_t * mpc_not_lift(mpc_parser_t *a, mpc_dtor_t da, mpc_ctor_t lf)
Definition: mpc.c:1877
mpc_parser_t * mpc_check(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *e)
Definition: mpc.c:1815
mpc_val_t * mpcf_strtrim(mpc_val_t *x)
Definition: mpc.c:2479
mpc_parser_t * mpc_apply_to(mpc_parser_t *a, mpc_apply_to_t f, void *x)
Definition: mpc.c:1806
static mpc_val_t * mpcf_input_state_ast(mpc_input_t *i, int n, mpc_val_t **xs)
Definition: mpc.c:985
static int mpc_input_noneof(mpc_input_t *i, const char *c, char **o)
Definition: mpc.c:510
mpc_ast_t * mpc_ast_add_child(mpc_ast_t *r, mpc_ast_t *a)
Definition: mpc.c:2974
static mpc_val_t * mpcf_re_repeat(int n, mpc_val_t **xs)
Definition: mpc.c:2180
@ MPC_PARSE_STACK_MIN
Definition: mpc.c:1036
mpc_val_t * mpcf_state_ast(int n, mpc_val_t **xs)
Definition: mpc.c:3256
static mpc_input_t * mpc_input_new_file(const char *filename, FILE *file)
Definition: mpc.c:192
mpc_val_t * mpcf_fold_ast(int n, mpc_val_t **xs)
Definition: mpc.c:3212
void mpc_ast_traverse_free(mpc_ast_trav_t **trav)
Definition: mpc.c:3201
mpc_val_t * mpcf_escape(mpc_val_t *x)
Definition: mpc.c:2576
static mpc_val_t * mpc_parse_fold(mpc_input_t *i, mpc_fold_t f, int n, mpc_val_t **xs)
Definition: mpc.c:994
mpc_parser_t * mpc_expectf(mpc_parser_t *a, const char *fmt,...)
Definition: mpc.c:1719
mpc_val_t * mpcf_int(mpc_val_t *x)
Definition: mpc.c:2434
mpc_val_t * mpcf_escape_regex(mpc_val_t *x)
Definition: mpc.c:2588
mpc_parser_t * mpca_tag(mpc_parser_t *a, const char *t)
Definition: mpc.c:3269
mpc_parser_t * mpc_hexdigit(void)
Definition: mpc.c:2017
mpc_val_t * mpcf_fst(int n, mpc_val_t **xs)
Definition: mpc.c:2625
mpc_parser_t * mpc_startwith(mpc_parser_t *a)
Definition: mpc.c:2081
@ MPC_INPUT_FILE
Definition: mpc.c:62
@ MPC_INPUT_PIPE
Definition: mpc.c:63
@ MPC_INPUT_STRING
Definition: mpc.c:61
mpc_val_t * mpcf_strtriml(mpc_val_t *x)
Definition: mpc.c:2462
void mpc_ast_print_to(mpc_ast_t *a, FILE *fp)
Definition: mpc.c:3040
static void mpc_undefine_and(mpc_parser_t *p)
Definition: mpc.c:1385
mpc_val_t * mpcf_strfold(int n, mpc_val_t **xs)
Definition: mpc.c:2648
#define MPC_FAILURE(x)
Definition: mpc.c:1040
void mpc_err_print_to(mpc_err_t *x, FILE *f)
Definition: mpc.c:588
mpc_parser_t * mpc_many1(mpc_fold_t f, mpc_parser_t *a)
Definition: mpc.c:1910
static mpc_err_t * mpc_err_new(mpc_input_t *i, const char *expected)
Definition: mpc.c:666
static int mpc_mem_ptr(mpc_input_t *i, void *p)
Definition: mpc.c:231
mpc_parser_t * mpc_stripr(mpc_parser_t *a)
Definition: mpc.c:2086
mpc_parser_t * mpc_underscore(void)
Definition: mpc.c:2026
static const char mpc_escape_input_raw_re[]
Definition: mpc.c:2491
mpc_parser_t * mpca_or(int n,...)
Definition: mpc.c:3287
static const char mpc_escape_input_c[]
Definition: mpc.c:2483
mpc_parser_t * mpca_grammar_st(const char *grammar, mpca_grammar_st_t *st)
Definition: mpc.c:3521
#define MPC_PRIMITIVE(x)
Definition: mpc.c:1041
static mpc_val_t * mpca_stmt_afold(int n, mpc_val_t **xs)
Definition: mpc.c:3611
mpc_val_t * mpcf_null(int n, mpc_val_t **xs)
Definition: mpc.c:2624
mpc_parser_t * mpc_char_lit(void)
Definition: mpc.c:2056
static mpc_val_t * mpca_stmt_fold(int n, mpc_val_t **xs)
Definition: mpc.c:3623
mpc_parser_t * mpc_and(int n, mpc_fold_t f,...)
Definition: mpc.c:1948
mpc_ast_t * mpc_ast_build(int n, const char *tag,...)
Definition: mpc.c:2928
mpc_parser_t * mpc_tok_squares(mpc_parser_t *a, mpc_dtor_t ad)
Definition: mpc.c:2113
static mpc_val_t * mpcf_input_trd_free(mpc_input_t *i, int n, mpc_val_t **xs)
Definition: mpc.c:973
void mpc_print(mpc_parser_t *p)
Definition: mpc.c:2811
mpc_parser_t * mpc_squares(mpc_parser_t *a, mpc_dtor_t ad)
Definition: mpc.c:2102
mpc_val_t * mpcf_unescape_regex(mpc_val_t *x)
Definition: mpc.c:2594
static mpc_err_t * mpc_err_fail(mpc_input_t *i, const char *failure)
Definition: mpc.c:682
static void mpc_optimise_unretained(mpc_parser_t *p, int force)
Definition: mpc.c:3895
mpc_parser_t * mpc_stripl(mpc_parser_t *a)
Definition: mpc.c:2085
static mpc_val_t * mpcf_input_nth_free(mpc_input_t *i, int n, mpc_val_t **xs, int x)
Definition: mpc.c:965
mpc_parser_t * mpc_alphanum(void)
Definition: mpc.c:2027
mpc_ast_t * mpc_ast_add_root(mpc_ast_t *a)
Definition: mpc.c:2946
static int mpc_input_satisfy(mpc_input_t *i, int(*cond)(char), char **o)
Definition: mpc.c:517
mpc_err_t * mpca_lang_file(int flags, FILE *f,...)
Definition: mpc.c:3746
static int mpc_nodecount_unretained(mpc_parser_t *p, int force)
Definition: mpc.c:3847
static mpc_val_t * mpcf_re_and(int n, mpc_val_t **xs)
Definition: mpc.c:2171
mpc_val_t * mpcf_unescape_string_raw(mpc_val_t *x)
Definition: mpc.c:2606
mpc_val_t * mpcf_str_ast(mpc_val_t *c)
Definition: mpc.c:3250
static mpc_val_t * mpcaf_fold_regex(int n, mpc_val_t **xs)
Definition: mpc.c:3434
static mpc_val_t * mpcaf_grammar_repeat(int n, mpc_val_t **xs)
Definition: mpc.c:3401
@ MPC_TYPE_LIFT_VAL
Definition: mpc.c:888
@ MPC_TYPE_PASS
Definition: mpc.c:885
@ MPC_TYPE_SINGLE
Definition: mpc.c:894
@ MPC_TYPE_LIFT
Definition: mpc.c:887
@ MPC_TYPE_FAIL
Definition: mpc.c:886
@ MPC_TYPE_PREDICT
Definition: mpc.c:903
@ MPC_TYPE_NONEOF
Definition: mpc.c:896
@ MPC_TYPE_NOT
Definition: mpc.c:904
@ MPC_TYPE_SATISFY
Definition: mpc.c:898
@ MPC_TYPE_MAYBE
Definition: mpc.c:905
@ MPC_TYPE_STRING
Definition: mpc.c:899
@ MPC_TYPE_CHECK_WITH
Definition: mpc.c:914
@ MPC_TYPE_SOI
Definition: mpc.c:916
@ MPC_TYPE_RANGE
Definition: mpc.c:897
@ MPC_TYPE_UNDEFINED
Definition: mpc.c:884
@ MPC_TYPE_MANY
Definition: mpc.c:906
@ MPC_TYPE_COUNT
Definition: mpc.c:908
@ MPC_TYPE_EOI
Definition: mpc.c:917
@ MPC_TYPE_APPLY
Definition: mpc.c:901
@ MPC_TYPE_CHECK
Definition: mpc.c:913
@ MPC_TYPE_APPLY_TO
Definition: mpc.c:902
@ MPC_TYPE_EXPECT
Definition: mpc.c:889
@ MPC_TYPE_ANCHOR
Definition: mpc.c:890
@ MPC_TYPE_OR
Definition: mpc.c:910
@ MPC_TYPE_ANY
Definition: mpc.c:893
@ MPC_TYPE_STATE
Definition: mpc.c:891
@ MPC_TYPE_ONEOF
Definition: mpc.c:895
@ MPC_TYPE_AND
Definition: mpc.c:911
@ MPC_TYPE_MANY1
Definition: mpc.c:907
int mpc_parse_input(mpc_input_t *i, mpc_parser_t *p, mpc_result_t *r)
Definition: mpc.c:1307
mpc_parser_t * mpc_float(void)
Definition: mpc.c:2052
int mpc_ast_eq(mpc_ast_t *a, mpc_ast_t *b)
Definition: mpc.c:2959
mpc_parser_t * mpc_whitespaces(void)
Definition: mpc.c:2009
static mpc_val_t * mpcf_input_str_ast(mpc_input_t *i, mpc_val_t *c)
Definition: mpc.c:1014
static mpc_state_t * mpc_input_state_copy(mpc_input_t *i)
Definition: mpc.c:565
mpc_parser_t * mpc_endwith(mpc_parser_t *a, mpc_dtor_t da)
Definition: mpc.c:2082
mpc_parser_t * mpca_state(mpc_parser_t *a)
Definition: mpc.c:3265
void mpc_cleanup(int n,...)
Definition: mpc.c:1596
mpc_ast_t * mpc_ast_state(mpc_ast_t *a, mpc_state_t s)
Definition: mpc.c:3004
static int mpc_input_oneof(mpc_input_t *i, const char *c, char **o)
Definition: mpc.c:503
mpc_parser_t * mpc_boundary(void)
Definition: mpc.c:2005
mpc_parser_t * mpc_tok_brackets(mpc_parser_t *a, mpc_dtor_t ad)
Definition: mpc.c:2112
mpc_parser_t * mpc_expect(mpc_parser_t *a, const char *expected)
Definition: mpc.c:1690
mpc_parser_t * mpc_hexdigits(void)
Definition: mpc.c:2020
static void * mpc_calloc(mpc_input_t *i, size_t n, size_t m)
Definition: mpc.c:257
int mpc_test_fail(mpc_parser_t *p, const char *s, const void *d, int(*tester)(const void *, const void *), mpc_dtor_t destructor, void(*printer)(const void *))
Definition: mpc.c:2831
static mpc_state_t mpc_state_invalid(void)
Definition: mpc.c:7
static mpc_val_t * mpcaf_grammar_char(mpc_val_t *x, void *s)
Definition: mpc.c:3426
static void mpc_free(mpc_input_t *i, void *p)
Definition: mpc.c:263
mpc_err_t * mpca_lang_pipe(int flags, FILE *p,...)
Definition: mpc.c:3768
mpc_parser_t * mpc_tok_braces(mpc_parser_t *a, mpc_dtor_t ad)
Definition: mpc.c:2111
mpc_parser_t * mpc_whole(mpc_parser_t *a, mpc_dtor_t da)
Definition: mpc.c:2083
mpc_parser_t * mpc_soi(void)
Definition: mpc.c:1977
mpc_parser_t * mpc_maybe(mpc_parser_t *a)
Definition: mpc.c:1898
static mpc_val_t * mpcf_input_strfold(mpc_input_t *i, int n, mpc_val_t **xs)
Definition: mpc.c:975
mpc_parser_t * mpc_range(char s, char e)
Definition: mpc.c:1754
static const char * mpc_escape_output_raw_re[]
Definition: mpc.c:2492
static mpc_val_t * mpcf_input_free(mpc_input_t *i, mpc_val_t *x)
Definition: mpc.c:1009
static int mpc_input_range(mpc_input_t *i, char c, char d, char **o)
Definition: mpc.c:496
static const char * mpc_re_range_escape_char(char c)
Definition: mpc.c:2273
static void mpc_undefine_unretained(mpc_parser_t *p, int force)
Definition: mpc.c:1396
static int is_number(const char *s)
Definition: mpc.c:3453
static char mpc_input_buffer_get(mpc_input_t *i)
Definition: mpc.c:366
static int mpc_input_buffer_in_range(mpc_input_t *i)
Definition: mpc.c:362
mpc_ast_t * mpc_ast_add_root_tag(mpc_ast_t *a, const char *t)
Definition: mpc.c:2990
static const char mpc_escape_input_raw_cchar[]
Definition: mpc.c:2497
int mpc_parse_file(const char *filename, FILE *file, mpc_parser_t *p, mpc_result_t *r)
Definition: mpc.c:1337
mpc_ast_t * mpc_ast_add_tag(mpc_ast_t *a, const char *t)
Definition: mpc.c:2981
static void mpc_input_unmark(mpc_input_t *i)
Definition: mpc.c:322
static void mpca_stmt_list_delete(mpc_val_t *x)
Definition: mpc.c:3636
mpc_ast_t * mpc_ast_traverse_next(mpc_ast_trav_t **trav)
Definition: mpc.c:3118
mpc_val_t * mpcf_snd_free(int n, mpc_val_t **xs)
Definition: mpc.c:2638
mpc_parser_t * mpc_lower(void)
Definition: mpc.c:2023
mpc_parser_t * mpc_regex_lit(void)
Definition: mpc.c:2065
mpc_parser_t * mpc_pass(void)
Definition: mpc.c:1610
static mpc_val_t * mpcf_unescape_new(mpc_val_t *x, const char *input, const char **output)
Definition: mpc.c:2536
static void mpc_err_add_expected(mpc_input_t *i, mpc_err_t *x, char *expected)
Definition: mpc.c:741
mpc_parser_t * mpca_grammar(int flags, const char *grammar,...)
Definition: mpc.c:3588
static void mpc_input_suppress_enable(mpc_input_t *i)
Definition: mpc.c:299
static void mpc_input_backtrack_disable(mpc_input_t *i)
Definition: mpc.c:295
static char mpc_input_peekc(mpc_input_t *i)
Definition: mpc.c:394
static mpc_err_t * mpc_err_export(mpc_input_t *i, mpc_err_t *x)
Definition: mpc.c:721
mpc_val_t * mpcf_unescape_char_raw(mpc_val_t *x)
Definition: mpc.c:2618
mpc_parser_t * mpca_many1(mpc_parser_t *a)
Definition: mpc.c:3284
mpc_val_t * mpcf_fst_free(int n, mpc_val_t **xs)
Definition: mpc.c:2637
static char char_unescape_buffer[4]
Definition: mpc.c:604
static int mpc_input_any(mpc_input_t *i, char **o)
Definition: mpc.c:482
void mpc_err_delete(mpc_err_t *x)
Definition: mpc.c:575
static mpc_err_t * mpc_err_repeat(mpc_input_t *i, mpc_err_t *x, const char *prefix)
Definition: mpc.c:801
static mpc_val_t * mpcaf_grammar_or(int n, mpc_val_t **xs)
Definition: mpc.c:3386
mpc_val_t * mpcf_trd_free(int n, mpc_val_t **xs)
Definition: mpc.c:2639
static const char * mpc_escape_output_c[]
Definition: mpc.c:2487
mpc_parser_t * mpc_checkf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *fmt,...)
Definition: mpc.c:1838
mpc_parser_t * mpc_state(void)
Definition: mpc.c:1684
static void mpc_ast_print_depth(mpc_ast_t *a, int d, FILE *fp)
Definition: mpc.c:3010
mpc_parser_t * mpca_total(mpc_parser_t *a)
Definition: mpc.c:3333
static mpc_val_t * mpcf_escape_new(mpc_val_t *x, const char *input, const char **output)
Definition: mpc.c:2500
static void * mpc_malloc(mpc_input_t *i, size_t n)
Definition: mpc.c:237
mpc_parser_t * mpc_octdigits(void)
Definition: mpc.c:2021
mpc_parser_t * mpc_count(int n, mpc_fold_t f, mpc_parser_t *a, mpc_dtor_t da)
Definition: mpc.c:1918
mpc_parser_t * mpc_sym(const char *s)
Definition: mpc.c:2089
mpc_parser_t * mpc_anchor(int(*f)(char, char))
Definition: mpc.c:1677
int mpc_test_pass(mpc_parser_t *p, const char *s, const void *d, int(*tester)(const void *, const void *), mpc_dtor_t destructor, void(*printer)(const void *))
Definition: mpc.c:2854
mpc_parser_t * mpc_oct(void)
Definition: mpc.c:2031
mpc_parser_t * mpc_tok(mpc_parser_t *a)
Definition: mpc.c:2088
mpc_parser_t * mpc_or(int n,...)
Definition: mpc.c:1928
int mpc_ast_get_index(mpc_ast_t *ast, const char *tag)
Definition: mpc.c:3044
static mpc_val_t * mpca_stmt_list_apply_to(mpc_val_t *x, void *s)
Definition: mpc.c:3652
mpc_val_t *(* mpc_apply_t)(mpc_val_t *)
Definition: mpc.h:81
mpc_ast_trav_order_t
Definition: mpc.h:310
@ mpc_ast_trav_order_pre
Definition: mpc.h:311
@ mpc_ast_trav_order_post
Definition: mpc.h:312
mpc_val_t *(* mpc_fold_t)(int, mpc_val_t **)
Definition: mpc.h:83
int(* mpc_check_with_t)(mpc_val_t **, void *)
Definition: mpc.h:86
void(* mpc_dtor_t)(mpc_val_t *)
Definition: mpc.h:78
int(* mpc_check_t)(mpc_val_t **)
Definition: mpc.h:85
void mpc_val_t
Definition: mpc.h:58
mpc_val_t *(* mpc_apply_to_t)(mpc_val_t *, void *)
Definition: mpc.h:82
@ MPCA_LANG_PREDICTIVE
Definition: mpc.h:356
@ MPCA_LANG_WHITESPACE_SENSITIVE
Definition: mpc.h:357
@ MPC_RE_MULTILINE
Definition: mpc.h:273
@ MPC_RE_DEFAULT
Definition: mpc.h:270
@ MPC_RE_DOTALL
Definition: mpc.h:274
mpc_val_t *(* mpc_ctor_t)(void)
Definition: mpc.h:79
string FILE
Definition: benchmark.py:21
static RzSocket * s
Definition: rtr.c:28
#define isspace(c)
Definition: safe-ctype.h:141
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
static int
Definition: sfsocketcall.h:114
int size_t
Definition: sftypes.h:40
#define d(i)
Definition: sha256.c:44
#define b(i)
Definition: sha256.c:42
#define f(i)
Definition: sha256.c:46
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
#define cond(bop, top, mask, flags)
Definition: buffer.h:15
Definition: gzappend.c:170
Definition: mpc.h:284
int children_num
Definition: mpc.h:288
char * tag
Definition: mpc.h:285
struct mpc_ast_t ** children
Definition: mpc.h:289
mpc_ast_trav_order_t order
Definition: mpc.h:319
mpc_ast_t * curr_node
Definition: mpc.h:316
struct mpc_ast_trav_t * parent
Definition: mpc.h:317
int curr_child
Definition: mpc.h:318
Definition: mpc.h:40
FILE * file
Definition: mpc.c:86
char * buffer
Definition: mpc.c:85
int backtrack
Definition: mpc.c:89
char * string
Definition: mpc.c:84
int marks_slots
Definition: mpc.c:90
char * filename
Definition: mpc.c:81
int type
Definition: mpc.c:80
size_t mem_index
Definition: mpc.c:97
int suppress
Definition: mpc.c:88
mpc_state_t * marks
Definition: mpc.c:92
mpc_state_t state
Definition: mpc.c:82
int marks_num
Definition: mpc.c:91
char * lasts
Definition: mpc.c:94
char last
Definition: mpc.c:95
Definition: mpc.c:74
mpc_pdata_t data
Definition: mpc.c:960
char * name
Definition: mpc.c:959
char retained
Definition: mpc.c:962
char type
Definition: mpc.c:961
mpc_parser_t ** xs
Definition: mpc.c:936
mpc_dtor_t * dxs
Definition: mpc.c:936
mpc_apply_t f
Definition: mpc.c:928
mpc_dtor_t dx
Definition: mpc.c:930
char * m
Definition: mpc.c:922
char * m
Definition: mpc.c:920
mpc_ctor_t lf
Definition: mpc.c:921
mpc_dtor_t dx
Definition: mpc.c:933
mpc_parser_t ** xs
Definition: mpc.c:935
mpc_parser_t * x
Definition: mpc.c:932
mpc_dtor_t dx
Definition: mpc.c:934
char * x
Definition: mpc.c:927
va_list * va
Definition: mpc.c:3380
int parsers_num
Definition: mpc.c:3381
mpc_parser_t ** parsers
Definition: mpc.c:3382
mpc_parser_t * grammar
Definition: mpc.c:3608
char * ident
Definition: mpc.c:3606
char * name
Definition: mpc.c:3607
Definition: z80asm.h:102
Definition: dis.h:43
int pos
Definition: main.c:11
mpc_pdata_not_t not
Definition: mpc.c:952
mpc_pdata_lift_t lift
Definition: mpc.c:940
mpc_pdata_apply_to_t apply_to
Definition: mpc.c:948
mpc_pdata_apply_t apply
Definition: mpc.c:947
mpc_pdata_check_t check
Definition: mpc.c:949
mpc_pdata_range_t range
Definition: mpc.c:944
mpc_pdata_string_t string
Definition: mpc.c:946
mpc_pdata_fail_t fail
Definition: mpc.c:939
mpc_pdata_repeat_t repeat
Definition: mpc.c:953
mpc_pdata_predict_t predict
Definition: mpc.c:951
mpc_pdata_single_t single
Definition: mpc.c:943
mpc_pdata_and_t and
Definition: mpc.c:954
mpc_pdata_expect_t expect
Definition: mpc.c:941
mpc_pdata_satisfy_t satisfy
Definition: mpc.c:945
mpc_pdata_anchor_t anchor
Definition: mpc.c:942
mpc_pdata_or_t or
Definition: mpc.c:955
mpc_pdata_check_with_t check_with
Definition: mpc.c:950
void error(const char *msg)
Definition: untgz.c:593
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
static int file
Definition: z80asm.c:58
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237