Implement NFA construction for empty expression and literals
This commit is contained in:
45
lib/construct.c
Normal file
45
lib/construct.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
14
lib/construct.h
Normal file
14
lib/construct.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef CONSTRUCT_H
|
||||
#define CONSTRUCT_H
|
||||
|
||||
#include "fsa.h"
|
||||
#include "regex.h"
|
||||
|
||||
void construct(const regex_t *regex, fsa_t *out);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user