summaryrefslogtreecommitdiffstats
path: root/regex.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-10-10 09:45:35 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-10-10 09:45:35 -0700
commit0c09759df87cdbbd33296a086debe58e58587f7e (patch)
tree1fcf56007ee3207058291a642ae2fff039804818 /regex.c
parent2d173febd859a05708e14273397df61f75bea895 (diff)
downloadtxr-0c09759df87cdbbd33296a086debe58e58587f7e.tar.gz
txr-0c09759df87cdbbd33296a086debe58e58587f7e.tar.bz2
txr-0c09759df87cdbbd33296a086debe58e58587f7e.zip
Simplify some regex tree walking code.
* regex.c (reg_expand_nongreedy, reg_compile_csets): Generalize the compound_s case slightly by referring to sym rather than hard-coded compound_s. Then handle most of the regex operators under this same case. Their semantics are not relevant to the expansions being performed in these functions: all their arguments are regexes to be recursed over.
Diffstat (limited to 'regex.c')
-rw-r--r--regex.c28
1 files changed, 10 insertions, 18 deletions
diff --git a/regex.c b/regex.c
index e72fc110..a3338d22 100644
--- a/regex.c
+++ b/regex.c
@@ -1393,19 +1393,15 @@ static val reg_expand_nongreedy(val exp)
if (sym == set_s || sym == cset_s) {
return exp;
- } else if (sym == compound_s) {
+ } else if (sym == compound_s || sym == zeroplus_s || sym == oneplus_s ||
+ sym == optional_s || sym == compl_s ||
+ sym == or_s || sym == and_s)
+ {
list_collect_decl (out, iter);
- iter = list_collect(iter, compound_s);
+ iter = list_collect(iter, sym);
for (; args; args = cdr(args))
iter = list_collect(iter, reg_expand_nongreedy(first(args)));
return out;
- } else if (sym == zeroplus_s || sym == oneplus_s ||
- sym == optional_s || sym == compl_s) {
- return cons(sym, cons(reg_expand_nongreedy(first(args)), nil));
- } else if (sym == or_s || sym == and_s) {
- val xfirst = reg_expand_nongreedy(first(args));
- val xsecond = reg_expand_nongreedy(second(args));
- return cons(sym, cons(xfirst, cons(xsecond, nil)));
} else if (sym == nongreedy_s) {
val xfirst = reg_expand_nongreedy(first(args));
val xsecond = reg_expand_nongreedy(second(args));
@@ -1463,19 +1459,15 @@ static val reg_compile_csets(val exp)
if (sym == set_s || sym == cset_s) {
char_set_t *set = char_set_compile(args, eq(sym, cset_s));
return cobj(coerce(mem_t *, set), chset_s, &char_set_obj_ops);
- } else if (sym == compound_s) {
+ } else if (sym == compound_s || sym == zeroplus_s || sym == oneplus_s ||
+ sym == optional_s || sym == compl_s || sym == nongreedy_s ||
+ sym == or_s || sym == and_s)
+ {
list_collect_decl (out, iter);
- iter = list_collect(iter, compound_s);
+ iter = list_collect(iter, sym);
for (; args; args = cdr(args))
iter = list_collect(iter, reg_compile_csets(first(args)));
return out;
- } else if (sym == zeroplus_s || sym == oneplus_s ||
- sym == optional_s || sym == compl_s || sym == nongreedy_s) {
- return cons(sym, cons(reg_compile_csets(first(args)), nil));
- } else if (sym == or_s || sym == and_s) {
- val xfirst = reg_compile_csets(first(args));
- val xsecond = reg_compile_csets(second(args));
- return cons(sym, cons(xfirst, cons(xsecond, nil)));
} else {
uw_throwf(error_s, lit("bad operator in regex syntax: ~s"), sym, nao);
}