diff --git a/README.md b/README.md index 1ddee9f..d251f26 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/mpc.c b/mpc.c index 0648ada..1189815 100644 --- a/mpc.c +++ b/mpc.c @@ -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; diff --git a/mpc.h b/mpc.h index 7c7374b..e6ef289 100644 --- a/mpc.h +++ b/mpc.h @@ -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); diff --git a/tests/combinators.c b/tests/combinators.c index 871e89e..8eff961 100644 --- a/tests/combinators.c +++ b/tests/combinators.c @@ -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");