Initialize everything explicitly in parser
This commit is contained in:
parent
b2f474336a
commit
01fb9be1e7
10
lib/parser.c
10
lib/parser.c
@ -55,6 +55,8 @@ static int parse_class(const char *input, int rem, class_t *out)
|
|||||||
if (used < rem && '^' == input[used]) {
|
if (used < rem && '^' == input[used]) {
|
||||||
out->negated = true;
|
out->negated = true;
|
||||||
++used;
|
++used;
|
||||||
|
} else {
|
||||||
|
out->negated = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
out->count = 0;
|
out->count = 0;
|
||||||
@ -139,6 +141,8 @@ static int parse_term(const char *input, int rem, term_t *out)
|
|||||||
default:
|
default:
|
||||||
out->quantifier = QUANTIFIER_NONE;
|
out->quantifier = QUANTIFIER_NONE;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
out->quantifier = QUANTIFIER_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return used;
|
return used;
|
||||||
@ -150,7 +154,7 @@ static int parse_sequence(const char *input, int rem, sequence_t *out)
|
|||||||
|
|
||||||
out->len = 0;
|
out->len = 0;
|
||||||
out->capacity = SEQUENCE_START_CAPACITY;
|
out->capacity = SEQUENCE_START_CAPACITY;
|
||||||
out->contents = calloc(out->capacity, sizeof(term_t));
|
out->contents = malloc(out->capacity * sizeof(term_t));
|
||||||
if (NULL == out->contents)
|
if (NULL == out->contents)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -186,13 +190,15 @@ int parse_regex(const char *input, int rem, regex_t *out)
|
|||||||
if (used < rem && '|' == input[used]) {
|
if (used < rem && '|' == input[used]) {
|
||||||
++used;
|
++used;
|
||||||
|
|
||||||
out->alternative = calloc(1, sizeof(regex_t));
|
out->alternative = malloc(sizeof(regex_t));
|
||||||
if (NULL == out->alternative)
|
if (NULL == out->alternative)
|
||||||
return -1;
|
return -1;
|
||||||
result = parse_regex(input + used, rem - used, out->alternative);
|
result = parse_regex(input + used, rem - used, out->alternative);
|
||||||
if (result < 0)
|
if (result < 0)
|
||||||
return -1;
|
return -1;
|
||||||
used += result;
|
used += result;
|
||||||
|
} else {
|
||||||
|
out->alternative = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return used;
|
return used;
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
static void a_has_no_alternative(void)
|
static void a_has_no_alternative(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("a", &r);
|
const int result = PARSE_REGEX_STRING("a", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
ASSERT_NULL(r.alternative);
|
ASSERT_NULL(r.alternative);
|
||||||
@ -19,7 +19,7 @@ static void a_has_no_alternative(void)
|
|||||||
|
|
||||||
static void a_pipe_b_has_alternative(void)
|
static void a_pipe_b_has_alternative(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("a|b", &r);
|
const int result = PARSE_REGEX_STRING("a|b", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
ASSERT_NOT_NULL(r.alternative);
|
ASSERT_NOT_NULL(r.alternative);
|
||||||
@ -28,7 +28,7 @@ static void a_pipe_b_has_alternative(void)
|
|||||||
|
|
||||||
static void a_pipe_b_pipe_c_result_alternative_has_alternative(void)
|
static void a_pipe_b_pipe_c_result_alternative_has_alternative(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("a|b|c", &r);
|
const int result = PARSE_REGEX_STRING("a|b|c", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
ASSERT_NOT_NULL(r.alternative);
|
ASSERT_NOT_NULL(r.alternative);
|
||||||
@ -38,7 +38,7 @@ static void a_pipe_b_pipe_c_result_alternative_has_alternative(void)
|
|||||||
|
|
||||||
static void a_is_parsed_as_unquantified_literal(void)
|
static void a_is_parsed_as_unquantified_literal(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("a", &r);
|
const int result = PARSE_REGEX_STRING("a", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ static void a_is_parsed_as_unquantified_literal(void)
|
|||||||
|
|
||||||
static void b_is_parsed_as_unquantified_literal(void)
|
static void b_is_parsed_as_unquantified_literal(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("b", &r);
|
const int result = PARSE_REGEX_STRING("b", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ static void b_is_parsed_as_unquantified_literal(void)
|
|||||||
|
|
||||||
static void abc_is_parsed_as_sequence_of_unquantified_literals(void)
|
static void abc_is_parsed_as_sequence_of_unquantified_literals(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("abc", &r);
|
const int result = PARSE_REGEX_STRING("abc", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ static void abc_is_parsed_as_sequence_of_unquantified_literals(void)
|
|||||||
|
|
||||||
static void dot_is_parsed_as_unquantified_wildcard_term(void)
|
static void dot_is_parsed_as_unquantified_wildcard_term(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING(".", &r);
|
const int result = PARSE_REGEX_STRING(".", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ static void dot_is_parsed_as_unquantified_wildcard_term(void)
|
|||||||
|
|
||||||
static void backslash_dot_is_parsed_as_unquantified_literal(void)
|
static void backslash_dot_is_parsed_as_unquantified_literal(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("\\.", &r);
|
const int result = PARSE_REGEX_STRING("\\.", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -113,7 +113,7 @@ static void backslash_dot_is_parsed_as_unquantified_literal(void)
|
|||||||
|
|
||||||
static void backslash_backslash_is_parsed_as_unquantified_literal(void)
|
static void backslash_backslash_is_parsed_as_unquantified_literal(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("\\\\", &r);
|
const int result = PARSE_REGEX_STRING("\\\\", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -127,7 +127,7 @@ static void backslash_backslash_is_parsed_as_unquantified_literal(void)
|
|||||||
|
|
||||||
static void a_pipe_b_in_parens_is_parsed_as_regex_term(void)
|
static void a_pipe_b_in_parens_is_parsed_as_regex_term(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("(a|b)", &r);
|
const int result = PARSE_REGEX_STRING("(a|b)", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -152,7 +152,7 @@ static void a_pipe_b_in_parens_is_parsed_as_regex_term(void)
|
|||||||
|
|
||||||
static void a_in_parens_b_is_parsed_as_sequence_with_regex_term(void)
|
static void a_in_parens_b_is_parsed_as_sequence_with_regex_term(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("(a)b", &r);
|
const int result = PARSE_REGEX_STRING("(a)b", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -174,7 +174,7 @@ static void a_in_parens_b_is_parsed_as_sequence_with_regex_term(void)
|
|||||||
|
|
||||||
static void dot_star_is_parsed_as_zero_or_more_wildcard(void)
|
static void dot_star_is_parsed_as_zero_or_more_wildcard(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING(".*", &r);
|
const int result = PARSE_REGEX_STRING(".*", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ static void dot_star_is_parsed_as_zero_or_more_wildcard(void)
|
|||||||
|
|
||||||
static void dot_plus_is_parsed_as_one_or_more_wildcard(void)
|
static void dot_plus_is_parsed_as_one_or_more_wildcard(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING(".+", &r);
|
const int result = PARSE_REGEX_STRING(".+", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -200,7 +200,7 @@ static void dot_plus_is_parsed_as_one_or_more_wildcard(void)
|
|||||||
|
|
||||||
static void dot_question_mark_is_parsed_as_zero_or_one_wildcard(void)
|
static void dot_question_mark_is_parsed_as_zero_or_one_wildcard(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING(".?", &r);
|
const int result = PARSE_REGEX_STRING(".?", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -213,7 +213,7 @@ static void dot_question_mark_is_parsed_as_zero_or_one_wildcard(void)
|
|||||||
|
|
||||||
static void a_in_brackets_is_parsed_as_class_containing_only_a(void)
|
static void a_in_brackets_is_parsed_as_class_containing_only_a(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("[a]", &r);
|
const int result = PARSE_REGEX_STRING("[a]", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
@ -230,7 +230,7 @@ static void a_in_brackets_is_parsed_as_class_containing_only_a(void)
|
|||||||
|
|
||||||
static void caret_a_in_brackets_parses_as_negated_class(void)
|
static void caret_a_in_brackets_parses_as_negated_class(void)
|
||||||
{
|
{
|
||||||
regex_t r = { 0 };
|
regex_t r;
|
||||||
const int result = PARSE_REGEX_STRING("[^a]", &r);
|
const int result = PARSE_REGEX_STRING("[^a]", &r);
|
||||||
ASSERT_NE(-1, result);
|
ASSERT_NE(-1, result);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user