From 3a0068d92b1c54dfdaff5788910b4f7e00b47e5f Mon Sep 17 00:00:00 2001 From: "Meredith L. Patterson" <clonearmy@gmail.com> Date: Fri, 18 May 2012 12:49:40 +0200 Subject: [PATCH] Changed resulttype of action_t to parsed_token_t; users shouldn't have to assign arenas in results. --- src/hammer.c | 6 +++--- src/hammer.h | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/hammer.c b/src/hammer.c index 6ce17e1e..3631a04d 100644 --- a/src/hammer.c +++ b/src/hammer.c @@ -305,8 +305,8 @@ typedef struct { static parse_result_t* parse_action(void *env, parse_state_t *state) { parse_action_t *a = (parse_action_t*)env; if (a->p && a->action) { - parse_result_t *ret = a->action(do_parse(a->p, state)); - return ret; + parsed_token_t *tok = a->action(do_parse(a->p, state)); + return make_result(state, tok); } else // either the parser's missing or the action's missing return NULL; } @@ -971,7 +971,7 @@ static void test_whitespace(void) { g_check_parse_failed(whitespace_, "_a", 2); } -parse_result_t* upcase(parse_result_t *p) { +parsed_token_t* upcase(parse_result_t *p) { return NULL; // shut compiler up } diff --git a/src/hammer.h b/src/hammer.h index b1cbc197..f09af099 100644 --- a/src/hammer.h +++ b/src/hammer.h @@ -66,10 +66,11 @@ typedef struct parsed_token { char bit_offset; } parsed_token_t; - - -/* If a parse fails, the parse result will be NULL. - * If a parse is successful but there's nothing there (i.e., if end_p succeeds) then there's a parse result but its ast is NULL. +/** + * The result of a successful parse. + * If a parse fails, the parse result will be NULL. + * If a parse is successful but there's nothing there (i.e., if end_p + * succeeds) then there's a parse result but its ast is NULL. */ typedef struct parse_result { const parsed_token_t *ast; @@ -78,13 +79,20 @@ typedef struct parse_result { /** * Type of an action to apply to an AST, used in the action() parser. + * It can be any (user-defined) function that takes a parse_result_t* + * and returns a parsed_token_t*. (This is so that the user doesn't + * have to worry about memory allocation; action() does that for you.) + * Note that the tagged union in parsed_token_t* supports user-defined + * types, so you can create your own token types (corresponding to, + * say, structs) and stuff values for them into the void* in the + * tagged union in parsed_token_t. */ -typedef parse_result_t* (*action_t)(parse_result_t *p); +typedef parsed_token_t* (*action_t)(parse_result_t *p); /** * Type of a boolean attribute-checking function, used in the * attr_bool() parser. It can be any (user-defined) function that takes - * a parse_result_t and returns true or false. + * a parse_result_t* and returns true or false. */ typedef bool (*predicate_t)(parse_result_t *p); -- GitLab