Rename structures and free procedures to reflect module change
This commit is contained in:
@@ -9,40 +9,40 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool desugar_class(parse_term_t *term)
|
||||
static bool desugar_class(regex_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));
|
||||
regex_sequence_t *alternatives
|
||||
= malloc(count * sizeof(regex_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;
|
||||
regex_term_t *terms = malloc(sizeof(regex_term_t));
|
||||
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms[0].type = REGEX_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;
|
||||
regex_free_class_children(&term->class);
|
||||
term->type = REGEX_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);
|
||||
static bool deep_copy_term(regex_term_t *dst, regex_term_t *src);
|
||||
|
||||
static bool deep_copy_sequence(parse_sequence_t *dst, parse_sequence_t *src)
|
||||
static bool deep_copy_sequence(regex_sequence_t *dst, regex_sequence_t *src)
|
||||
{
|
||||
dst->count = dst->capacity = src->count;
|
||||
dst->contents = malloc(dst->capacity * sizeof(parse_term_t));
|
||||
dst->contents = malloc(dst->capacity * sizeof(regex_term_t));
|
||||
if (NULL == dst->contents)
|
||||
return false;
|
||||
|
||||
@@ -52,16 +52,16 @@ static bool deep_copy_sequence(parse_sequence_t *dst, parse_sequence_t *src)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool deep_copy_term(parse_term_t *dst, parse_term_t *src)
|
||||
static bool deep_copy_term(regex_term_t *dst, regex_term_t *src)
|
||||
{
|
||||
assert(PARSE_TERM_WILDCARD != src->type);
|
||||
assert(PARSE_TERM_CLASS != src->type);
|
||||
assert(REGEX_TERM_WILDCARD != src->type);
|
||||
assert(REGEX_TERM_CLASS != src->type);
|
||||
|
||||
memcpy(dst, src, sizeof(parse_term_t));
|
||||
if (PARSE_TERM_SUBEXPR == src->type) {
|
||||
memcpy(dst, src, sizeof(regex_term_t));
|
||||
if (REGEX_TERM_SUBEXPR == src->type) {
|
||||
dst->subexpr.capacity = src->subexpr.count;
|
||||
dst->subexpr.contents
|
||||
= malloc(dst->subexpr.capacity * sizeof(parse_sequence_t));
|
||||
= malloc(dst->subexpr.capacity * sizeof(regex_sequence_t));
|
||||
if (NULL == dst->subexpr.contents)
|
||||
return false;
|
||||
|
||||
@@ -74,92 +74,92 @@ static bool deep_copy_term(parse_term_t *dst, parse_term_t *src)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool desugar_plus(parse_term_t *term)
|
||||
static bool desugar_plus(regex_term_t *term)
|
||||
{
|
||||
parse_sequence_t *alternatives = malloc(sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(sizeof(regex_sequence_t));
|
||||
if (NULL == alternatives)
|
||||
return false;
|
||||
alternatives[0].count = alternatives[0].capacity = 2;
|
||||
alternatives[0].contents = malloc(2 * sizeof(parse_term_t));
|
||||
alternatives[0].contents = malloc(2 * sizeof(regex_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(regex_term_t));
|
||||
if (!deep_copy_term(&alternatives[0].contents[1], term))
|
||||
return false;
|
||||
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||
alternatives[0].contents[1].quantifier = PARSE_QUANTIFIER_STAR;
|
||||
alternatives[0].contents[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
alternatives[0].contents[1].quantifier = REGEX_QUANTIFIER_STAR;
|
||||
|
||||
term->quantifier = PARSE_QUANTIFIER_NONE;
|
||||
term->type = PARSE_TERM_SUBEXPR;
|
||||
term->quantifier = REGEX_QUANTIFIER_NONE;
|
||||
term->type = REGEX_TERM_SUBEXPR;
|
||||
term->subexpr.count = term->subexpr.capacity = 1;
|
||||
term->subexpr.contents = alternatives;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool desugar_qmark(parse_term_t *term)
|
||||
static bool desugar_qmark(regex_term_t *term)
|
||||
{
|
||||
parse_sequence_t *alternatives = malloc(2 * sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(2 * sizeof(regex_sequence_t));
|
||||
if (NULL == alternatives)
|
||||
return false;
|
||||
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = malloc(sizeof(parse_term_t));
|
||||
alternatives[0].contents = malloc(sizeof(regex_term_t));
|
||||
if (NULL == alternatives[0].contents)
|
||||
return false;
|
||||
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||
alternatives[0].contents[0].type = PARSE_TERM_EMPTY;
|
||||
alternatives[0].contents[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
alternatives[0].contents[0].type = REGEX_TERM_EMPTY;
|
||||
|
||||
alternatives[1].count = alternatives[0].capacity = 1;
|
||||
alternatives[1].contents = malloc(sizeof(parse_term_t));
|
||||
alternatives[1].contents = malloc(sizeof(regex_term_t));
|
||||
if (NULL == alternatives[1].contents)
|
||||
return false;
|
||||
memcpy(&alternatives[1].contents[0], term, sizeof(parse_term_t));
|
||||
alternatives[1].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||
memcpy(&alternatives[1].contents[0], term, sizeof(regex_term_t));
|
||||
alternatives[1].contents[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
|
||||
term->quantifier = PARSE_QUANTIFIER_NONE;
|
||||
term->type = PARSE_TERM_SUBEXPR;
|
||||
term->quantifier = REGEX_QUANTIFIER_NONE;
|
||||
term->type = REGEX_TERM_SUBEXPR;
|
||||
term->subexpr.count = term->subexpr.capacity = 2;
|
||||
term->subexpr.contents = alternatives;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool desugar_term(parse_term_t *term)
|
||||
static bool desugar_term(regex_term_t *term)
|
||||
{
|
||||
switch (term->type) {
|
||||
case PARSE_TERM_WILDCARD:
|
||||
case REGEX_TERM_WILDCARD:
|
||||
return false;
|
||||
case PARSE_TERM_CLASS:
|
||||
case REGEX_TERM_CLASS:
|
||||
if (!desugar_class(term))
|
||||
return false;
|
||||
break;
|
||||
|
||||
case PARSE_TERM_LITERAL:
|
||||
case PARSE_TERM_SUBEXPR:
|
||||
case PARSE_TERM_EMPTY:
|
||||
case REGEX_TERM_LITERAL:
|
||||
case REGEX_TERM_SUBEXPR:
|
||||
case REGEX_TERM_EMPTY:
|
||||
break;
|
||||
}
|
||||
|
||||
switch (term->quantifier) {
|
||||
case PARSE_QUANTIFIER_PLUS:
|
||||
case REGEX_QUANTIFIER_PLUS:
|
||||
if (!desugar_plus(term))
|
||||
return false;
|
||||
break;
|
||||
case PARSE_QUANTIFIER_QMARK:
|
||||
case REGEX_QUANTIFIER_QMARK:
|
||||
if (!desugar_qmark(term))
|
||||
return false;
|
||||
break;
|
||||
case PARSE_QUANTIFIER_NONE:
|
||||
case PARSE_QUANTIFIER_STAR:
|
||||
case REGEX_QUANTIFIER_NONE:
|
||||
case REGEX_QUANTIFIER_STAR:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool desugar_regex(parse_tree_t *regex)
|
||||
bool desugar_regex(regex_t *regex)
|
||||
{
|
||||
for (int i = 0; i < regex->count; ++i) {
|
||||
for (int j = 0; j < regex->contents[i].count; ++j) {
|
||||
|
||||
@@ -8,6 +8,6 @@
|
||||
|
||||
#include "regex.h"
|
||||
|
||||
bool desugar_regex(parse_tree_t *regex);
|
||||
bool desugar_regex(regex_t *regex);
|
||||
|
||||
#endif
|
||||
|
||||
34
lib/parse.c
34
lib/parse.c
@@ -44,7 +44,7 @@ static int parse_literal(const char *input, int rem, char *out)
|
||||
}
|
||||
}
|
||||
|
||||
static int parse_class(const char *input, int rem, parse_class_t *out)
|
||||
static int parse_class(const char *input, int rem, regex_class_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
@@ -88,7 +88,7 @@ static int parse_class(const char *input, int rem, parse_class_t *out)
|
||||
return out->count > 0 ? used : -1;
|
||||
}
|
||||
|
||||
static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
static int parse_term(const char *input, int rem, regex_term_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
@@ -96,7 +96,7 @@ static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
return -1;
|
||||
|
||||
if ('.' == input[0]) {
|
||||
out->type = PARSE_TERM_WILDCARD;
|
||||
out->type = REGEX_TERM_WILDCARD;
|
||||
++used;
|
||||
} else if ('(' == input[0]) {
|
||||
++used;
|
||||
@@ -104,7 +104,7 @@ static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
result = parse_expr(input + used, rem - used, &out->subexpr);
|
||||
if (result < 0)
|
||||
return -1;
|
||||
out->type = PARSE_TERM_SUBEXPR;
|
||||
out->type = REGEX_TERM_SUBEXPR;
|
||||
used += result;
|
||||
|
||||
if (')' != input[used])
|
||||
@@ -114,47 +114,47 @@ static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
result = parse_class(input + used, rem - used, &out->class);
|
||||
if (result < 0)
|
||||
return -1;
|
||||
out->type = PARSE_TERM_CLASS;
|
||||
out->type = REGEX_TERM_CLASS;
|
||||
used += result;
|
||||
} else {
|
||||
result = parse_literal(input + used, rem - used, &out->literal);
|
||||
if (result < 0)
|
||||
return -1;
|
||||
out->type = PARSE_TERM_LITERAL;
|
||||
out->type = REGEX_TERM_LITERAL;
|
||||
used += result;
|
||||
}
|
||||
|
||||
if (used < rem) {
|
||||
switch (input[used]) {
|
||||
case '*':
|
||||
out->quantifier = PARSE_QUANTIFIER_STAR;
|
||||
out->quantifier = REGEX_QUANTIFIER_STAR;
|
||||
++used;
|
||||
break;
|
||||
case '+':
|
||||
out->quantifier = PARSE_QUANTIFIER_PLUS;
|
||||
out->quantifier = REGEX_QUANTIFIER_PLUS;
|
||||
++used;
|
||||
break;
|
||||
case '?':
|
||||
out->quantifier = PARSE_QUANTIFIER_QMARK;
|
||||
out->quantifier = REGEX_QUANTIFIER_QMARK;
|
||||
++used;
|
||||
break;
|
||||
default:
|
||||
out->quantifier = PARSE_QUANTIFIER_NONE;
|
||||
out->quantifier = REGEX_QUANTIFIER_NONE;
|
||||
}
|
||||
} else {
|
||||
out->quantifier = PARSE_QUANTIFIER_NONE;
|
||||
out->quantifier = REGEX_QUANTIFIER_NONE;
|
||||
}
|
||||
|
||||
return used;
|
||||
}
|
||||
|
||||
static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
|
||||
static int parse_sequence(const char *input, int rem, regex_sequence_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
out->count = 0;
|
||||
out->capacity = SEQUENCE_START_CAPACITY;
|
||||
out->contents = malloc(out->capacity * sizeof(parse_term_t));
|
||||
out->contents = malloc(out->capacity * sizeof(regex_term_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
|
||||
@@ -162,7 +162,7 @@ static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
|
||||
if (out->count >= out->capacity) {
|
||||
out->capacity *= 2;
|
||||
out->contents = realloc(
|
||||
out->contents, out->capacity * sizeof(parse_term_t));
|
||||
out->contents, out->capacity * sizeof(regex_term_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
}
|
||||
@@ -178,13 +178,13 @@ static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
|
||||
return out->count > 0 ? used : -1;
|
||||
}
|
||||
|
||||
int parse_expr(const char *input, int rem, parse_tree_t *out)
|
||||
int parse_expr(const char *input, int rem, regex_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
out->count = 0;
|
||||
out->capacity = TREE_START_CAPACITY;
|
||||
out->contents = malloc(out->capacity * sizeof(parse_sequence_t));
|
||||
out->contents = malloc(out->capacity * sizeof(regex_sequence_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
|
||||
@@ -202,7 +202,7 @@ int parse_expr(const char *input, int rem, parse_tree_t *out)
|
||||
if (out->count >= out->capacity) {
|
||||
out->capacity *= 2;
|
||||
out->contents = realloc(
|
||||
out->contents, out->capacity * sizeof(parse_sequence_t));
|
||||
out->contents, out->capacity * sizeof(regex_sequence_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,6 @@
|
||||
|
||||
#include "regex.h"
|
||||
|
||||
int parse_expr(const char *input, int rem, parse_tree_t *out);
|
||||
int parse_expr(const char *input, int rem, regex_t *out);
|
||||
|
||||
#endif
|
||||
|
||||
16
lib/regex.c
16
lib/regex.c
@@ -7,25 +7,25 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void parse_free_tree_children(const parse_tree_t *t)
|
||||
void regex_free_children(const regex_t *t)
|
||||
{
|
||||
if (NULL != t->contents) {
|
||||
for (int i = 0; i < t->count; ++i)
|
||||
parse_free_sequence_children(&t->contents[i]);
|
||||
regex_free_sequence_children(&t->contents[i]);
|
||||
free(t->contents);
|
||||
}
|
||||
}
|
||||
|
||||
void parse_free_sequence_children(const parse_sequence_t *s)
|
||||
void regex_free_sequence_children(const regex_sequence_t *s)
|
||||
{
|
||||
if (NULL != s->contents) {
|
||||
for (int i = 0; i < s->count; ++i) {
|
||||
switch (s->contents[i].type) {
|
||||
case PARSE_TERM_CLASS:
|
||||
parse_free_class_children(&s->contents[i].class);
|
||||
case REGEX_TERM_CLASS:
|
||||
regex_free_class_children(&s->contents[i].class);
|
||||
break;
|
||||
case PARSE_TERM_SUBEXPR:
|
||||
parse_free_tree_children(&s->contents[i].subexpr);
|
||||
case REGEX_TERM_SUBEXPR:
|
||||
regex_free_children(&s->contents[i].subexpr);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -35,7 +35,7 @@ void parse_free_sequence_children(const parse_sequence_t *s)
|
||||
}
|
||||
}
|
||||
|
||||
void parse_free_class_children(const parse_class_t *c)
|
||||
void regex_free_class_children(const regex_class_t *c)
|
||||
{
|
||||
if (NULL != c->contents)
|
||||
free(c->contents);
|
||||
|
||||
52
lib/regex.h
52
lib/regex.h
@@ -12,46 +12,46 @@ typedef struct {
|
||||
bool negated;
|
||||
int count, capacity;
|
||||
char *contents;
|
||||
} parse_class_t;
|
||||
} regex_class_t;
|
||||
|
||||
typedef enum {
|
||||
PARSE_QUANTIFIER_NONE,
|
||||
PARSE_QUANTIFIER_STAR,
|
||||
PARSE_QUANTIFIER_PLUS,
|
||||
PARSE_QUANTIFIER_QMARK,
|
||||
} parse_quantifier_t;
|
||||
REGEX_QUANTIFIER_NONE,
|
||||
REGEX_QUANTIFIER_STAR,
|
||||
REGEX_QUANTIFIER_PLUS,
|
||||
REGEX_QUANTIFIER_QMARK,
|
||||
} regex_quantifier_t;
|
||||
|
||||
typedef enum {
|
||||
PARSE_TERM_WILDCARD,
|
||||
PARSE_TERM_CLASS,
|
||||
PARSE_TERM_LITERAL,
|
||||
PARSE_TERM_SUBEXPR,
|
||||
PARSE_TERM_EMPTY,
|
||||
} parse_term_type_t;
|
||||
REGEX_TERM_WILDCARD,
|
||||
REGEX_TERM_CLASS,
|
||||
REGEX_TERM_LITERAL,
|
||||
REGEX_TERM_SUBEXPR,
|
||||
REGEX_TERM_EMPTY,
|
||||
} regex_term_type_t;
|
||||
|
||||
struct _parse_term;
|
||||
struct _regex_term;
|
||||
typedef struct {
|
||||
int count, capacity;
|
||||
struct _parse_term *contents;
|
||||
} parse_sequence_t;
|
||||
struct _regex_term *contents;
|
||||
} regex_sequence_t;
|
||||
|
||||
typedef struct {
|
||||
int count, capacity;
|
||||
parse_sequence_t *contents;
|
||||
} parse_tree_t;
|
||||
regex_sequence_t *contents;
|
||||
} regex_t;
|
||||
|
||||
typedef struct _parse_term {
|
||||
parse_quantifier_t quantifier;
|
||||
parse_term_type_t type;
|
||||
typedef struct _regex_term {
|
||||
regex_quantifier_t quantifier;
|
||||
regex_term_type_t type;
|
||||
union {
|
||||
parse_class_t class;
|
||||
regex_class_t class;
|
||||
char literal;
|
||||
parse_tree_t subexpr;
|
||||
regex_t subexpr;
|
||||
};
|
||||
} parse_term_t;
|
||||
} regex_term_t;
|
||||
|
||||
void parse_free_tree_children(const parse_tree_t *t);
|
||||
void parse_free_sequence_children(const parse_sequence_t *s);
|
||||
void parse_free_class_children(const parse_class_t *c);
|
||||
void regex_free_children(const regex_t *t);
|
||||
void regex_free_sequence_children(const regex_sequence_t *s);
|
||||
void regex_free_class_children(const regex_class_t *c);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,14 +10,14 @@
|
||||
|
||||
static void a_is_unchanged(void)
|
||||
{
|
||||
parse_term_t *terms = malloc(1 * sizeof(parse_term_t));
|
||||
terms[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||
terms[0].type = PARSE_TERM_LITERAL;
|
||||
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
||||
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms[0].type = REGEX_TERM_LITERAL;
|
||||
terms[0].literal = 'a';
|
||||
parse_sequence_t *alternatives = malloc(1 * sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = terms;
|
||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
regex_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
|
||||
const bool success = desugar_regex(&t);
|
||||
ASSERT_TRUE(success);
|
||||
@@ -26,26 +26,26 @@ static void a_is_unchanged(void)
|
||||
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_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ('a', t.contents[0].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void abc_is_unchanged(void)
|
||||
{
|
||||
parse_term_t *terms = malloc(3 * sizeof(parse_term_t));
|
||||
terms[0].type = PARSE_TERM_LITERAL;
|
||||
regex_term_t *terms = malloc(3 * sizeof(regex_term_t));
|
||||
terms[0].type = REGEX_TERM_LITERAL;
|
||||
terms[0].literal = 'a';
|
||||
terms[1].type = PARSE_TERM_LITERAL;
|
||||
terms[1].type = REGEX_TERM_LITERAL;
|
||||
terms[1].literal = 'b';
|
||||
terms[2].type = PARSE_TERM_LITERAL;
|
||||
terms[2].type = REGEX_TERM_LITERAL;
|
||||
terms[2].literal = 'c';
|
||||
parse_sequence_t *alternatives = malloc(1 * sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 3;
|
||||
alternatives[0].contents = terms;
|
||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
regex_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
|
||||
const bool success = desugar_regex(&t);
|
||||
ASSERT_TRUE(success);
|
||||
@@ -54,28 +54,28 @@ static void abc_is_unchanged(void)
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
ASSERT_EQ(3, t.contents[0].count);
|
||||
ASSERT_NOT_NULL(t.contents[0].contents);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ('a', t.contents[0].contents[0].literal);
|
||||
ASSERT_NOT_NULL(t.contents[0].contents);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[1].type);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[1].type);
|
||||
ASSERT_EQ('b', t.contents[0].contents[1].literal);
|
||||
ASSERT_NOT_NULL(t.contents[0].contents);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[2].type);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[2].type);
|
||||
ASSERT_EQ('c', t.contents[0].contents[2].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_star_is_unchanged(void)
|
||||
{
|
||||
parse_term_t *terms = malloc(1 * sizeof(parse_term_t));
|
||||
terms[0].quantifier = PARSE_QUANTIFIER_STAR;
|
||||
terms[0].type = PARSE_TERM_LITERAL;
|
||||
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
||||
terms[0].quantifier = REGEX_QUANTIFIER_STAR;
|
||||
terms[0].type = REGEX_TERM_LITERAL;
|
||||
terms[0].literal = 'a';
|
||||
parse_sequence_t *alternatives = malloc(1 * sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = terms;
|
||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
regex_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
|
||||
const bool success = desugar_regex(&t);
|
||||
ASSERT_TRUE(success);
|
||||
@@ -84,27 +84,27 @@ static void a_star_is_unchanged(void)
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_NOT_NULL(t.contents[0].contents);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_STAR, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_STAR, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ('a', t.contents[0].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_or_b_or_c_is_unchanged(void)
|
||||
{
|
||||
const char *literals = "abc";
|
||||
parse_sequence_t *alternatives = malloc(3 * sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(3 * sizeof(regex_sequence_t));
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
parse_term_t *terms = malloc(1 * sizeof(parse_term_t));
|
||||
terms[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||
terms[0].type = PARSE_TERM_LITERAL;
|
||||
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
||||
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms[0].type = REGEX_TERM_LITERAL;
|
||||
terms[0].literal = literals[i];
|
||||
|
||||
alternatives[i].count = alternatives[i].capacity = 1;
|
||||
alternatives[i].contents = terms;
|
||||
}
|
||||
parse_tree_t t = { .count = 3, .capacity = 3, .contents = alternatives };
|
||||
regex_t t = { .count = 3, .capacity = 3, .contents = alternatives };
|
||||
|
||||
const bool success = desugar_regex(&t);
|
||||
ASSERT_TRUE(success);
|
||||
@@ -115,33 +115,33 @@ static void a_or_b_or_c_is_unchanged(void)
|
||||
ASSERT_EQ(1, t.contents[i].count);
|
||||
ASSERT_NOT_NULL(t.contents[i].contents);
|
||||
ASSERT_EQ(
|
||||
PARSE_QUANTIFIER_NONE, t.contents[i].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[i].contents[0].type);
|
||||
REGEX_QUANTIFIER_NONE, t.contents[i].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[i].contents[0].type);
|
||||
ASSERT_EQ(literals[i], t.contents[i].contents[0].literal);
|
||||
}
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void subexpr_a_is_unchanged(void)
|
||||
{
|
||||
parse_term_t *inner_terms = malloc(1 * sizeof(parse_term_t));
|
||||
inner_terms[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||
inner_terms[0].type = PARSE_TERM_LITERAL;
|
||||
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';
|
||||
parse_sequence_t *inner_alternatives
|
||||
= malloc(1 * sizeof(parse_sequence_t));
|
||||
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;
|
||||
parse_term_t *terms = malloc(1 * sizeof(parse_term_t));
|
||||
terms[0].quantifier = PARSE_QUANTIFIER_NONE;
|
||||
terms[0].type = PARSE_TERM_SUBEXPR;
|
||||
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;
|
||||
parse_sequence_t *alternatives = malloc(1 * sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = terms;
|
||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
regex_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
|
||||
const bool success = desugar_regex(&t);
|
||||
ASSERT_TRUE(success);
|
||||
@@ -150,32 +150,32 @@ static void subexpr_a_is_unchanged(void)
|
||||
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);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_SUBEXPR, t.contents[0].contents[0].type);
|
||||
|
||||
const parse_tree_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
const regex_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
ASSERT_EQ(1, 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);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[0].contents[0].type);
|
||||
ASSERT_EQ('a', inner->contents[0].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_plus_becomes_subexpr_aa_star(void)
|
||||
{
|
||||
parse_term_t *terms = malloc(1 * sizeof(parse_term_t));
|
||||
terms[0].quantifier = PARSE_QUANTIFIER_PLUS;
|
||||
terms[0].type = PARSE_TERM_LITERAL;
|
||||
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
||||
terms[0].quantifier = REGEX_QUANTIFIER_PLUS;
|
||||
terms[0].type = REGEX_TERM_LITERAL;
|
||||
terms[0].literal = 'a';
|
||||
parse_sequence_t *alternatives = malloc(1 * sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = terms;
|
||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
regex_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
|
||||
const bool success = desugar_regex(&t);
|
||||
ASSERT_TRUE(success);
|
||||
@@ -184,36 +184,36 @@ static void a_plus_becomes_subexpr_aa_star(void)
|
||||
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);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_SUBEXPR, t.contents[0].contents[0].type);
|
||||
|
||||
const parse_tree_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
const regex_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
ASSERT_EQ(1, inner->count);
|
||||
ASSERT_NOT_NULL(inner->contents);
|
||||
ASSERT_EQ(2, 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);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[0].contents[0].type);
|
||||
ASSERT_EQ('a', inner->contents[0].contents[0].literal);
|
||||
ASSERT_EQ(
|
||||
PARSE_QUANTIFIER_STAR, inner->contents[0].contents[1].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, inner->contents[0].contents[1].type);
|
||||
REGEX_QUANTIFIER_STAR, inner->contents[0].contents[1].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[0].contents[1].type);
|
||||
ASSERT_EQ('a', inner->contents[0].contents[1].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_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;
|
||||
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
||||
terms[0].quantifier = REGEX_QUANTIFIER_QMARK;
|
||||
terms[0].type = REGEX_TERM_LITERAL;
|
||||
terms[0].literal = 'a';
|
||||
parse_sequence_t *alternatives = malloc(1 * sizeof(parse_sequence_t));
|
||||
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = terms;
|
||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
regex_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
|
||||
const bool success = desugar_regex(&t);
|
||||
ASSERT_TRUE(success);
|
||||
@@ -222,25 +222,25 @@ static void a_qmark_becomes_subexpr_empty_or_a(void)
|
||||
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);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_SUBEXPR, t.contents[0].contents[0].type);
|
||||
|
||||
const parse_tree_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
const regex_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);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_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);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[1].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[1].contents[0].type);
|
||||
ASSERT_EQ('a', inner->contents[1].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void class_abc_becomes_subexpr_a_or_b_or_c(void)
|
||||
@@ -249,16 +249,16 @@ static void class_abc_becomes_subexpr_a_or_b_or_c(void)
|
||||
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;
|
||||
regex_term_t *terms = malloc(1 * sizeof(regex_term_t));
|
||||
terms[0].quantifier = REGEX_QUANTIFIER_NONE;
|
||||
terms[0].type = REGEX_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));
|
||||
regex_sequence_t *alternatives = malloc(1 * sizeof(regex_sequence_t));
|
||||
alternatives[0].count = alternatives[0].capacity = 1;
|
||||
alternatives[0].contents = terms;
|
||||
parse_tree_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
regex_t t = { .count = 1, .capacity = 1, .contents = alternatives };
|
||||
|
||||
const bool success = desugar_regex(&t);
|
||||
ASSERT_TRUE(success);
|
||||
@@ -267,32 +267,32 @@ static void class_abc_becomes_subexpr_a_or_b_or_c(void)
|
||||
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);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_SUBEXPR, t.contents[0].contents[0].type);
|
||||
|
||||
const parse_tree_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
const regex_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);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_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);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[1].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_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);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[2].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[2].contents[0].type);
|
||||
ASSERT_EQ('c', inner->contents[2].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
|
||||
@@ -10,268 +10,268 @@
|
||||
|
||||
static void a_has_1_alternative(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("a", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_pipe_b_has_2_alternatives(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("a|b", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(2, t.count);
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_pipe_b_pipe_c_has_3_alternatives(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("a|b|c", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(3, t.count);
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_is_parsed_as_unquantified_literal(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("a", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ('a', t.contents[0].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void b_is_parsed_as_unquantified_literal(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("b", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ('b', t.contents[0].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void abc_is_parsed_as_sequence_of_unquantified_literals(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("abc", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(3, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ('a', t.contents[0].contents[0].literal);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[1].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[1].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[1].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[1].type);
|
||||
ASSERT_EQ('b', t.contents[0].contents[1].literal);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[2].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[2].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[2].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[2].type);
|
||||
ASSERT_EQ('c', t.contents[0].contents[2].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void dot_is_parsed_as_unquantified_wildcard_term(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING(".", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_WILDCARD, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void backslash_dot_is_parsed_as_unquantified_literal(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("\\.", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ('.', t.contents[0].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void backslash_backslash_is_parsed_as_unquantified_literal(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("\\\\", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ('\\', t.contents[0].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_pipe_b_in_parens_is_parsed_as_subexpr_term(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("(a|b)", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_SUBEXPR, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_SUBEXPR, t.contents[0].contents[0].type);
|
||||
|
||||
const parse_tree_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
const regex_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
ASSERT_EQ(2, inner->count);
|
||||
|
||||
ASSERT_EQ(1, inner->contents[0].count);
|
||||
ASSERT_EQ(
|
||||
PARSE_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, inner->contents[0].contents[0].type);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_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_EQ(
|
||||
PARSE_QUANTIFIER_NONE, inner->contents[1].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, inner->contents[1].contents[0].type);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[1].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[1].contents[0].type);
|
||||
ASSERT_EQ('b', inner->contents[1].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_in_parens_b_is_parsed_as_sequence_with_subexpr_term(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("(a)b", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(2, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_SUBEXPR, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[1].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, t.contents[0].contents[1].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_SUBEXPR, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[1].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, t.contents[0].contents[1].type);
|
||||
ASSERT_EQ('b', t.contents[0].contents[1].literal);
|
||||
|
||||
const parse_tree_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
const regex_t *inner = &t.contents[0].contents[0].subexpr;
|
||||
ASSERT_EQ(1, inner->contents[0].count);
|
||||
ASSERT_EQ(
|
||||
PARSE_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_LITERAL, inner->contents[0].contents[0].type);
|
||||
REGEX_QUANTIFIER_NONE, inner->contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_LITERAL, inner->contents[0].contents[0].type);
|
||||
ASSERT_EQ('a', inner->contents[0].contents[0].literal);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void dot_star_is_parsed_as_star_quantified_wildcard(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING(".*", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_STAR, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_WILDCARD, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_STAR, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void dot_plus_is_parsed_as_plus_quantified_wildcard(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING(".+", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_PLUS, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_WILDCARD, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_PLUS, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void dot_question_mark_is_parsed_as_qmrk_quantified_wildcard(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING(".?", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_QMARK, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_WILDCARD, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_QMARK, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_WILDCARD, t.contents[0].contents[0].type);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void a_in_brackets_is_parsed_as_class_containing_only_a(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("[a]", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_CLASS, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_CLASS, t.contents[0].contents[0].type);
|
||||
ASSERT_FALSE(t.contents[0].contents[0].class.negated);
|
||||
ASSERT_EQ(1, t.contents[0].contents[0].class.count);
|
||||
ASSERT_NOT_NULL(t.contents[0].contents[0].class.contents);
|
||||
ASSERT_EQ('a', t.contents[0].contents[0].class.contents[0]);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
static void caret_a_in_brackets_parses_as_negated_class(void)
|
||||
{
|
||||
parse_tree_t t;
|
||||
regex_t t;
|
||||
const int result = PARSE_EXPR_STRING("[^a]", &t);
|
||||
ASSERT_NE(-1, result);
|
||||
ASSERT_EQ(1, t.count);
|
||||
ASSERT_NOT_NULL(t.contents);
|
||||
|
||||
ASSERT_EQ(1, t.contents[0].count);
|
||||
ASSERT_EQ(PARSE_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(PARSE_TERM_CLASS, t.contents[0].contents[0].type);
|
||||
ASSERT_EQ(REGEX_QUANTIFIER_NONE, t.contents[0].contents[0].quantifier);
|
||||
ASSERT_EQ(REGEX_TERM_CLASS, t.contents[0].contents[0].type);
|
||||
ASSERT_TRUE(t.contents[0].contents[0].class.negated);
|
||||
ASSERT_EQ(1, t.contents[0].contents[0].class.count);
|
||||
ASSERT_NOT_NULL(t.contents[0].contents[0].class.contents);
|
||||
ASSERT_EQ('a', t.contents[0].contents[0].class.contents[0]);
|
||||
|
||||
parse_free_tree_children(&t);
|
||||
regex_free_children(&t);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
|
||||
Reference in New Issue
Block a user