hammer/src/parsers/action.c

94 lines
2.6 KiB
C
Raw Normal View History

#include <assert.h>
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->arena, (HParsedToken*)tok);
2012-06-09 14:41:32 +02:00
} 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 void desugar_action(HAllocator *mm__, HCFStack *stk__, void *env) {
HParseAction *a = (HParseAction*)env;
HCFS_BEGIN_CHOICE() {
HCFS_BEGIN_SEQ() {
HCFS_DESUGAR(a->p);
} HCFS_END_SEQ();
HCFS_THIS_CHOICE->action = a->action;
HCFS_THIS_CHOICE->reshape = h_act_first;
} HCFS_END_CHOICE();
}
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);
}
static bool h_svm_action_action(HArena *arena, HSVMContext *ctx, void* arg) {
HParseResult res;
HAction action = arg;
assert(ctx->stack_count >= 1);
if (ctx->stack[ctx->stack_count-1]->token_type != TT_MARK) {
assert(ctx->stack_count >= 2 && ctx->stack[ctx->stack_count-2]->token_type == TT_MARK);
res.ast = ctx->stack[ctx->stack_count-2] = ctx->stack[ctx->stack_count-1];
ctx->stack_count--;
// mark replaced.
} else {
res.ast = NULL;
}
res.arena = arena;
HParsedToken *tok = action(&res);
if (tok != NULL)
ctx->stack[ctx->stack_count-1] = tok;
else
ctx->stack_count--;
return true; // action can't fail
}
static bool action_ctrvm(HRVMProg *prog, void* env) {
HParseAction *a = (HParseAction*)env;
h_rvm_insert_insn(prog, RVM_PUSH, 0);
if (!h_compile_regex(prog, a->p))
return false;
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_action, a->action));
return true;
}
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,
.compile_to_rvm = action_ctrvm,
2012-05-26 16:00:43 +02:00
};
2013-04-26 20:36:54 -07:00
HParser* h_action(const HParser* p, const HAction a) {
2012-10-10 15:58:03 +02:00
return h_action__m(&system_allocator, p, a);
}
2013-04-26 20:36:54 -07:00
HParser* h_action__m(HAllocator* mm__, const HParser* p, const HAction a) {
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;
return h_new_parser(mm__, &action_vt, env);
2012-05-26 16:00:43 +02:00
}