From 3b72569b33246856f38fb09686070053973f5c7d Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 5 Feb 2018 22:09:12 -0800 Subject: [PATCH] Store information about tokens when they are added. --- include/eval.h | 17 +++++++++++++++++ src/eval.c | 27 ++++++++++++++++++--------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/include/eval.h b/include/eval.h index c60acca..30e39ec 100644 --- a/include/eval.h +++ b/include/eval.h @@ -74,9 +74,26 @@ struct eval_config_s { ll states; }; +/** + * Information about a state that has been + * added with eval_config_add. + */ +struct eval_state_s { + /** + * The string that was used to compile + * this state. + */ + const char* source; + /** + * The pattern that was generated with this state. + */ + pattern* pattern; +}; + typedef struct match_s match; typedef struct eval_s eval; typedef struct eval_config_s eval_config; +typedef struct eval_state_s eval_state; /** * Initializes the evaluation configuration. diff --git a/src/eval.c b/src/eval.c index d6b0a62..890487f 100644 --- a/src/eval.c +++ b/src/eval.c @@ -16,27 +16,36 @@ void eval_config_init(eval_config* config){ } liblex_result eval_config_add(eval_config* config, const char* ptrn, int pattern_id){ liblex_result result; - pattern* new_pattern = malloc(sizeof(*new_pattern)); - if(new_pattern){ + pattern* new_pattern; + eval_state* new_state; + new_pattern = malloc(sizeof(*new_pattern)); + new_state = malloc(sizeof(*new_pattern)); + if(new_pattern && new_state){ result = pattern_compile(new_pattern, ptrn, pattern_id); if(result == LIBLEX_SUCCESS){ - result = (ll_append(&config->states, new_pattern) == LIBDS_SUCCESS) ? + new_state->source = ptrn; + new_state->pattern = new_pattern; + result = (ll_append(&config->states, new_state) == LIBDS_SUCCESS) ? LIBLEX_SUCCESS : LIBLEX_MALLOC; } if(result != LIBLEX_SUCCESS){ pattern_free(new_pattern); - free(new_pattern); } } else { result = LIBLEX_MALLOC; } + if(result != LIBLEX_SUCCESS) { + free(new_pattern); + free(new_state); + } return result; } int _eval_config_foreach_free(void *data, va_list args){ - pattern* ptrn = data; - int result = pattern_free(ptrn) == LIBLEX_SUCCESS ? 0 : EVAL_FOREACH_MALLOC; - free(ptrn); + eval_state* state = data; + int result = pattern_free(state->pattern) == LIBLEX_SUCCESS ? 0 : EVAL_FOREACH_MALLOC; + free(state->pattern); + free(state); return result; } @@ -79,9 +88,9 @@ liblex_result _eval_pairmap_add_node(ht *table, pattern_node *node){ } int _eval_foreach_add_node(void *data, va_list args){ - pattern* new_pattern = data; + eval_state* state = data; ht* pairmap = va_arg(args, ht*); - pattern_node* pattern_head = new_pattern->head; + pattern_node* pattern_head = state->pattern->head; return _eval_pairmap_add_node(pairmap, pattern_head) == LIBLEX_SUCCESS? 0 : EVAL_FOREACH_MALLOC; }