Remove desugaring step
This commit is contained in:
@@ -2,7 +2,6 @@ add_library(lib
|
||||
compile.c
|
||||
construct.c
|
||||
convert.c
|
||||
desugar.c
|
||||
fsa.c
|
||||
min_heap.c
|
||||
parse.c
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include "construct.h"
|
||||
#include "convert.h"
|
||||
#include "desugar.h"
|
||||
#include "parse.h"
|
||||
|
||||
bool compile(const char *regex, int len, fsa_t *dfa_out)
|
||||
@@ -15,7 +14,6 @@ bool compile(const char *regex, int len, fsa_t *dfa_out)
|
||||
regex_t pt;
|
||||
if (-1 == parse_expr(regex, len, &pt))
|
||||
return false;
|
||||
desugar_regex(&pt);
|
||||
|
||||
fsa_t nfa;
|
||||
construct_nfa(&pt, &nfa);
|
||||
|
||||
124
lib/desugar.c
124
lib/desugar.c
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "desugar.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void deep_copy_term(regex_term_t *dst, regex_term_t *src);
|
||||
|
||||
static void deep_copy_sequence(regex_sequence_t *dst, regex_sequence_t *src)
|
||||
{
|
||||
dst->count = dst->capacity = src->count;
|
||||
dst->contents = malloc(dst->capacity * sizeof(regex_term_t));
|
||||
assert(NULL != dst->contents);
|
||||
|
||||
for (int i = 0; i < dst->count; ++i)
|
||||
deep_copy_term(&dst->contents[i], &src->contents[i]);
|
||||
}
|
||||
|
||||
static void deep_copy_term(regex_term_t *dst, regex_term_t *src)
|
||||
{
|
||||
assert(REGEX_TERM_WILDCARD != src->type);
|
||||
|
||||
memcpy(dst, src, sizeof(regex_term_t));
|
||||
switch (src->type) {
|
||||
case REGEX_TERM_SUBEXPR:
|
||||
dst->subexpr.capacity = src->subexpr.count;
|
||||
dst->subexpr.contents
|
||||
= malloc(dst->subexpr.capacity * sizeof(regex_sequence_t));
|
||||
assert(NULL != dst->subexpr.contents);
|
||||
|
||||
for (int i = 0; i < dst->subexpr.count; ++i) {
|
||||
deep_copy_sequence(
|
||||
&dst->subexpr.contents[i], &src->subexpr.contents[i]);
|
||||
}
|
||||
break;
|
||||
|
||||
case REGEX_TERM_CLASS:
|
||||
dst->class.count = src->class.count;
|
||||
dst->class.capacity = src->class.capacity;
|
||||
dst->class.contents
|
||||
= malloc(dst->class.capacity * sizeof(regex_sequence_t));
|
||||
assert(NULL != dst->class.contents);
|
||||
memcpy(dst->class.contents, src->class.contents, src->class.count);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void desugar_plus(regex_term_t *term)
|
||||
{
|
||||
regex_sequence_t *alternatives = malloc(sizeof(regex_sequence_t));
|
||||
assert(NULL != alternatives);
|
||||
|
||||
alternatives[0].count = alternatives[0].capacity = 2;
|
||||
alternatives[0].contents = malloc(2 * sizeof(regex_term_t));
|
||||
assert(NULL != alternatives[0].contents);
|
||||
|
||||
memcpy(&alternatives[0].contents[0], term, sizeof(regex_term_t));
|
||||
deep_copy_term(&alternatives[0].contents[1], term);
|
||||
alternatives[0].contents[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
alternatives[0].contents[1].quantifier = REGEX_QUANTIFIER_STAR;
|
||||
|
||||
term->quantifier = REGEX_QUANTIFIER_NONE;
|
||||
term->type = REGEX_TERM_SUBEXPR;
|
||||
term->subexpr.count = term->subexpr.capacity = 1;
|
||||
term->subexpr.contents = alternatives;
|
||||
}
|
||||
|
||||
static void desugar_qmark(regex_term_t *term)
|
||||
{
|
||||
regex_sequence_t *alternatives = malloc(2 * sizeof(regex_sequence_t));
|
||||
assert(NULL != alternatives);
|
||||
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = malloc(sizeof(regex_term_t));
|
||||
assert(NULL != alternatives[0].contents);
|
||||
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(regex_term_t));
|
||||
assert(NULL != alternatives[1].contents);
|
||||
memcpy(&alternatives[1].contents[0], term, sizeof(regex_term_t));
|
||||
alternatives[1].contents[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
|
||||
term->quantifier = REGEX_QUANTIFIER_NONE;
|
||||
term->type = REGEX_TERM_SUBEXPR;
|
||||
term->subexpr.count = term->subexpr.capacity = 2;
|
||||
term->subexpr.contents = alternatives;
|
||||
}
|
||||
|
||||
static void desugar_term(regex_term_t *term)
|
||||
{
|
||||
if (REGEX_TERM_SUBEXPR == term->type)
|
||||
desugar_regex(&term->subexpr);
|
||||
|
||||
switch (term->quantifier) {
|
||||
case REGEX_QUANTIFIER_PLUS:
|
||||
desugar_plus(term);
|
||||
break;
|
||||
case REGEX_QUANTIFIER_QMARK:
|
||||
desugar_qmark(term);
|
||||
break;
|
||||
|
||||
case REGEX_QUANTIFIER_NONE:
|
||||
case REGEX_QUANTIFIER_STAR:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void desugar_regex(regex_t *regex)
|
||||
{
|
||||
for (int i = 0; i < regex->count; ++i) {
|
||||
for (int j = 0; j < regex->contents[i].count; ++j)
|
||||
desugar_term(®ex->contents[i].contents[j]);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef DESUGAR_H
|
||||
#define DESUGAR_H
|
||||
|
||||
#include "regex.h"
|
||||
|
||||
void desugar_regex(regex_t *regex);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user