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
44 lines
1.2 KiB
C
44 lines
1.2 KiB
C
#include "parser_internal.h"
|
|
|
|
static HParseResult* parse_ch(void* env, HParseState *state) {
|
|
uint8_t c = (uint8_t)(unsigned long)(env);
|
|
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;
|
|
return make_result(state->arena, tok);
|
|
} else {
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
static bool ch_ctrvm(HRVMProg *prog, void* env) {
|
|
uint8_t c = (uint8_t)(unsigned long)(env);
|
|
// TODO: Does this capture anything?
|
|
h_rvm_insert_insn(prog, RVM_MATCH, c & c << 8);
|
|
h_rvm_insert_insn(prog, RVM_STEP, 0);
|
|
return true;
|
|
}
|
|
|
|
static const HParserVtable ch_vt = {
|
|
.parse = parse_ch,
|
|
.isValidRegular = h_true,
|
|
.isValidCF = h_true,
|
|
.desugar = desugar_ch,
|
|
.compile_to_rvm = ch_ctrvm,
|
|
};
|
|
|
|
HParser* h_ch(const uint8_t c) {
|
|
return h_ch__m(&system_allocator, c);
|
|
}
|
|
HParser* h_ch__m(HAllocator* mm__, const uint8_t c) {
|
|
return h_new_parser(mm__, &ch_vt, (void *)(uintptr_t)c);
|
|
}
|