Define PARSE_FAIL and use instead of -1 in parse.c

This commit is contained in:
2024-10-27 02:16:00 +00:00
parent 8743a45b2a
commit 99b8d3bcd0
2 changed files with 18 additions and 16 deletions

View File

@@ -8,6 +8,8 @@
#include "regex.h"
#define PARSE_FAIL (-1)
int parse_expr(const char *input, int rem, regex_t *out);
#endif

View File

@@ -41,7 +41,7 @@ static int parse_literal(const char *input, int rem, char *out)
*out = input[1];
return 2;
} else {
return -1;
return PARSE_FAIL;
}
}
@@ -50,7 +50,7 @@ static int parse_class(const char *input, int rem, regex_class_t *out)
int result, used = 0;
if (used >= rem || '[' != input[used])
return -1;
return PARSE_FAIL;
++used;
if (used < rem && '^' == input[used]) {
@@ -74,14 +74,14 @@ static int parse_class(const char *input, int rem, regex_class_t *out)
result = parse_literal(
input + used, rem - used, &out->contents[out->count]);
if (result < 0)
if (PARSE_FAIL == result)
break;
used += result;
++out->count;
}
if (used >= rem || ']' != input[used])
return -1;
return PARSE_FAIL;
++used;
return out->count > 0 ? used : -1;
@@ -92,7 +92,7 @@ static int parse_term(const char *input, int rem, regex_term_t *out)
int result, used = 0;
if (1 > rem)
return -1;
return PARSE_FAIL;
if ('.' == input[0]) {
out->type = REGEX_TERM_WILDCARD;
@@ -101,24 +101,24 @@ static int parse_term(const char *input, int rem, regex_term_t *out)
++used;
result = parse_expr(input + used, rem - used, &out->subexpr);
if (result < 0)
return -1;
if (PARSE_FAIL == result)
return PARSE_FAIL;
out->type = REGEX_TERM_SUBEXPR;
used += result;
if (')' != input[used])
return -1;
return PARSE_FAIL;
++used;
} else if ('[' == input[0]) {
result = parse_class(input + used, rem - used, &out->class);
if (result < 0)
return -1;
if (PARSE_FAIL == result)
return PARSE_FAIL;
out->type = REGEX_TERM_CLASS;
used += result;
} else {
result = parse_literal(input + used, rem - used, &out->literal);
if (result < 0)
return -1;
if (PARSE_FAIL == result)
return PARSE_FAIL;
out->type = REGEX_TERM_LITERAL;
used += result;
}
@@ -166,7 +166,7 @@ static int parse_sequence(const char *input, int rem, regex_sequence_t *out)
result = parse_term(
input + used, rem - used, &out->contents[out->count]);
if (result < 0)
if (PARSE_FAIL == result)
break;
++out->count;
used += result;
@@ -185,8 +185,8 @@ int parse_expr(const char *input, int rem, regex_t *out)
assert(NULL != out->contents);
result = parse_sequence(input + used, rem - used, &out->contents[0]);
if (result < 0)
return -1;
if (PARSE_FAIL == result)
return PARSE_FAIL;
++out->count;
used += result;
@@ -204,7 +204,7 @@ int parse_expr(const char *input, int rem, regex_t *out)
result = parse_sequence(
input + used, rem - used, &out->contents[out->count]);
if (result < 0)
if (PARSE_FAIL == result)
break;
++out->count;
used += result;