Add a pattern id variable to all nodes.

This commit is contained in:
Danila Fedorin 2017-01-26 15:02:44 -08:00
parent ee40622ceb
commit bae15027c3
2 changed files with 30 additions and 30 deletions

View File

@ -52,6 +52,10 @@ struct pattern_node_s {
* The id is unique within the pattern. * The id is unique within the pattern.
*/ */
int id; int id;
/**
* The id of the pattern that this node belongs to.
*/
int pattern_id;
/** /**
* The node's data that varies based on type. * The node's data that varies based on type.
@ -122,16 +126,6 @@ struct pattern_node_s {
*/ */
struct pattern_node_s* right; struct pattern_node_s* right;
} fork_s; } fork_s;
/**
* Data for an "end" node.
*/
struct {
/**
* The ID of the pattern that just finished matching.
*/
int pattern_id;
} end_s;
} data_u; } data_u;
}; };

View File

@ -8,6 +8,7 @@
void _pattern_node_clear(pattern_node* to_clear){ 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;
memset(&(to_clear->data_u), 0, sizeof(to_clear->data_u)); memset(&(to_clear->data_u), 0, sizeof(to_clear->data_u));
} }
@ -22,22 +23,24 @@ liblex_result _pattern_node_create_clear(pattern_node** into){
return result; return result;
} }
liblex_result _pattern_node_create_value(pattern_node** into, int id, char value, pattern_node* next){ liblex_result _pattern_node_create_value(pattern_node** into, int id, int pattern_id, char value, pattern_node* next){
liblex_result result = _pattern_node_create_clear(into); liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
(*into)->type = PNODE_VALUE; (*into)->type = PNODE_VALUE;
(*into)->id = id; (*into)->id = id;
(*into)->pattern_id = pattern_id;
(*into)->data_u.value_s.value = value; (*into)->data_u.value_s.value = value;
(*into)->data_u.value_s.next = next; (*into)->data_u.value_s.next = next;
} }
return result; return result;
} }
liblex_result _pattern_node_create_range(pattern_node** into, int id, char from, char to, pattern_node* next){ liblex_result _pattern_node_create_range(pattern_node** into, int id, int pattern_id, char from, char to, pattern_node* next){
liblex_result result = _pattern_node_create_clear(into); liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
(*into)->type = PNODE_RANGE; (*into)->type = PNODE_RANGE;
(*into)->id = id; (*into)->id = id;
(*into)->pattern_id = pattern_id;
(*into)->data_u.range_s.from = from; (*into)->data_u.range_s.from = from;
(*into)->data_u.range_s.to = to; (*into)->data_u.range_s.to = to;
(*into)->data_u.range_s.next = next; (*into)->data_u.range_s.next = next;
@ -45,31 +48,34 @@ liblex_result _pattern_node_create_range(pattern_node** into, int id, char from,
return result; return result;
} }
liblex_result _pattern_node_create_any(pattern_node** into, int id, pattern_node* next){ liblex_result _pattern_node_create_any(pattern_node** into, int id, int pattern_id, pattern_node* next){
liblex_result result = _pattern_node_create_clear(into); liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
(*into)->type = PNODE_ANY; (*into)->type = PNODE_ANY;
(*into)->id = id; (*into)->id = id;
(*into)->pattern_id = pattern_id;
(*into)->data_u.any_s.next = next; (*into)->data_u.any_s.next = next;
} }
return result; return result;
} }
liblex_result _pattern_node_create_connect(pattern_node** into, int id, pattern_node* next){ liblex_result _pattern_node_create_connect(pattern_node** into, int id, int pattern_id, pattern_node* next){
liblex_result result = _pattern_node_create_clear(into); liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
(*into)->type = PNODE_CONNECT; (*into)->type = PNODE_CONNECT;
(*into)->id = id; (*into)->id = id;
(*into)->pattern_id = pattern_id;
(*into)->data_u.connect_s.next = next; (*into)->data_u.connect_s.next = next;
} }
return result; return result;
} }
liblex_result _pattern_node_create_fork(pattern_node** into, int id, pattern_node* left, pattern_node* right){ liblex_result _pattern_node_create_fork(pattern_node** into, int id, int pattern_id, pattern_node* left, pattern_node* right){
liblex_result result = _pattern_node_create_clear(into); liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
(*into)->type = PNODE_FORK; (*into)->type = PNODE_FORK;
(*into)->id = id; (*into)->id = id;
(*into)->pattern_id = pattern_id;
(*into)->data_u.fork_s.left = left; (*into)->data_u.fork_s.left = left;
(*into)->data_u.fork_s.right = right; (*into)->data_u.fork_s.right = right;
} }
@ -81,7 +87,7 @@ liblex_result _pattern_node_create_end(pattern_node** into, int id, int pattern_
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
(*into)->type = PNODE_END; (*into)->type = PNODE_END;
(*into)->id = id; (*into)->id = id;
(*into)->data_u.end_s.pattern_id = pattern_id; (*into)->pattern_id = pattern_id;
} }
return result; return result;
} }
@ -189,10 +195,10 @@ liblex_result _pattern_read_value(char* read_into, char* string, int* index){
return result; return result;
} }
liblex_result _pattern_build_or(pattern_chain** into, int* ids, char* string, int* index) { liblex_result _pattern_build_or(pattern_chain** into, int* ids, int pattern_id, char* string, int* index) {
pattern_node *tail_node; pattern_node *tail_node;
liblex_result result = LIBLEX_SUCCESS; liblex_result result = LIBLEX_SUCCESS;
result = _pattern_node_create_connect(&tail_node, (*ids)++, NULL); result = _pattern_node_create_connect(&tail_node, (*ids)++, pattern_id, NULL);
if (result == LIBLEX_SUCCESS) { if (result == LIBLEX_SUCCESS) {
result = _pattern_chain_create(into, NULL, tail_node); result = _pattern_chain_create(into, NULL, tail_node);
} }
@ -213,15 +219,15 @@ liblex_result _pattern_build_or(pattern_chain** into, int* ids, char* string, in
if (result == LIBLEX_SUCCESS) { if (result == LIBLEX_SUCCESS) {
if (to) { if (to) {
result = _pattern_node_create_range(&new_node, (*ids)++, from, to, tail_node); result = _pattern_node_create_range(&new_node, (*ids)++, pattern_id, from, to, tail_node);
} else { } else {
result = _pattern_node_create_value(&new_node, (*ids)++, from, tail_node); result = _pattern_node_create_value(&new_node, (*ids)++, pattern_id, from, tail_node);
} }
add_node = new_node; add_node = new_node;
} }
if (result == LIBLEX_SUCCESS && (*into)->head) { if (result == LIBLEX_SUCCESS && (*into)->head) {
result = _pattern_node_create_fork(&fork_node, (*ids)++, (*into)->head, new_node); result = _pattern_node_create_fork(&fork_node, (*ids)++, pattern_id, (*into)->head, new_node);
if (result == LIBLEX_SUCCESS) { if (result == LIBLEX_SUCCESS) {
add_node = fork_node; add_node = fork_node;
} }
@ -253,7 +259,7 @@ void _pattern_chain_append_chain_discard(pattern_chain* append_to, pattern_chain
*to_append = NULL; *to_append = NULL;
} }
liblex_result _pattern_build_chain(pattern_chain** into, int* ids, char* string, int* index){ liblex_result _pattern_build_chain(pattern_chain** into, int* ids, int pattern_id, char* string, int* index){
liblex_result result = LIBLEX_SUCCESS; liblex_result result = LIBLEX_SUCCESS;
ll or_stack; ll or_stack;
pattern_chain* current_chain = NULL; pattern_chain* current_chain = NULL;
@ -269,18 +275,18 @@ liblex_result _pattern_build_chain(pattern_chain** into, int* ids, char* string,
while(string[*index] && string[*index] != ')' && result == LIBLEX_SUCCESS){ while(string[*index] && string[*index] != ')' && result == LIBLEX_SUCCESS){
if(string[*index] == '('){ if(string[*index] == '('){
_pattern_chain_append_chain_discard(current_chain, &sub_chain); _pattern_chain_append_chain_discard(current_chain, &sub_chain);
result = _pattern_build_chain(&sub_chain, ids, 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, string, index); 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;
pattern_node* fork_node = NULL; pattern_node* fork_node = NULL;
result = _pattern_node_create_connect(&connection_node, (*ids)++, NULL); result = _pattern_node_create_connect(&connection_node, (*ids)++, pattern_id, NULL);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
result = _pattern_node_create_fork(&fork_node, (*ids)++, sub_chain->head, connection_node); result = _pattern_node_create_fork(&fork_node, (*ids)++, pattern_id, sub_chain->head, connection_node);
} }
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
@ -326,7 +332,7 @@ liblex_result _pattern_build_chain(pattern_chain** into, int* ids, char* string,
result = _pattern_chain_create(&sub_chain, NULL, NULL); result = _pattern_chain_create(&sub_chain, NULL, NULL);
} }
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
result = _pattern_node_create_value(&new_node, (*ids)++, new_char, NULL); result = _pattern_node_create_value(&new_node, (*ids)++, pattern_id, new_char, NULL);
} }
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
sub_chain->head = sub_chain->tail = new_node; sub_chain->head = sub_chain->tail = new_node;
@ -347,7 +353,7 @@ liblex_result _pattern_build_chain(pattern_chain** into, int* ids, char* string,
if(result == LIBLEX_SUCCESS && or_stack.head){ if(result == LIBLEX_SUCCESS && or_stack.head){
pattern_node* connect_node = NULL; pattern_node* connect_node = NULL;
result = _pattern_node_create_connect(&connect_node, (*ids)++, NULL); result = _pattern_node_create_connect(&connect_node, (*ids)++, pattern_id, NULL);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
_pattern_chain_append_node(current_chain, connect_node); _pattern_chain_append_node(current_chain, connect_node);
} }
@ -356,7 +362,7 @@ liblex_result _pattern_build_chain(pattern_chain** into, int* ids, char* string,
pattern_chain* new_chain = ll_pophead(&or_stack); pattern_chain* new_chain = ll_pophead(&or_stack);
if(new_chain && new_chain->head){ if(new_chain && new_chain->head){
result = _pattern_node_create_fork(&fork, (*ids)++, current_chain->head, new_chain->head); result = _pattern_node_create_fork(&fork, (*ids)++, pattern_id, current_chain->head, new_chain->head);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
_pattern_chain_append_node(new_chain, connect_node); _pattern_chain_append_node(new_chain, connect_node);
current_chain->head = fork; current_chain->head = fork;
@ -403,7 +409,7 @@ liblex_result pattern_compile(pattern* ptrn, char* expression, int id){
int index = -1; int index = -1;
int ids = 0; int ids = 0;
result = _pattern_build_chain(&full_chain, &ids, expression, &index); result = _pattern_build_chain(&full_chain, &ids, id, expression, &index);
if(result == LIBLEX_SUCCESS){ if(result == LIBLEX_SUCCESS){
result = _pattern_node_create_end(&end_node, ids, id); result = _pattern_node_create_end(&end_node, ids, id);
} }