Add mpc_checkf
and mpc_check_withf
combinators.
This commit is contained in:
@@ -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`.
|
||||
|
32
mpc.c
32
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;
|
||||
|
2
mpc.h
2
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);
|
||||
|
@@ -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");
|
||||
|
Reference in New Issue
Block a user