diff --git a/lib/desugar.h b/lib/desugar.h index f0a9f28..87d9ab7 100644 --- a/lib/desugar.h +++ b/lib/desugar.h @@ -6,7 +6,7 @@ #ifndef DESUGAR_H #define DESUGAR_H -#include "parse.h" +#include "regex.h" bool desugar_regex(parse_tree_t *regex); diff --git a/lib/parse.c b/lib/parse.c index 24e53b2..0bc95a6 100644 --- a/lib/parse.c +++ b/lib/parse.c @@ -5,7 +5,6 @@ #include "parse.h" -#include #include #define CLASS_START_CAPACITY 4 @@ -168,8 +167,8 @@ static int parse_sequence(const char *input, int rem, parse_sequence_t *out) return -1; } - result - = parse_term(input + used, rem - used, &out->contents[out->count]); + result = parse_term( + input + used, rem - used, &out->contents[out->count]); if (result < 0) break; ++out->count; @@ -218,38 +217,3 @@ int parse_expr(const char *input, int rem, parse_tree_t *out) return used; } - -void parse_free_tree_children(const parse_tree_t *t) -{ - if (NULL != t->contents) { - for (int i = 0; i < t->count; ++i) - parse_free_sequence_children(&t->contents[i]); - free(t->contents); - } -} - -void parse_free_sequence_children(const parse_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); - break; - case PARSE_TERM_SUBEXPR: - parse_free_tree_children(&s->contents[i].subexpr); - break; - default: - break; - } - } - free(s->contents); - } -} - -void parse_free_class_children(const parse_class_t *c) -{ - if (NULL != c->contents) - free(c->contents); -} - diff --git a/lib/parse.h b/lib/parse.h index a35b191..78dedc7 100644 --- a/lib/parse.h +++ b/lib/parse.h @@ -6,54 +6,8 @@ #ifndef PARSE_H #define PARSE_H -#include - -typedef struct { - bool negated; - int count, capacity; - char *contents; -} parse_class_t; - -typedef enum { - PARSE_QUANTIFIER_NONE, - PARSE_QUANTIFIER_STAR, - PARSE_QUANTIFIER_PLUS, - PARSE_QUANTIFIER_QMARK, -} parse_quantifier_t; - -typedef enum { - PARSE_TERM_WILDCARD, - PARSE_TERM_CLASS, - PARSE_TERM_LITERAL, - PARSE_TERM_SUBEXPR, - PARSE_TERM_EMPTY, -} parse_term_type_t; - -struct _parse_term; -typedef struct { - int count, capacity; - struct _parse_term *contents; -} parse_sequence_t; - -typedef struct { - int count, capacity; - parse_sequence_t *contents; -} parse_tree_t; - -typedef struct _parse_term { - parse_quantifier_t quantifier; - parse_term_type_t type; - union { - parse_class_t class; - char literal; - parse_tree_t subexpr; - }; -} parse_term_t; +#include "regex.h" int parse_expr(const char *input, int rem, parse_tree_t *out); -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); - #endif diff --git a/lib/regex.c b/lib/regex.c new file mode 100644 index 0000000..ef1df48 --- /dev/null +++ b/lib/regex.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include "regex.h" + +#include + +void parse_free_tree_children(const parse_tree_t *t) +{ + if (NULL != t->contents) { + for (int i = 0; i < t->count; ++i) + parse_free_sequence_children(&t->contents[i]); + free(t->contents); + } +} + +void parse_free_sequence_children(const parse_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); + break; + case PARSE_TERM_SUBEXPR: + parse_free_tree_children(&s->contents[i].subexpr); + break; + default: + break; + } + } + free(s->contents); + } +} + +void parse_free_class_children(const parse_class_t *c) +{ + if (NULL != c->contents) + free(c->contents); +} diff --git a/lib/regex.h b/lib/regex.h new file mode 100644 index 0000000..d2e2c7c --- /dev/null +++ b/lib/regex.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#ifndef REGEX_H +#define REGEX_H + +#include + +typedef struct { + bool negated; + int count, capacity; + char *contents; +} parse_class_t; + +typedef enum { + PARSE_QUANTIFIER_NONE, + PARSE_QUANTIFIER_STAR, + PARSE_QUANTIFIER_PLUS, + PARSE_QUANTIFIER_QMARK, +} parse_quantifier_t; + +typedef enum { + PARSE_TERM_WILDCARD, + PARSE_TERM_CLASS, + PARSE_TERM_LITERAL, + PARSE_TERM_SUBEXPR, + PARSE_TERM_EMPTY, +} parse_term_type_t; + +struct _parse_term; +typedef struct { + int count, capacity; + struct _parse_term *contents; +} parse_sequence_t; + +typedef struct { + int count, capacity; + parse_sequence_t *contents; +} parse_tree_t; + +typedef struct _parse_term { + parse_quantifier_t quantifier; + parse_term_type_t type; + union { + parse_class_t class; + char literal; + parse_tree_t subexpr; + }; +} parse_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); + +#endif diff --git a/scripts/build.sh b/scripts/build.sh index f7b2401..1993c8c 100644 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -9,7 +9,8 @@ if [ ! -e build ]; then mkdir build; else rm build/*; fi # Build library clang $CFLAGS -Ilib -c lib/parse.c -o build/parse.o clang $CFLAGS -Ilib -c lib/desugar.c -o build/desugar.o -ar -crs build/lib.a build/parse.o build/desugar.o +clang $CFLAGS -Ilib -c lib/regex.c -o build/regex.o +ar -crs build/lib.a build/parse.o build/desugar.o build/regex.o # Build tests clang $CFLAGS -Itests -c tests/testing.c -o build/testing.o