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-02-02 19:31:18 -05:00
|
|
|
HCFChoice* gen_int_range(HAllocator *mm__, uint64_t low, uint64_t high, uint8_t bytes) {
|
|
|
|
|
if (1 == bytes) {
|
|
|
|
|
HCFChoice *cs = h_new(HCFChoice, 1);
|
|
|
|
|
cs->type = HCF_CHARSET;
|
|
|
|
|
cs->charset = new_charset(mm__);
|
|
|
|
|
for (uint64_t i=low; i<=high; ++i) {
|
|
|
|
|
charset_set(cs->charset, i, 1);
|
|
|
|
|
}
|
|
|
|
|
cs->action = NULL;
|
|
|
|
|
return cs;
|
|
|
|
|
}
|
|
|
|
|
else if (1 < bytes) {
|
|
|
|
|
HCFChoice *root = h_new(HCFChoice, 1);
|
|
|
|
|
root->type = HCF_CHOICE;
|
|
|
|
|
root->seq = h_new(HCFSequence*, 4);
|
|
|
|
|
root->seq[0] = h_new(HCFSequence, 1);
|
|
|
|
|
root->seq[0]->items = h_new(HCFChoice*, 2);
|
|
|
|
|
root->seq[0]->items[0] = gen_int_range(mm__, low, high, FIXME);
|
|
|
|
|
root->seq[0]->items[1] = NULL;
|
|
|
|
|
root->seq[1] = h_new(HCFSequence, 1);
|
|
|
|
|
root->seq[1]->items = h_new(HCFChoice*, 2);
|
|
|
|
|
root->seq[1]->items[0] = h_new(HCFChoice, 1);
|
|
|
|
|
/* do something with root->seq[1]->items[0] */
|
|
|
|
|
root->seq[1]->items[1] = NULL;
|
|
|
|
|
root->seq[2] = h_new(HCFSequence, 1);
|
|
|
|
|
root->seq[2]->items = h_new(HCFChoice*, 2);
|
|
|
|
|
root->seq[2]->items[0] = gen_int_range(mm__, low, high, FIXME);
|
|
|
|
|
root->seq[2]->items[1] = NULL;
|
|
|
|
|
root->seq[3] = NULL;
|
|
|
|
|
root->action = NULL;
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
else { // idk why this would ever be <1, but whatever
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static HCFChoice* desugar_int_range(HAllocator *mm__, void *env) {
|
|
|
|
|
HRange *r = (HRange*)env;
|
|
|
|
|
HCFChoice *ret = h_new(HCFChoice, 1);
|
|
|
|
|
ret->type = HCF_CHOICE;
|
|
|
|
|
uint8_t bytes = r->p->env->length / 8;
|
|
|
|
|
HCFSequence *seq = h_new(HCFSequence, 1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2012-05-26 16:00:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const 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);
|
|
|
|
|
}
|
|
|
|
|
const 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;
|
2012-10-10 15:58:03 +02:00
|
|
|
HParser *ret = h_new(HParser, 1);
|
2012-05-26 16:00:43 +02:00
|
|
|
ret->vtable = &int_range_vt;
|
|
|
|
|
ret->env = (void*)r_env;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|