Rename structures and free procedures to reflect module change
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -8,6 +8,6 @@
|
||||
|
||||
#include "regex.h"
|
||||
|
||||
bool desugar_regex(parse_tree_t *regex);
|
||||
bool desugar_regex(regex_t *regex);
|
||||
|
||||
#endif
|
||||
|
||||
34
lib/parse.c
34
lib/parse.c
@@ -44,7 +44,7 @@ static int parse_literal(const char *input, int rem, char *out)
|
||||
}
|
||||
}
|
||||
|
||||
static int parse_class(const char *input, int rem, parse_class_t *out)
|
||||
static int parse_class(const char *input, int rem, regex_class_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
@@ -88,7 +88,7 @@ static int parse_class(const char *input, int rem, parse_class_t *out)
|
||||
return out->count > 0 ? used : -1;
|
||||
}
|
||||
|
||||
static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
static int parse_term(const char *input, int rem, regex_term_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
@@ -96,7 +96,7 @@ static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
return -1;
|
||||
|
||||
if ('.' == input[0]) {
|
||||
out->type = PARSE_TERM_WILDCARD;
|
||||
out->type = REGEX_TERM_WILDCARD;
|
||||
++used;
|
||||
} else if ('(' == input[0]) {
|
||||
++used;
|
||||
@@ -104,7 +104,7 @@ static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
result = parse_expr(input + used, rem - used, &out->subexpr);
|
||||
if (result < 0)
|
||||
return -1;
|
||||
out->type = PARSE_TERM_SUBEXPR;
|
||||
out->type = REGEX_TERM_SUBEXPR;
|
||||
used += result;
|
||||
|
||||
if (')' != input[used])
|
||||
@@ -114,47 +114,47 @@ static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
result = parse_class(input + used, rem - used, &out->class);
|
||||
if (result < 0)
|
||||
return -1;
|
||||
out->type = PARSE_TERM_CLASS;
|
||||
out->type = REGEX_TERM_CLASS;
|
||||
used += result;
|
||||
} else {
|
||||
result = parse_literal(input + used, rem - used, &out->literal);
|
||||
if (result < 0)
|
||||
return -1;
|
||||
out->type = PARSE_TERM_LITERAL;
|
||||
out->type = REGEX_TERM_LITERAL;
|
||||
used += result;
|
||||
}
|
||||
|
||||
if (used < rem) {
|
||||
switch (input[used]) {
|
||||
case '*':
|
||||
out->quantifier = PARSE_QUANTIFIER_STAR;
|
||||
out->quantifier = REGEX_QUANTIFIER_STAR;
|
||||
++used;
|
||||
break;
|
||||
case '+':
|
||||
out->quantifier = PARSE_QUANTIFIER_PLUS;
|
||||
out->quantifier = REGEX_QUANTIFIER_PLUS;
|
||||
++used;
|
||||
break;
|
||||
case '?':
|
||||
out->quantifier = PARSE_QUANTIFIER_QMARK;
|
||||
out->quantifier = REGEX_QUANTIFIER_QMARK;
|
||||
++used;
|
||||
break;
|
||||
default:
|
||||
out->quantifier = PARSE_QUANTIFIER_NONE;
|
||||
out->quantifier = REGEX_QUANTIFIER_NONE;
|
||||
}
|
||||
} else {
|
||||
out->quantifier = PARSE_QUANTIFIER_NONE;
|
||||
out->quantifier = REGEX_QUANTIFIER_NONE;
|
||||
}
|
||||
|
||||
return used;
|
||||
}
|
||||
|
||||
static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
|
||||
static int parse_sequence(const char *input, int rem, regex_sequence_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
out->count = 0;
|
||||
out->capacity = SEQUENCE_START_CAPACITY;
|
||||
out->contents = malloc(out->capacity * sizeof(parse_term_t));
|
||||
out->contents = malloc(out->capacity * sizeof(regex_term_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
|
||||
@@ -162,7 +162,7 @@ static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
|
||||
if (out->count >= out->capacity) {
|
||||
out->capacity *= 2;
|
||||
out->contents = realloc(
|
||||
out->contents, out->capacity * sizeof(parse_term_t));
|
||||
out->contents, out->capacity * sizeof(regex_term_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
}
|
||||
@@ -178,13 +178,13 @@ static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
|
||||
return out->count > 0 ? used : -1;
|
||||
}
|
||||
|
||||
int parse_expr(const char *input, int rem, parse_tree_t *out)
|
||||
int parse_expr(const char *input, int rem, regex_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
out->count = 0;
|
||||
out->capacity = TREE_START_CAPACITY;
|
||||
out->contents = malloc(out->capacity * sizeof(parse_sequence_t));
|
||||
out->contents = malloc(out->capacity * sizeof(regex_sequence_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
|
||||
@@ -202,7 +202,7 @@ int parse_expr(const char *input, int rem, parse_tree_t *out)
|
||||
if (out->count >= out->capacity) {
|
||||
out->capacity *= 2;
|
||||
out->contents = realloc(
|
||||
out->contents, out->capacity * sizeof(parse_sequence_t));
|
||||
out->contents, out->capacity * sizeof(regex_sequence_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,6 @@
|
||||
|
||||
#include "regex.h"
|
||||
|
||||
int parse_expr(const char *input, int rem, parse_tree_t *out);
|
||||
int parse_expr(const char *input, int rem, regex_t *out);
|
||||
|
||||
#endif
|
||||
|
||||
16
lib/regex.c
16
lib/regex.c
@@ -7,25 +7,25 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void parse_free_tree_children(const parse_tree_t *t)
|
||||
void regex_free_children(const regex_t *t)
|
||||
{
|
||||
if (NULL != t->contents) {
|
||||
for (int i = 0; i < t->count; ++i)
|
||||
parse_free_sequence_children(&t->contents[i]);
|
||||
regex_free_sequence_children(&t->contents[i]);
|
||||
free(t->contents);
|
||||
}
|
||||
}
|
||||
|
||||
void parse_free_sequence_children(const parse_sequence_t *s)
|
||||
void regex_free_sequence_children(const regex_sequence_t *s)
|
||||
{
|
||||
if (NULL != s->contents) {
|
||||
for (int i = 0; i < s->count; ++i) {
|
||||
switch (s->contents[i].type) {
|
||||
case PARSE_TERM_CLASS:
|
||||
parse_free_class_children(&s->contents[i].class);
|
||||
case REGEX_TERM_CLASS:
|
||||
regex_free_class_children(&s->contents[i].class);
|
||||
break;
|
||||
case PARSE_TERM_SUBEXPR:
|
||||
parse_free_tree_children(&s->contents[i].subexpr);
|
||||
case REGEX_TERM_SUBEXPR:
|
||||
regex_free_children(&s->contents[i].subexpr);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -35,7 +35,7 @@ void parse_free_sequence_children(const parse_sequence_t *s)
|
||||
}
|
||||
}
|
||||
|
||||
void parse_free_class_children(const parse_class_t *c)
|
||||
void regex_free_class_children(const regex_class_t *c)
|
||||
{
|
||||
if (NULL != c->contents)
|
||||
free(c->contents);
|
||||
|
||||
52
lib/regex.h
52
lib/regex.h
@@ -12,46 +12,46 @@ typedef struct {
|
||||
bool negated;
|
||||
int count, capacity;
|
||||
char *contents;
|
||||
} parse_class_t;
|
||||
} regex_class_t;
|
||||
|
||||
typedef enum {
|
||||
PARSE_QUANTIFIER_NONE,
|
||||
PARSE_QUANTIFIER_STAR,
|
||||
PARSE_QUANTIFIER_PLUS,
|
||||
PARSE_QUANTIFIER_QMARK,
|
||||
} parse_quantifier_t;
|
||||
REGEX_QUANTIFIER_NONE,
|
||||
REGEX_QUANTIFIER_STAR,
|
||||
REGEX_QUANTIFIER_PLUS,
|
||||
REGEX_QUANTIFIER_QMARK,
|
||||
} regex_quantifier_t;
|
||||
|
||||
typedef enum {
|
||||
PARSE_TERM_WILDCARD,
|
||||
PARSE_TERM_CLASS,
|
||||
PARSE_TERM_LITERAL,
|
||||
PARSE_TERM_SUBEXPR,
|
||||
PARSE_TERM_EMPTY,
|
||||
} parse_term_type_t;
|
||||
REGEX_TERM_WILDCARD,
|
||||
REGEX_TERM_CLASS,
|
||||
REGEX_TERM_LITERAL,
|
||||
REGEX_TERM_SUBEXPR,
|
||||
REGEX_TERM_EMPTY,
|
||||
} regex_term_type_t;
|
||||
|
||||
struct _parse_term;
|
||||
struct _regex_term;
|
||||
typedef struct {
|
||||
int count, capacity;
|
||||
struct _parse_term *contents;
|
||||
} parse_sequence_t;
|
||||
struct _regex_term *contents;
|
||||
} regex_sequence_t;
|
||||
|
||||
typedef struct {
|
||||
int count, capacity;
|
||||
parse_sequence_t *contents;
|
||||
} parse_tree_t;
|
||||
regex_sequence_t *contents;
|
||||
} regex_t;
|
||||
|
||||
typedef struct _parse_term {
|
||||
parse_quantifier_t quantifier;
|
||||
parse_term_type_t type;
|
||||
typedef struct _regex_term {
|
||||
regex_quantifier_t quantifier;
|
||||
regex_term_type_t type;
|
||||
union {
|
||||
parse_class_t class;
|
||||
regex_class_t class;
|
||||
char literal;
|
||||
parse_tree_t subexpr;
|
||||
regex_t subexpr;
|
||||
};
|
||||
} parse_term_t;
|
||||
} regex_term_t;
|
||||
|
||||
void parse_free_tree_children(const parse_tree_t *t);
|
||||
void parse_free_sequence_children(const parse_sequence_t *s);
|
||||
void parse_free_class_children(const parse_class_t *c);
|
||||
void regex_free_children(const regex_t *t);
|
||||
void regex_free_sequence_children(const regex_sequence_t *s);
|
||||
void regex_free_class_children(const regex_class_t *c);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user