Skip to content

Commit

Permalink
Added destructor to check combinators
Browse files Browse the repository at this point in the history
  • Loading branch information
orangeduck committed Jun 15, 2019
1 parent 001a2c3 commit dbe7308
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ examples/maths
examples/smallc
examples/foobar
examples/tree_traversal
build/*
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
* * *
Expand Down
48 changes: 26 additions & 22 deletions mpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -1799,55 +1801,57 @@ 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);
strcpy(p->data.check_with.e, e);
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;
}
Expand Down
8 changes: 4 additions & 4 deletions mpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions tests/combinators.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit dbe7308

Please sign in to comment.