diff options
-rw-r--r-- | parse-pattern.y | 8 | ||||
-rw-r--r-- | regex.c | 22 |
2 files changed, 10 insertions, 20 deletions
diff --git a/parse-pattern.y b/parse-pattern.y index e5acd3c..2c0556a 100644 --- a/parse-pattern.y +++ b/parse-pattern.y @@ -62,7 +62,13 @@ input: ; molecule: - repeat sequence { $$ = mkatom(&(struct atom) { .type = ATOM_REPETITION, .u = { .repeat = { .counts = $1, .child = $2 } } }); } + repeat sequence { + if ($1.min == 1 && $1.max == 1) { + $$ = $2; + } else { + $$ = mkatom(&(struct atom) { .type = ATOM_REPETITION, .u = { .repeat = { .counts = $1, .child = $2 } } }); + } + } ; repeat: @@ -61,21 +61,12 @@ static void print_literal(const char *s) static int get_precedence(const struct atom *a) { - const struct repeat *r; - - switch (a->type) { - case ATOM_LITERAL: + if (a->type == ATOM_LITERAL) { switch (strlen(a->u.literal)) { case 0: return -1; case 1: return precedence[ATOM_EVERYTHING]; default: return precedence[ATOM_SEQUENCE]; } - - case ATOM_REPETITION: - r = &a->u.repeat.counts; - if (r->min == 1 && r->max == 1) - return get_precedence(a->u.repeat.child); - break; } return precedence[a->type]; @@ -93,8 +84,6 @@ static void print_child_atom(const struct atom *child, const struct atom *parent static void print_atom(const struct atom *a) { - const struct repeat *r; - switch (a->type) { case ATOM_ALTERNATION: print_child_atom(a->u.children[0], a); @@ -108,13 +97,8 @@ static void print_atom(const struct atom *a) break; case ATOM_REPETITION: - r = &a->u.repeat.counts; - if (r->min == 1 && r->max == 1) { - print_atom(a->u.repeat.child); - } else { - print_child_atom(a->u.repeat.child, a); - print_repeat(r); - } + print_child_atom(a->u.repeat.child, a); + print_repeat(&a->u.repeat.counts); break; case ATOM_LITERAL: |