2012-05-26 16:00:43 +02:00
|
|
|
#include "parser_internal.h"
|
|
|
|
|
|
|
|
|
|
static HParseResult* parse_ch(void* env, HParseState *state) {
|
2012-10-10 16:24:12 +02:00
|
|
|
uint8_t c = (uint8_t)(unsigned long)(env);
|
2012-05-26 16:00:43 +02:00
|
|
|
uint8_t r = (uint8_t)h_read_bits(&state->input_stream, 8, false);
|
|
|
|
|
if (c == r) {
|
|
|
|
|
HParsedToken *tok = a_new(HParsedToken, 1);
|
|
|
|
|
tok->token_type = TT_UINT; tok->uint = r;
|
2013-05-11 19:04:59 +02:00
|
|
|
return make_result(state->arena, tok);
|
2012-05-26 16:00:43 +02:00
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-02 19:31:18 -05:00
|
|
|
static HCFChoice* desugar_ch(HAllocator *mm__, void *env) {
|
|
|
|
|
HCFChoice *ret = h_new(HCFChoice, 1);
|
|
|
|
|
ret->type = HCF_CHAR;
|
|
|
|
|
ret->chr = (uint8_t)(unsigned long)(env);
|
|
|
|
|
ret->action = NULL;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-18 01:18:39 -04:00
|
|
|
static bool ch_ctrvm(HRVMProg *prog, void* env) {
|
|
|
|
|
uint8_t c = (uint8_t)(unsigned long)(env);
|
2013-04-26 20:36:54 -07:00
|
|
|
// TODO: Does this capture anything?
|
2013-03-18 01:18:39 -04:00
|
|
|
h_rvm_insert_insn(prog, RVM_MATCH, c & c << 8);
|
|
|
|
|
h_rvm_insert_insn(prog, RVM_STEP, 0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 16:00:43 +02:00
|
|
|
static const HParserVtable ch_vt = {
|
|
|
|
|
.parse = parse_ch,
|
2012-12-18 18:10:40 -05:00
|
|
|
.isValidRegular = h_true,
|
|
|
|
|
.isValidCF = h_true,
|
2013-02-02 19:31:18 -05:00
|
|
|
.desugar = desugar_ch,
|
2013-03-18 01:18:39 -04:00
|
|
|
.compile_to_rvm = ch_ctrvm,
|
2012-05-26 16:00:43 +02:00
|
|
|
};
|
2012-10-10 15:58:03 +02:00
|
|
|
|
2013-04-26 20:36:54 -07:00
|
|
|
HParser* h_ch(const uint8_t c) {
|
2012-10-10 15:58:03 +02:00
|
|
|
return h_ch__m(&system_allocator, c);
|
|
|
|
|
}
|
2013-04-26 20:36:54 -07:00
|
|
|
HParser* h_ch__m(HAllocator* mm__, const uint8_t c) {
|
2013-04-27 04:17:47 +02:00
|
|
|
return h_new_parser(mm__, &ch_vt, (void *)(uintptr_t)c);
|
2012-05-26 16:00:43 +02:00
|
|
|
}
|