Implement question mark desugaring
This commit is contained in:
@@ -53,12 +53,35 @@ static void desugar_plus(parse_term_t *term)
|
|||||||
term->subexpr.contents = alternatives;
|
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)
|
static void desugar_term(parse_term_t *term)
|
||||||
{
|
{
|
||||||
switch (term->quantifier) {
|
switch (term->quantifier) {
|
||||||
case PARSE_QUANTIFIER_PLUS:
|
case PARSE_QUANTIFIER_PLUS:
|
||||||
desugar_plus(term);
|
desugar_plus(term);
|
||||||
break;
|
break;
|
||||||
|
case PARSE_QUANTIFIER_QMARK:
|
||||||
|
desugar_qmark(term);
|
||||||
|
break;
|
||||||
case PARSE_QUANTIFIER_NONE:
|
case PARSE_QUANTIFIER_NONE:
|
||||||
case PARSE_QUANTIFIER_STAR:
|
case PARSE_QUANTIFIER_STAR:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -176,6 +176,43 @@ static void a_plus_becomes_subexpr_aa_star(void)
|
|||||||
parse_free_tree_children(&t);
|
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)
|
int main(void)
|
||||||
{
|
{
|
||||||
TESTING_BEGIN();
|
TESTING_BEGIN();
|
||||||
@@ -185,5 +222,6 @@ int main(void)
|
|||||||
a_or_b_or_c_is_unchanged();
|
a_or_b_or_c_is_unchanged();
|
||||||
subexpr_a_is_unchanged();
|
subexpr_a_is_unchanged();
|
||||||
a_plus_becomes_subexpr_aa_star();
|
a_plus_becomes_subexpr_aa_star();
|
||||||
|
a_qmark_becomes_subexpr_empty_or_a();
|
||||||
return TESTING_END();
|
return TESTING_END();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user