Implement (non-negated) class desugaring

This commit is contained in:
2024-10-26 17:50:12 +01:00
parent 8e7e0f1bc0
commit ecfbf2e5c4
2 changed files with 94 additions and 0 deletions

View File

@@ -9,6 +9,33 @@
#include <stdlib.h>
#include <string.h>
static bool desugar_class(parse_term_t *term)
{
if (term->class.negated)
return false;
const int count = term->class.count;
parse_sequence_t *alternatives
= malloc(count * sizeof(parse_sequence_t));
if (NULL == alternatives)
return false;
for (int i = 0; i < count; ++i) {
parse_term_t *terms = malloc(sizeof(parse_term_t));
terms[0].quantifier = PARSE_QUANTIFIER_NONE;
terms[0].type = PARSE_TERM_LITERAL;
terms[0].literal = term->class.contents[i];
alternatives[i].count = alternatives[i].capacity = 1;
alternatives[i].contents = terms;
}
parse_free_class_children(&term->class);
term->type = PARSE_TERM_SUBEXPR;
term->subexpr.count = term->subexpr.capacity = count;
term->subexpr.contents = alternatives;
return true;
}
static bool deep_copy_term(parse_term_t *dst, parse_term_t *src);
@@ -104,6 +131,20 @@ static bool desugar_qmark(parse_term_t *term)
static bool desugar_term(parse_term_t *term)
{
switch (term->type) {
case PARSE_TERM_WILDCARD:
return false;
case PARSE_TERM_CLASS:
if (!desugar_class(term))
return false;
break;
case PARSE_TERM_LITERAL:
case PARSE_TERM_SUBEXPR:
case PARSE_TERM_EMPTY:
break;
}
switch (term->quantifier) {
case PARSE_QUANTIFIER_PLUS:
if (!desugar_plus(term))

View File

@@ -243,6 +243,58 @@ static void a_qmark_becomes_subexpr_empty_or_a(void)
parse_free_tree_children(&t);
}
static void class_abc_becomes_subexpr_a_or_b_or_c(void)
{
char *options = malloc(3 * sizeof(char));
options[0] = 'a';
options[1] = 'b';
options[2] = 'c';
parse_term_t *terms = malloc(1 * sizeof(parse_term_t));
terms[0].quantifier = PARSE_QUANTIFIER_NONE;
terms[0].type = PARSE_TERM_CLASS;
terms[0].class.negated = false;
terms[0].class.count = terms[0].class.capacity = 3;
terms[0].class.contents = options;
parse_sequence_t *alternatives = malloc(1 * sizeof(parse_sequence_t));
alternatives[0].count = alternatives[0].capacity = 1;
alternatives[0].contents = terms;
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
const bool success = desugar_regex(&t);
ASSERT_TRUE(success);
ASSERT_EQ(1, t.count);
ASSERT_NOT_NULL(t.contents);
ASSERT_EQ(1, t.contents[0].count);
ASSERT_NOT_NULL(t.contents[0].contents);
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
ASSERT_EQ(PARSE_TERM_SUBEXPR, t.contents[0].contents[0].type);
const parse_tree_t *inner = &t.contents[0].contents[0].subexpr;
ASSERT_EQ(3, inner->count);
ASSERT_NOT_NULL(inner->contents);
ASSERT_EQ(1, inner->contents[0].count);
ASSERT_NOT_NULL(inner->contents[0].contents);
ASSERT_EQ(
PARSE_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
ASSERT_EQ(PARSE_TERM_LITERAL, inner->contents[0].contents[0].type);
ASSERT_EQ('a', inner->contents[0].contents[0].literal);
ASSERT_EQ(1, inner->contents[1].count);
ASSERT_NOT_NULL(inner->contents[1].contents);
ASSERT_EQ(
PARSE_QUANTIFIER_NONE, inner->contents[1].contents[0].quantifier);
ASSERT_EQ(PARSE_TERM_LITERAL, inner->contents[1].contents[0].type);
ASSERT_EQ('b', inner->contents[1].contents[0].literal);
ASSERT_EQ(1, inner->contents[2].count);
ASSERT_NOT_NULL(inner->contents[2].contents);
ASSERT_EQ(
PARSE_QUANTIFIER_NONE, inner->contents[2].contents[0].quantifier);
ASSERT_EQ(PARSE_TERM_LITERAL, inner->contents[2].contents[0].type);
ASSERT_EQ('c', inner->contents[2].contents[0].literal);
parse_free_tree_children(&t);
}
int main(void)
{
TESTING_BEGIN();
@@ -253,5 +305,6 @@ int main(void)
subexpr_a_is_unchanged();
a_plus_becomes_subexpr_aa_star();
a_qmark_becomes_subexpr_empty_or_a();
class_abc_becomes_subexpr_a_or_b_or_c();
return TESTING_END();
}