From ab89b0fd4d0c6a1a876513754fe6d8a9d15eefe6 Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" Date: Wed, 20 Feb 2013 20:43:16 -0500 Subject: [PATCH] desugaring done for many and attr_bool --- src/internal.h | 1 + src/parsers/attr_bool.c | 3 +- src/parsers/epsilon.c | 13 --------- src/parsers/int_range.c | 1 + src/parsers/many.c | 54 +++++++++++++++++++++++++++++++++++ src/parsers/parser_internal.h | 13 +++++++++ 6 files changed, 70 insertions(+), 15 deletions(-) diff --git a/src/internal.h b/src/internal.h index 7e03657..279f998 100644 --- a/src/internal.h +++ b/src/internal.h @@ -257,6 +257,7 @@ typedef struct HCFChoice_ { uint8_t chr; }; HAction action; + HPredicate pred; } HCFChoice; struct HCFSequence_ { diff --git a/src/parsers/attr_bool.c b/src/parsers/attr_bool.c index 46f511f..9e69ebe 100644 --- a/src/parsers/attr_bool.c +++ b/src/parsers/attr_bool.c @@ -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; } diff --git a/src/parsers/epsilon.c b/src/parsers/epsilon.c index d802246..ffb47d1 100644 --- a/src/parsers/epsilon.c +++ b/src/parsers/epsilon.c @@ -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, diff --git a/src/parsers/int_range.c b/src/parsers/int_range.c index f2ee03f..64450da 100644 --- a/src/parsers/int_range.c +++ b/src/parsers/int_range.c @@ -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; diff --git a/src/parsers/many.c b/src/parsers/many.c index 15f020b..7ea63d9 100644 --- a/src/parsers/many.c +++ b/src/parsers/many.c @@ -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 = { diff --git a/src/parsers/parser_internal.h b/src/parsers/parser_internal.h index d966ecc..7e5bb06 100644 --- a/src/parsers/parser_internal.h +++ b/src/parsers/parser_internal.h @@ -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