From 27b4b28fba5250c96782bc51c7fe3c409e36a129 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Fri, 25 Oct 2024 13:30:33 +0100 Subject: [PATCH] Define regex_t data structure and write build script --- lib/regex.c | 43 ++++++++++++++++++++++++++++++++++++++ lib/regex.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ scripts/build.sh | 11 ++++++++++ 3 files changed, 108 insertions(+) create mode 100644 lib/regex.c create mode 100644 lib/regex.h create mode 100644 scripts/build.sh diff --git a/lib/regex.c b/lib/regex.c new file mode 100644 index 0000000..2100db9 --- /dev/null +++ b/lib/regex.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include "regex.h" + +#include + +static void class_free(class_t *c) +{ + if (NULL != c->contents) + free(c->contents); +} + +static void sequence_free(sequence_t *s) +{ + if (NULL != s->contents) { + for (int i = 0; i < s->len; ++i) { + switch (s->contents[i].type) { + case TERM_TYPE_CLASS: + class_free(&s->contents[i].class); + break; + case TERM_TYPE_REGEX: + regex_free_children(&s->contents[i].regex); + break; + case TERM_TYPE_WILDCARD: + case TERM_TYPE_LITERAL: + break; + } + } + free(s->contents); + } +} + +void regex_free_children(regex_t *r) +{ + sequence_free(&r->sequence); + if (NULL != r->alternative) { + regex_free_children(r->alternative); + free(r->alternative); + } +} diff --git a/lib/regex.h b/lib/regex.h new file mode 100644 index 0000000..5d9efe2 --- /dev/null +++ b/lib/regex.h @@ -0,0 +1,54 @@ +/* + * 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; +} class_t; + +typedef enum { + QUANTIFIER_NONE, + QUANTIFIER_ZERO_OR_MORE, + QUANTIFIER_ONE_OR_MORE, + QUANTIFIER_ZERO_OR_ONE, +} quantifier_t; + +typedef enum { + TERM_TYPE_WILDCARD, + TERM_TYPE_CLASS, + TERM_TYPE_LITERAL, + TERM_TYPE_REGEX, +} term_type_t; + +struct _term; +typedef struct { + int len, capacity; + struct _term *contents; +} sequence_t; + +typedef struct _regex { + sequence_t sequence; + struct _regex *alternative; +} regex_t; + +typedef struct _term { + quantifier_t quantifier; + term_type_t type; + union { + class_t class; + char literal; + regex_t regex; + }; +} term_t; + +void regex_free_children(regex_t *r); + +#endif diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100644 index 0000000..2dd4a01 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,11 @@ +cd "$(git rev-parse --show-toplevel)" + +CFLAGS="$CFLAGS -std=c11 -pedantic -Wall -Wextra" +CFLAGS="$CFLAGS -fsanitize=address,undefined" +CFLAGS="$CFLAGS -O0 -ggdb" + +mkdir -p build + +# Build library +clang $CFLAGS -Ilib -c lib/regex.c -o build/regex.o +ar -crs build/lib.a build/regex.o