From dbe7308b9c3672357554fdab140dac28bdffb158 Mon Sep 17 00:00:00 2001 From: Daniel Holden Date: Sat, 15 Jun 2019 14:30:41 -0400 Subject: [PATCH] Added destructor to check combinators --- .gitignore | 1 + README.md | 10 +++++----- mpc.c | 48 ++++++++++++++++++++++++--------------------- mpc.h | 8 ++++---- tests/combinators.c | 8 ++++---- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index a82e7ec..788a748 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ examples/maths examples/smallc examples/foobar examples/tree_traversal +build/* \ No newline at end of file diff --git a/README.md b/README.md index 2e044a4..98ea680 100644 --- a/README.md +++ b/README.md @@ -312,13 +312,13 @@ Returns a parser that applies function `f` (optionality taking extra input `x`) * * * ```c -mpc_parser_t *mpc_check(mpc_parser_t *a, mpc_check_t f, const char *e); -mpc_parser_t *mpc_check_with(mpc_parser_t *a, mpc_check_with_t f, void *x, const char *e); -mpc_parser_t *mpc_checkf(mpc_parser_t *a, mpc_check_t f, const char *fmt, ...); -mpc_parser_t *mpc_check_withf(mpc_parser_t *a, mpc_check_with_t f, void *x, const char *fmt, ...); +mpc_parser_t *mpc_check(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *e); +mpc_parser_t *mpc_check_with(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *e); +mpc_parser_t *mpc_checkf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *fmt, ...); +mpc_parser_t *mpc_check_withf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *fmt, ...); ``` -Returns a parser that applies function `f` (optionally taking extra input `x`) to the result of parser `a`. If `f` returns non-zero, then the parser succeeds and returns the value of `a` (possibly modified by `f`). If `f` returns zero, then the parser fails with message `e`. +Returns a parser that applies function `f` (optionally taking extra input `x`) to the result of parser `a`. If `f` returns non-zero, then the parser succeeds and returns the value of `a` (possibly modified by `f`). If `f` returns zero, then the parser fails with message `e`, and the result of `a` is destroyed with the destructor `da`. * * * diff --git a/mpc.c b/mpc.c index 8e8e83e..9856efa 100644 --- a/mpc.c +++ b/mpc.c @@ -923,8 +923,8 @@ typedef struct { int(*f)(char); } mpc_pdata_satisfy_t; typedef struct { char *x; } mpc_pdata_string_t; typedef struct { mpc_parser_t *x; mpc_apply_t f; } mpc_pdata_apply_t; typedef struct { mpc_parser_t *x; mpc_apply_to_t f; void *d; } mpc_pdata_apply_to_t; -typedef struct { mpc_parser_t *x; mpc_check_t f; char *e; } mpc_pdata_check_t; -typedef struct { mpc_parser_t *x; mpc_check_with_t f; void *d; char *e; } mpc_pdata_check_with_t; +typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_check_t f; char *e; } mpc_pdata_check_t; +typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_check_with_t f; void *d; char *e; } mpc_pdata_check_with_t; typedef struct { mpc_parser_t *x; } mpc_pdata_predict_t; typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_ctor_t lf; } mpc_pdata_not_t; typedef struct { int n; mpc_fold_t f; mpc_parser_t *x; mpc_dtor_t dx; } mpc_pdata_repeat_t; @@ -1090,6 +1090,7 @@ static int mpc_parse_run(mpc_input_t *i, mpc_parser_t *p, mpc_result_t *r, mpc_e if (p->data.check.f(&r->output)) { MPC_SUCCESS(r->output); } else { + mpc_parse_dtor(i, p->data.check.dx, r->output); MPC_FAILURE(mpc_err_fail(i, p->data.check.e)); } } else { @@ -1101,6 +1102,7 @@ static int mpc_parse_run(mpc_input_t *i, mpc_parser_t *p, mpc_result_t *r, mpc_e if (p->data.check_with.f(&r->output, p->data.check_with.d)) { MPC_SUCCESS(r->output); } else { + mpc_parse_dtor(i, p->data.check.dx, r->output); MPC_FAILURE(mpc_err_fail(i, p->data.check_with.e)); } } else { @@ -1799,20 +1801,22 @@ mpc_parser_t *mpc_apply_to(mpc_parser_t *a, mpc_apply_to_t f, void *x) { return p; } -mpc_parser_t *mpc_check(mpc_parser_t *a, mpc_check_t f, const char *e) { +mpc_parser_t *mpc_check(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *e) { mpc_parser_t *p = mpc_undefined(); - p->type = MPC_TYPE_CHECK; - p->data.check.x = a; - p->data.check.f = f; - p->data.check.e = malloc(strlen(e) + 1); + p->type = MPC_TYPE_CHECK; + p->data.check.x = a; + p->data.check.dx = da; + p->data.check.f = f; + p->data.check.e = malloc(strlen(e) + 1); strcpy(p->data.check.e, e); return p; } -mpc_parser_t *mpc_check_with(mpc_parser_t *a, mpc_check_with_t f, void *x, const char *e) { - mpc_parser_t *p = mpc_undefined(); - p->type = MPC_TYPE_CHECK_WITH; +mpc_parser_t *mpc_check_with(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *e) { + mpc_parser_t *p = mpc_undefined(); + p->type = MPC_TYPE_CHECK_WITH; p->data.check_with.x = a; + p->data.check_with.dx = da; p->data.check_with.f = f; p->data.check_with.d = x; p->data.check_with.e = malloc(strlen(e) + 1); @@ -1820,34 +1824,34 @@ mpc_parser_t *mpc_check_with(mpc_parser_t *a, mpc_check_with_t f, void *x, const return p; } -mpc_parser_t *mpc_checkf(mpc_parser_t *a, mpc_check_t f, const char *fmt, ...) { - va_list va; - char *buffer; - mpc_parser_t *p; +mpc_parser_t *mpc_checkf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *fmt, ...) { + va_list va; + char *buffer; + mpc_parser_t *p; va_start(va, fmt); buffer = malloc(2048); vsprintf(buffer, fmt, va); va_end(va); - p = mpc_check (a, f, buffer); - free (buffer); + p = mpc_check(a, da, f, buffer); + free(buffer); return p; } -mpc_parser_t *mpc_check_withf(mpc_parser_t *a, mpc_check_with_t f, void *x, const char *fmt, ...) { - va_list va; - char *buffer; - mpc_parser_t *p; +mpc_parser_t *mpc_check_withf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *fmt, ...) { + va_list va; + char *buffer; + mpc_parser_t *p; va_start(va, fmt); buffer = malloc(2048); vsprintf(buffer, fmt, va); va_end(va); - p = mpc_check_with (a, f, x, buffer); - free (buffer); + p = mpc_check_with(a, da, f, x, buffer); + free(buffer); return p; } diff --git a/mpc.h b/mpc.h index 7ea12e3..b3c51a2 100644 --- a/mpc.h +++ b/mpc.h @@ -129,10 +129,10 @@ mpc_parser_t *mpc_expect(mpc_parser_t *a, const char *e); mpc_parser_t *mpc_expectf(mpc_parser_t *a, const char *fmt, ...); mpc_parser_t *mpc_apply(mpc_parser_t *a, mpc_apply_t f); mpc_parser_t *mpc_apply_to(mpc_parser_t *a, mpc_apply_to_t f, void *x); -mpc_parser_t *mpc_check(mpc_parser_t *a, mpc_check_t f, const char *e); -mpc_parser_t *mpc_check_with(mpc_parser_t *a, mpc_check_with_t f, void *x, const char *e); -mpc_parser_t *mpc_checkf(mpc_parser_t *a, mpc_check_t f, const char *fmt, ...); -mpc_parser_t *mpc_check_withf(mpc_parser_t *a, mpc_check_with_t f, void *x, const char *fmt, ...); +mpc_parser_t *mpc_check(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *e); +mpc_parser_t *mpc_check_with(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *e); +mpc_parser_t *mpc_checkf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *fmt, ...); +mpc_parser_t *mpc_check_withf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *fmt, ...); mpc_parser_t *mpc_not(mpc_parser_t *a, mpc_dtor_t da); mpc_parser_t *mpc_not_lift(mpc_parser_t *a, mpc_dtor_t da, mpc_ctor_t lf); diff --git a/tests/combinators.c b/tests/combinators.c index d9fe203..0bd0ae9 100644 --- a/tests/combinators.c +++ b/tests/combinators.c @@ -12,7 +12,7 @@ static int check_is(mpc_val_t** x, void* t) { void test_check(void) { int success; mpc_result_t r; - mpc_parser_t* p = mpc_check(mpc_or(2, mpc_char('a'), mpc_char('b')), check_is_a, "Expected 'a'"); + mpc_parser_t* p = mpc_check(mpc_or(2, mpc_char('a'), mpc_char('b')), free, check_is_a, "Expected 'a'"); success = mpc_parse("test", "a", p, &r); PT_ASSERT(success); @@ -30,7 +30,7 @@ void test_check(void) { void test_check_with(void) { int success; mpc_result_t r; - mpc_parser_t* p = mpc_check_with(mpc_or(2, mpc_char('a'), mpc_char('b')), check_is, (void*)"a", "Expected 'a'"); + mpc_parser_t* p = mpc_check_with(mpc_or(2, mpc_char('a'), mpc_char('b')), free, check_is, (void*)"a", "Expected 'a'"); success = mpc_parse("test", "a", p, &r); PT_ASSERT(success); @@ -48,7 +48,7 @@ void test_check_with(void) { void test_checkf(void) { int success; mpc_result_t r; - mpc_parser_t* p = mpc_checkf(mpc_or(2, mpc_char('a'), mpc_char('b')), check_is_a, "Expected '%s'", "a"); + mpc_parser_t* p = mpc_checkf(mpc_or(2, mpc_char('a'), mpc_char('b')), free, check_is_a, "Expected '%s'", "a"); success = mpc_parse("test", "a", p, &r); PT_ASSERT(success); @@ -66,7 +66,7 @@ void test_checkf(void) { void test_check_withf(void) { int success; mpc_result_t r; - mpc_parser_t* p = mpc_check_withf(mpc_or(2, mpc_char('a'), mpc_char('b')), check_is, (void*)"a", "Expected '%s'", "a"); + mpc_parser_t* p = mpc_check_withf(mpc_or(2, mpc_char('a'), mpc_char('b')), free, check_is, (void*)"a", "Expected '%s'", "a"); success = mpc_parse("test", "a", p, &r); PT_ASSERT(success);