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
|
|
@ -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