2012-05-26 16:00:43 +02:00
|
|
|
#include "parser_internal.h"
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const HParser *p;
|
|
|
|
|
HPredicate pred;
|
|
|
|
|
} HAttrBool;
|
|
|
|
|
|
|
|
|
|
static HParseResult* parse_attr_bool(void *env, HParseState *state) {
|
|
|
|
|
HAttrBool *a = (HAttrBool*)env;
|
|
|
|
|
HParseResult *res = h_do_parse(a->p, state);
|
|
|
|
|
if (res && res->ast) {
|
|
|
|
|
if (a->pred(res))
|
|
|
|
|
return res;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
} else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 18:10:40 -05:00
|
|
|
static bool ab_isValidRegular(void *env) {
|
|
|
|
|
HAttrBool *ab = (HAttrBool*)env;
|
|
|
|
|
return ab->p->vtable->isValidRegular(ab->p->env);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:58:15 -05:00
|
|
|
/* Strictly speaking, this does not adhere to the definition of context-free.
|
|
|
|
|
However, for the purpose of making it easier to handle weird constraints on
|
|
|
|
|
fields, we've decided to allow boolean attribute validation at parse time
|
|
|
|
|
for now. We might change our minds later. */
|
|
|
|
|
|
2012-12-18 18:10:40 -05:00
|
|
|
static bool ab_isValidCF(void *env) {
|
|
|
|
|
HAttrBool *ab = (HAttrBool*)env;
|
|
|
|
|
return ab->p->vtable->isValidCF(ab->p->env);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 02:18:19 -05:00
|
|
|
static HCFChoice* desugar_ab(HAllocator *mm__, void *env) {
|
2013-02-20 18:58:15 -05:00
|
|
|
HAttrBool *a = (HAttrBool*)env;
|
|
|
|
|
HCFSequence *seq = h_new(HCFSequence, 1);
|
|
|
|
|
seq->items = h_new(HCFChoice*, 2);
|
2013-03-17 13:25:02 -07:00
|
|
|
seq->items[0] = h_desugar(mm__, a->p);
|
2013-02-20 18:58:15 -05:00
|
|
|
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;
|
2013-02-20 20:43:16 -05:00
|
|
|
ret->pred = a->pred;
|
2013-05-20 16:59:38 +02:00
|
|
|
ret->reshape = h_act_first;
|
2013-02-20 18:58:15 -05:00
|
|
|
return ret;
|
2013-02-03 02:18:19 -05:00
|
|
|
}
|
|
|
|
|
|
2013-04-22 18:06:17 -07:00
|
|
|
static bool ab_ctrvm(HRVMProg *prog, void *env) {
|
|
|
|
|
HAttrBool *ab = (HAttrBool*)env;
|
|
|
|
|
return h_compile_regex(prog, ab->p);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 16:00:43 +02:00
|
|
|
static const HParserVtable attr_bool_vt = {
|
|
|
|
|
.parse = parse_attr_bool,
|
2012-12-18 18:10:40 -05:00
|
|
|
.isValidRegular = ab_isValidRegular,
|
|
|
|
|
.isValidCF = ab_isValidCF,
|
2013-02-03 02:18:19 -05:00
|
|
|
.desugar = desugar_ab,
|
2013-04-22 18:06:17 -07:00
|
|
|
.compile_to_rvm = ab_ctrvm,
|
2012-05-26 16:00:43 +02:00
|
|
|
};
|
|
|
|
|
|
2012-10-10 15:58:03 +02:00
|
|
|
|
2013-04-26 20:36:54 -07:00
|
|
|
HParser* h_attr_bool(const HParser* p, HPredicate pred) {
|
2012-10-10 15:58:03 +02:00
|
|
|
return h_attr_bool__m(&system_allocator, p, pred);
|
|
|
|
|
}
|
2013-04-26 20:36:54 -07:00
|
|
|
HParser* h_attr_bool__m(HAllocator* mm__, const HParser* p, HPredicate pred) {
|
2012-10-10 15:58:03 +02:00
|
|
|
HAttrBool *env = h_new(HAttrBool, 1);
|
2012-05-26 16:00:43 +02:00
|
|
|
env->p = p;
|
|
|
|
|
env->pred = pred;
|
2013-04-27 04:17:47 +02:00
|
|
|
return h_new_parser(mm__, &attr_bool_vt, env);
|
2012-05-26 16:00:43 +02:00
|
|
|
}
|