desugaring done for many and attr_bool

This commit is contained in:
Meredith L. Patterson 2013-02-20 20:43:16 -05:00
parent 101e90ad9b
commit ab89b0fd4d
6 changed files with 70 additions and 15 deletions

View file

@ -257,6 +257,7 @@ typedef struct HCFChoice_ {
uint8_t chr;
};
HAction action;
HPredicate pred;
} HCFChoice;
struct HCFSequence_ {

View file

@ -43,8 +43,7 @@ static HCFChoice* desugar_ab(HAllocator *mm__, void *env) {
ret->seq = h_new(HCFSequence*, 2);
ret->seq[0] = seq;
ret->seq[1] = NULL;
/* TODO: need to process this as an HPredicate */
ret->action = a->action;
ret->pred = a->pred;
return ret;
}

View file

@ -8,19 +8,6 @@ static HParseResult* parse_epsilon(void* env, HParseState* state) {
return res;
}
static 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
};
return &res_ch;
}
static const HParserVtable epsilon_vt = {
.parse = parse_epsilon,
.isValidRegular = h_true,

View file

@ -29,6 +29,7 @@ static HParseResult* parse_int_range(void *env, HParseState *state) {
}
HCFChoice* gen_int_range(HAllocator *mm__, 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;

View file

@ -57,6 +57,60 @@ static bool many_isValidCF(void *env) {
}
static HCFChoice* desugar_many(HAllocator *mm__, void *env) {
HRepeat *repeat = (HRepeat*)env;
/* many(A) =>
Ma -> A Mar
-> \epsilon (but not if many1/sepBy1 is used)
Mar -> Sep A Mar
-> \epsilon
*/
HCFChoice *ret = h_new(HCFChoice, 1);
ret->type = HCF_CHOICE;
if (repeat->min_p)
ret->seq = h_new(HCFSequence*, 2); /* choice of 1 sequence, A Mar */
else
ret->seq = h_new(HCFSequence*, 3); /* choice of 2 sequences, A Mar | epsilon */
ret->seq[0] = h_new(HCFSequence, 1);
ret->seq[0]->items = h_new(HCFChoice*, 2);
/* create first subrule */
HCFChoice *ma = h_new(HCFChoice, 3);
ma->type = HCF_CHOICE;
ma->seq[0]->items[0] = repeat->p->vtable->desugar(mm__, repeat->p->env);
/* create second subrule */
HCFChoice *mar = h_new(HCFChoice, 3);
mar->type = HCF_CHOICE;
mar->seq = h_new(HCFSequence*, 2);
mar->seq[0] = h_new(HCFSequence, 1);
mar->seq[0]->items = h_new(HCFChoice*, 4);
mar->seq[0]->items[0] = repeat->sep->vtable->desugar(mm__, repeat->sep->env);
mar->seq[0]->items[1] = repeat->p->vtable->desugar(mm__, repeat->p->env);
mar->seq[0]->items[2] = mar; // woo recursion!
mar->seq[0]->items[3] = NULL;
mar->seq[1]->items = h_new(HCFChoice*, 2);
mar->seq[1]->items[0] = desugar_epsilon(mm__, NULL);\
mar->seq[1]->items[1] = NULL;
mar->seq[2]->items = NULL;
/* attach second subrule to first subrule */
ma->seq[0]->items[1] = mar;
ma->seq[0]->items[2] = NULL;
/* attach first subrule to ret */
ret->seq[0]->items[0] = ma;
ret->seq[0]->items[1] = NULL;
/* if not many1/sepBy1, attach epsilon */
if (repeat->min_p) {
ret->seq[1]->items = NULL;
} else {
ret->seq[1]->items = h_new(HCFChoice*, 2);
ret->seq[1]->items[0] = desugar_epsilon(mm__, NULL);
ret->seq[1]->items[1] = NULL;
ret->seq[2]->items = NULL;
}
return ret;
}
static const HParserVtable many_vt = {

View file

@ -26,4 +26,17 @@ static inline size_t token_length(HParseResult *pr) {
static inline bool h_true(void *env) { return true; }
static inline bool h_false(void *env) { return false; }
/* 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
};
return &res_ch;
}
#endif // HAMMER_PARSE_INTERNAL__H