2013-04-26 20:36:54 -07:00
|
|
|
#include <assert.h>
|
2012-05-26 16:00:43 +02:00
|
|
|
#include "parser_internal.h"
|
|
|
|
|
|
|
|
|
|
static HParseResult* parse_optional(void* env, HParseState* state) {
|
|
|
|
|
HInputStream bak = state->input_stream;
|
|
|
|
|
HParseResult *res0 = h_do_parse((HParser*)env, state);
|
|
|
|
|
if (res0)
|
|
|
|
|
return res0;
|
|
|
|
|
state->input_stream = bak;
|
|
|
|
|
HParsedToken *ast = a_new(HParsedToken, 1);
|
|
|
|
|
ast->token_type = TT_NONE;
|
2013-05-11 19:04:59 +02:00
|
|
|
return make_result(state->arena, ast);
|
2012-05-26 16:00:43 +02:00
|
|
|
}
|
|
|
|
|
|
2012-12-18 18:10:40 -05:00
|
|
|
static bool opt_isValidRegular(void *env) {
|
|
|
|
|
HParser *p = (HParser*) env;
|
|
|
|
|
return p->vtable->isValidRegular(p->env);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool opt_isValidCF(void *env) {
|
|
|
|
|
HParser *p = (HParser*) env;
|
|
|
|
|
return p->vtable->isValidCF(p->env);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-23 12:53:11 -06:00
|
|
|
static HParsedToken* reshape_optional(const HParseResult *p, void* user_data) {
|
2013-05-20 16:25:42 +02:00
|
|
|
assert(p->ast);
|
|
|
|
|
assert(p->ast->token_type == TT_SEQUENCE);
|
|
|
|
|
|
2013-05-25 03:35:42 +02:00
|
|
|
if (p->ast->seq->used > 0) {
|
|
|
|
|
HParsedToken *res = p->ast->seq->elements[0];
|
|
|
|
|
if(res)
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2013-05-20 16:25:42 +02:00
|
|
|
|
|
|
|
|
HParsedToken *ret = h_arena_malloc(p->arena, sizeof(HParsedToken));
|
|
|
|
|
ret->token_type = TT_NONE;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 03:35:42 +02:00
|
|
|
static void desugar_optional(HAllocator *mm__, HCFStack *stk__, void *env) {
|
2013-02-20 18:58:15 -05:00
|
|
|
HParser *p = (HParser*) env;
|
2013-05-20 16:25:42 +02:00
|
|
|
|
|
|
|
|
/* optional(A) =>
|
|
|
|
|
S -> A
|
|
|
|
|
-> \epsilon
|
|
|
|
|
*/
|
|
|
|
|
|
2013-05-25 03:35:42 +02:00
|
|
|
HCFS_BEGIN_CHOICE() {
|
|
|
|
|
HCFS_BEGIN_SEQ() {
|
|
|
|
|
HCFS_DESUGAR(p);
|
|
|
|
|
} HCFS_END_SEQ();
|
|
|
|
|
HCFS_BEGIN_SEQ() {
|
|
|
|
|
} HCFS_END_SEQ();
|
|
|
|
|
HCFS_THIS_CHOICE->reshape = reshape_optional;
|
2013-11-23 12:53:11 -06:00
|
|
|
HCFS_THIS_CHOICE->user_data = NULL;
|
2013-05-25 03:35:42 +02:00
|
|
|
} HCFS_END_CHOICE();
|
2013-02-03 02:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
2013-04-26 20:36:54 -07:00
|
|
|
static bool h_svm_action_optional(HArena *arena, HSVMContext *ctx, void *env) {
|
|
|
|
|
if (ctx->stack[ctx->stack_count-1]->token_type == TT_MARK) {
|
|
|
|
|
ctx->stack[ctx->stack_count-1]->token_type = TT_NONE;
|
|
|
|
|
} else {
|
|
|
|
|
ctx->stack_count--;
|
|
|
|
|
assert(ctx->stack[ctx->stack_count-1]->token_type == TT_MARK);
|
|
|
|
|
ctx->stack[ctx->stack_count-1] = ctx->stack[ctx->stack_count];
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-18 01:18:39 -04:00
|
|
|
static bool opt_ctrvm(HRVMProg *prog, void* env) {
|
2013-04-26 20:36:54 -07:00
|
|
|
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
2013-03-18 01:02:59 -04:00
|
|
|
uint16_t insn = h_rvm_insert_insn(prog, RVM_FORK, 0);
|
2013-03-18 01:18:39 -04:00
|
|
|
HParser *p = (HParser*) env;
|
2013-05-24 15:07:47 +02:00
|
|
|
if (!h_compile_regex(prog, p))
|
2013-03-18 01:02:59 -04:00
|
|
|
return false;
|
|
|
|
|
h_rvm_patch_arg(prog, insn, h_rvm_get_ip(prog));
|
2013-04-26 20:36:54 -07:00
|
|
|
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_optional, NULL));
|
2013-03-18 01:02:59 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 16:00:43 +02:00
|
|
|
static const HParserVtable optional_vt = {
|
|
|
|
|
.parse = parse_optional,
|
2012-12-18 18:10:40 -05:00
|
|
|
.isValidRegular = opt_isValidRegular,
|
|
|
|
|
.isValidCF = opt_isValidCF,
|
2013-02-03 02:18:19 -05:00
|
|
|
.desugar = desugar_optional,
|
2013-03-18 01:18:39 -04:00
|
|
|
.compile_to_rvm = opt_ctrvm,
|
2012-05-26 16:00:43 +02:00
|
|
|
};
|
|
|
|
|
|
2013-04-26 20:36:54 -07:00
|
|
|
HParser* h_optional(const HParser* p) {
|
2012-10-10 15:58:03 +02:00
|
|
|
return h_optional__m(&system_allocator, p);
|
|
|
|
|
}
|
2013-04-26 20:36:54 -07:00
|
|
|
HParser* h_optional__m(HAllocator* mm__, const HParser* p) {
|
2012-05-26 16:00:43 +02:00
|
|
|
// TODO: re-add this
|
|
|
|
|
//assert_message(p->vtable != &ignore_vt, "Thou shalt ignore an option, rather than the other way 'round.");
|
2013-04-27 04:17:47 +02:00
|
|
|
return h_new_parser(mm__, &optional_vt, (void *)p);
|
2012-05-26 16:00:43 +02:00
|
|
|
}
|
|
|
|
|
|