46 lines
878 B
C
46 lines
878 B
C
/*
|
|
* Copyright (c) Camden Dixie O'Brien
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#include "construct.h"
|
|
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
|
|
static void construct_literal(char literal, fsa_t *out)
|
|
{
|
|
fsa_init(out);
|
|
const int id = fsa_add_state(out);
|
|
fsa_add_rule(out, id, out->initial, literal);
|
|
out->initial = id;
|
|
}
|
|
|
|
static void construct_term(const regex_term_t *term, fsa_t *out)
|
|
{
|
|
switch (term->type) {
|
|
case REGEX_TERM_EMPTY:
|
|
fsa_init(out);
|
|
break;
|
|
case REGEX_TERM_LITERAL:
|
|
construct_literal(term->literal, out);
|
|
break;
|
|
case REGEX_TERM_SUBEXPR:
|
|
return;
|
|
|
|
case REGEX_TERM_WILDCARD:
|
|
case REGEX_TERM_CLASS:
|
|
assert(false);
|
|
}
|
|
}
|
|
|
|
static void construct_sequence(const regex_sequence_t *seq, fsa_t *out)
|
|
{
|
|
construct_term(&seq->contents[0], out);
|
|
}
|
|
|
|
void construct(const regex_t *regex, fsa_t *out)
|
|
{
|
|
construct_sequence(®ex->contents[0], out);
|
|
}
|