2013-05-24 15:07:47 +02:00
|
|
|
#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);
|
2013-05-11 19:04:59 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 03:35:42 +02:00
|
|
|
static void desugar_action(HAllocator *mm__, HCFStack *stk__, void *env) {
|
2013-02-02 19:31:18 -05:00
|
|
|
HParseAction *a = (HParseAction*)env;
|
2013-05-25 03:35:42 +02:00
|
|
|
|
|
|
|
|
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();
|
2013-02-02 19:31:18 -05:00
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 15:07:47 +02:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-22 18:06:17 -07:00
|
|
|
static bool action_ctrvm(HRVMProg *prog, void* env) {
|
|
|
|
|
HParseAction *a = (HParseAction*)env;
|
2013-05-24 15:07:47 +02:00
|
|
|
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;
|
2013-04-22 18:06:17 -07:00
|
|
|
}
|
|
|
|
|
|
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,
|
2013-04-22 18:06:17 -07:00
|
|
|
.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;
|
2013-04-27 04:17:47 +02:00
|
|
|
return h_new_parser(mm__, &action_vt, env);
|
2012-05-26 16:00:43 +02:00
|
|
|
}
|