Compare commits
3 Commits
945426e245
...
17ab9beb71
Author | SHA1 | Date |
---|---|---|
Danila Fedorin | 17ab9beb71 | |
Danila Fedorin | eecfe653e3 | |
Danila Fedorin | e849cc8ba2 |
|
@ -56,6 +56,11 @@ struct pattern_node_s {
|
||||||
* The id of the pattern that this node belongs to.
|
* The id of the pattern that this node belongs to.
|
||||||
*/
|
*/
|
||||||
int pattern_id;
|
int pattern_id;
|
||||||
|
/**
|
||||||
|
* Whether to "invert" this node - inverted
|
||||||
|
* nodes indicate "match anything but this".
|
||||||
|
*/
|
||||||
|
int invert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The node's data that varies based on type.
|
* The node's data that varies based on type.
|
||||||
|
|
21
src/eval.c
21
src/eval.c
|
@ -50,16 +50,18 @@ liblex_result _eval_config_free_state(eval_state* state) {
|
||||||
|
|
||||||
liblex_result eval_config_remove(eval_config* config, const char* pattern, int pattern_id) {
|
liblex_result eval_config_remove(eval_config* config, const char* pattern, int pattern_id) {
|
||||||
liblex_result result = LIBLEX_SUCCESS;
|
liblex_result result = LIBLEX_SUCCESS;
|
||||||
ll_node** head = &(config->states.head);
|
ll_node* head = config->states.head;
|
||||||
while(*head && result == LIBLEX_SUCCESS) {
|
while(head && result == LIBLEX_SUCCESS) {
|
||||||
eval_state* current_state = (*head)->data;
|
eval_state* current_state = head->data;
|
||||||
if(strcmp(current_state->source, pattern) == 0 && current_state->pattern->head->pattern_id == pattern_id) {
|
if(strcmp(current_state->source, pattern) == 0 && current_state->pattern->head->pattern_id == pattern_id) {
|
||||||
ll_node* to_delete = *head;
|
ll_node* to_delete = head;
|
||||||
(*head) = (*head)->next;
|
head = head->next;
|
||||||
|
*(to_delete->prev ? &to_delete->prev->next : &(config->states.head)) = to_delete->next;
|
||||||
|
*(to_delete->next ? &to_delete->next->prev : &(config->states.tail)) = to_delete->prev;
|
||||||
free(to_delete);
|
free(to_delete);
|
||||||
result = _eval_config_free_state(current_state);
|
result = _eval_config_free_state(current_state);
|
||||||
} else {
|
} else {
|
||||||
head = &((*head)->next);
|
head = head->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -128,10 +130,15 @@ int _eval_node_matches(pattern_node *node, eval *eval){
|
||||||
}
|
}
|
||||||
|
|
||||||
int _eval_foreach_check_node(void *data, va_list args){
|
int _eval_foreach_check_node(void *data, va_list args){
|
||||||
|
int inverted_matches = 1;
|
||||||
int return_code = 0;
|
int return_code = 0;
|
||||||
pattern_node* node = data;
|
pattern_node* node = data;
|
||||||
eval* evl = va_arg(args, eval*);
|
eval* evl = va_arg(args, eval*);
|
||||||
if(_eval_node_matches(node, evl)){
|
while(node && node->invert && inverted_matches) {
|
||||||
|
inverted_matches &= !_eval_node_matches(node, evl);
|
||||||
|
node = _eval_pattern_node_get_next(node);
|
||||||
|
}
|
||||||
|
if(inverted_matches && _eval_node_matches(node, evl)){
|
||||||
return_code = _eval_pairmap_add_node(evl->set_next, _eval_pattern_node_get_next(node)) == LIBLEX_SUCCESS ? 0 : EVAL_FOREACH_MALLOC;
|
return_code = _eval_pairmap_add_node(evl->set_next, _eval_pattern_node_get_next(node)) == LIBLEX_SUCCESS ? 0 : EVAL_FOREACH_MALLOC;
|
||||||
if(return_code == 0){
|
if(return_code == 0){
|
||||||
evl->matched++;
|
evl->matched++;
|
||||||
|
|
|
@ -9,6 +9,7 @@ void _pattern_node_clear(pattern_node* to_clear){
|
||||||
to_clear->type = PNODE_CLEAR;
|
to_clear->type = PNODE_CLEAR;
|
||||||
to_clear->id = -1;
|
to_clear->id = -1;
|
||||||
to_clear->pattern_id = -1;
|
to_clear->pattern_id = -1;
|
||||||
|
to_clear->invert = 0;
|
||||||
memset(&(to_clear->data_u), 0, sizeof(to_clear->data_u));
|
memset(&(to_clear->data_u), 0, sizeof(to_clear->data_u));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,6 +196,57 @@ liblex_result _pattern_read_value(char* read_into, const char* string, int* inde
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
liblex_result _pattern_build_inverted_or(pattern_chain** into, int* ids, int pattern_id, const char* string, int* index) {
|
||||||
|
pattern_node* new_node = NULL;
|
||||||
|
liblex_result result = LIBLEX_SUCCESS;
|
||||||
|
result = _pattern_chain_create(into, NULL, NULL);
|
||||||
|
|
||||||
|
if(result == LIBLEX_SUCCESS){
|
||||||
|
(*index)++;
|
||||||
|
if(string[*index]) (*index)++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(string[*index] && string[*index] != ']' && result == LIBDS_SUCCESS) {
|
||||||
|
char from = '\0';
|
||||||
|
char to = '\0';
|
||||||
|
result = _pattern_read_value(&from, string, index);
|
||||||
|
if(result == LIBLEX_SUCCESS && string[*index] == '-') {
|
||||||
|
(*index)++;
|
||||||
|
result = _pattern_read_value(&to, string, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(result == LIBLEX_SUCCESS) {
|
||||||
|
if(to) {
|
||||||
|
result = _pattern_node_create_range(&new_node, (*ids)++, pattern_id, from, to, NULL);
|
||||||
|
} else {
|
||||||
|
result = _pattern_node_create_value(&new_node, (*ids)++, pattern_id, from, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(result == LIBLEX_SUCCESS) {
|
||||||
|
new_node->invert = 1;
|
||||||
|
_pattern_chain_append_node(*into, new_node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(result == LIBLEX_SUCCESS) {
|
||||||
|
result = _pattern_node_create_any(&new_node, (*ids)++, pattern_id, NULL);
|
||||||
|
if(result == LIBLEX_SUCCESS) {
|
||||||
|
_pattern_chain_append_node(*into, new_node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(result != LIBLEX_SUCCESS) {
|
||||||
|
if(*into && (*into)->head) {
|
||||||
|
_pattern_free((*into)->head, *ids);
|
||||||
|
}
|
||||||
|
free(*into);
|
||||||
|
} else {
|
||||||
|
(*index)++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
liblex_result _pattern_build_or(pattern_chain** into, int* ids, int pattern_id, const char* string, int* index) {
|
liblex_result _pattern_build_or(pattern_chain** into, int* ids, int pattern_id, const char* string, int* index) {
|
||||||
pattern_node *tail_node;
|
pattern_node *tail_node;
|
||||||
liblex_result result = LIBLEX_SUCCESS;
|
liblex_result result = LIBLEX_SUCCESS;
|
||||||
|
@ -284,7 +336,11 @@ liblex_result _pattern_build_chain(pattern_chain** into, int* ids, int pattern_i
|
||||||
result = _pattern_build_chain(&sub_chain, ids, pattern_id, string, index);
|
result = _pattern_build_chain(&sub_chain, ids, pattern_id, string, index);
|
||||||
} else if(string[*index] == '['){
|
} else if(string[*index] == '['){
|
||||||
_pattern_chain_append_chain_discard(current_chain, &sub_chain);
|
_pattern_chain_append_chain_discard(current_chain, &sub_chain);
|
||||||
result = _pattern_build_or(&sub_chain, ids, pattern_id, string, index);
|
if(string[(*index) + 1] == '^') {
|
||||||
|
result = _pattern_build_inverted_or(&sub_chain, ids, pattern_id, string, index);
|
||||||
|
} else {
|
||||||
|
result = _pattern_build_or(&sub_chain, ids, pattern_id, string, index);
|
||||||
|
}
|
||||||
} else if(string[*index] == '?' || string[*index] == '*' || string[*index] == '+'){
|
} else if(string[*index] == '?' || string[*index] == '*' || string[*index] == '+'){
|
||||||
if(sub_chain != NULL && sub_chain->head){
|
if(sub_chain != NULL && sub_chain->head){
|
||||||
pattern_node* connection_node = NULL;
|
pattern_node* connection_node = NULL;
|
||||||
|
|
Loading…
Reference in New Issue