add monadic bind combinator h_bind
This commit is contained in:
parent
47f34b81e4
commit
42d51ed479
4 changed files with 102 additions and 12 deletions
42
src/parsers/bind.c
Normal file
42
src/parsers/bind.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include "parser_internal.h"
|
||||
|
||||
typedef struct {
|
||||
const HParser *p;
|
||||
HContinuation k;
|
||||
void *env;
|
||||
} BindEnv;
|
||||
|
||||
static HParseResult *parse_bind(void *be_, HParseState *state) {
|
||||
BindEnv *be = be_;
|
||||
|
||||
HParseResult *res = h_do_parse(be->p, state);
|
||||
if(!res)
|
||||
return NULL;
|
||||
|
||||
HParser *kx = be->k(res->ast, be->env);
|
||||
return h_do_parse(kx, state);
|
||||
}
|
||||
|
||||
static const HParserVtable bind_vt = {
|
||||
.parse = parse_bind,
|
||||
.isValidRegular = h_false,
|
||||
.isValidCF = h_false,
|
||||
.compile_to_rvm = h_not_regular,
|
||||
};
|
||||
|
||||
HParser *h_bind(const HParser *p, HContinuation k, void *env)
|
||||
{
|
||||
return h_bind__m(&system_allocator, p, k, env);
|
||||
}
|
||||
|
||||
HParser *h_bind__m(HAllocator *mm__,
|
||||
const HParser *p, HContinuation k, void *env)
|
||||
{
|
||||
BindEnv *be = h_new(BindEnv, 1);
|
||||
|
||||
be->p = p;
|
||||
be->k = k;
|
||||
be->env = env;
|
||||
|
||||
return h_new_parser(mm__, &bind_vt, be);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue