Rename structures and free procedures to reflect module change

This commit is contained in:
2024-10-26 19:32:14 +01:00
parent b3d54c307b
commit e508cc62f5
8 changed files with 262 additions and 262 deletions

View File

@@ -9,40 +9,40 @@
#include <stdlib.h>
#include <string.h>
static bool desugar_class(parse_term_t *term)
static bool desugar_class(regex_term_t *term)
{
if (term->class.negated)
return false;
const int count = term->class.count;
parse_sequence_t *alternatives
= malloc(count * sizeof(parse_sequence_t));
regex_sequence_t *alternatives
= malloc(count * sizeof(regex_sequence_t));
if (NULL == alternatives)
return false;
for (int i = 0; i < count; ++i) {
parse_term_t *terms = malloc(sizeof(parse_term_t));
terms[0].quantifier = PARSE_QUANTIFIER_NONE;
terms[0].type = PARSE_TERM_LITERAL;
regex_term_t *terms = malloc(sizeof(regex_term_t));
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
terms[0].type = REGEX_TERM_LITERAL;
terms[0].literal = term->class.contents[i];
alternatives[i].count = alternatives[i].capacity = 1;
alternatives[i].contents = terms;
}
parse_free_class_children(&term->class);
term->type = PARSE_TERM_SUBEXPR;
regex_free_class_children(&term->class);
term->type = REGEX_TERM_SUBEXPR;
term->subexpr.count = term->subexpr.capacity = count;
term->subexpr.contents = alternatives;
return true;
}
static bool deep_copy_term(parse_term_t *dst, parse_term_t *src);
static bool deep_copy_term(regex_term_t *dst, regex_term_t *src);
static bool deep_copy_sequence(parse_sequence_t *dst, parse_sequence_t *src)
static bool deep_copy_sequence(regex_sequence_t *dst, regex_sequence_t *src)
{
dst->count = dst->capacity = src->count;
dst->contents = malloc(dst->capacity * sizeof(parse_term_t));
dst->contents = malloc(dst->capacity * sizeof(regex_term_t));
if (NULL == dst->contents)
return false;
@@ -52,16 +52,16 @@ static bool deep_copy_sequence(parse_sequence_t *dst, parse_sequence_t *src)
return true;
}
static bool deep_copy_term(parse_term_t *dst, parse_term_t *src)
static bool deep_copy_term(regex_term_t *dst, regex_term_t *src)
{
assert(PARSE_TERM_WILDCARD != src->type);
assert(PARSE_TERM_CLASS != src->type);
assert(REGEX_TERM_WILDCARD != src->type);
assert(REGEX_TERM_CLASS != src->type);
memcpy(dst, src, sizeof(parse_term_t));
if (PARSE_TERM_SUBEXPR == src->type) {
memcpy(dst, src, sizeof(regex_term_t));
if (REGEX_TERM_SUBEXPR == src->type) {
dst->subexpr.capacity = src->subexpr.count;
dst->subexpr.contents
= malloc(dst->subexpr.capacity * sizeof(parse_sequence_t));
= malloc(dst->subexpr.capacity * sizeof(regex_sequence_t));
if (NULL == dst->subexpr.contents)
return false;
@@ -74,92 +74,92 @@ static bool deep_copy_term(parse_term_t *dst, parse_term_t *src)
return true;
}
static bool desugar_plus(parse_term_t *term)
static bool desugar_plus(regex_term_t *term)
{
parse_sequence_t *alternatives = malloc(sizeof(parse_sequence_t));
regex_sequence_t *alternatives = malloc(sizeof(regex_sequence_t));
if (NULL == alternatives)
return false;
alternatives[0].count = alternatives[0].capacity = 2;
alternatives[0].contents = malloc(2 * sizeof(parse_term_t));
alternatives[0].contents = malloc(2 * sizeof(regex_term_t));
if (NULL == alternatives[0].contents)
return false;
memcpy(&alternatives[0].contents[0], term, sizeof(parse_term_t));
memcpy(&alternatives[0].contents[0], term, sizeof(regex_term_t));
if (!deep_copy_term(&alternatives[0].contents[1], term))
return false;
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
alternatives[0].contents[1].quantifier = PARSE_QUANTIFIER_STAR;
alternatives[0].contents[0].quantifier = REGEX_QUANTIFIER_NONE;
alternatives[0].contents[1].quantifier = REGEX_QUANTIFIER_STAR;
term->quantifier = PARSE_QUANTIFIER_NONE;
term->type = PARSE_TERM_SUBEXPR;
term->quantifier = REGEX_QUANTIFIER_NONE;
term->type = REGEX_TERM_SUBEXPR;
term->subexpr.count = term->subexpr.capacity = 1;
term->subexpr.contents = alternatives;
return true;
}
static bool desugar_qmark(parse_term_t *term)
static bool desugar_qmark(regex_term_t *term)
{
parse_sequence_t *alternatives = malloc(2 * sizeof(parse_sequence_t));
regex_sequence_t *alternatives = malloc(2 * sizeof(regex_sequence_t));
if (NULL == alternatives)
return false;
alternatives[0].count = alternatives[0].capacity = 1;
alternatives[0].contents = malloc(sizeof(parse_term_t));
alternatives[0].contents = malloc(sizeof(regex_term_t));
if (NULL == alternatives[0].contents)
return false;
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
alternatives[0].contents[0].type = PARSE_TERM_EMPTY;
alternatives[0].contents[0].quantifier = REGEX_QUANTIFIER_NONE;
alternatives[0].contents[0].type = REGEX_TERM_EMPTY;
alternatives[1].count = alternatives[0].capacity = 1;
alternatives[1].contents = malloc(sizeof(parse_term_t));
alternatives[1].contents = malloc(sizeof(regex_term_t));
if (NULL == alternatives[1].contents)
return false;
memcpy(&alternatives[1].contents[0], term, sizeof(parse_term_t));
alternatives[1].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
memcpy(&alternatives[1].contents[0], term, sizeof(regex_term_t));
alternatives[1].contents[0].quantifier = REGEX_QUANTIFIER_NONE;
term->quantifier = PARSE_QUANTIFIER_NONE;
term->type = PARSE_TERM_SUBEXPR;
term->quantifier = REGEX_QUANTIFIER_NONE;
term->type = REGEX_TERM_SUBEXPR;
term->subexpr.count = term->subexpr.capacity = 2;
term->subexpr.contents = alternatives;
return true;
}
static bool desugar_term(parse_term_t *term)
static bool desugar_term(regex_term_t *term)
{
switch (term->type) {
case PARSE_TERM_WILDCARD:
case REGEX_TERM_WILDCARD:
return false;
case PARSE_TERM_CLASS:
case REGEX_TERM_CLASS:
if (!desugar_class(term))
return false;
break;
case PARSE_TERM_LITERAL:
case PARSE_TERM_SUBEXPR:
case PARSE_TERM_EMPTY:
case REGEX_TERM_LITERAL:
case REGEX_TERM_SUBEXPR:
case REGEX_TERM_EMPTY:
break;
}
switch (term->quantifier) {
case PARSE_QUANTIFIER_PLUS:
case REGEX_QUANTIFIER_PLUS:
if (!desugar_plus(term))
return false;
break;
case PARSE_QUANTIFIER_QMARK:
case REGEX_QUANTIFIER_QMARK:
if (!desugar_qmark(term))
return false;
break;
case PARSE_QUANTIFIER_NONE:
case PARSE_QUANTIFIER_STAR:
case REGEX_QUANTIFIER_NONE:
case REGEX_QUANTIFIER_STAR:
break;
}
return true;
}
bool desugar_regex(parse_tree_t *regex)
bool desugar_regex(regex_t *regex)
{
for (int i = 0; i < regex->count; ++i) {
for (int j = 0; j < regex->contents[i].count; ++j) {