Added destructor to check combinators
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,3 +8,4 @@ examples/maths
|
|||||||
examples/smallc
|
examples/smallc
|
||||||
examples/foobar
|
examples/foobar
|
||||||
examples/tree_traversal
|
examples/tree_traversal
|
||||||
|
build/*
|
10
README.md
10
README.md
@@ -312,13 +312,13 @@ Returns a parser that applies function `f` (optionality taking extra input `x`)
|
|||||||
* * *
|
* * *
|
||||||
|
|
||||||
```c
|
```c
|
||||||
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 *mpc_check_with(mpc_parser_t *a, mpc_check_with_t f, void *x, 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_check_t f, const char *fmt, ...);
|
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_check_with_t f, void *x, 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`.
|
||||||
|
|
||||||
* * *
|
* * *
|
||||||
|
|
||||||
|
48
mpc.c
48
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 { 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_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_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_dtor_t dx; 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_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_pdata_predict_t;
|
||||||
typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_ctor_t lf; } mpc_pdata_not_t;
|
typedef struct { mpc_parser_t *x; mpc_dtor_t dx; mpc_ctor_t lf; } mpc_pdata_not_t;
|
||||||
typedef struct { int n; mpc_fold_t f; mpc_parser_t *x; mpc_dtor_t dx; } mpc_pdata_repeat_t;
|
typedef struct { int n; mpc_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)) {
|
if (p->data.check.f(&r->output)) {
|
||||||
MPC_SUCCESS(r->output);
|
MPC_SUCCESS(r->output);
|
||||||
} else {
|
} else {
|
||||||
|
mpc_parse_dtor(i, p->data.check.dx, r->output);
|
||||||
MPC_FAILURE(mpc_err_fail(i, p->data.check.e));
|
MPC_FAILURE(mpc_err_fail(i, p->data.check.e));
|
||||||
}
|
}
|
||||||
} else {
|
} 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)) {
|
if (p->data.check_with.f(&r->output, p->data.check_with.d)) {
|
||||||
MPC_SUCCESS(r->output);
|
MPC_SUCCESS(r->output);
|
||||||
} else {
|
} else {
|
||||||
|
mpc_parse_dtor(i, p->data.check.dx, r->output);
|
||||||
MPC_FAILURE(mpc_err_fail(i, p->data.check_with.e));
|
MPC_FAILURE(mpc_err_fail(i, p->data.check_with.e));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -1799,20 +1801,22 @@ mpc_parser_t *mpc_apply_to(mpc_parser_t *a, mpc_apply_to_t f, void *x) {
|
|||||||
return p;
|
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();
|
mpc_parser_t *p = mpc_undefined();
|
||||||
p->type = MPC_TYPE_CHECK;
|
p->type = MPC_TYPE_CHECK;
|
||||||
p->data.check.x = a;
|
p->data.check.x = a;
|
||||||
p->data.check.f = f;
|
p->data.check.dx = da;
|
||||||
p->data.check.e = malloc(strlen(e) + 1);
|
p->data.check.f = f;
|
||||||
|
p->data.check.e = malloc(strlen(e) + 1);
|
||||||
strcpy(p->data.check.e, e);
|
strcpy(p->data.check.e, e);
|
||||||
return p;
|
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 *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();
|
mpc_parser_t *p = mpc_undefined();
|
||||||
p->type = MPC_TYPE_CHECK_WITH;
|
p->type = MPC_TYPE_CHECK_WITH;
|
||||||
p->data.check_with.x = a;
|
p->data.check_with.x = a;
|
||||||
|
p->data.check_with.dx = da;
|
||||||
p->data.check_with.f = f;
|
p->data.check_with.f = f;
|
||||||
p->data.check_with.d = x;
|
p->data.check_with.d = x;
|
||||||
p->data.check_with.e = malloc(strlen(e) + 1);
|
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;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
mpc_parser_t *mpc_checkf(mpc_parser_t *a, mpc_check_t f, const char *fmt, ...) {
|
mpc_parser_t *mpc_checkf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_t f, const char *fmt, ...) {
|
||||||
va_list va;
|
va_list va;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
mpc_parser_t *p;
|
mpc_parser_t *p;
|
||||||
|
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
buffer = malloc(2048);
|
buffer = malloc(2048);
|
||||||
vsprintf(buffer, fmt, va);
|
vsprintf(buffer, fmt, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
p = mpc_check (a, f, buffer);
|
p = mpc_check(a, da, f, buffer);
|
||||||
free (buffer);
|
free(buffer);
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
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_withf(mpc_parser_t *a, mpc_dtor_t da, mpc_check_with_t f, void *x, const char *fmt, ...) {
|
||||||
va_list va;
|
va_list va;
|
||||||
char *buffer;
|
char *buffer;
|
||||||
mpc_parser_t *p;
|
mpc_parser_t *p;
|
||||||
|
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
buffer = malloc(2048);
|
buffer = malloc(2048);
|
||||||
vsprintf(buffer, fmt, va);
|
vsprintf(buffer, fmt, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
p = mpc_check_with (a, f, x, buffer);
|
p = mpc_check_with(a, da, f, x, buffer);
|
||||||
free (buffer);
|
free(buffer);
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
8
mpc.h
8
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_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(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_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(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_check_with_t f, void *x, 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_check_t f, const char *fmt, ...);
|
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_check_with_t f, void *x, 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(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);
|
mpc_parser_t *mpc_not_lift(mpc_parser_t *a, mpc_dtor_t da, mpc_ctor_t lf);
|
||||||
|
@@ -12,7 +12,7 @@ static int check_is(mpc_val_t** x, void* t) {
|
|||||||
void test_check(void) {
|
void test_check(void) {
|
||||||
int success;
|
int success;
|
||||||
mpc_result_t r;
|
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);
|
success = mpc_parse("test", "a", p, &r);
|
||||||
PT_ASSERT(success);
|
PT_ASSERT(success);
|
||||||
@@ -30,7 +30,7 @@ void test_check(void) {
|
|||||||
void test_check_with(void) {
|
void test_check_with(void) {
|
||||||
int success;
|
int success;
|
||||||
mpc_result_t r;
|
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);
|
success = mpc_parse("test", "a", p, &r);
|
||||||
PT_ASSERT(success);
|
PT_ASSERT(success);
|
||||||
@@ -48,7 +48,7 @@ void test_check_with(void) {
|
|||||||
void test_checkf(void) {
|
void test_checkf(void) {
|
||||||
int success;
|
int success;
|
||||||
mpc_result_t r;
|
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);
|
success = mpc_parse("test", "a", p, &r);
|
||||||
PT_ASSERT(success);
|
PT_ASSERT(success);
|
||||||
@@ -66,7 +66,7 @@ void test_checkf(void) {
|
|||||||
void test_check_withf(void) {
|
void test_check_withf(void) {
|
||||||
int success;
|
int success;
|
||||||
mpc_result_t r;
|
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);
|
success = mpc_parse("test", "a", p, &r);
|
||||||
PT_ASSERT(success);
|
PT_ASSERT(success);
|
||||||
|
Reference in New Issue
Block a user