Support subexpressions in construct()

This commit is contained in:
Camden Dixie O'Brien 2024-10-27 13:35:24 +00:00
parent bbecd12c91
commit d5b40f9324
2 changed files with 39 additions and 1 deletions

View File

@ -74,7 +74,8 @@ static void construct_term(const regex_term_t *term, fsa_t *out)
construct_literal(term->literal, out); construct_literal(term->literal, out);
break; break;
case REGEX_TERM_SUBEXPR: case REGEX_TERM_SUBEXPR:
return; construct(&term->subexpr, out);
break;
case REGEX_TERM_WILDCARD: case REGEX_TERM_WILDCARD:
case REGEX_TERM_CLASS: case REGEX_TERM_CLASS:
assert(false); assert(false);

View File

@ -156,6 +156,42 @@ static void test_star(void)
fsa_free(&fsa); fsa_free(&fsa);
} }
static void test_subexpression(void)
{
regex_term_t *inner_terms = malloc(1 * sizeof(regex_term_t));
inner_terms[0].quantifier = REGEX_QUANTIFIER_NONE;
inner_terms[0].type = REGEX_TERM_LITERAL;
inner_terms[0].literal = 'a';
regex_sequence_t *inner_alternatives
= malloc(1 * sizeof(regex_sequence_t));
inner_alternatives[0].count = inner_alternatives[0].capacity = 1;
inner_alternatives[0].contents = inner_terms;
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
terms[0].type = REGEX_TERM_SUBEXPR;
terms[0].subexpr.count = terms[0].subexpr.capacity = 1;
terms[0].subexpr.contents = inner_alternatives;
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
alternatives[0].count = alternatives[0].capacity = 1;
alternatives[0].contents = terms;
regex_t regex = { .count = 1, .capacity = 1, .contents = alternatives };
fsa_t fsa;
construct(&regex, &fsa);
const fsa_state_t *initial = &fsa.states[fsa.initial];
ASSERT_EQ(2, fsa.count);
ASSERT_EQ(1, initial->count);
ASSERT_EQ('a', initial->rules[0].input);
const int next = initial->rules[0].next;
ASSERT_TRUE(fsa.states[next].final);
ASSERT_EQ(0, fsa.states[next].count);
regex_free(&regex);
fsa_free(&fsa);
}
int main(void) int main(void)
{ {
TESTING_BEGIN(); TESTING_BEGIN();
@ -164,5 +200,6 @@ int main(void)
test_sequence(); test_sequence();
test_union(); test_union();
test_star(); test_star();
test_subexpression();
return TESTING_END(); return TESTING_END();
} }