Added a void* user_data pointer to HAction
This commit is contained in:
parent
bba60f5db9
commit
e487d5435c
18 changed files with 83 additions and 73 deletions
|
|
@ -23,7 +23,7 @@
|
|||
// They must be named act_<rulename>.
|
||||
///
|
||||
|
||||
HParsedToken *act_bsfdig(const HParseResult *p)
|
||||
HParsedToken *act_bsfdig(const HParseResult *p, void* user_data)
|
||||
{
|
||||
HParsedToken *res = H_MAKE_UINT(0);
|
||||
|
||||
|
|
@ -54,7 +54,7 @@ H_ACT_APPLY(act_index0, h_act_index, 0);
|
|||
#define act_document act_index0
|
||||
|
||||
// General-form action to turn a block of base64 digits into bytes.
|
||||
HParsedToken *act_base64_n(int n, const HParseResult *p)
|
||||
HParsedToken *act_base64_n(int n, const HParseResult *p, void* user_data)
|
||||
{
|
||||
HParsedToken *res = H_MAKE_SEQN(n);
|
||||
|
||||
|
|
@ -83,7 +83,7 @@ H_ACT_APPLY(act_base64_3, act_base64_n, 3);
|
|||
H_ACT_APPLY(act_base64_2, act_base64_n, 2);
|
||||
H_ACT_APPLY(act_base64_1, act_base64_n, 1);
|
||||
|
||||
HParsedToken *act_base64(const HParseResult *p)
|
||||
HParsedToken *act_base64(const HParseResult *p, void* user_data)
|
||||
{
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used == 2);
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ uint8_t bsfdig_value(const HParsedToken *p)
|
|||
// helper: append a byte value to a sequence
|
||||
#define seq_append_byte(res, b) h_seq_snoc(res, H_MAKE_UINT(b))
|
||||
|
||||
HParsedToken *act_base64(const HParseResult *p)
|
||||
HParsedToken *act_base64(const HParseResult *p, void* user_data)
|
||||
{
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used == 2);
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ void set_rdata(struct dns_rr *rr, HCountedArray *rdata) {
|
|||
}
|
||||
}
|
||||
|
||||
HParsedToken* act_header(const HParseResult *p) {
|
||||
HParsedToken* act_header(const HParseResult *p, void* user_data) {
|
||||
HParsedToken **fields = h_seq_elements(p->ast);
|
||||
dns_header_t header_ = {
|
||||
.id = H_CAST_UINT(fields[0]),
|
||||
|
|
@ -109,7 +109,7 @@ HParsedToken* act_header(const HParseResult *p) {
|
|||
return H_MAKE(dns_header_t, header);
|
||||
}
|
||||
|
||||
HParsedToken* act_label(const HParseResult *p) {
|
||||
HParsedToken* act_label(const HParseResult *p, void* user_data) {
|
||||
dns_label_t *r = H_ALLOC(dns_label_t);
|
||||
|
||||
r->len = h_seq_len(p->ast);
|
||||
|
|
@ -121,7 +121,7 @@ HParsedToken* act_label(const HParseResult *p) {
|
|||
return H_MAKE(dns_label_t, r);
|
||||
}
|
||||
|
||||
HParsedToken* act_rr(const HParseResult *p) {
|
||||
HParsedToken* act_rr(const HParseResult *p, void* user_data) {
|
||||
dns_rr_t *rr = H_ALLOC(dns_rr_t);
|
||||
|
||||
rr->name = *H_FIELD(dns_domain_t, 0);
|
||||
|
|
@ -136,7 +136,7 @@ HParsedToken* act_rr(const HParseResult *p) {
|
|||
return H_MAKE(dns_rr_t, rr);
|
||||
}
|
||||
|
||||
HParsedToken* act_question(const HParseResult *p) {
|
||||
HParsedToken* act_question(const HParseResult *p, void* user_data) {
|
||||
dns_question_t *q = H_ALLOC(dns_question_t);
|
||||
HParsedToken **fields = h_seq_elements(p->ast);
|
||||
|
||||
|
|
@ -153,7 +153,7 @@ HParsedToken* act_question(const HParseResult *p) {
|
|||
return H_MAKE(dns_question_t, q);
|
||||
}
|
||||
|
||||
HParsedToken* act_message(const HParseResult *p) {
|
||||
HParsedToken* act_message(const HParseResult *p, void* user_data) {
|
||||
h_pprint(stdout, p->ast, 0, 2);
|
||||
dns_message_t *msg = H_ALLOC(dns_message_t);
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ bool validate_label(HParseResult *p) {
|
|||
|
||||
#define act_label h_act_flatten
|
||||
|
||||
HParsedToken* act_domain(const HParseResult *p) {
|
||||
HParsedToken* act_domain(const HParseResult *p, void* user_data) {
|
||||
HParsedToken *ret = NULL;
|
||||
char *arr = NULL;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@
|
|||
HParser* init_domain();
|
||||
HParser* init_character_string();
|
||||
|
||||
HParsedToken* act_index0(const HParseResult *p);
|
||||
HParsedToken* act_index0(const HParseResult *p, void* user_data);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ bool validate_null(HParseResult *p) {
|
|||
return (65536 > p->ast->seq->used);
|
||||
}
|
||||
|
||||
HParsedToken *act_null(const HParseResult *p) {
|
||||
HParsedToken *act_null(const HParseResult *p, void* user_data) {
|
||||
dns_rr_null_t *null = H_ALLOC(dns_rr_null_t);
|
||||
|
||||
size_t len = h_seq_len(p->ast);
|
||||
|
|
@ -28,7 +28,7 @@ HParsedToken *act_null(const HParseResult *p) {
|
|||
return H_MAKE(dns_rr_null_t, null);
|
||||
}
|
||||
|
||||
HParsedToken *act_txt(const HParseResult *p) {
|
||||
HParsedToken *act_txt(const HParseResult *p, void* user_data) {
|
||||
dns_rr_txt_t *txt = H_ALLOC(dns_rr_txt_t);
|
||||
|
||||
const HCountedArray *arr = H_CAST_SEQ(p->ast);
|
||||
|
|
@ -47,7 +47,7 @@ HParsedToken *act_txt(const HParseResult *p) {
|
|||
return H_MAKE(dns_rr_txt_t, txt);
|
||||
}
|
||||
|
||||
HParsedToken* act_cstr(const HParseResult *p) {
|
||||
HParsedToken* act_cstr(const HParseResult *p, void* user_data) {
|
||||
dns_cstr_t *cs = H_ALLOC(dns_cstr_t);
|
||||
|
||||
const HCountedArray *arr = H_CAST_SEQ(p->ast);
|
||||
|
|
@ -60,7 +60,7 @@ HParsedToken* act_cstr(const HParseResult *p) {
|
|||
return H_MAKE(dns_cstr_t, cs);
|
||||
}
|
||||
|
||||
HParsedToken* act_soa(const HParseResult *p) {
|
||||
HParsedToken* act_soa(const HParseResult *p, void* user_data) {
|
||||
dns_rr_soa_t *soa = H_ALLOC(dns_rr_soa_t);
|
||||
|
||||
soa->mname = *H_FIELD(dns_domain_t, 0);
|
||||
|
|
@ -74,7 +74,7 @@ HParsedToken* act_soa(const HParseResult *p) {
|
|||
return H_MAKE(dns_rr_soa_t, soa);
|
||||
}
|
||||
|
||||
HParsedToken* act_wks(const HParseResult *p) {
|
||||
HParsedToken* act_wks(const HParseResult *p, void* user_data) {
|
||||
dns_rr_wks_t *wks = H_ALLOC(dns_rr_wks_t);
|
||||
|
||||
wks->address = H_FIELD_UINT(0);
|
||||
|
|
@ -87,7 +87,7 @@ HParsedToken* act_wks(const HParseResult *p) {
|
|||
return H_MAKE(dns_rr_wks_t, wks);
|
||||
}
|
||||
|
||||
HParsedToken* act_hinfo(const HParseResult *p) {
|
||||
HParsedToken* act_hinfo(const HParseResult *p, void* user_data) {
|
||||
dns_rr_hinfo_t *hinfo = H_ALLOC(dns_rr_hinfo_t);
|
||||
|
||||
hinfo->cpu = *H_FIELD(dns_cstr_t, 0);
|
||||
|
|
@ -96,7 +96,7 @@ HParsedToken* act_hinfo(const HParseResult *p) {
|
|||
return H_MAKE(dns_rr_hinfo_t, hinfo);
|
||||
}
|
||||
|
||||
HParsedToken* act_minfo(const HParseResult *p) {
|
||||
HParsedToken* act_minfo(const HParseResult *p, void* user_data) {
|
||||
dns_rr_minfo_t *minfo = H_ALLOC(dns_rr_minfo_t);
|
||||
|
||||
minfo->rmailbx = *H_FIELD(dns_domain_t, 0);
|
||||
|
|
@ -105,7 +105,7 @@ HParsedToken* act_minfo(const HParseResult *p) {
|
|||
return H_MAKE(dns_rr_minfo_t, minfo);
|
||||
}
|
||||
|
||||
HParsedToken* act_mx(const HParseResult *p) {
|
||||
HParsedToken* act_mx(const HParseResult *p, void* user_data) {
|
||||
dns_rr_mx_t *mx = H_ALLOC(dns_rr_mx_t);
|
||||
|
||||
mx->preference = H_FIELD_UINT(0);
|
||||
|
|
@ -184,7 +184,7 @@ HParser* init_rdata(uint16_t type) {
|
|||
for(uint16_t i = 0; i<RDATA_TYPE_MAX+1; i++) {
|
||||
if(parsers[i]) {
|
||||
parsers[i] = h_action(h_sequence(parsers[i], h_end_p(), NULL),
|
||||
act_index0);
|
||||
act_index0, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -372,13 +372,13 @@ HParseResult *h_llk_parse(HAllocator* mm__, const HParser* parser, HInputStream*
|
|||
|
||||
// perform token reshape if indicated
|
||||
if(x->reshape)
|
||||
tok = (HParsedToken *)x->reshape(make_result(arena, tok));
|
||||
tok = (HParsedToken *)x->reshape(make_result(arena, tok), x->user_data);
|
||||
|
||||
// call validation and semantic action, if present
|
||||
if(x->pred && !x->pred(make_result(tarena, tok)))
|
||||
goto no_parse; // validation failed -> no parse
|
||||
if(x->action)
|
||||
tok = (HParsedToken *)x->action(make_result(arena, tok));
|
||||
tok = (HParsedToken *)x->action(make_result(arena, tok), x->user_data);
|
||||
|
||||
// append to result sequence
|
||||
h_carray_append(seq, tok);
|
||||
|
|
|
|||
|
|
@ -307,13 +307,13 @@ bool h_lrengine_step(HLREngine *engine, const HLRAction *action)
|
|||
|
||||
// perform token reshape if indicated
|
||||
if(symbol->reshape)
|
||||
value = (HParsedToken *)symbol->reshape(make_result(arena, value));
|
||||
value = (HParsedToken *)symbol->reshape(make_result(arena, value), symbol->user_data);
|
||||
|
||||
// call validation and semantic action, if present
|
||||
if(symbol->pred && !symbol->pred(make_result(tarena, value)))
|
||||
return false; // validation failed -> no parse; terminate
|
||||
if(symbol->action)
|
||||
value = (HParsedToken *)symbol->action(make_result(arena, value));
|
||||
value = (HParsedToken *)symbol->action(make_result(arena, value), symbol->user_data);
|
||||
|
||||
// this is LR, building a right-most derivation bottom-up, so no reduce can
|
||||
// follow a reduce. we can also assume no conflict follows for GLR if we
|
||||
|
|
|
|||
12
src/glue.c
12
src/glue.c
|
|
@ -5,7 +5,7 @@
|
|||
#include "parsers/parser_internal.h"
|
||||
|
||||
// Helper to build HAction's that pick one index out of a sequence.
|
||||
HParsedToken *h_act_index(int i, const HParseResult *p)
|
||||
HParsedToken *h_act_index(int i, const HParseResult *p, void* user_data)
|
||||
{
|
||||
if(!p) return NULL;
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ HParsedToken *h_act_index(int i, const HParseResult *p)
|
|||
return tok->seq->elements[i];
|
||||
}
|
||||
|
||||
HParsedToken *h_act_first(const HParseResult *p) {
|
||||
HParsedToken *h_act_first(const HParseResult *p, void* user_data) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used > 0);
|
||||
|
|
@ -31,7 +31,7 @@ HParsedToken *h_act_first(const HParseResult *p) {
|
|||
return p->ast->seq->elements[0];
|
||||
}
|
||||
|
||||
HParsedToken *h_act_second(const HParseResult *p) {
|
||||
HParsedToken *h_act_second(const HParseResult *p, void* user_data) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used > 0);
|
||||
|
|
@ -39,7 +39,7 @@ HParsedToken *h_act_second(const HParseResult *p) {
|
|||
return p->ast->seq->elements[1];
|
||||
}
|
||||
|
||||
HParsedToken *h_act_last(const HParseResult *p) {
|
||||
HParsedToken *h_act_last(const HParseResult *p, void* user_data) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used > 0);
|
||||
|
|
@ -59,7 +59,7 @@ static void act_flatten_(HCountedArray *seq, const HParsedToken *tok) {
|
|||
}
|
||||
}
|
||||
|
||||
HParsedToken *h_act_flatten(const HParseResult *p) {
|
||||
HParsedToken *h_act_flatten(const HParseResult *p, void* user_data) {
|
||||
HCountedArray *seq = h_carray_new(p->arena);
|
||||
|
||||
act_flatten_(seq, p->ast);
|
||||
|
|
@ -72,7 +72,7 @@ HParsedToken *h_act_flatten(const HParseResult *p) {
|
|||
return res;
|
||||
}
|
||||
|
||||
HParsedToken *h_act_ignore(const HParseResult *p) {
|
||||
HParsedToken *h_act_ignore(const HParseResult *p, void* user_data) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
|||
30
src/glue.h
30
src/glue.h
|
|
@ -56,13 +56,19 @@
|
|||
|
||||
|
||||
#define H_RULE(rule, def) HParser *rule = def
|
||||
#define H_ARULE(rule, def) HParser *rule = h_action(def, act_ ## rule)
|
||||
#define H_ARULE(rule, def) HParser *rule = h_action(def, act_ ## rule, NULL)
|
||||
#define H_VRULE(rule, def) HParser *rule = \
|
||||
h_attr_bool(def, validate_ ## rule)
|
||||
h_attr_bool(def, validate_ ## rule)
|
||||
#define H_VARULE(rule, def) HParser *rule = \
|
||||
h_attr_bool(h_action(def, act_ ## rule), validate_ ## rule)
|
||||
h_attr_bool(h_action(def, act_ ## rule, NULL), validate_ ## rule)
|
||||
#define H_AVRULE(rule, def) HParser *rule = \
|
||||
h_action(h_attr_bool(def, validate_ ## rule), act_ ## rule)
|
||||
h_action(h_attr_bool(def, validate_ ## rule), act_ ## rule, NULL)
|
||||
#define H_ADRULE(rule, def, data) HParser *rule = \
|
||||
h_action(def, act_ ## rule, data)
|
||||
#define H_VADRULE(rule, def, data) HParser *rule = \
|
||||
h_attr_bool(h_action(def, act_ ## rule, data), validate_ ## rule)
|
||||
#define H_AVDRULE(rule, def, data) HParser *rule = \
|
||||
h_action(h_attr_bool(def, validate_ ## rule), act_ ## rule, data)
|
||||
|
||||
|
||||
//
|
||||
|
|
@ -88,18 +94,18 @@
|
|||
// action such as h_act_index.
|
||||
//
|
||||
|
||||
HParsedToken *h_act_index(int i, const HParseResult *p);
|
||||
HParsedToken *h_act_first(const HParseResult *p);
|
||||
HParsedToken *h_act_second(const HParseResult *p);
|
||||
HParsedToken *h_act_last(const HParseResult *p);
|
||||
HParsedToken *h_act_flatten(const HParseResult *p);
|
||||
HParsedToken *h_act_ignore(const HParseResult *p);
|
||||
HParsedToken *h_act_index(int i, const HParseResult *p, void* user_data);
|
||||
HParsedToken *h_act_first(const HParseResult *p, void* user_data);
|
||||
HParsedToken *h_act_second(const HParseResult *p, void* user_data);
|
||||
HParsedToken *h_act_last(const HParseResult *p, void* user_data);
|
||||
HParsedToken *h_act_flatten(const HParseResult *p, void* user_data);
|
||||
HParsedToken *h_act_ignore(const HParseResult *p, void* user_data);
|
||||
|
||||
// Define 'myaction' as a specialization of 'paction' by supplying the leading
|
||||
// parameters.
|
||||
#define H_ACT_APPLY(myaction, paction, ...) \
|
||||
HParsedToken *myaction(const HParseResult *p) { \
|
||||
return paction(__VA_ARGS__, p); \
|
||||
HParsedToken *myaction(const HParseResult *p, void* user_data) { \
|
||||
return paction(__VA_ARGS__, p, user_data); \
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
14
src/hammer.h
14
src/hammer.h
|
|
@ -113,7 +113,7 @@ typedef struct HBitWriter_ HBitWriter;
|
|||
* say, structs) and stuff values for them into the void* in the
|
||||
* tagged union in HParsedToken.
|
||||
*/
|
||||
typedef HParsedToken* (*HAction)(const HParseResult *p);
|
||||
typedef HParsedToken* (*HAction)(const HParseResult *p, void* user_data);
|
||||
|
||||
/**
|
||||
* Type of a boolean attribute-checking function, used in the
|
||||
|
|
@ -349,7 +349,7 @@ HAMMER_FN_DECL(HParser*, h_middle, const HParser* p, const HParser* x, const HPa
|
|||
*
|
||||
* Result token type: any
|
||||
*/
|
||||
HAMMER_FN_DECL(HParser*, h_action, const HParser* p, const HAction a);
|
||||
HAMMER_FN_DECL(HParser*, h_action, const HParser* p, const HAction a, void* user_data);
|
||||
|
||||
/**
|
||||
* Parse a single character in the given charset.
|
||||
|
|
@ -621,11 +621,11 @@ void h_bit_writer_free(HBitWriter* w);
|
|||
|
||||
// General-purpose actions for use with h_action
|
||||
// XXX to be consolidated with glue.h when merged upstream
|
||||
HParsedToken *h_act_first(const HParseResult *p);
|
||||
HParsedToken *h_act_second(const HParseResult *p);
|
||||
HParsedToken *h_act_last(const HParseResult *p);
|
||||
HParsedToken *h_act_flatten(const HParseResult *p);
|
||||
HParsedToken *h_act_ignore(const HParseResult *p);
|
||||
HParsedToken *h_act_first(const HParseResult *p, void* userdata);
|
||||
HParsedToken *h_act_second(const HParseResult *p, void* userdata);
|
||||
HParsedToken *h_act_last(const HParseResult *p, void* userdata);
|
||||
HParsedToken *h_act_flatten(const HParseResult *p, void* userdata);
|
||||
HParsedToken *h_act_ignore(const HParseResult *p, void* userdata);
|
||||
|
||||
// {{{ Benchmark functions
|
||||
HAMMER_FN_DECL(HBenchmarkResults *, h_benchmark, HParser* parser, HParserTestcase* testcases);
|
||||
|
|
|
|||
|
|
@ -365,6 +365,7 @@ struct HCFChoice_ {
|
|||
// to execute before action and pred are applied.
|
||||
HAction action;
|
||||
HPredicate pred;
|
||||
void* user_data;
|
||||
};
|
||||
|
||||
struct HCFSequence_ {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
typedef struct {
|
||||
const HParser *p;
|
||||
HAction action;
|
||||
void* user_data;
|
||||
} HParseAction;
|
||||
|
||||
static HParseResult* parse_action(void *env, HParseState *state) {
|
||||
|
|
@ -12,8 +13,8 @@ static HParseResult* parse_action(void *env, HParseState *state) {
|
|||
HParseResult *tmp = h_do_parse(a->p, state);
|
||||
//HParsedToken *tok = a->action(h_do_parse(a->p, state));
|
||||
if(tmp) {
|
||||
const HParsedToken *tok = a->action(tmp);
|
||||
return make_result(state->arena, (HParsedToken*)tok);
|
||||
const HParsedToken *tok = a->action(tmp, a->user_data);
|
||||
return make_result(state->arena, (HParsedToken*)tok);
|
||||
} else
|
||||
return NULL;
|
||||
} else // either the parser's missing or the action's missing
|
||||
|
|
@ -27,6 +28,7 @@ static void desugar_action(HAllocator *mm__, HCFStack *stk__, void *env) {
|
|||
HCFS_BEGIN_SEQ() {
|
||||
HCFS_DESUGAR(a->p);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->user_data = a->user_data;
|
||||
HCFS_THIS_CHOICE->action = a->action;
|
||||
HCFS_THIS_CHOICE->reshape = h_act_first;
|
||||
} HCFS_END_CHOICE();
|
||||
|
|
@ -44,7 +46,7 @@ static bool action_isValidCF(void *env) {
|
|||
|
||||
static bool h_svm_action_action(HArena *arena, HSVMContext *ctx, void* arg) {
|
||||
HParseResult res;
|
||||
HAction action = arg;
|
||||
HParseAction *a = arg;
|
||||
assert(ctx->stack_count >= 1);
|
||||
if (ctx->stack[ctx->stack_count-1]->token_type != TT_MARK) {
|
||||
assert(ctx->stack_count >= 2 && ctx->stack[ctx->stack_count-2]->token_type == TT_MARK);
|
||||
|
|
@ -56,7 +58,7 @@ static bool h_svm_action_action(HArena *arena, HSVMContext *ctx, void* arg) {
|
|||
}
|
||||
res.arena = arena;
|
||||
|
||||
HParsedToken *tok = action(&res);
|
||||
HParsedToken *tok = a->action(&res, a->user_data);
|
||||
if (tok != NULL)
|
||||
ctx->stack[ctx->stack_count-1] = tok;
|
||||
else
|
||||
|
|
@ -69,7 +71,7 @@ static bool action_ctrvm(HRVMProg *prog, void* env) {
|
|||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
if (!h_compile_regex(prog, a->p))
|
||||
return false;
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_action, a->action));
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_action, a));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -81,13 +83,14 @@ static const HParserVtable action_vt = {
|
|||
.compile_to_rvm = action_ctrvm,
|
||||
};
|
||||
|
||||
HParser* h_action(const HParser* p, const HAction a) {
|
||||
return h_action__m(&system_allocator, p, a);
|
||||
HParser* h_action(const HParser* p, const HAction a, void* user_data) {
|
||||
return h_action__m(&system_allocator, p, a, user_data);
|
||||
}
|
||||
|
||||
HParser* h_action__m(HAllocator* mm__, const HParser* p, const HAction a) {
|
||||
HParser* h_action__m(HAllocator* mm__, const HParser* p, const HAction a, void* user_data) {
|
||||
HParseAction *env = h_new(HParseAction, 1);
|
||||
env->p = p;
|
||||
env->action = a;
|
||||
env->user_data = user_data;
|
||||
return h_new_parser(mm__, &action_vt, env);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,9 @@ static HParseResult* parse_bits(void* env, HParseState *state) {
|
|||
return make_result(state->arena, result);
|
||||
}
|
||||
|
||||
static HParsedToken *reshape_bits(const HParseResult *p, bool signedp) {
|
||||
static HParsedToken *reshape_bits(const HParseResult *p, void* signedp_p) {
|
||||
// signedp == NULL iff unsigned
|
||||
bool signedp = (signedp_p != NULL);
|
||||
// XXX works only for whole bytes
|
||||
// XXX assumes big-endian
|
||||
assert(p->ast);
|
||||
|
|
@ -45,12 +47,6 @@ static HParsedToken *reshape_bits(const HParseResult *p, bool signedp) {
|
|||
|
||||
return ret;
|
||||
}
|
||||
static HParsedToken *reshape_bits_unsigned(const HParseResult *p) {
|
||||
return reshape_bits(p, false);
|
||||
}
|
||||
static HParsedToken *reshape_bits_signed(const HParseResult *p) {
|
||||
return reshape_bits(p, true);
|
||||
}
|
||||
|
||||
static void desugar_bits(HAllocator *mm__, HCFStack *stk__, void *env) {
|
||||
struct bits_env *bits = (struct bits_env*)env;
|
||||
|
|
@ -67,9 +63,9 @@ static void desugar_bits(HAllocator *mm__, HCFStack *stk__, void *env) {
|
|||
HCFS_ADD_CHARSET(match_all);
|
||||
}
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = bits->signedp
|
||||
? reshape_bits_signed
|
||||
: reshape_bits_unsigned;
|
||||
HCFS_THIS_CHOICE->reshape = reshape_bits;
|
||||
HCFS_THIS_CHOICE->user_data = bits->signedp ? HCFS_THIS_CHOICE : NULL; // HCFS_THIS_CHOICE is an arbitrary non-null pointer
|
||||
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ static bool opt_isValidCF(void *env) {
|
|||
return p->vtable->isValidCF(p->env);
|
||||
}
|
||||
|
||||
static HParsedToken* reshape_optional(const HParseResult *p) {
|
||||
static HParsedToken* reshape_optional(const HParseResult *p, void* user_data) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
|
||||
|
|
@ -52,6 +52,7 @@ static void desugar_optional(HAllocator *mm__, HCFStack *stk__, void *env) {
|
|||
HCFS_BEGIN_SEQ() {
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = reshape_optional;
|
||||
HCFS_THIS_CHOICE->user_data = NULL;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ static bool sequence_isValidCF(void *env) {
|
|||
return true;
|
||||
}
|
||||
|
||||
static HParsedToken *reshape_sequence(const HParseResult *p) {
|
||||
static HParsedToken *reshape_sequence(const HParseResult *p, void* user_data) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
|
||||
|
|
@ -72,6 +72,7 @@ static void desugar_sequence(HAllocator *mm__, HCFStack *stk__, void *env) {
|
|||
HCFS_DESUGAR(s->p_array[i]);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = reshape_sequence;
|
||||
HCFS_THIS_CHOICE->user_data = NULL;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ static HParseResult* parse_token(void *env, HParseState *state) {
|
|||
}
|
||||
|
||||
|
||||
static HParsedToken *reshape_token(const HParseResult *p) {
|
||||
static HParsedToken *reshape_token(const HParseResult *p, void* user_data) {
|
||||
// fetch sequence of uints from p
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
|
|
@ -52,6 +52,7 @@ static void desugar_token(HAllocator *mm__, HCFStack *stk__, void *env) {
|
|||
HCFS_ADD_CHAR(tok->str[i]);
|
||||
} HCFS_END_SEQ();
|
||||
HCFS_THIS_CHOICE->reshape = reshape_token;
|
||||
HCFS_THIS_CHOICE->user_data = NULL;
|
||||
} HCFS_END_CHOICE();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ static void test_middle(gconstpointer backend) {
|
|||
|
||||
#include <ctype.h>
|
||||
|
||||
HParsedToken* upcase(const HParseResult *p) {
|
||||
HParsedToken* upcase(const HParseResult *p, void* user_data) {
|
||||
switch(p->ast->token_type) {
|
||||
case TT_SEQUENCE:
|
||||
{
|
||||
|
|
@ -202,7 +202,8 @@ static void test_action(gconstpointer backend) {
|
|||
h_ch('B'),
|
||||
NULL),
|
||||
NULL),
|
||||
upcase);
|
||||
upcase,
|
||||
NULL);
|
||||
|
||||
g_check_parse_match(action_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x41 u0x42)");
|
||||
g_check_parse_match(action_, (HParserBackend)GPOINTER_TO_INT(backend), "AB", 2, "(u0x41 u0x42)");
|
||||
|
|
@ -433,7 +434,7 @@ static void test_ambiguous(gconstpointer backend) {
|
|||
HParser *p_ = h_ch('+');
|
||||
HParser *E_ = h_indirect();
|
||||
h_bind_indirect(E_, h_choice(h_sequence(E_, p_, E_, NULL), d_, NULL));
|
||||
HParser *expr_ = h_action(E_, h_act_flatten);
|
||||
HParser *expr_ = h_action(E_, h_act_flatten, NULL);
|
||||
|
||||
g_check_parse_match(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1, "(u0x64)");
|
||||
g_check_parse_match(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d+d", 3, "(u0x64 u0x2b u0x64)");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue