Add parse_ prefix to parser type names
This commit is contained in:
50
lib/parser.c
50
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;
|
||||
}
|
||||
}
|
||||
|
||||
42
lib/parser.h
42
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);
|
||||
|
||||
Reference in New Issue
Block a user