Skip to content

Commit

Permalink
Add mpc_checkf and mpc_check_withf combinators.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome M. BERGER committed Apr 3, 2018
1 parent dfda9d3 commit a63bedc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ 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, ...);
```
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`.
Expand Down
32 changes: 32 additions & 0 deletions mpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,38 @@ 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;

va_start(va, fmt);
buffer = malloc(2048);
vsprintf(buffer, fmt, va);
va_end(va);

p = mpc_check (a, 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;

va_start(va, fmt);
buffer = malloc(2048);
vsprintf(buffer, fmt, va);
va_end(va);

p = mpc_check_with (a, f, x, buffer);
free (buffer);

return p;
}

mpc_parser_t *mpc_predictive(mpc_parser_t *a) {
mpc_parser_t *p = mpc_undefined();
p->type = MPC_TYPE_PREDICT;
Expand Down
2 changes: 2 additions & 0 deletions mpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ 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_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
36 changes: 36 additions & 0 deletions tests/combinators.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,42 @@ void test_check_with(void) {
mpc_delete(p);
}

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");

success = mpc_parse("test", "a", p, &r);
PT_ASSERT(success);
PT_ASSERT_STR_EQ(r.output, "a");
if (success) free(r.output); else mpc_err_delete(r.error);

success = mpc_parse("test", "b", p, &r);
PT_ASSERT(!success);
PT_ASSERT_STR_EQ(r.error->failure, "Expected 'a'");
if (success) free(r.output); else mpc_err_delete(r.error);

mpc_delete(p);
}

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, "a", "Expected '%s'", "a");

success = mpc_parse("test", "a", p, &r);
PT_ASSERT(success);
if (success) PT_ASSERT_STR_EQ(r.output, "a");
if (success) free(r.output); else mpc_err_delete(r.error);

success = mpc_parse("test", "b", p, &r);
PT_ASSERT(!success);
if (!success) PT_ASSERT_STR_EQ(r.error->failure, "Expected 'a'");
if (success) free(r.output); else mpc_err_delete(r.error);

mpc_delete(p);
}

void suite_combinators(void) {
pt_add_test(test_check, "Test Check", "Suite Combinators");
pt_add_test(test_check_with, "Test Check with", "Suite Combinators");
Expand Down

0 comments on commit a63bedc

Please sign in to comment.