feat: add sepby1 combinator

`sepby1` is a common reusable combinator in Haskell Parsec.

This adds `mpc_sepby1(mpc_fold_t f, mpc_parser_t *sep, mpc_parser_t *a)` according to Haskell's implementation:

https://hackage.haskell.org/package/parsec-3.1.16.1/docs/src/Text.Parsec.Combinator.html#sepBy1

Reuses existing `mpc_and`, `mpc_many`, and `mpcf_snd_free`.
This commit is contained in:
steve-chavez
2023-08-08 14:15:55 -05:00
parent 6912695fe5
commit c24e27f0b8
4 changed files with 29 additions and 0 deletions

View File

@@ -242,6 +242,18 @@ void test_eoi(void) {
}
void test_sepby(void) {
mpc_parser_t* CommaSepIdent = mpc_sepby1(mpcf_strfold, mpc_char(','), mpc_ident());
PT_ASSERT(mpc_test_fail(CommaSepIdent, "", "", streq, free, strprint));
PT_ASSERT(mpc_test_pass(CommaSepIdent, "one", "one", streq, free, strprint));
PT_ASSERT(mpc_test_pass(CommaSepIdent, "one,", "one", streq, free, strprint));
PT_ASSERT(mpc_test_pass(CommaSepIdent, "one,two,", "onetwo", streq, free, strprint));
PT_ASSERT(mpc_test_pass(CommaSepIdent, "one,two,three", "onetwothree", streq, free, strprint));
mpc_delete(CommaSepIdent);
}
void suite_core(void) {
pt_add_test(test_ident, "Test Ident", "Suite Core");
pt_add_test(test_maths, "Test Maths", "Suite Core");
@@ -251,4 +263,5 @@ void suite_core(void) {
pt_add_test(test_reader, "Test Reader", "Suite Core");
pt_add_test(test_tokens, "Test Tokens", "Suite Core");
pt_add_test(test_eoi, "Test EOI", "Suite Core");
pt_add_test(test_sepby, "Test SepBy", "Suite Core");
}