Changed resulttype of action_t to parsed_token_t; users shouldn't have to assign arenas in results.
This commit is contained in:
parent
348e22dcfa
commit
3a0068d92b
2 changed files with 17 additions and 9 deletions
|
|
@ -305,8 +305,8 @@ typedef struct {
|
||||||
static parse_result_t* parse_action(void *env, parse_state_t *state) {
|
static parse_result_t* parse_action(void *env, parse_state_t *state) {
|
||||||
parse_action_t *a = (parse_action_t*)env;
|
parse_action_t *a = (parse_action_t*)env;
|
||||||
if (a->p && a->action) {
|
if (a->p && a->action) {
|
||||||
parse_result_t *ret = a->action(do_parse(a->p, state));
|
parsed_token_t *tok = a->action(do_parse(a->p, state));
|
||||||
return ret;
|
return make_result(state, tok);
|
||||||
} else // either the parser's missing or the action's missing
|
} else // either the parser's missing or the action's missing
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -971,7 +971,7 @@ static void test_whitespace(void) {
|
||||||
g_check_parse_failed(whitespace_, "_a", 2);
|
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
|
return NULL; // shut compiler up
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
20
src/hammer.h
20
src/hammer.h
|
|
@ -66,10 +66,11 @@ typedef struct parsed_token {
|
||||||
char bit_offset;
|
char bit_offset;
|
||||||
} parsed_token_t;
|
} parsed_token_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The result of a successful parse.
|
||||||
/* If a parse fails, the parse result will be NULL.
|
* 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.
|
* 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 {
|
typedef struct parse_result {
|
||||||
const parsed_token_t *ast;
|
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.
|
* 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
|
* Type of a boolean attribute-checking function, used in the
|
||||||
* attr_bool() parser. It can be any (user-defined) function that takes
|
* 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);
|
typedef bool (*predicate_t)(parse_result_t *p);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue