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-05-24 15:07:47 +02:00
|
|
|
static HParsedToken* reshape_optional(const HParseResult *p) {
|
2013-05-20 16:25:42 +02:00
|
|
|
assert(p->ast);
|
|
|
|
|
assert(p->ast->token_type == TT_SEQUENCE);
|
|
|
|
|
assert(p->ast->seq->used > 0);
|
|
|
|
|
|
|
|
|
|
HParsedToken *res = p->ast->seq->elements[0];
|
|
|
|
|
if(res)
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
HParsedToken *ret = h_arena_malloc(p->arena, sizeof(HParsedToken));
|
|
|
|
|
ret->token_type = TT_NONE;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 02:18:19 -05:00
|
|
|
static HCFChoice* desugar_optional(HAllocator *mm__, 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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
HCFChoice *ret = h_new(HCFChoice, 1);
|
|
|
|
|
HCFChoice *a = h_desugar(mm__, p);
|
|
|
|
|
HCFChoice *eps = desugar_epsilon(mm__, NULL);
|
|
|
|
|
|
|
|
|
|
ret->type = HCF_CHOICE;
|
|
|
|
|
ret->seq = h_new(HCFSequence*, 3); /* enough for 2 productions */
|
|
|
|
|
|
|
|
|
|
ret->seq[0] = h_new(HCFSequence, 1);
|
|
|
|
|
ret->seq[0]->items = h_new(HCFChoice*, 2);
|
|
|
|
|
ret->seq[0]->items[0] = a;
|
|
|
|
|
ret->seq[0]->items[1] = NULL;
|
|
|
|
|
|
|
|
|
|
ret->seq[1] = h_new(HCFSequence, 1);
|
|
|
|
|
ret->seq[1]->items = h_new(HCFChoice*, 2);
|
|
|
|
|
ret->seq[1]->items[0] = eps;
|
|
|
|
|
ret->seq[1]->items[1] = NULL;
|
|
|
|
|
|
|
|
|
|
ret->seq[2] = NULL;
|
|
|
|
|
|
|
|
|
|
ret->reshape = reshape_optional;
|
|
|
|
|
|
|
|
|
|
return ret;
|
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
|
|
|
}
|
|
|
|
|
|