Define regex_t data structure and write build script

This commit is contained in:
Camden Dixie O'Brien 2024-10-25 13:30:33 +01:00
parent 8f4fe21bcf
commit 27b4b28fba
3 changed files with 108 additions and 0 deletions

43
lib/regex.c Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "regex.h"
#include <stdlib.h>
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);
}
}

54
lib/regex.h Normal file
View File

@ -0,0 +1,54 @@
/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#ifndef REGEX_H
#define REGEX_H
#include <stdbool.h>
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

11
scripts/build.sh Normal file
View File

@ -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