2012-05-26 16:00:43 +02:00
|
|
|
#include "parser_internal.h"
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const HParser *p;
|
|
|
|
|
HAction action;
|
|
|
|
|
} HParseAction;
|
|
|
|
|
|
|
|
|
|
static HParseResult* parse_action(void *env, HParseState *state) {
|
|
|
|
|
HParseAction *a = (HParseAction*)env;
|
|
|
|
|
if (a->p && a->action) {
|
|
|
|
|
HParseResult *tmp = h_do_parse(a->p, state);
|
|
|
|
|
//HParsedToken *tok = a->action(h_do_parse(a->p, state));
|
2012-06-09 14:41:32 +02:00
|
|
|
if(tmp) {
|
|
|
|
|
const HParsedToken *tok = a->action(tmp);
|
|
|
|
|
return make_result(state, (HParsedToken*)tok);
|
|
|
|
|
} else
|
|
|
|
|
return NULL;
|
2012-05-26 16:00:43 +02:00
|
|
|
} else // either the parser's missing or the action's missing
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-02 19:31:18 -05:00
|
|
|
static HCFChoice* desugar_action(HAllocator *mm__, void *env) {
|
|
|
|
|
HParseAction *a = (HParseAction*)env;
|
|
|
|
|
HCFSequence *seq = h_new(HCFSequence, 1);
|
|
|
|
|
seq->items = h_new(HCFChoice*, 2);
|
2013-03-17 13:25:02 -07:00
|
|
|
seq->items[0] = h_desugar(mm__, a->p);
|
2013-02-02 19:31:18 -05:00
|
|
|
seq->items[1] = NULL;
|
|
|
|
|
HCFChoice *ret = h_new(HCFChoice, 1);
|
|
|
|
|
ret->type = HCF_CHOICE;
|
|
|
|
|
ret->seq = h_new(HCFSequence*, 2);
|
|
|
|
|
ret->seq[0] = seq;
|
|
|
|
|
ret->seq[1] = NULL;
|
|
|
|
|
ret->action = a->action;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 18:10:40 -05:00
|
|
|
static bool action_isValidRegular(void *env) {
|
|
|
|
|
HParseAction *a = (HParseAction*)env;
|
|
|
|
|
return a->p->vtable->isValidRegular(a->p->env);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool action_isValidCF(void *env) {
|
|
|
|
|
HParseAction *a = (HParseAction*)env;
|
|
|
|
|
return a->p->vtable->isValidCF(a->p->env);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 16:00:43 +02:00
|
|
|
static const HParserVtable action_vt = {
|
|
|
|
|
.parse = parse_action,
|
2012-12-18 18:10:40 -05:00
|
|
|
.isValidRegular = action_isValidRegular,
|
|
|
|
|
.isValidCF = action_isValidCF,
|
2013-02-02 19:31:18 -05:00
|
|
|
.desugar = desugar_action,
|
2012-05-26 16:00:43 +02:00
|
|
|
};
|
|
|
|
|
|
2012-10-10 15:58:03 +02:00
|
|
|
const HParser* h_action(const HParser* p, const HAction a) {
|
|
|
|
|
return h_action__m(&system_allocator, p, a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HParser* h_action__m(HAllocator* mm__, const HParser* p, const HAction a) {
|
|
|
|
|
HParser *res = h_new(HParser, 1);
|
2012-05-26 16:00:43 +02:00
|
|
|
res->vtable = &action_vt;
|
2012-10-10 15:58:03 +02:00
|
|
|
HParseAction *env = h_new(HParseAction, 1);
|
2012-05-26 16:00:43 +02:00
|
|
|
env->p = p;
|
|
|
|
|
env->action = a;
|
|
|
|
|
res->env = (void*)env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|