Conflicts: src/Makefile src/backends/packrat.c src/compile.c src/hammer.h src/internal.h src/parsers/action.c src/parsers/and.c src/parsers/attr_bool.c src/parsers/bits.c src/parsers/butnot.c src/parsers/ch.c src/parsers/charset.c src/parsers/choice.c src/parsers/difference.c src/parsers/end.c src/parsers/epsilon.c src/parsers/ignore.c src/parsers/ignoreseq.c src/parsers/indirect.c src/parsers/int_range.c src/parsers/many.c src/parsers/not.c src/parsers/nothing.c src/parsers/optional.c src/parsers/sequence.c src/parsers/token.c src/parsers/unimplemented.c src/parsers/whitespace.c src/parsers/xor.c
68 lines
2 KiB
C
68 lines
2 KiB
C
#include <assert.h>
|
|
#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;
|
|
return make_result(state->arena, ast);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
static HCFChoice* desugar_optional(HAllocator *mm__, void *env) {
|
|
HParser *p = (HParser*) env;
|
|
return h_desugar(mm__, p);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
static bool opt_ctrvm(HRVMProg *prog, void* env) {
|
|
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
|
uint16_t insn = h_rvm_insert_insn(prog, RVM_FORK, 0);
|
|
HParser *p = (HParser*) env;
|
|
if (!h_compile_regex(prog, p->env))
|
|
return false;
|
|
h_rvm_patch_arg(prog, insn, h_rvm_get_ip(prog));
|
|
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_optional, NULL));
|
|
return true;
|
|
}
|
|
|
|
static const HParserVtable optional_vt = {
|
|
.parse = parse_optional,
|
|
.isValidRegular = opt_isValidRegular,
|
|
.isValidCF = opt_isValidCF,
|
|
.desugar = desugar_optional,
|
|
.compile_to_rvm = opt_ctrvm,
|
|
};
|
|
|
|
HParser* h_optional(const HParser* p) {
|
|
return h_optional__m(&system_allocator, p);
|
|
}
|
|
HParser* h_optional__m(HAllocator* mm__, const HParser* p) {
|
|
// TODO: re-add this
|
|
//assert_message(p->vtable != &ignore_vt, "Thou shalt ignore an option, rather than the other way 'round.");
|
|
return h_new_parser(mm__, &optional_vt, (void *)p);
|
|
}
|
|
|