Rename some things in parse tree data structures
This commit is contained in:
34
lib/parse.c
34
lib/parse.c
@@ -136,7 +136,7 @@ static int parse_term(const char *input, int rem, parse_term_t *out)
|
||||
++used;
|
||||
break;
|
||||
case '?':
|
||||
out->quantifier = PARSE_QUANTIFIER_QMRK;
|
||||
out->quantifier = PARSE_QUANTIFIER_QMARK;
|
||||
++used;
|
||||
break;
|
||||
default:
|
||||
@@ -153,14 +153,14 @@ static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
|
||||
{
|
||||
int result, used = 0;
|
||||
|
||||
out->len = 0;
|
||||
out->count = 0;
|
||||
out->capacity = SEQUENCE_START_CAPACITY;
|
||||
out->contents = malloc(out->capacity * sizeof(parse_term_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
|
||||
while (used < rem) {
|
||||
if (out->len >= out->capacity) {
|
||||
if (out->count >= out->capacity) {
|
||||
out->capacity *= 2;
|
||||
out->contents = realloc(
|
||||
out->contents, out->capacity * sizeof(parse_term_t));
|
||||
@@ -169,14 +169,14 @@ static int parse_sequence(const char *input, int rem, parse_sequence_t *out)
|
||||
}
|
||||
|
||||
result
|
||||
= parse_term(input + used, rem - used, &out->contents[out->len]);
|
||||
= parse_term(input + used, rem - used, &out->contents[out->count]);
|
||||
if (result < 0)
|
||||
break;
|
||||
++out->len;
|
||||
++out->count;
|
||||
used += result;
|
||||
}
|
||||
|
||||
return out->len > 0 ? used : -1;
|
||||
return out->count > 0 ? used : -1;
|
||||
}
|
||||
|
||||
int parse_expr(const char *input, int rem, parse_tree_t *out)
|
||||
@@ -185,11 +185,11 @@ int parse_expr(const char *input, int rem, parse_tree_t *out)
|
||||
|
||||
out->count = 0;
|
||||
out->capacity = TREE_START_CAPACITY;
|
||||
out->alternatives = malloc(out->capacity * sizeof(parse_sequence_t));
|
||||
if (NULL == out->alternatives)
|
||||
out->contents = malloc(out->capacity * sizeof(parse_sequence_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
|
||||
result = parse_sequence(input + used, rem - used, &out->alternatives[0]);
|
||||
result = parse_sequence(input + used, rem - used, &out->contents[0]);
|
||||
if (result < 0)
|
||||
return -1;
|
||||
++out->count;
|
||||
@@ -202,14 +202,14 @@ 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(parse_sequence_t));
|
||||
if (NULL == out->alternatives)
|
||||
out->contents = realloc(
|
||||
out->contents, out->capacity * sizeof(parse_sequence_t));
|
||||
if (NULL == out->contents)
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = parse_sequence(
|
||||
input + used, rem - used, &out->alternatives[out->count]);
|
||||
input + used, rem - used, &out->contents[out->count]);
|
||||
if (result < 0)
|
||||
break;
|
||||
++out->count;
|
||||
@@ -221,17 +221,17 @@ int parse_expr(const char *input, int rem, parse_tree_t *out)
|
||||
|
||||
void parse_free_tree_children(const parse_tree_t *t)
|
||||
{
|
||||
if (NULL != t->alternatives) {
|
||||
if (NULL != t->contents) {
|
||||
for (int i = 0; i < t->count; ++i)
|
||||
parse_free_sequence_children(&t->alternatives[i]);
|
||||
free(t->alternatives);
|
||||
parse_free_sequence_children(&t->contents[i]);
|
||||
free(t->contents);
|
||||
}
|
||||
}
|
||||
|
||||
void parse_free_sequence_children(const parse_sequence_t *s)
|
||||
{
|
||||
if (NULL != s->contents) {
|
||||
for (int i = 0; i < s->len; ++i) {
|
||||
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);
|
||||
|
||||
@@ -18,7 +18,7 @@ typedef enum {
|
||||
PARSE_QUANTIFIER_NONE,
|
||||
PARSE_QUANTIFIER_STAR,
|
||||
PARSE_QUANTIFIER_PLUS,
|
||||
PARSE_QUANTIFIER_QMRK,
|
||||
PARSE_QUANTIFIER_QMARK,
|
||||
} parse_quantifier_t;
|
||||
|
||||
typedef enum {
|
||||
@@ -31,13 +31,13 @@ typedef enum {
|
||||
|
||||
struct _parse_term;
|
||||
typedef struct {
|
||||
int len, capacity;
|
||||
int count, capacity;
|
||||
struct _parse_term *contents;
|
||||
} parse_sequence_t;
|
||||
|
||||
typedef struct {
|
||||
int count, capacity;
|
||||
parse_sequence_t *alternatives;
|
||||
parse_sequence_t *contents;
|
||||
} parse_tree_t;
|
||||
|
||||
typedef struct _parse_term {
|
||||
|
||||
Reference in New Issue
Block a user