Handle sequences in parser

This commit is contained in:
2024-10-25 14:10:07 +01:00
parent 584e92c29c
commit f434af5c96
2 changed files with 47 additions and 9 deletions

View File

@@ -8,6 +8,8 @@
#include <stdbool.h>
#include <stdlib.h>
#define SEQUENCE_START_CAPACITY 8
static bool is_special(char c)
{
switch (c) {
@@ -20,7 +22,7 @@ static bool is_special(char c)
static int parse_literal(const char *input, int rem, char *out)
{
if (rem > 0 || !is_special(input[0])) {
if (rem > 0 && !is_special(input[0])) {
*out = input[0];
return 1;
} else {
@@ -46,17 +48,30 @@ static int parse_sequence(const char *input, int rem, sequence_t *out)
{
int result, used = 0;
out->contents = calloc(1, sizeof(term_t));
out->len = 0;
out->capacity = 1;
result = parse_term(input + used, rem - used, &out->contents[0]);
if (result < 0)
out->capacity = SEQUENCE_START_CAPACITY;
out->contents = calloc(out->capacity, sizeof(term_t));
if (NULL == out->contents)
return -1;
++out->len;
used += result;
return used;
while (used < rem) {
if (out->len >= out->capacity) {
out->capacity *= 2;
out->contents
= realloc(out->contents, out->capacity * sizeof(term_t));
if (NULL == out->contents)
return -1;
}
result
= parse_term(input + used, rem - used, &out->contents[out->len]);
if (result < 0)
break;
++out->len;
used += result;
}
return out->len > 0 ? used : -1;
}
int parse_regex(const char *input, int rem, regex_t *out)
@@ -74,6 +89,8 @@ int parse_regex(const char *input, int rem, regex_t *out)
++used;
out->alternative = calloc(1, sizeof(regex_t));
if (NULL == out->alternative)
return -1;
result = parse_regex(input + used, rem - used, out->alternative);
if (result < 0)
return -1;