hammer/src/parsers/action.c

64 lines
1.7 KiB
C
Raw Normal View History

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;
}
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);
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;
}
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,
.isValidRegular = action_isValidRegular,
.isValidCF = action_isValidCF,
.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) {
HParseAction *env = h_new(HParseAction, 1);
2012-05-26 16:00:43 +02:00
env->p = p;
env->action = a;
return h_new_parser(mm__, &action_vt, env);
2012-05-26 16:00:43 +02:00
}