Rename regex_t to parse_tree_t and merge module into parser

This commit is contained in:
2024-10-26 11:52:03 +01:00
parent a85367c2df
commit 0c4b033d75
6 changed files with 206 additions and 227 deletions

View File

@@ -101,10 +101,10 @@ static int parse_term(const char *input, int rem, term_t *out)
} else if ('(' == input[0]) {
++used;
result = parse_regex(input + used, rem - used, &out->regex);
result = parse_expr(input + used, rem - used, &out->subexpr);
if (result < 0)
return -1;
out->type = TERM_TYPE_REGEX;
out->type = TERM_TYPE_SUBEXPR;
used += result;
if (')' != input[used])
@@ -178,7 +178,7 @@ static int parse_sequence(const char *input, int rem, sequence_t *out)
return out->len > 0 ? used : -1;
}
int parse_regex(const char *input, int rem, regex_t *out)
int parse_expr(const char *input, int rem, parse_tree_t *out)
{
int result, used = 0;
@@ -190,10 +190,10 @@ int parse_regex(const char *input, int rem, regex_t *out)
if (used < rem && '|' == input[used]) {
++used;
out->alternative = malloc(sizeof(regex_t));
out->alternative = malloc(sizeof(parse_tree_t));
if (NULL == out->alternative)
return -1;
result = parse_regex(input + used, rem - used, out->alternative);
result = parse_expr(input + used, rem - used, out->alternative);
if (result < 0)
return -1;
used += result;
@@ -203,3 +203,38 @@ int parse_regex(const char *input, int rem, regex_t *out)
return used;
}
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_SUBEXPR:
parse_tree_free_children(&s->contents[i].subexpr);
break;
case TERM_TYPE_WILDCARD:
case TERM_TYPE_LITERAL:
break;
}
}
free(s->contents);
}
}
void parse_tree_free_children(parse_tree_t *t)
{
sequence_free(&t->sequence);
if (NULL != t->alternative) {
parse_tree_free_children(t->alternative);
free(t->alternative);
}
}

View File

@@ -6,8 +6,50 @@
#ifndef PARSER_H
#define PARSER_H
#include "regex.h"
#include <stdbool.h>
int parse_regex(const char *input, int rem, regex_t *out);
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_SUBEXPR,
} term_type_t;
struct _term;
typedef struct {
int len, capacity;
struct _term *contents;
} sequence_t;
typedef struct _parse_tree {
sequence_t sequence;
struct _parse_tree *alternative;
} parse_tree_t;
typedef struct _term {
quantifier_t quantifier;
term_type_t type;
union {
class_t class;
char literal;
parse_tree_t subexpr;
};
} term_t;
int parse_expr(const char *input, int rem, parse_tree_t *out);
void parse_tree_free_children(parse_tree_t *t);
#endif

View File

@@ -1,43 +0,0 @@
/*
* 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);
}
}

View File

@@ -1,54 +0,0 @@
/*
* 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