[augeas-devel] [PATCH 2 of 4] Add REGEXP_MINUS; move REGEXP_TO_FA to regexp.c

David Lutterkort dlutter at redhat.com
Fri May 9 19:59:27 UTC 2008


3 files changed, 70 insertions(+), 12 deletions(-)
src/lens.c   |   12 ------------
src/regexp.c |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/syntax.h |   14 ++++++++++++++


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1210363160 25200
# Node ID a23b4e8f59df90d2a83e30ac0d9bae32cfc30e93
# Parent  1c5506d2ca01aa0e90439e6f9b88266a5e965f85
Add REGEXP_MINUS; move REGEXP_TO_FA to regexp.c

diff -r 1c5506d2ca01 -r a23b4e8f59df src/lens.c
--- a/src/lens.c	Fri May 09 12:59:17 2008 -0700
+++ b/src/lens.c	Fri May 09 12:59:20 2008 -0700
@@ -39,18 +39,6 @@ static const char *const tags[] = {
     "del", "store", "key", "label", "seq", "counter", "concat", "union",
     "subtree", "star", "maybe"
 };
-
-static fa_t regexp_to_fa(struct regexp *regexp) {
-    fa_t fa;
-    int error = fa_compile(regexp->pattern->str, &fa);
-    if (error != REG_NOERROR) {
-        syntax_error(regexp->info,
-                     "unexpected error from fa_compile %d compiling %s",
-                     error, regexp->pattern->str);
-        return NULL;
-    }
-    return fa;
-}
 
 static struct lens *make_lens(enum lens_tag tag, struct info *info) {
     struct lens *lens;
diff -r 1c5506d2ca01 -r a23b4e8f59df src/regexp.c
--- a/src/regexp.c	Fri May 09 12:59:17 2008 -0700
+++ b/src/regexp.c	Fri May 09 12:59:20 2008 -0700
@@ -103,6 +103,50 @@ regexp_concat(struct info *info, struct 
 }
 
 struct regexp *
+regexp_minus(struct info *info, struct regexp *r1, struct regexp *r2) {
+    const char *p1 = r1->pattern->str;
+    const char *p2 = r2->pattern->str;
+    struct regexp *result = NULL;
+    fa_t fa = NULL, fa1 = NULL, fa2 = NULL;
+    int r;
+    char *s = NULL;
+
+    r = fa_compile(p1, &fa1);
+    if (r != REG_NOERROR)
+        goto error;
+
+    r = fa_compile(p2, &fa2);
+    if (r != REG_NOERROR)
+        goto error;
+
+    fa = fa_minus(fa1, fa2);
+    if (fa == NULL)
+        goto error;
+
+    r = fa_as_regexp(fa, &s);
+    if (r < 0)
+        goto error;
+
+    if (s == NULL) {
+        /* FA is the empty set, which we can't represent as a regexp */
+        goto error;
+    }
+
+    result = make_regexp(info, s);
+
+ done:
+    fa_free(fa);
+    fa_free(fa1);
+    fa_free(fa2);
+    free(s);
+    return result;
+ error:
+    unref(result, regexp);
+    goto done;
+}
+
+
+struct regexp *
 regexp_iter(struct info *info, struct regexp *r, int min, int max) {
     const char *p = r->pattern->str;
     char *s;
@@ -177,6 +221,18 @@ int regexp_nsub(struct regexp *r) {
     return r->re->re_nsub;
 }
 
+fa_t regexp_to_fa(struct regexp *regexp) {
+    fa_t fa;
+    int error = fa_compile(regexp->pattern->str, &fa);
+    if (error != REG_NOERROR) {
+        syntax_error(regexp->info,
+                     "unexpected error from fa_compile %d compiling %s",
+                     error, regexp->pattern->str);
+        return NULL;
+    }
+    return fa;
+}
+
 /*
  * Local variables:
  *  indent-tabs-mode: nil
diff -r 1c5506d2ca01 -r a23b4e8f59df src/syntax.h
--- a/src/syntax.h	Fri May 09 12:59:17 2008 -0700
+++ b/src/syntax.h	Fri May 09 12:59:20 2008 -0700
@@ -28,6 +28,7 @@
 #include "internal.h"
 #include "lens.h"
 #include "ref.h"
+#include "fa.h"
 
 struct info {
     unsigned int ref;
@@ -180,10 +181,23 @@ struct regexp *
 struct regexp *
 regexp_iter(struct info *info, struct regexp *r, int min, int max);
 
+/* Return a new REGEXP that matches all the words matched by R1 but
+ * not by R2
+ */
+struct regexp *
+regexp_minus(struct info *info, struct regexp *r1, struct regexp *r2);
+
 struct regexp *
 regexp_maybe(struct info *info, struct regexp *r);
 
 struct regexp *regexp_make_empty(struct info *);
+
+/* Construct a finite automaton from REGEXP. The pattern for REGEXP
+ * must be known to be syntactically correct.
+ *
+ * Return a new automaton, or NULL if the REGEXP has a syntax error
+ */
+fa_t regexp_to_fa(struct regexp *regexp);
 
 struct native {
     unsigned int argc;




More information about the augeas-devel mailing list