add monadic bind combinator h_bind
This commit is contained in:
parent
47f34b81e4
commit
42d51ed479
4 changed files with 102 additions and 12 deletions
|
|
@ -568,6 +568,34 @@ static void test_permutation(gconstpointer backend) {
|
|||
g_check_parse_failed(po2, be, "ccc", 3);
|
||||
}
|
||||
|
||||
static HParser *f_test_bind(const HParsedToken *p, void *env) {
|
||||
uint8_t one = (uintptr_t)env;
|
||||
|
||||
assert(p);
|
||||
assert(p->token_type == TT_SEQUENCE);
|
||||
|
||||
int v=0;
|
||||
for(size_t i=0; i<p->seq->used; i++) {
|
||||
assert(p->seq->elements[i]->token_type == TT_UINT);
|
||||
v = v*10 + p->seq->elements[i]->uint - '0';
|
||||
}
|
||||
|
||||
return h_ch(one - 1 + v);
|
||||
}
|
||||
static void test_bind(gconstpointer backend) {
|
||||
HParserBackend be = (HParserBackend)GPOINTER_TO_INT(backend);
|
||||
const HParser *digit = h_ch_range('0', '9');
|
||||
const HParser *nat = h_many1(digit);
|
||||
const HParser *p = h_bind(nat, f_test_bind, (void *)(uintptr_t)'a');
|
||||
|
||||
g_check_parse_match(p, be, "1a", 2, "u0x61");
|
||||
g_check_parse_match(p, be, "2b", 2, "u0x62");
|
||||
g_check_parse_match(p, be, "26z", 3, "u0x7a");
|
||||
g_check_parse_failed(p, be, "1x", 2);
|
||||
g_check_parse_failed(p, be, "29y", 3);
|
||||
g_check_parse_failed(p, be, "@", 1);
|
||||
}
|
||||
|
||||
void register_parser_tests(void) {
|
||||
g_test_add_data_func("/core/parser/packrat/token", GINT_TO_POINTER(PB_PACKRAT), test_token);
|
||||
g_test_add_data_func("/core/parser/packrat/ch", GINT_TO_POINTER(PB_PACKRAT), test_ch);
|
||||
|
|
@ -617,6 +645,7 @@ void register_parser_tests(void) {
|
|||
g_test_add_data_func("/core/parser/packrat/endianness", GINT_TO_POINTER(PB_PACKRAT), test_endianness);
|
||||
g_test_add_data_func("/core/parser/packrat/putget", GINT_TO_POINTER(PB_PACKRAT), test_put_get);
|
||||
g_test_add_data_func("/core/parser/packrat/permutation", GINT_TO_POINTER(PB_PACKRAT), test_permutation);
|
||||
g_test_add_data_func("/core/parser/packrat/bind", GINT_TO_POINTER(PB_PACKRAT), test_bind);
|
||||
|
||||
g_test_add_data_func("/core/parser/llk/token", GINT_TO_POINTER(PB_LLk), test_token);
|
||||
g_test_add_data_func("/core/parser/llk/ch", GINT_TO_POINTER(PB_LLk), test_ch);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue