Add success result to desugar_regex()
This commit is contained in:
parent
ac4e9911d8
commit
07fc46ce38
@ -9,17 +9,25 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static void deep_copy_term(parse_term_t *dst, parse_term_t *src);
|
|
||||||
|
|
||||||
static void deep_copy_sequence(parse_sequence_t *dst, parse_sequence_t *src)
|
static bool deep_copy_term(parse_term_t *dst, parse_term_t *src);
|
||||||
|
|
||||||
|
static bool deep_copy_sequence(parse_sequence_t *dst, parse_sequence_t *src)
|
||||||
{
|
{
|
||||||
dst->count = dst->capacity = src->count;
|
dst->count = dst->capacity = src->count;
|
||||||
dst->contents = malloc(dst->capacity * sizeof(parse_term_t));
|
dst->contents = malloc(dst->capacity * sizeof(parse_term_t));
|
||||||
for (int i = 0; i < dst->count; ++i)
|
if (NULL == dst->contents)
|
||||||
deep_copy_term(&dst->contents[i], &src->contents[i]);
|
return false;
|
||||||
|
|
||||||
|
for (int i = 0; i < dst->count; ++i) {
|
||||||
|
if (!deep_copy_term(&dst->contents[i], &src->contents[i]))
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deep_copy_term(parse_term_t *dst, parse_term_t *src)
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool deep_copy_term(parse_term_t *dst, parse_term_t *src)
|
||||||
{
|
{
|
||||||
assert(PARSE_TERM_WILDCARD != src->type);
|
assert(PARSE_TERM_WILDCARD != src->type);
|
||||||
assert(PARSE_TERM_CLASS != src->type);
|
assert(PARSE_TERM_CLASS != src->type);
|
||||||
@ -29,21 +37,32 @@ static void deep_copy_term(parse_term_t *dst, parse_term_t *src)
|
|||||||
dst->subexpr.capacity = src->subexpr.count;
|
dst->subexpr.capacity = src->subexpr.count;
|
||||||
dst->subexpr.contents
|
dst->subexpr.contents
|
||||||
= malloc(dst->subexpr.capacity * sizeof(parse_sequence_t));
|
= malloc(dst->subexpr.capacity * sizeof(parse_sequence_t));
|
||||||
|
if (NULL == dst->subexpr.contents)
|
||||||
|
return false;
|
||||||
|
|
||||||
for (int i = 0; i < dst->subexpr.count; ++i) {
|
for (int i = 0; i < dst->subexpr.count; ++i) {
|
||||||
deep_copy_sequence(
|
if (!deep_copy_sequence(
|
||||||
&dst->subexpr.contents[i], &src->subexpr.contents[i]);
|
&dst->subexpr.contents[i], &src->subexpr.contents[i]))
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void desugar_plus(parse_term_t *term)
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool desugar_plus(parse_term_t *term)
|
||||||
{
|
{
|
||||||
parse_sequence_t *alternatives = malloc(sizeof(parse_sequence_t));
|
parse_sequence_t *alternatives = malloc(sizeof(parse_sequence_t));
|
||||||
|
if (NULL == alternatives)
|
||||||
|
return false;
|
||||||
alternatives[0].count = alternatives[0].capacity = 2;
|
alternatives[0].count = alternatives[0].capacity = 2;
|
||||||
alternatives[0].contents = malloc(2 * sizeof(parse_term_t));
|
alternatives[0].contents = malloc(2 * sizeof(parse_term_t));
|
||||||
|
if (NULL == alternatives[0].contents)
|
||||||
|
return false;
|
||||||
|
|
||||||
memcpy(&alternatives[0].contents[0], term, sizeof(parse_term_t));
|
memcpy(&alternatives[0].contents[0], term, sizeof(parse_term_t));
|
||||||
deep_copy_term(&alternatives[0].contents[1], term);
|
if (!deep_copy_term(&alternatives[0].contents[1], term))
|
||||||
|
return false;
|
||||||
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||||
alternatives[0].contents[1].quantifier = PARSE_QUANTIFIER_STAR;
|
alternatives[0].contents[1].quantifier = PARSE_QUANTIFIER_STAR;
|
||||||
|
|
||||||
@ -51,19 +70,27 @@ static void desugar_plus(parse_term_t *term)
|
|||||||
term->type = PARSE_TERM_SUBEXPR;
|
term->type = PARSE_TERM_SUBEXPR;
|
||||||
term->subexpr.count = term->subexpr.capacity = 1;
|
term->subexpr.count = term->subexpr.capacity = 1;
|
||||||
term->subexpr.contents = alternatives;
|
term->subexpr.contents = alternatives;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void desugar_qmark(parse_term_t *term)
|
static bool desugar_qmark(parse_term_t *term)
|
||||||
{
|
{
|
||||||
parse_sequence_t *alternatives = malloc(2 * sizeof(parse_sequence_t));
|
parse_sequence_t *alternatives = malloc(2 * sizeof(parse_sequence_t));
|
||||||
|
if (NULL == alternatives)
|
||||||
|
return false;
|
||||||
|
|
||||||
alternatives[0].count = alternatives[0].capacity = 1;
|
alternatives[0].count = alternatives[0].capacity = 1;
|
||||||
alternatives[0].contents = malloc(sizeof(parse_term_t));
|
alternatives[0].contents = malloc(sizeof(parse_term_t));
|
||||||
|
if (NULL == alternatives[0].contents)
|
||||||
|
return false;
|
||||||
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||||
alternatives[0].contents[0].type = PARSE_TERM_EMPTY;
|
alternatives[0].contents[0].type = PARSE_TERM_EMPTY;
|
||||||
|
|
||||||
alternatives[1].count = alternatives[0].capacity = 1;
|
alternatives[1].count = alternatives[0].capacity = 1;
|
||||||
alternatives[1].contents = malloc(sizeof(parse_term_t));
|
alternatives[1].contents = malloc(sizeof(parse_term_t));
|
||||||
|
if (NULL == alternatives[1].contents)
|
||||||
|
return false;
|
||||||
memcpy(&alternatives[1].contents[0], term, sizeof(parse_term_t));
|
memcpy(&alternatives[1].contents[0], term, sizeof(parse_term_t));
|
||||||
alternatives[1].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
alternatives[1].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||||
|
|
||||||
@ -71,27 +98,36 @@ static void desugar_qmark(parse_term_t *term)
|
|||||||
term->type = PARSE_TERM_SUBEXPR;
|
term->type = PARSE_TERM_SUBEXPR;
|
||||||
term->subexpr.count = term->subexpr.capacity = 2;
|
term->subexpr.count = term->subexpr.capacity = 2;
|
||||||
term->subexpr.contents = alternatives;
|
term->subexpr.contents = alternatives;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void desugar_term(parse_term_t *term)
|
static bool desugar_term(parse_term_t *term)
|
||||||
{
|
{
|
||||||
switch (term->quantifier) {
|
switch (term->quantifier) {
|
||||||
case PARSE_QUANTIFIER_PLUS:
|
case PARSE_QUANTIFIER_PLUS:
|
||||||
desugar_plus(term);
|
if (!desugar_plus(term))
|
||||||
|
return false;
|
||||||
break;
|
break;
|
||||||
case PARSE_QUANTIFIER_QMARK:
|
case PARSE_QUANTIFIER_QMARK:
|
||||||
desugar_qmark(term);
|
if (!desugar_qmark(term))
|
||||||
|
return false;
|
||||||
break;
|
break;
|
||||||
case PARSE_QUANTIFIER_NONE:
|
case PARSE_QUANTIFIER_NONE:
|
||||||
case PARSE_QUANTIFIER_STAR:
|
case PARSE_QUANTIFIER_STAR:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void desugar_regex(parse_tree_t *regex)
|
bool desugar_regex(parse_tree_t *regex)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < regex->count; ++i) {
|
for (int i = 0; i < regex->count; ++i) {
|
||||||
for (int j = 0; j < regex->contents[i].count; ++j)
|
for (int j = 0; j < regex->contents[i].count; ++j) {
|
||||||
desugar_term(®ex->contents[i].contents[j]);
|
if (!desugar_term(®ex->contents[i].contents[j]))
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -8,6 +8,6 @@
|
|||||||
|
|
||||||
#include "parse.h"
|
#include "parse.h"
|
||||||
|
|
||||||
void desugar_regex(parse_tree_t *regex);
|
bool desugar_regex(parse_tree_t *regex);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -19,7 +19,9 @@ static void a_is_unchanged(void)
|
|||||||
alternatives[0].contents = terms;
|
alternatives[0].contents = terms;
|
||||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||||
|
|
||||||
desugar_regex(&t);
|
const bool success = desugar_regex(&t);
|
||||||
|
ASSERT_TRUE(success);
|
||||||
|
|
||||||
ASSERT_EQ(1, t.count);
|
ASSERT_EQ(1, t.count);
|
||||||
ASSERT_NOT_NULL(t.contents);
|
ASSERT_NOT_NULL(t.contents);
|
||||||
ASSERT_EQ(1, t.contents[0].count);
|
ASSERT_EQ(1, t.contents[0].count);
|
||||||
@ -45,7 +47,9 @@ static void abc_is_unchanged(void)
|
|||||||
alternatives[0].contents = terms;
|
alternatives[0].contents = terms;
|
||||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||||
|
|
||||||
desugar_regex(&t);
|
const bool success = desugar_regex(&t);
|
||||||
|
ASSERT_TRUE(success);
|
||||||
|
|
||||||
ASSERT_EQ(1, t.count);
|
ASSERT_EQ(1, t.count);
|
||||||
ASSERT_NOT_NULL(t.contents);
|
ASSERT_NOT_NULL(t.contents);
|
||||||
ASSERT_EQ(3, t.contents[0].count);
|
ASSERT_EQ(3, t.contents[0].count);
|
||||||
@ -73,7 +77,9 @@ static void a_star_is_unchanged(void)
|
|||||||
alternatives[0].contents = terms;
|
alternatives[0].contents = terms;
|
||||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||||
|
|
||||||
desugar_regex(&t);
|
const bool success = desugar_regex(&t);
|
||||||
|
ASSERT_TRUE(success);
|
||||||
|
|
||||||
ASSERT_EQ(1, t.count);
|
ASSERT_EQ(1, t.count);
|
||||||
ASSERT_NOT_NULL(t.contents);
|
ASSERT_NOT_NULL(t.contents);
|
||||||
ASSERT_EQ(1, t.contents[0].count);
|
ASSERT_EQ(1, t.contents[0].count);
|
||||||
@ -100,7 +106,9 @@ static void a_or_b_or_c_is_unchanged(void)
|
|||||||
}
|
}
|
||||||
parse_tree_t t = { .count = 3, .capacity = 3, .contents = alternatives };
|
parse_tree_t t = { .count = 3, .capacity = 3, .contents = alternatives };
|
||||||
|
|
||||||
desugar_regex(&t);
|
const bool success = desugar_regex(&t);
|
||||||
|
ASSERT_TRUE(success);
|
||||||
|
|
||||||
ASSERT_EQ(3, t.count);
|
ASSERT_EQ(3, t.count);
|
||||||
ASSERT_NOT_NULL(t.contents);
|
ASSERT_NOT_NULL(t.contents);
|
||||||
for (int i = 0; i < 3; ++i) {
|
for (int i = 0; i < 3; ++i) {
|
||||||
@ -135,7 +143,8 @@ static void subexpr_a_is_unchanged(void)
|
|||||||
alternatives[0].contents = terms;
|
alternatives[0].contents = terms;
|
||||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||||
|
|
||||||
desugar_regex(&t);
|
const bool success = desugar_regex(&t);
|
||||||
|
ASSERT_TRUE(success);
|
||||||
|
|
||||||
parse_free_tree_children(&t);
|
parse_free_tree_children(&t);
|
||||||
}
|
}
|
||||||
@ -151,7 +160,9 @@ static void a_plus_becomes_subexpr_aa_star(void)
|
|||||||
alternatives[0].contents = terms;
|
alternatives[0].contents = terms;
|
||||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||||
|
|
||||||
desugar_regex(&t);
|
const bool success = desugar_regex(&t);
|
||||||
|
ASSERT_TRUE(success);
|
||||||
|
|
||||||
ASSERT_EQ(1, t.count);
|
ASSERT_EQ(1, t.count);
|
||||||
ASSERT_NOT_NULL(t.contents);
|
ASSERT_NOT_NULL(t.contents);
|
||||||
ASSERT_EQ(1, t.contents[0].count);
|
ASSERT_EQ(1, t.contents[0].count);
|
||||||
@ -187,7 +198,9 @@ static void a_qmark_becomes_subexpr_empty_or_a(void)
|
|||||||
alternatives[0].contents = terms;
|
alternatives[0].contents = terms;
|
||||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||||
|
|
||||||
desugar_regex(&t);
|
const bool success = desugar_regex(&t);
|
||||||
|
ASSERT_TRUE(success);
|
||||||
|
|
||||||
ASSERT_EQ(1, t.count);
|
ASSERT_EQ(1, t.count);
|
||||||
ASSERT_NOT_NULL(t.contents);
|
ASSERT_NOT_NULL(t.contents);
|
||||||
ASSERT_EQ(1, t.contents[0].count);
|
ASSERT_EQ(1, t.contents[0].count);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user