Store information about tokens when they are added.
This commit is contained in:
parent
bb07382b59
commit
3b72569b33
|
@ -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.
|
||||
|
|
27
src/eval.c
27
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user