Add internal function, mostly for memory allocation and concatenation.

This commit is contained in:
Danila Fedorin 2017-01-19 23:32:09 -08:00
parent e86518b716
commit 961136a41b

View File

@ -0,0 +1,131 @@
#include "pattern.h"
#include <stdlib.h>
#include <string.h>
void _pattern_node_clear(pattern_node* to_clear){
to_clear->type = PNODE_CLEAR;
to_clear->id = -1;
memset(&(to_clear->data_u), 0, sizeof(to_clear->data_u));
}
liblex_result _pattern_node_create_clear(pattern_node** into){
liblex_result result = LIBLEX_SUCCESS;
*into = malloc(sizeof(**into));
if(*into){
_pattern_node_clear(*into);
} else {
result = LIBLEX_MALLOC;
}
return result;
}
liblex_result _pattern_node_create_value(pattern_node** into, int id, char value, pattern_node* next){
liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){
(*into)->id = id;
(*into)->data_u.value_s.value = value;
(*into)->data_u.value_s.next = next;
}
return result;
}
liblex_result _pattern_node_create_range(pattern_node** into, int id, char from, char to, pattern_node* next){
liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){
(*into)->id = id;
(*into)->data_u.range_s.from = from;
(*into)->data_u.range_s.to = to;
(*into)->data_u.range_s.next = next;
}
return result;
}
liblex_result _pattern_node_create_any(pattern_node** into, int id, pattern_node* next){
liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){
(*into)->id = id;
(*into)->data_u.any_s.next = next;
}
return result;
}
liblex_result _pattern_node_create_connect(pattern_node** into, int id, pattern_node* next){
liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){
(*into)->id = id;
(*into)->data_u.connect_s.next = next;
}
return result;
}
liblex_result _pattern_node_create_fork(pattern_node** into, int id, pattern_node* left, pattern_node* right){
liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){
(*into)->id = id;
(*into)->data_u.fork_s.left = left;
(*into)->data_u.fork_s.right = right;
}
return result;
}
liblex_result _pattern_node_create_end(pattern_node** into, int id, int pattern_id){
liblex_result result = _pattern_node_create_clear(into);
if(result == LIBLEX_SUCCESS){
(*into)->id = id;
(*into)->data_u.end_s.pattern_id = pattern_id;
}
return result;
}
liblex_result _pattern_chain_create(pattern_chain** into, pattern_node* from, pattern_node* to){
liblex_result result = LIBLEX_SUCCESS;
*into = malloc(sizeof(**into));
if(*into){
(*into)->head = from;
(*into)->tail = to;
} else {
result = LIBLEX_SUCCESS;
}
return result;
}
pattern_node** _pattern_node_get_next(pattern_node* node) {
pattern_node** next_node = NULL;
if(node->type == PNODE_ANY){
next_node = &(node->data_u.any_s.next);
} else if(node->type == PNODE_RANGE){
next_node = &(node->data_u.range_s.next);
} else if(node->type == PNODE_VALUE) {
next_node = &(node->data_u.value_s.next);
} else if(node->type == PNODE_CONNECT){
next_node = &(node->data_u.connect_s.next);
}
return next_node;
}
void _pattern_node_append_node(pattern_node* append_to, pattern_node* to_append){
if(append_to && to_append){
pattern_node** next_node = _pattern_node_get_next(append_to);
if(next_node){
*next_node = to_append;
}
}
}
void _pattern_chain_append_node(pattern_chain* append_to, pattern_node* to_append){
if(append_to && to_append){
pattern_node** next_node = (append_to->head) ? &append_to->head : _pattern_node_get_next(append_to->tail);
if(next_node) {
*next_node = append_to->tail = to_append;
}
}
}
void _pattern_chain_append_chain(pattern_chain* append_to, pattern_chain* to_append){
if(append_to && to_append){
_pattern_chain_append_node(append_to, to_append->head);
if(to_append->tail) {
append_to->tail = to_append->tail;
}
}
}