Refactored all of the desugaring code to no longer depend on memory being initialized to 0. Everything is about 12% faster now.
This commit is contained in:
parent
d71215d494
commit
ec404ca8fe
36 changed files with 411 additions and 417 deletions
|
|
@ -20,20 +20,16 @@ static HParseResult* parse_action(void *env, HParseState *state) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_action(HAllocator *mm__, void *env) {
|
||||
static void desugar_action(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HParseAction *a = (HParseAction*)env;
|
||||
HCFSequence *seq = h_new(HCFSequence, 1);
|
||||
seq->items = h_new(HCFChoice*, 2);
|
||||
seq->items[0] = h_desugar(mm__, a->p);
|
||||
seq->items[1] = NULL;
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 2);
|
||||
ret->seq[0] = seq;
|
||||
ret->seq[1] = NULL;
|
||||
ret->action = a->action;
|
||||
ret->reshape = h_act_first;
|
||||
return ret;
|
||||
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_DESUGAR(a->p);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->action = a->action;
|
||||
HCFS_THIS_CHOICE->reshape = h_act_first;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool action_isValidRegular(void *env) {
|
||||
|
|
|
|||
|
|
@ -9,11 +9,6 @@ static HParseResult *parse_and(void* env, HParseState* state) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_and(HAllocator *mm__, void *env) {
|
||||
assert_message(0, "Not context-free, can't be desugared");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const HParserVtable and_vt = {
|
||||
.parse = parse_and,
|
||||
.isValidRegular = h_false, /* TODO: strictly speaking this should be regular,
|
||||
|
|
@ -21,7 +16,6 @@ static const HParserVtable and_vt = {
|
|||
to get right, so we're leaving it for a future
|
||||
revision. --mlp, 18/12/12 */
|
||||
.isValidCF = h_false, /* despite TODO above, this remains false. */
|
||||
.desugar = desugar_and,
|
||||
.compile_to_rvm = h_not_regular,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -33,20 +33,16 @@ static bool ab_isValidCF(void *env) {
|
|||
return ab->p->vtable->isValidCF(ab->p->env);
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_ab(HAllocator *mm__, void *env) {
|
||||
static void desugar_ab(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
|
||||
HAttrBool *a = (HAttrBool*)env;
|
||||
HCFSequence *seq = h_new(HCFSequence, 1);
|
||||
seq->items = h_new(HCFChoice*, 2);
|
||||
seq->items[0] = h_desugar(mm__, a->p);
|
||||
seq->items[1] = NULL;
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 2);
|
||||
ret->seq[0] = seq;
|
||||
ret->seq[1] = NULL;
|
||||
ret->pred = a->pred;
|
||||
ret->reshape = h_act_first;
|
||||
return ret;
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_DESUGAR(a->p);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->pred = a->pred;
|
||||
HCFS_THIS_CHOICE->reshape = h_act_first;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool h_svm_action_attr_bool(HArena *arena, HSVMContext *ctx, void* arg) {
|
||||
|
|
|
|||
|
|
@ -52,41 +52,25 @@ static HParsedToken *reshape_bits_signed(const HParseResult *p) {
|
|||
return reshape_bits(p, true);
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_bits(HAllocator *mm__, void *env) {
|
||||
static void desugar_bits(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
struct bits_env *bits = (struct bits_env*)env;
|
||||
if (0 != bits->length % 8)
|
||||
return NULL; // can't handle non-byte-aligned for now
|
||||
assert (0 == bits->length % 8);
|
||||
|
||||
HCharset match_all = new_charset(mm__);
|
||||
for (int i = 0; i < 256; i++)
|
||||
charset_set(match_all, i, 1);
|
||||
|
||||
HCFChoice *match_all_choice = h_new(HCFChoice, 1);
|
||||
match_all_choice->type = HCF_CHARSET;
|
||||
match_all_choice->charset = match_all;
|
||||
match_all_choice->action = NULL;
|
||||
|
||||
size_t n = bits->length/8;
|
||||
HCFSequence *seq = h_new(HCFSequence, 1);
|
||||
seq->items = h_new(HCFChoice*, n+1);
|
||||
for (size_t i=0; i<n; ++i) {
|
||||
seq->items[i] = match_all_choice;
|
||||
}
|
||||
seq->items[n] = NULL;
|
||||
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 2);
|
||||
ret->seq[0] = seq;
|
||||
ret->seq[1] = NULL;
|
||||
ret->action = NULL;
|
||||
|
||||
if(bits->signedp)
|
||||
ret->reshape = reshape_bits_signed;
|
||||
else
|
||||
ret->reshape = reshape_bits_unsigned;
|
||||
|
||||
return ret;
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
size_t n = bits->length/8;
|
||||
for (size_t i=0; i<n; ++i) {
|
||||
HCFS_ADD_CHARSET(match_all);
|
||||
}
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = bits->signedp
|
||||
? reshape_bits_signed
|
||||
: reshape_bits_unsigned;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool h_svm_action_bits(HArena *arena, HSVMContext *ctx, void* env) {
|
||||
|
|
|
|||
|
|
@ -35,16 +35,10 @@ static HParseResult* parse_butnot(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_butnot(HAllocator *mm__, void *env) {
|
||||
assert_message(0, "'h_butnot' is not context-free, can't be desugared");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const HParserVtable butnot_vt = {
|
||||
.parse = parse_butnot,
|
||||
.isValidRegular = h_false,
|
||||
.isValidCF = h_false, // XXX should this be true if both p1 and p2 are CF?
|
||||
.desugar = desugar_butnot,
|
||||
.compile_to_rvm = h_not_regular,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -13,12 +13,8 @@ static HParseResult* parse_ch(void* env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
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 void desugar_ch(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HCFS_ADD_CHAR( (uint8_t)(unsigned long)(env) );
|
||||
}
|
||||
|
||||
static bool h_svm_action_ch(HArena *arena, HSVMContext *ctx, void* env) {
|
||||
|
|
|
|||
|
|
@ -15,12 +15,8 @@ static HParseResult* parse_charset(void *env, HParseState *state) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_charset(HAllocator *mm__, void *env) {
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHARSET;
|
||||
ret->charset = (HCharset)env;
|
||||
ret->action = NULL;
|
||||
return ret;
|
||||
static void desugar_charset(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HCFS_ADD_CHARSET( (HCharset)env );
|
||||
}
|
||||
|
||||
static bool h_svm_action_ch(HArena *arena, HSVMContext *ctx, void* env) {
|
||||
|
|
|
|||
|
|
@ -39,20 +39,16 @@ static bool choice_isValidCF(void *env) {
|
|||
return true;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_choice(HAllocator *mm__, void *env) {
|
||||
static void desugar_choice(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HSequence *s = (HSequence*)env;
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 1+s->len);
|
||||
for (size_t i=0; i<s->len; ++i) {
|
||||
ret->seq[i] = h_new(HCFSequence, 1);
|
||||
ret->seq[i]->items = h_new(HCFChoice*, 2);
|
||||
ret->seq[i]->items[0] = h_desugar(mm__, s->p_array[i]);
|
||||
ret->seq[i]->items[1] = NULL;
|
||||
}
|
||||
ret->seq[s->len] = NULL;
|
||||
ret->reshape = h_act_first;
|
||||
return ret;
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
for (size_t i = 0; i < s->len; i++) {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_DESUGAR(s->p_array[i]);
|
||||
} HCFS_END_SEQ();
|
||||
}
|
||||
HCFS_THIS_CHOICE->reshape = h_act_first;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool choice_ctrvm(HRVMProg *prog, void* env) {
|
||||
|
|
|
|||
|
|
@ -34,16 +34,10 @@ static HParseResult* parse_difference(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_difference(HAllocator *mm__, void *env) {
|
||||
assert_message(0, "'h_difference' is not context-free, can't be desugared");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static HParserVtable difference_vt = {
|
||||
.parse = parse_difference,
|
||||
.isValidRegular = h_false,
|
||||
.isValidCF = h_false, // XXX should this be true if both p1 and p2 are CF?
|
||||
.desugar = desugar_difference,
|
||||
.compile_to_rvm = h_not_regular,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -10,11 +10,8 @@ static HParseResult* parse_end(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_end(HAllocator *mm__, void *env) {
|
||||
static HCFChoice ret = {
|
||||
.type = HCF_END
|
||||
};
|
||||
return &ret;
|
||||
static void desugar_end(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HCFS_ADD_END();
|
||||
}
|
||||
|
||||
static bool end_ctrvm(HRVMProg *prog, void *env) {
|
||||
|
|
|
|||
|
|
@ -25,6 +25,9 @@ HParser* h_epsilon_p() {
|
|||
}
|
||||
HParser* h_epsilon_p__m(HAllocator* mm__) {
|
||||
HParser *epsilon_p = h_new(HParser, 1);
|
||||
epsilon_p->desugared = NULL;
|
||||
epsilon_p->backend_data = NULL;
|
||||
epsilon_p->backend = 0;
|
||||
epsilon_p->vtable = &epsilon_vt;
|
||||
return epsilon_p;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,22 +21,13 @@ static bool ignore_isValidCF(void *env) {
|
|||
return (p->vtable->isValidCF(p->env));
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_ignore(HAllocator *mm__, void *env) {
|
||||
HParser *p = (HParser*)env;
|
||||
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
HCFChoice *a = h_desugar(mm__, p);
|
||||
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 2);
|
||||
ret->seq[0] = h_new(HCFSequence, 1);
|
||||
ret->seq[0]->items = h_new(HCFChoice*, 2);
|
||||
ret->seq[0]->items[0] = a;
|
||||
ret->seq[0]->items[1] = NULL;
|
||||
ret->seq[1] = NULL;
|
||||
ret->reshape = h_act_ignore;
|
||||
|
||||
return ret;
|
||||
static void desugar_ignore(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_DESUGAR( (HParser*)env );
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = h_act_ignore;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool h_svm_action_pop(HArena *arena, HSVMContext *ctx, void* arg) {
|
||||
|
|
|
|||
|
|
@ -31,31 +31,24 @@ static HParseResult* parse_ignoreseq(void* env, HParseState *state) {
|
|||
return res;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_ignoreseq(HAllocator *mm__, void *env) {
|
||||
static void desugar_ignoreseq(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HIgnoreSeq *seq = (HIgnoreSeq*)env;
|
||||
HCFSequence *hseq = h_new(HCFSequence, 1);
|
||||
hseq->items = h_new(HCFChoice*, 1+seq->len);
|
||||
for (size_t i=0; i<seq->len; ++i) {
|
||||
hseq->items[i] = h_desugar(mm__, seq->parsers[i]);
|
||||
}
|
||||
hseq->items[seq->len] = NULL;
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 2);
|
||||
ret->seq[0] = hseq;
|
||||
ret->seq[1] = NULL;
|
||||
ret->action = NULL;
|
||||
|
||||
if(seq->which == 0)
|
||||
ret->reshape = h_act_first;
|
||||
else if(seq->which == 1)
|
||||
ret->reshape = h_act_second; // for h_middle
|
||||
else if(seq->which == seq->len-1)
|
||||
ret->reshape = h_act_last;
|
||||
else
|
||||
ret->reshape = NULL; // XXX
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
for (size_t i=0; i<seq->len; ++i)
|
||||
HCFS_DESUGAR(seq->parsers[i]);
|
||||
} HCFS_END_SEQ();
|
||||
|
||||
return ret;
|
||||
if(seq->which == 0)
|
||||
HCFS_THIS_CHOICE->reshape = h_act_first;
|
||||
else if(seq->which == 1)
|
||||
HCFS_THIS_CHOICE->reshape = h_act_second; // for h_middle
|
||||
else if(seq->which == seq->len-1)
|
||||
HCFS_THIS_CHOICE->reshape = h_act_last;
|
||||
else
|
||||
assert(!"Ignoreseq must select item 0, 1, or n-1");
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool is_isValidRegular(void *env) {
|
||||
|
|
|
|||
|
|
@ -9,9 +9,8 @@ static bool indirect_isValidCF(void *env) {
|
|||
return p->vtable->isValidCF(p->env);
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_indirect(HAllocator *mm__, void *env) {
|
||||
HParser *p = (HParser*)env;
|
||||
return h_desugar(mm__, p);
|
||||
static void desugar_indirect(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HCFS_DESUGAR( (HParser*)env );
|
||||
}
|
||||
|
||||
static const HParserVtable indirect_vt = {
|
||||
|
|
|
|||
|
|
@ -28,85 +28,54 @@ static HParseResult* parse_int_range(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
HCFChoice* gen_int_range(HAllocator *mm__, uint64_t low, uint64_t high, uint8_t bytes) {
|
||||
void gen_int_range(HAllocator *mm__, HCFStack *stk__, uint64_t low, uint64_t high, uint8_t bytes) {
|
||||
/* Possible FIXME: TallerThanMe */
|
||||
if (1 == bytes) {
|
||||
HCFChoice *cs = h_new(HCFChoice, 1);
|
||||
cs->type = HCF_CHARSET;
|
||||
cs->charset = new_charset(mm__);
|
||||
HCharset cs = new_charset(mm__);
|
||||
for (uint64_t i=low; i<=high; ++i) {
|
||||
charset_set(cs->charset, i, 1);
|
||||
charset_set(cs, i, 1);
|
||||
}
|
||||
cs->action = NULL;
|
||||
return cs;
|
||||
HCFS_ADD_CHARSET(cs);
|
||||
}
|
||||
else if (1 < bytes) {
|
||||
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) {
|
||||
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*, 3);
|
||||
root->seq[0]->items[0] = h_new(HCFChoice, 1);
|
||||
root->seq[0]->items[0]->type = HCF_CHAR;
|
||||
root->seq[0]->items[0]->chr = low_head;
|
||||
root->seq[0]->items[0]->action = NULL;
|
||||
root->seq[0]->items[1] = gen_int_range(mm__, low & ((1 << (8 * (bytes - 1))) - 1), ((1 << (8*(bytes-1)))-1), bytes-1);
|
||||
root->seq[0]->items[2] = NULL;
|
||||
root->seq[1] = h_new(HCFSequence, 1);
|
||||
root->seq[1]->items = h_new(HCFChoice*, bytes+1);
|
||||
root->seq[1]->items[0] = h_new(HCFChoice, 2);
|
||||
root->seq[1]->items[0]->type = HCF_CHARSET;
|
||||
root->seq[1]->items[0]->charset = new_charset(mm__);
|
||||
root->seq[1]->items[0]->action = NULL;
|
||||
root->seq[1]->items[1] = root->seq[1]->items[0] + 1;
|
||||
root->seq[1]->items[1]->type = HCF_CHARSET;
|
||||
root->seq[1]->items[1]->charset = new_charset(mm__);
|
||||
for (int i = 0; i < 256; i++) {
|
||||
charset_set(root->seq[1]->items[0]->charset, i, (i > low_head && i < hi_head));
|
||||
charset_set(root->seq[1]->items[1]->charset, i, 1);
|
||||
}
|
||||
root->seq[1]->items[1]->action = NULL;
|
||||
for (int i = 2; i < bytes; i++)
|
||||
root->seq[1]->items[i] = root->seq[1]->items[1];
|
||||
root->seq[1]->items[bytes] = NULL;
|
||||
root->seq[2] = h_new(HCFSequence, 1);
|
||||
root->seq[2]->items = h_new(HCFChoice*, 3);
|
||||
root->seq[2]->items[0] = h_new(HCFChoice, 1);
|
||||
root->seq[2]->items[0]->type = HCF_CHAR;
|
||||
root->seq[2]->items[0]->type = hi_head;
|
||||
root->seq[2]->items[0]->action = NULL;
|
||||
root->seq[2]->items[1] = gen_int_range(mm__, 0, high & ((1 << (8 * (bytes - 1))) - 1), bytes-1);
|
||||
root->seq[2]->items[2] = NULL;
|
||||
root->seq[3] = NULL;
|
||||
root->action = NULL;
|
||||
return root;
|
||||
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();
|
||||
} else {
|
||||
HCFChoice *root = h_new(HCFChoice, 1);
|
||||
root->type = HCF_CHOICE;
|
||||
root->seq = h_new(HCFSequence*, 2);
|
||||
root->seq[0] = h_new(HCFSequence, 1);
|
||||
root->seq[0]->items = h_new(HCFChoice*, 3);
|
||||
root->seq[0]->items[0] = h_new(HCFChoice, 1);
|
||||
root->seq[0]->items[0]->type = HCF_CHAR;
|
||||
root->seq[0]->items[0]->chr = low_head;
|
||||
root->seq[0]->items[0]->action = NULL;
|
||||
root->seq[0]->items[1] = gen_int_range(mm__,
|
||||
low & ((1 << (8 * (bytes - 1))) - 1),
|
||||
high & ((1 << (8 * (bytes - 1))) - 1),
|
||||
bytes - 1);
|
||||
root->seq[0]->items[2] = NULL;
|
||||
root->seq[1] = NULL;
|
||||
root->action = NULL;
|
||||
return root;
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
else { // idk why this would ever be <1, but whatever
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct bits_env {
|
||||
|
|
@ -114,11 +83,11 @@ struct bits_env {
|
|||
uint8_t signedp;
|
||||
};
|
||||
|
||||
static HCFChoice* desugar_int_range(HAllocator *mm__, void *env) {
|
||||
static void desugar_int_range(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HRange *r = (HRange*)env;
|
||||
struct bits_env* be = (struct bits_env*)r->p->env;
|
||||
uint8_t bytes = be->length / 8;
|
||||
return gen_int_range(mm__, r->lower, r->upper, bytes);
|
||||
gen_int_range(mm__, stk__, r->lower, r->upper, bytes);
|
||||
}
|
||||
|
||||
bool h_svm_action_validate_int_range(HArena *arena, HSVMContext *ctx, void* env) {
|
||||
|
|
|
|||
|
|
@ -59,11 +59,25 @@ static bool many_isValidCF(void *env) {
|
|||
repeat->sep->vtable->isValidCF(repeat->sep->env)));
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_many(HAllocator *mm__, void *env) {
|
||||
static void desugar_many(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
// TODO: refactor this.
|
||||
HRepeat *repeat = (HRepeat*)env;
|
||||
if (!repeat->min_p) {
|
||||
assert(!"Unreachable");
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
for (size_t i = 0; i < repeat->count; i++) {
|
||||
if (i != 0 && repeat->sep != NULL)
|
||||
HCFS_DESUGAR(repeat->sep); // Should be ignored.
|
||||
HCFS_DESUGAR(repeat->p);
|
||||
}
|
||||
} HCFS_END_SEQ();
|
||||
} HCFS_END_CHOICE();
|
||||
return;
|
||||
}
|
||||
if(repeat->count > 1) {
|
||||
assert_message(0, "'h_repeat_n' is not context-free, can't be desugared");
|
||||
return NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
/* many(A) =>
|
||||
|
|
@ -73,53 +87,29 @@ static HCFChoice* desugar_many(HAllocator *mm__, void *env) {
|
|||
-> \epsilon
|
||||
*/
|
||||
|
||||
HParser *epsilon = h_epsilon_p__m(mm__);
|
||||
|
||||
HCFChoice *sep = h_desugar(mm__, (repeat->sep != NULL) ? repeat->sep : epsilon);
|
||||
HCFChoice *a = h_desugar(mm__, repeat->p);
|
||||
HCFChoice *ma = h_new(HCFChoice, 1);
|
||||
HCFChoice *mar = h_new(HCFChoice, 1);
|
||||
HCFChoice *eps = desugar_epsilon(mm__, NULL);
|
||||
|
||||
/* create first subrule */
|
||||
ma->type = HCF_CHOICE;
|
||||
ma->seq = h_new(HCFSequence*, 3); /* enough for 2 productions */
|
||||
ma->seq[0] = h_new(HCFSequence, 1);
|
||||
ma->seq[0]->items = h_new(HCFChoice*, 3);
|
||||
ma->seq[0]->items[0] = a;
|
||||
ma->seq[0]->items[1] = mar;
|
||||
ma->seq[0]->items[2] = NULL;
|
||||
ma->seq[1] = NULL;
|
||||
|
||||
/* if not many1/sepBy1, attach epsilon */
|
||||
if (repeat->count == 0) {
|
||||
ma->seq[1] = h_new(HCFSequence, 1);
|
||||
ma->seq[1]->items = h_new(HCFChoice*, 2);
|
||||
ma->seq[1]->items[0] = eps;
|
||||
ma->seq[1]->items[1] = NULL;
|
||||
ma->seq[2] = NULL;
|
||||
}
|
||||
|
||||
/* create second subrule */
|
||||
mar->type = HCF_CHOICE;
|
||||
mar->seq = h_new(HCFSequence*, 3);
|
||||
mar->seq[0] = h_new(HCFSequence, 1);
|
||||
mar->seq[0]->items = h_new(HCFChoice*, 4);
|
||||
mar->seq[0]->items[0] = sep;
|
||||
mar->seq[0]->items[1] = a;
|
||||
mar->seq[0]->items[2] = mar; // woo recursion!
|
||||
mar->seq[0]->items[3] = NULL;
|
||||
mar->seq[1] = h_new(HCFSequence, 1);
|
||||
mar->seq[1]->items = h_new(HCFChoice*, 2);
|
||||
mar->seq[1]->items[0] = eps;
|
||||
mar->seq[1]->items[1] = NULL;
|
||||
mar->seq[2] = NULL;
|
||||
|
||||
/* attach reshapers */
|
||||
sep->reshape = h_act_ignore;
|
||||
ma->reshape = h_act_flatten;
|
||||
|
||||
return ma;
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_DESUGAR(repeat->p);
|
||||
HCFS_BEGIN_CHOICE() { // Mar
|
||||
HCFS_BEGIN_SEQ() {
|
||||
if (repeat->sep != NULL) {
|
||||
HCFS_DESUGAR(h_ignore__m(mm__, repeat->sep));
|
||||
}
|
||||
//stk__->last_completed->reshape = h_act_ignore; // BUG: This modifies a memoized entry.
|
||||
HCFS_DESUGAR(repeat->p);
|
||||
HCFS_APPEND(HCFS_THIS_CHOICE);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_BEGIN_SEQ() {
|
||||
} HCFS_END_SEQ();
|
||||
} HCFS_END_CHOICE(); // Mar
|
||||
}
|
||||
if (repeat->count == 0) {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
//HCFS_DESUGAR(h_ignore__m(mm__, h_epsilon_p()));
|
||||
} HCFS_END_SEQ();
|
||||
}
|
||||
HCFS_THIS_CHOICE->reshape = h_act_flatten;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool many_ctrvm(HRVMProg *prog, void *env) {
|
||||
|
|
@ -266,16 +256,10 @@ static HParseResult* parse_length_value(void *env, HParseState *state) {
|
|||
return parse_many(&repeat, state);
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_length_value(HAllocator *mm__, void *env) {
|
||||
assert_message(0, "'h_length_value' is not context-free, can't be desugared");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const HParserVtable length_value_vt = {
|
||||
.parse = parse_length_value,
|
||||
.isValidRegular = h_false,
|
||||
.isValidCF = h_false,
|
||||
.desugar = desugar_length_value,
|
||||
};
|
||||
|
||||
HParser* h_length_value(const HParser* length, const HParser* value) {
|
||||
|
|
|
|||
|
|
@ -10,16 +10,10 @@ static HParseResult* parse_not(void* env, HParseState* state) {
|
|||
}
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_not(HAllocator *mm__, void *env) {
|
||||
assert_message(0, "'h_not' is not context-free, can't be desugared");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const HParserVtable not_vt = {
|
||||
.parse = parse_not,
|
||||
.isValidRegular = h_false, /* see and.c for why */
|
||||
.isValidCF = h_false, /* also see and.c for why */
|
||||
.desugar = desugar_not,
|
||||
.isValidCF = h_false,
|
||||
.compile_to_rvm = h_not_regular, // Is actually regular, but the generation step is currently unable to handle it. TODO: fix this.
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -5,13 +5,9 @@ static HParseResult* parse_nothing() {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static HCFChoice *desugar_nothing(HAllocator *mm__, void *env) {
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 1);
|
||||
ret->seq[0] = NULL;
|
||||
ret->action = NULL;
|
||||
return ret;
|
||||
static void desugar_nothing(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool nothing_ctrvm(HRVMProg *prog, void* env) {
|
||||
|
|
|
|||
|
|
@ -25,18 +25,19 @@ static bool opt_isValidCF(void *env) {
|
|||
static HParsedToken* reshape_optional(const HParseResult *p) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used > 0);
|
||||
|
||||
HParsedToken *res = p->ast->seq->elements[0];
|
||||
if(res)
|
||||
return res;
|
||||
if (p->ast->seq->used > 0) {
|
||||
HParsedToken *res = p->ast->seq->elements[0];
|
||||
if(res)
|
||||
return res;
|
||||
}
|
||||
|
||||
HParsedToken *ret = h_arena_malloc(p->arena, sizeof(HParsedToken));
|
||||
ret->token_type = TT_NONE;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_optional(HAllocator *mm__, void *env) {
|
||||
static void desugar_optional(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HParser *p = (HParser*) env;
|
||||
|
||||
/* optional(A) =>
|
||||
|
|
@ -44,28 +45,14 @@ static HCFChoice* desugar_optional(HAllocator *mm__, void *env) {
|
|||
-> \epsilon
|
||||
*/
|
||||
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
HCFChoice *a = h_desugar(mm__, p);
|
||||
HCFChoice *eps = desugar_epsilon(mm__, NULL);
|
||||
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 3); /* enough for 2 productions */
|
||||
|
||||
ret->seq[0] = h_new(HCFSequence, 1);
|
||||
ret->seq[0]->items = h_new(HCFChoice*, 2);
|
||||
ret->seq[0]->items[0] = a;
|
||||
ret->seq[0]->items[1] = NULL;
|
||||
|
||||
ret->seq[1] = h_new(HCFSequence, 1);
|
||||
ret->seq[1]->items = h_new(HCFChoice*, 2);
|
||||
ret->seq[1]->items[0] = eps;
|
||||
ret->seq[1]->items[1] = NULL;
|
||||
|
||||
ret->seq[2] = NULL;
|
||||
|
||||
ret->reshape = reshape_optional;
|
||||
|
||||
return ret;
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_DESUGAR(p);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_BEGIN_SEQ() {
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = reshape_optional;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool h_svm_action_optional(HArena *arena, HSVMContext *ctx, void *env) {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include "../hammer.h"
|
||||
#include "../internal.h"
|
||||
#include "../backends/regex.h"
|
||||
#include "../backends/contextfree.h"
|
||||
|
||||
#define a_new_(arena, typ, count) ((typ*)h_arena_malloc((arena), sizeof(typ)*(count)))
|
||||
#define a_new(typ, count) a_new_(state->arena, typ, count)
|
||||
|
|
@ -25,17 +26,12 @@ static inline size_t token_length(HParseResult *pr) {
|
|||
}
|
||||
|
||||
/* Epsilon rules happen during desugaring. This handles them. */
|
||||
static inline HCFChoice* desugar_epsilon(HAllocator *mm__, void *env) {
|
||||
static HCFChoice *res_seq_l[] = {NULL};
|
||||
static HCFSequence res_seq = {res_seq_l};
|
||||
static HCFSequence *res_ch_l[] = {&res_seq, NULL};
|
||||
static HCFChoice res_ch = {
|
||||
.type = HCF_CHOICE,
|
||||
.seq = res_ch_l,
|
||||
.action = NULL,
|
||||
.reshape = h_act_ignore
|
||||
};
|
||||
return &res_ch;
|
||||
static inline void desugar_epsilon(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = h_act_ignore;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
#endif // HAMMER_PARSE_INTERNAL__H
|
||||
|
|
|
|||
|
|
@ -64,22 +64,15 @@ static HParsedToken *reshape_sequence(const HParseResult *p) {
|
|||
return res;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_sequence(HAllocator *mm__, void *env) {
|
||||
static void desugar_sequence(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HSequence *s = (HSequence*)env;
|
||||
HCFSequence *seq = h_new(HCFSequence, 1);
|
||||
seq->items = h_new(HCFChoice*, s->len+1);
|
||||
for (size_t i=0; i<s->len; ++i) {
|
||||
seq->items[i] = h_desugar(mm__, s->p_array[i]);
|
||||
}
|
||||
seq->items[s->len] = NULL;
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 2);
|
||||
ret->seq[0] = seq;
|
||||
ret->seq[1] = NULL;
|
||||
ret->action = NULL;
|
||||
ret->reshape = reshape_sequence;
|
||||
return ret;
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
for (size_t i = 0; i < s->len; i++)
|
||||
HCFS_DESUGAR(s->p_array[i]);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = reshape_sequence;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool sequence_ctrvm(HRVMProg *prog, void *env) {
|
||||
|
|
|
|||
|
|
@ -44,25 +44,15 @@ static HParsedToken *reshape_token(const HParseResult *p) {
|
|||
return tok;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_token(HAllocator *mm__, void *env) {
|
||||
static void desugar_token(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
HToken *tok = (HToken*)env;
|
||||
HCFSequence *seq = h_new(HCFSequence, 1);
|
||||
seq->items = h_new(HCFChoice*, 1+tok->len);
|
||||
for (size_t i=0; i<tok->len; ++i) {
|
||||
seq->items[i] = h_new(HCFChoice, 1);
|
||||
seq->items[i]->type = HCF_CHAR;
|
||||
seq->items[i]->chr = tok->str[i];
|
||||
}
|
||||
seq->items[tok->len] = NULL;
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 2);
|
||||
ret->seq[0] = seq;
|
||||
ret->seq[1] = NULL;
|
||||
ret->action = NULL;
|
||||
ret->pred = NULL;
|
||||
ret->reshape = reshape_token;
|
||||
return ret;
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
for (size_t i = 0; i < tok->len; i++)
|
||||
HCFS_ADD_CHAR(tok->str[i]);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = reshape_token;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool token_ctrvm(HRVMProg *prog, void *env) {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ static HParseResult* parse_unimplemented(void* env, HParseState *state) {
|
|||
return &result;
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_unimplemented(HAllocator *mm__, void *env) {
|
||||
static HCFChoice* desugar_unimplemented(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
assert_message(0, "'h_unimplemented' is not context-free, can't be desugared");
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,39 +17,26 @@ static HParseResult* parse_whitespace(void* env, HParseState *state) {
|
|||
|
||||
static const char SPACE_CHRS[6] = {' ', '\f', '\n', '\r', '\t', '\v'};
|
||||
|
||||
static HCFChoice* desugar_whitespace(HAllocator *mm__, void *env) {
|
||||
HCFChoice *ws = h_new(HCFChoice, 1);
|
||||
ws->type = HCF_CHOICE;
|
||||
ws->seq = h_new(HCFSequence*, 3);
|
||||
HCFSequence *nonempty = h_new(HCFSequence, 1);
|
||||
nonempty->items = h_new(HCFChoice*, 3);
|
||||
nonempty->items[0] = h_new(HCFChoice, 1);
|
||||
nonempty->items[0]->type = HCF_CHARSET;
|
||||
nonempty->items[0]->charset = new_charset(mm__);
|
||||
static void desugar_whitespace(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
|
||||
HCharset ws_cs = new_charset(mm__);
|
||||
for(size_t i=0; i<sizeof(SPACE_CHRS); i++)
|
||||
charset_set(nonempty->items[0]->charset, SPACE_CHRS[i], 1);
|
||||
nonempty->items[1] = ws; // yay circular pointer!
|
||||
nonempty->items[2] = NULL;
|
||||
ws->seq[0] = nonempty;
|
||||
HCFSequence *empty = h_new(HCFSequence, 1);
|
||||
empty->items = h_new(HCFChoice*, 1);
|
||||
empty->items[0] = NULL;
|
||||
ws->seq[1] = empty;
|
||||
ws->seq[2] = NULL;
|
||||
|
||||
HCFChoice *ret = h_new(HCFChoice, 1);
|
||||
ret->type = HCF_CHOICE;
|
||||
ret->seq = h_new(HCFSequence*, 2);
|
||||
ret->seq[0] = h_new(HCFSequence, 1);
|
||||
ret->seq[0]->items = h_new(HCFChoice*, 3);
|
||||
ret->seq[0]->items[0] = ws;
|
||||
ret->seq[0]->items[1] = h_desugar(mm__, (HParser *)env);
|
||||
ret->seq[0]->items[2] = NULL;
|
||||
ret->seq[1] = NULL;
|
||||
|
||||
ret->reshape = h_act_last;
|
||||
charset_set(ws_cs, SPACE_CHRS[i], 1);
|
||||
|
||||
return ret;
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_BEGIN_CHOICE() {
|
||||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_ADD_CHARSET(ws_cs);
|
||||
HCFS_APPEND(HCFS_THIS_CHOICE); // yay circular pointer!
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_BEGIN_SEQ() {
|
||||
} HCFS_END_SEQ();
|
||||
} HCFS_END_CHOICE();
|
||||
HCFS_DESUGAR( (HParser*)env );
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = h_act_last;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
static bool ws_isValidRegular(void *env) {
|
||||
|
|
|
|||
|
|
@ -31,16 +31,10 @@ static HParseResult* parse_xor(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_xor(HAllocator *mm__, void *env) {
|
||||
assert_message(0, "'h_xor' is not context-free, can't be desugared");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const HParserVtable xor_vt = {
|
||||
.parse = parse_xor,
|
||||
.isValidRegular = h_false,
|
||||
.isValidCF = h_false, // XXX should this be true if both p1 and p2 are CF?
|
||||
.desugar = desugar_xor,
|
||||
.compile_to_rvm = h_not_regular,
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue