summaryrefslogtreecommitdiff
path: root/regex.c
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2017-07-25 21:08:44 -0500
committerBobby Bingham <koorogi@koorogi.info>2017-07-25 21:09:45 -0500
commit0e96b8551b5293ffeadfebc31b85a165b0a74b99 (patch)
tree1057a6bc62f64b9172d3d73a2d4a6cf422cc2f7e /regex.c
parentee78102672afdede839489fa0b1932b64335eaaf (diff)
create general regex simplification passHEADmaster
Move the previous special-case logic for removing 1-1 repeats into this pass.
Diffstat (limited to 'regex.c')
-rw-r--r--regex.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/regex.c b/regex.c
new file mode 100644
index 0000000..63bd748
--- /dev/null
+++ b/regex.c
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+
+#include "ast.h"
+
+static void repeat(struct atom *a)
+{
+ struct atom *child = a->u.repeat.child;
+ simplify(child);
+
+ const struct repeat *r = &a->u.repeat.counts;
+ if (r->min == 1 && r->max == 1) {
+ *a = *child;
+ free(child);
+ }
+}
+
+static void children(struct atom *a)
+{
+ simplify(a->u.children[0]);
+ simplify(a->u.children[1]);
+}
+
+void simplify(struct atom *a)
+{
+ switch (a->type) {
+ case ATOM_REPETITION:
+ repeat(a);
+ break;
+
+ case ATOM_ALTERNATION:
+ case ATOM_SEQUENCE:
+ children(a);
+ }
+}
+