2012-05-26 16:00:43 +02:00
|
|
|
#include "parser_internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const HParser *p;
|
|
|
|
|
int64_t lower;
|
|
|
|
|
int64_t upper;
|
|
|
|
|
} HRange;
|
|
|
|
|
|
|
|
|
|
static HParseResult* parse_int_range(void *env, HParseState *state) {
|
|
|
|
|
HRange *r_env = (HRange*)env;
|
|
|
|
|
HParseResult *ret = h_do_parse(r_env->p, state);
|
|
|
|
|
if (!ret || !ret->ast)
|
|
|
|
|
return NULL;
|
|
|
|
|
switch(ret->ast->token_type) {
|
|
|
|
|
case TT_SINT:
|
|
|
|
|
if (r_env->lower <= ret->ast->sint && r_env->upper >= ret->ast->sint)
|
|
|
|
|
return ret;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
case TT_UINT:
|
|
|
|
|
if ((uint64_t)r_env->lower <= ret->ast->uint && (uint64_t)r_env->upper >= ret->ast->uint)
|
|
|
|
|
return ret;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
default:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 03:35:42 +02:00
|
|
|
void gen_int_range(HAllocator *mm__, HCFStack *stk__, uint64_t low, uint64_t high, uint8_t bytes) {
|
2013-02-20 20:43:16 -05:00
|
|
|
/* Possible FIXME: TallerThanMe */
|
2013-02-02 19:31:18 -05:00
|
|
|
if (1 == bytes) {
|
2013-05-25 03:35:42 +02:00
|
|
|
HCharset cs = new_charset(mm__);
|
2013-02-02 19:31:18 -05:00
|
|
|
for (uint64_t i=low; i<=high; ++i) {
|
2013-05-25 03:35:42 +02:00
|
|
|
charset_set(cs, i, 1);
|
2013-02-02 19:31:18 -05:00
|
|
|
}
|
2013-05-25 03:35:42 +02:00
|
|
|
HCFS_ADD_CHARSET(cs);
|
2013-02-02 19:31:18 -05:00
|
|
|
}
|
|
|
|
|
else if (1 < bytes) {
|
2013-02-03 02:18:19 -05:00
|
|
|
uint8_t low_head, hi_head;
|
|
|
|
|
low_head = ((low >> (8*(bytes - 1))) & 0xFF);
|
|
|
|
|
hi_head = ((high >> (8*(bytes - 1))) & 0xFF);
|
|
|
|
|
if (low_head != hi_head) {
|
2013-05-25 03:35:42 +02:00
|
|
|
HCFS_BEGIN_CHOICE() {
|
|
|
|
|
HCFS_BEGIN_SEQ() {
|
|
|
|
|
HCFS_ADD_CHAR(low_head);
|
|
|
|
|
gen_int_range(mm__, stk__, low & ((1 << (8 * (bytes - 1))) - 1), ((1 << (8*(bytes-1)))-1), bytes-1);
|
|
|
|
|
} HCFS_END_SEQ();
|
|
|
|
|
HCFS_BEGIN_SEQ() {
|
|
|
|
|
HCharset hd = new_charset(mm__);
|
|
|
|
|
HCharset rest = new_charset(mm__);
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
|
charset_set(hd, i, (i > low_head && i < hi_head));
|
|
|
|
|
charset_set(rest, i, 1);
|
|
|
|
|
}
|
|
|
|
|
HCFS_ADD_CHARSET(hd);
|
|
|
|
|
for (int i = 2; i < bytes; i++)
|
|
|
|
|
HCFS_ADD_CHARSET(rest);
|
|
|
|
|
} HCFS_END_SEQ();
|
|
|
|
|
HCFS_BEGIN_SEQ() {
|
|
|
|
|
HCFS_ADD_CHAR(hi_head);
|
|
|
|
|
gen_int_range(mm__, stk__, 0, high & ((1 << (8 * (bytes - 1))) - 1), bytes-1);
|
|
|
|
|
} HCFS_END_SEQ();
|
|
|
|
|
} HCFS_END_CHOICE();
|
2013-02-03 02:18:19 -05:00
|
|
|
} else {
|
2013-05-25 03:35:42 +02:00
|
|
|
// TODO: find a way to merge this with the higher-up SEQ
|
|
|
|
|
HCFS_BEGIN_CHOICE() {
|
|
|
|
|
HCFS_BEGIN_SEQ() {
|
|
|
|
|
HCFS_ADD_CHAR(low_head);
|
|
|
|
|
gen_int_range(mm__, stk__,
|
|
|
|
|
low & ((1 << (8 * (bytes - 1))) - 1),
|
|
|
|
|
high & ((1 << (8 * (bytes - 1))) - 1),
|
|
|
|
|
bytes - 1);
|
|
|
|
|
} HCFS_END_SEQ();
|
|
|
|
|
} HCFS_END_CHOICE();
|
2013-02-03 02:18:19 -05:00
|
|
|
}
|
2013-02-02 19:31:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 02:18:19 -05:00
|
|
|
struct bits_env {
|
|
|
|
|
uint8_t length;
|
|
|
|
|
uint8_t signedp;
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-25 03:35:42 +02:00
|
|
|
static void desugar_int_range(HAllocator *mm__, HCFStack *stk__, void *env) {
|
2013-02-02 19:31:18 -05:00
|
|
|
HRange *r = (HRange*)env;
|
2013-02-03 02:18:19 -05:00
|
|
|
struct bits_env* be = (struct bits_env*)r->p->env;
|
|
|
|
|
uint8_t bytes = be->length / 8;
|
2013-05-25 03:35:42 +02:00
|
|
|
gen_int_range(mm__, stk__, r->lower, r->upper, bytes);
|
2013-02-02 19:31:18 -05:00
|
|
|
}
|
|
|
|
|
|
2013-04-22 18:06:17 -07:00
|
|
|
bool h_svm_action_validate_int_range(HArena *arena, HSVMContext *ctx, void* env) {
|
2013-04-26 20:36:54 -07:00
|
|
|
HRange *r_env = (HRange*)env;
|
2013-04-22 18:06:17 -07:00
|
|
|
HParsedToken *head = ctx->stack[ctx->stack_count-1];
|
|
|
|
|
switch (head-> token_type) {
|
|
|
|
|
case TT_SINT:
|
|
|
|
|
return head->sint >= r_env->lower && head->sint <= r_env->upper;
|
|
|
|
|
case TT_UINT:
|
|
|
|
|
return head->uint >= (uint64_t)r_env->lower && head->uint <= (uint64_t)r_env->upper;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-11 19:04:59 +02:00
|
|
|
|
2013-04-22 18:06:17 -07:00
|
|
|
static bool ir_ctrvm(HRVMProg *prog, void *env) {
|
2013-04-26 20:36:54 -07:00
|
|
|
HRange *r_env = (HRange*)env;
|
2013-04-22 18:06:17 -07:00
|
|
|
|
|
|
|
|
h_compile_regex(prog, r_env->p);
|
|
|
|
|
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_validate_int_range, env));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 16:00:43 +02:00
|
|
|
static const HParserVtable int_range_vt = {
|
|
|
|
|
.parse = parse_int_range,
|
2012-12-18 18:10:40 -05:00
|
|
|
.isValidRegular = h_true,
|
|
|
|
|
.isValidCF = h_true,
|
2013-02-02 19:31:18 -05:00
|
|
|
.desugar = desugar_int_range,
|
2013-04-22 18:06:17 -07:00
|
|
|
.compile_to_rvm = ir_ctrvm,
|
2015-10-03 17:54:11 +02:00
|
|
|
.higher = false,
|
2012-05-26 16:00:43 +02:00
|
|
|
};
|
|
|
|
|
|
2013-04-26 20:36:54 -07:00
|
|
|
HParser* h_int_range(const HParser *p, const int64_t lower, const int64_t upper) {
|
2012-10-10 15:58:03 +02:00
|
|
|
return h_int_range__m(&system_allocator, p, lower, upper);
|
|
|
|
|
}
|
2013-04-26 20:36:54 -07:00
|
|
|
HParser* h_int_range__m(HAllocator* mm__, const HParser *p, const int64_t lower, const int64_t upper) {
|
2012-05-26 16:00:43 +02:00
|
|
|
// p must be an integer parser, which means it's using parse_bits
|
|
|
|
|
// TODO: re-add this check
|
|
|
|
|
//assert_message(p->vtable == &bits_vt, "int_range requires an integer parser");
|
|
|
|
|
|
|
|
|
|
// and regardless, the bounds need to fit in the parser in question
|
|
|
|
|
// TODO: check this as well.
|
|
|
|
|
|
2012-10-10 15:58:03 +02:00
|
|
|
HRange *r_env = h_new(HRange, 1);
|
2012-05-26 16:00:43 +02:00
|
|
|
r_env->p = p;
|
|
|
|
|
r_env->lower = lower;
|
|
|
|
|
r_env->upper = upper;
|
2013-04-27 04:17:47 +02:00
|
|
|
return h_new_parser(mm__, &int_range_vt, r_env);
|
2012-05-26 16:00:43 +02:00
|
|
|
}
|