diff --git a/lib/parser.c b/lib/parser.c index 2436b99..206c0c9 100644 --- a/lib/parser.c +++ b/lib/parser.c @@ -10,7 +10,7 @@ #define CLASS_START_CAPACITY 4 #define SEQUENCE_START_CAPACITY 8 -#define PARSE_TREE_START_CAPACITY 4 +#define TREE_START_CAPACITY 4 static bool is_special(char c) { @@ -45,7 +45,7 @@ static int parse_literal(const char *input, int rem, char *out) } } -static int parse_class(const char *input, int rem, class_t *out) +static int parse_class(const char *input, int rem, parse_class_t *out) { int result, used = 0; @@ -89,7 +89,7 @@ static int parse_class(const char *input, int rem, class_t *out) return out->count > 0 ? used : -1; } -static int parse_term(const char *input, int rem, term_t *out) +static int parse_term(const char *input, int rem, parse_term_t *out) { int result, used = 0; @@ -97,7 +97,7 @@ static int parse_term(const char *input, int rem, term_t *out) return -1; if ('.' == input[0]) { - out->type = TERM_TYPE_WILDCARD; + out->type = PARSE_TERM_WILDCARD; ++used; } else if ('(' == input[0]) { ++used; @@ -105,7 +105,7 @@ static int parse_term(const char *input, int rem, term_t *out) result = parse_expr(input + used, rem - used, &out->subexpr); if (result < 0) return -1; - out->type = TERM_TYPE_SUBEXPR; + out->type = PARSE_TERM_SUBEXPR; used += result; if (')' != input[used]) @@ -115,55 +115,55 @@ static int parse_term(const char *input, int rem, term_t *out) result = parse_class(input + used, rem - used, &out->class); if (result < 0) return -1; - out->type = TERM_TYPE_CLASS; + out->type = PARSE_TERM_CLASS; used += result; } else { result = parse_literal(input + used, rem - used, &out->literal); if (result < 0) return -1; - out->type = TERM_TYPE_LITERAL; + out->type = PARSE_TERM_LITERAL; used += result; } if (used < rem) { switch (input[used]) { case '*': - out->quantifier = QUANTIFIER_ZERO_OR_MORE; + out->quantifier = PARSE_QUANTIFIER_STAR; ++used; break; case '+': - out->quantifier = QUANTIFIER_ONE_OR_MORE; + out->quantifier = PARSE_QUANTIFIER_PLUS; ++used; break; case '?': - out->quantifier = QUANTIFIER_ZERO_OR_ONE; + out->quantifier = PARSE_QUANTIFIER_QMRK; ++used; break; default: - out->quantifier = QUANTIFIER_NONE; + out->quantifier = PARSE_QUANTIFIER_NONE; } } else { - out->quantifier = QUANTIFIER_NONE; + out->quantifier = PARSE_QUANTIFIER_NONE; } return used; } -static int parse_sequence(const char *input, int rem, sequence_t *out) +static int parse_sequence(const char *input, int rem, parse_sequence_t *out) { int result, used = 0; out->len = 0; out->capacity = SEQUENCE_START_CAPACITY; - out->contents = malloc(out->capacity * sizeof(term_t)); + out->contents = malloc(out->capacity * sizeof(parse_term_t)); if (NULL == out->contents) return -1; while (used < rem) { if (out->len >= out->capacity) { out->capacity *= 2; - out->contents - = realloc(out->contents, out->capacity * sizeof(term_t)); + out->contents = realloc( + out->contents, out->capacity * sizeof(parse_term_t)); if (NULL == out->contents) return -1; } @@ -184,8 +184,8 @@ int parse_expr(const char *input, int rem, parse_tree_t *out) int result, used = 0; out->count = 0; - out->capacity = PARSE_TREE_START_CAPACITY; - out->alternatives = malloc(out->capacity * sizeof(sequence_t)); + out->capacity = TREE_START_CAPACITY; + out->alternatives = malloc(out->capacity * sizeof(parse_sequence_t)); if (NULL == out->alternatives) return -1; @@ -203,7 +203,7 @@ int parse_expr(const char *input, int rem, parse_tree_t *out) if (out->count >= out->capacity) { out->capacity *= 2; out->alternatives = realloc( - out->alternatives, out->capacity * sizeof(sequence_t)); + out->alternatives, out->capacity * sizeof(parse_sequence_t)); if (NULL == out->alternatives) return -1; } @@ -219,25 +219,25 @@ int parse_expr(const char *input, int rem, parse_tree_t *out) return used; } -static void class_free(class_t *c) +static void class_free(parse_class_t *c) { if (NULL != c->contents) free(c->contents); } -static void sequence_free(sequence_t *s) +static void sequence_free(parse_sequence_t *s) { if (NULL != s->contents) { for (int i = 0; i < s->len; ++i) { switch (s->contents[i].type) { - case TERM_TYPE_CLASS: + case PARSE_TERM_CLASS: class_free(&s->contents[i].class); break; - case TERM_TYPE_SUBEXPR: + case PARSE_TERM_SUBEXPR: parse_tree_free_children(&s->contents[i].subexpr); break; - case TERM_TYPE_WILDCARD: - case TERM_TYPE_LITERAL: + case PARSE_TERM_WILDCARD: + case PARSE_TERM_LITERAL: break; } } diff --git a/lib/parser.h b/lib/parser.h index 345c2bb..f88d5ec 100644 --- a/lib/parser.h +++ b/lib/parser.h @@ -12,42 +12,42 @@ typedef struct { bool negated; int count, capacity; char *contents; -} class_t; +} parse_class_t; typedef enum { - QUANTIFIER_NONE, - QUANTIFIER_ZERO_OR_MORE, - QUANTIFIER_ONE_OR_MORE, - QUANTIFIER_ZERO_OR_ONE, -} quantifier_t; + PARSE_QUANTIFIER_NONE, + PARSE_QUANTIFIER_STAR, + PARSE_QUANTIFIER_PLUS, + PARSE_QUANTIFIER_QMRK, +} parse_quantifier_t; typedef enum { - TERM_TYPE_WILDCARD, - TERM_TYPE_CLASS, - TERM_TYPE_LITERAL, - TERM_TYPE_SUBEXPR, -} term_type_t; + PARSE_TERM_WILDCARD, + PARSE_TERM_CLASS, + PARSE_TERM_LITERAL, + PARSE_TERM_SUBEXPR, +} parse_term_type_t; -struct _term; +struct _parse_term; typedef struct { int len, capacity; - struct _term *contents; -} sequence_t; + struct _parse_term *contents; +} parse_sequence_t; -typedef struct _parse_tree { +typedef struct { int count, capacity; - sequence_t *alternatives; + parse_sequence_t *alternatives; } parse_tree_t; -typedef struct _term { - quantifier_t quantifier; - term_type_t type; +typedef struct _parse_term { + parse_quantifier_t quantifier; + parse_term_type_t type; union { - class_t class; + parse_class_t class; char literal; parse_tree_t subexpr; }; -} term_t; +} parse_term_t; int parse_expr(const char *input, int rem, parse_tree_t *out); void parse_tree_free_children(parse_tree_t *t); diff --git a/tests/parser_tests.c b/tests/parser_tests.c index 1f5a99c..c120767 100644 --- a/tests/parser_tests.c +++ b/tests/parser_tests.c @@ -44,8 +44,9 @@ static void a_is_parsed_as_unquantified_literal(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(1, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, t.alternatives[0].contents[0].type); ASSERT_EQ('a', t.alternatives[0].contents[0].literal); parse_tree_free_children(&t); @@ -60,8 +61,9 @@ static void b_is_parsed_as_unquantified_literal(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(1, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, t.alternatives[0].contents[0].type); ASSERT_EQ('b', t.alternatives[0].contents[0].literal); parse_tree_free_children(&t); @@ -76,14 +78,17 @@ static void abc_is_parsed_as_sequence_of_unquantified_literals(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(3, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, t.alternatives[0].contents[0].type); ASSERT_EQ('a', t.alternatives[0].contents[0].literal); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[1].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, t.alternatives[0].contents[1].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[1].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, t.alternatives[0].contents[1].type); ASSERT_EQ('b', t.alternatives[0].contents[1].literal); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[2].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, t.alternatives[0].contents[2].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[2].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, t.alternatives[0].contents[2].type); ASSERT_EQ('c', t.alternatives[0].contents[2].literal); parse_tree_free_children(&t); @@ -98,8 +103,9 @@ static void dot_is_parsed_as_unquantified_wildcard_term(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(1, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_WILDCARD, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_WILDCARD, t.alternatives[0].contents[0].type); parse_tree_free_children(&t); } @@ -113,8 +119,9 @@ static void backslash_dot_is_parsed_as_unquantified_literal(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(1, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, t.alternatives[0].contents[0].type); ASSERT_EQ('.', t.alternatives[0].contents[0].literal); parse_tree_free_children(&t); @@ -129,8 +136,9 @@ static void backslash_backslash_is_parsed_as_unquantified_literal(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(1, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, t.alternatives[0].contents[0].type); ASSERT_EQ('\\', t.alternatives[0].contents[0].literal); parse_tree_free_children(&t); @@ -145,22 +153,25 @@ static void a_pipe_b_in_parens_is_parsed_as_subexpr_term(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(1, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_SUBEXPR, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_SUBEXPR, t.alternatives[0].contents[0].type); const parse_tree_t *inner = &t.alternatives[0].contents[0].subexpr; ASSERT_EQ(2, inner->count); ASSERT_EQ(1, inner->alternatives[0].len); ASSERT_EQ( - QUANTIFIER_NONE, inner->alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, inner->alternatives[0].contents[0].type); + PARSE_QUANTIFIER_NONE, + inner->alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, inner->alternatives[0].contents[0].type); ASSERT_EQ('a', inner->alternatives[0].contents[0].literal); ASSERT_EQ(1, inner->alternatives[1].len); ASSERT_EQ( - QUANTIFIER_NONE, inner->alternatives[1].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, inner->alternatives[1].contents[0].type); + PARSE_QUANTIFIER_NONE, + inner->alternatives[1].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, inner->alternatives[1].contents[0].type); ASSERT_EQ('b', inner->alternatives[1].contents[0].literal); parse_tree_free_children(&t); @@ -175,23 +186,26 @@ static void a_in_parens_b_is_parsed_as_sequence_with_subexpr_term(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(2, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_SUBEXPR, t.alternatives[0].contents[0].type); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[1].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, t.alternatives[0].contents[1].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_SUBEXPR, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[1].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, t.alternatives[0].contents[1].type); ASSERT_EQ('b', t.alternatives[0].contents[1].literal); const parse_tree_t *inner = &t.alternatives[0].contents[0].subexpr; ASSERT_EQ(1, inner->alternatives[0].len); ASSERT_EQ( - QUANTIFIER_NONE, inner->alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_LITERAL, inner->alternatives[0].contents[0].type); + PARSE_QUANTIFIER_NONE, + inner->alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_LITERAL, inner->alternatives[0].contents[0].type); ASSERT_EQ('a', inner->alternatives[0].contents[0].literal); parse_tree_free_children(&t); } -static void dot_star_is_parsed_as_zero_or_more_wildcard(void) +static void dot_star_is_parsed_as_star_quantified_wildcard(void) { parse_tree_t t; const int result = PARSE_EXPR_STRING(".*", &t); @@ -201,13 +215,13 @@ static void dot_star_is_parsed_as_zero_or_more_wildcard(void) ASSERT_EQ(1, t.alternatives[0].len); ASSERT_EQ( - QUANTIFIER_ZERO_OR_MORE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_WILDCARD, t.alternatives[0].contents[0].type); + PARSE_QUANTIFIER_STAR, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_WILDCARD, t.alternatives[0].contents[0].type); parse_tree_free_children(&t); } -static void dot_plus_is_parsed_as_one_or_more_wildcard(void) +static void dot_plus_is_parsed_as_plus_quantified_wildcard(void) { parse_tree_t t; const int result = PARSE_EXPR_STRING(".+", &t); @@ -217,13 +231,13 @@ static void dot_plus_is_parsed_as_one_or_more_wildcard(void) ASSERT_EQ(1, t.alternatives[0].len); ASSERT_EQ( - QUANTIFIER_ONE_OR_MORE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_WILDCARD, t.alternatives[0].contents[0].type); + PARSE_QUANTIFIER_PLUS, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_WILDCARD, t.alternatives[0].contents[0].type); parse_tree_free_children(&t); } -static void dot_question_mark_is_parsed_as_zero_or_one_wildcard(void) +static void dot_question_mark_is_parsed_as_qmrk_quantified_wildcard(void) { parse_tree_t t; const int result = PARSE_EXPR_STRING(".?", &t); @@ -233,8 +247,8 @@ static void dot_question_mark_is_parsed_as_zero_or_one_wildcard(void) ASSERT_EQ(1, t.alternatives[0].len); ASSERT_EQ( - QUANTIFIER_ZERO_OR_ONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_WILDCARD, t.alternatives[0].contents[0].type); + PARSE_QUANTIFIER_QMRK, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_WILDCARD, t.alternatives[0].contents[0].type); parse_tree_free_children(&t); } @@ -248,8 +262,9 @@ static void a_in_brackets_is_parsed_as_class_containing_only_a(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(1, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_CLASS, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_CLASS, t.alternatives[0].contents[0].type); ASSERT_FALSE(t.alternatives[0].contents[0].class.negated); ASSERT_EQ(1, t.alternatives[0].contents[0].class.count); ASSERT_NOT_NULL(t.alternatives[0].contents[0].class.contents); @@ -267,8 +282,9 @@ static void caret_a_in_brackets_parses_as_negated_class(void) ASSERT_NOT_NULL(t.alternatives); ASSERT_EQ(1, t.alternatives[0].len); - ASSERT_EQ(QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); - ASSERT_EQ(TERM_TYPE_CLASS, t.alternatives[0].contents[0].type); + ASSERT_EQ( + PARSE_QUANTIFIER_NONE, t.alternatives[0].contents[0].quantifier); + ASSERT_EQ(PARSE_TERM_CLASS, t.alternatives[0].contents[0].type); ASSERT_TRUE(t.alternatives[0].contents[0].class.negated); ASSERT_EQ(1, t.alternatives[0].contents[0].class.count); ASSERT_NOT_NULL(t.alternatives[0].contents[0].class.contents); @@ -291,9 +307,9 @@ int main(void) backslash_backslash_is_parsed_as_unquantified_literal(); a_pipe_b_in_parens_is_parsed_as_subexpr_term(); a_in_parens_b_is_parsed_as_sequence_with_subexpr_term(); - dot_star_is_parsed_as_zero_or_more_wildcard(); - dot_plus_is_parsed_as_one_or_more_wildcard(); - dot_question_mark_is_parsed_as_zero_or_one_wildcard(); + dot_star_is_parsed_as_star_quantified_wildcard(); + dot_plus_is_parsed_as_plus_quantified_wildcard(); + dot_question_mark_is_parsed_as_qmrk_quantified_wildcard(); a_in_brackets_is_parsed_as_class_containing_only_a(); caret_a_in_brackets_parses_as_negated_class(); return TESTING_END();