diff --git a/examples/foobar.c b/examples/foobar.c new file mode 100644 index 0000000..43691f7 --- /dev/null +++ b/examples/foobar.c @@ -0,0 +1,20 @@ +#include "../mpc.h" + +int main(int argc, char** argv) { + + mpc_result_t r; + mpc_parser_t* Foobar = mpc_new("foobar"); + mpca_lang(MPCA_LANG_DEFAULT, "foobar : \"foo\" | \"bar\";", Foobar); + (void)argc; + + if (mpc_parse("", argv[1], Foobar, &r)) { + mpc_ast_print(r.output); + mpc_ast_delete(r.output); + } else { + mpc_err_print(r.error); + mpc_err_delete(r.error); + } + mpc_cleanup(1, Foobar); + return 0; +} + diff --git a/mpc.c b/mpc.c index 1a9591c..99de0d9 100644 --- a/mpc.c +++ b/mpc.c @@ -149,11 +149,11 @@ static const char *mpc_err_char_unescape(char c) { } char *mpc_err_string(mpc_err_t *x) { - - char *buffer = calloc(1, 1024); - int max = 1023; + + int i; int pos = 0; - int i; + int max = 1023; + char *buffer = calloc(1, 1024); if (x->failure) { mpc_err_string_cat(buffer, &pos, &max, @@ -1945,12 +1945,12 @@ static const char *mpc_re_range_escape_char(char c) { static mpc_val_t *mpcf_re_range(mpc_val_t *x) { mpc_parser_t *out; - char *range = calloc(1,1); + size_t i, j; + size_t start, end; const char *tmp = NULL; const char *s = x; int comp = s[0] == '^' ? 1 : 0; - size_t start, end; - size_t i, j; + char *range = calloc(1,1); if (s[0] == '\0') { free(x); return mpc_fail("Invalid Regex Range Expression"); } if (s[0] == '^' && @@ -2123,9 +2123,9 @@ static mpc_val_t *mpcf_escape_new(mpc_val_t *x, const char *input, const char ** int i; int found; + char buff[2]; char *s = x; char *y = calloc(1, 1); - char buff[2]; while (*s) { @@ -2159,10 +2159,10 @@ static mpc_val_t *mpcf_unescape_new(mpc_val_t *x, const char *input, const char int i; int found = 0; + char buff[2]; char *s = x; char *y = calloc(1, 1); - char buff[2]; - + while (*s) { i = 0; @@ -2180,7 +2180,7 @@ static mpc_val_t *mpcf_unescape_new(mpc_val_t *x, const char *input, const char } i++; } - + if (!found) { y = realloc(y, strlen(y) + 2); buff[0] = *s; buff[1] = '\0'; @@ -2261,8 +2261,9 @@ mpc_val_t *mpcf_snd_free(int n, mpc_val_t **xs) { return mpcf_nth_free(n, xs, 1) mpc_val_t *mpcf_trd_free(int n, mpc_val_t **xs) { return mpcf_nth_free(n, xs, 2); } mpc_val_t *mpcf_strfold(int n, mpc_val_t **xs) { - char *x = calloc(1, 1); int i; + char *x = calloc(1, 1); + for (i = 0; i < n; i++) { x = realloc(x, strlen(x) + strlen(xs[i]) + 1); strcat(x, xs[i]);