Implement question mark desugaring

This commit is contained in:
2024-10-26 16:49:18 +01:00
parent 7e7a7f1cb7
commit ac4e9911d8
2 changed files with 61 additions and 0 deletions

View File

@@ -53,12 +53,35 @@ static void desugar_plus(parse_term_t *term)
term->subexpr.contents = alternatives;
}
static void desugar_qmark(parse_term_t *term)
{
parse_sequence_t *alternatives = malloc(2 * sizeof(parse_sequence_t));
alternatives[0].count = alternatives[0].capacity = 1;
alternatives[0].contents = malloc(sizeof(parse_term_t));
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
alternatives[0].contents[0].type = PARSE_TERM_EMPTY;
alternatives[1].count = alternatives[0].capacity = 1;
alternatives[1].contents = malloc(sizeof(parse_term_t));
memcpy(&alternatives[1].contents[0], term, sizeof(parse_term_t));
alternatives[1].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
term->quantifier = PARSE_QUANTIFIER_NONE;
term->type = PARSE_TERM_SUBEXPR;
term->subexpr.count = term->subexpr.capacity = 2;
term->subexpr.contents = alternatives;
}
static void desugar_term(parse_term_t *term)
{
switch (term->quantifier) {
case PARSE_QUANTIFIER_PLUS:
desugar_plus(term);
break;
case PARSE_QUANTIFIER_QMARK:
desugar_qmark(term);
break;
case PARSE_QUANTIFIER_NONE:
case PARSE_QUANTIFIER_STAR:
break;

View File

@@ -176,6 +176,43 @@ static void a_plus_becomes_subexpr_aa_star(void)
parse_free_tree_children(&t);
}
static void a_qmark_becomes_subexpr_empty_or_a(void)
{
parse_term_t *terms = malloc(1 * sizeof(parse_term_t));
terms[0].quantifier = PARSE_QUANTIFIER_QMARK;
terms[0].type = PARSE_TERM_LITERAL;
terms[0].literal = 'a';
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 };
desugar_regex(&t);
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(2, 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_EMPTY, inner->contents[0].contents[0].type);
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('a', inner->contents[1].contents[0].literal);
parse_free_tree_children(&t);
}
int main(void)
{
TESTING_BEGIN();
@@ -185,5 +222,6 @@ int main(void)
a_or_b_or_c_is_unchanged();
subexpr_a_is_unchanged();
a_plus_becomes_subexpr_aa_star();
a_qmark_becomes_subexpr_empty_or_a();
return TESTING_END();
}