You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2015/08/06 18:19:55 UTC

[18/20] lucy-clownfish git commit: Replace erroneous Clownfish URLs with error message

Replace erroneous Clownfish URLs with error message


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/520a87ef
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/520a87ef
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/520a87ef

Branch: refs/heads/master
Commit: 520a87ef45be57d271b34f1d966664c8fb551db7
Parents: 37504e4
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Jul 26 19:39:10 2015 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Thu Aug 6 18:19:19 2015 +0200

----------------------------------------------------------------------
 compiler/src/CFCC.c       |  12 +++-
 compiler/src/CFCCHtml.c   |  16 ++++-
 compiler/src/CFCPerlPod.c |  14 +++--
 compiler/src/CFCUri.c     | 137 +++++++++++++++++++++++------------------
 compiler/src/CFCUri.h     |  18 ++++--
 5 files changed, 125 insertions(+), 72 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/520a87ef/compiler/src/CFCC.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCC.c b/compiler/src/CFCC.c
index 6fba122..6d5adc6 100644
--- a/compiler/src/CFCC.c
+++ b/compiler/src/CFCC.c
@@ -174,9 +174,15 @@ CFCC_write_hostdefs(CFCC *self) {
 char*
 CFCC_link_text(CFCUri *uri_obj) {
     char *link_text = NULL;
-    int   type      = CFCUri_get_type(uri_obj);
+    CFCUriType type = CFCUri_get_type(uri_obj);
 
     switch (type) {
+        case CFC_URI_ERROR: {
+            const char *error = CFCUri_get_error(uri_obj);
+            link_text = CFCUtil_sprintf("[%s]", error);
+            break;
+        }
+
         case CFC_URI_NULL:
             link_text = CFCUtil_strdup("NULL");
             break;
@@ -203,6 +209,10 @@ CFCC_link_text(CFCUri *uri_obj) {
             link_text = CFCUtil_strdup(name);
             break;
         }
+
+        default:
+            CFCUtil_die("Unsupported node type: %d", type);
+            break;
     }
 
     return link_text;

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/520a87ef/compiler/src/CFCCHtml.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCCHtml.c b/compiler/src/CFCCHtml.c
index 52740f5..c51da80 100644
--- a/compiler/src/CFCCHtml.c
+++ b/compiler/src/CFCCHtml.c
@@ -1018,7 +1018,16 @@ S_transform_link(cmark_node *link, CFCClass *doc_class, int dir_level) {
     CFCUri *uri_obj = CFCUri_new(uri_string, doc_class);
     char   *url     = S_cfc_uri_to_url(uri_obj, doc_class, dir_level);
 
-    if (url) {
+    if (CFCUri_get_type(uri_obj) == CFC_URI_ERROR) {
+        // Replace link with error.
+        char *link_text = CFCC_link_text(uri_obj);
+        cmark_node *text_node = cmark_node_new(CMARK_NODE_TEXT);
+        cmark_node_set_literal(text_node, link_text);
+        cmark_node_insert_after(link, text_node);
+        cmark_node_free(link);
+        FREEMEM(link_text);
+    }
+    else if (url) {
         cmark_node_set_url(link, url);
 
         if (!cmark_node_first_child(link)) {
@@ -1089,7 +1098,7 @@ S_type_to_html(CFCClass *klass, CFCType *type) {
 static char*
 S_cfc_uri_to_url(CFCUri *uri_obj, CFCClass *doc_class, int dir_level) {
     char *url = NULL;
-    int   type    = CFCUri_get_type(uri_obj);
+    CFCUriType type = CFCUri_get_type(uri_obj);
 
     switch (type) {
         case CFC_URI_CLASS: {
@@ -1113,6 +1122,9 @@ S_cfc_uri_to_url(CFCUri *uri_obj, CFCClass *doc_class, int dir_level) {
             url = S_document_to_url(doc, doc_class, dir_level);
             break;
         }
+
+        default:
+            break;
     }
 
     return url;

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/520a87ef/compiler/src/CFCPerlPod.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlPod.c b/compiler/src/CFCPerlPod.c
index ffa0f04..8b728e0 100644
--- a/compiler/src/CFCPerlPod.c
+++ b/compiler/src/CFCPerlPod.c
@@ -570,12 +570,18 @@ S_convert_link(cmark_node *link, CFCClass *doc_class, int header_level) {
         return retval;
     }
 
-    char   *new_uri  = NULL;
-    char   *new_text = NULL;
-    CFCUri *uri_obj  = CFCUri_new(uri, doc_class);
-    int     type     = CFCUri_get_type(uri_obj);
+    char       *new_uri  = NULL;
+    char       *new_text = NULL;
+    CFCUri     *uri_obj  = CFCUri_new(uri, doc_class);
+    CFCUriType  type     = CFCUri_get_type(uri_obj);
 
     switch (type) {
+        case CFC_URI_ERROR: {
+            const char *error = CFCUri_get_error(uri_obj);
+            new_text = CFCUtil_sprintf("[%s]", error);
+            break;
+        }
+
         case CFC_URI_NULL:
             // Change all instances of NULL to 'undef'
             new_text = CFCUtil_strdup("undef");

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/520a87ef/compiler/src/CFCUri.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCUri.c b/compiler/src/CFCUri.c
index fc6e8e8..8c7c555 100644
--- a/compiler/src/CFCUri.c
+++ b/compiler/src/CFCUri.c
@@ -29,10 +29,11 @@ struct CFCUri {
     CFCBase      base;
     char        *string;
     CFCClass    *doc_class;
-    int          type;
+    CFCUriType   type;
     CFCClass    *klass;
     CFCDocument *document;
     char        *callable;
+    char        *error;
 };
 
 static const CFCMeta CFCURI_META = {
@@ -48,6 +49,9 @@ static void
 S_resolve(CFCUri *self, const char *prefix, const char *struct_sym,
           const char *callable);
 
+static void
+S_set_error(CFCUri *self, const char *error);
+
 static char*
 S_next_component(char **iter);
 
@@ -80,6 +84,7 @@ void
 CFCUri_destroy(CFCUri *self) {
     FREEMEM(self->string);
     FREEMEM(self->callable);
+    FREEMEM(self->error);
     CFCBase_decref((CFCBase*)self->doc_class);
     CFCBase_decref((CFCBase*)self->klass);
     CFCBase_decref((CFCBase*)self->document);
@@ -107,36 +112,34 @@ S_parse(CFCUri *self) {
         // Parcel
         parcel = component;
         component = S_next_component(&iter);
-    }
 
-    if (component) {
-        if (isupper(component[0])) {
-            // Class
-            struct_sym = component;
-        }
-        else if (component == buf && component[0] == '\0' && iter) {
-            // "cfish:.Method" style URL.
-            ;
-        }
-        else {
-            CFCUtil_die("Invalid component in Clownfish URI: %s",
-                        self->string);
+        if (!component) {
+            S_set_error(self, "Missing component in Clownfish URI");
+            goto done;
         }
+    }
 
-        component = S_next_component(&iter);
+    // struct_sym == NULL for "cfish:.Method" style URL.
+    // parcel implies struct_sym.
+    if (parcel || component[0] != '\0') {
+        struct_sym = component;
     }
 
+    component = S_next_component(&iter);
+
     if (component) {
         callable = component;
         component = S_next_component(&iter);
     }
 
     if (component) {
-        CFCUtil_die("Trailing components in Clownfish URI: %s", self->string);
+        S_set_error(self, "Trailing component in Clownfish URI");
+        goto done;
     }
 
     S_resolve(self, parcel, struct_sym, callable);
 
+done:
     FREEMEM(buf);
 }
 
@@ -145,67 +148,74 @@ S_resolve(CFCUri *self, const char *parcel, const char *struct_sym,
           const char *callable) {
 
     // Try to find a CFCClass.
-    if (struct_sym || callable) {
-        CFCClass *doc_class = self->doc_class;
-        CFCClass *klass     = NULL;
-
-        if (parcel) {
-            char *full_struct_sym = CFCUtil_sprintf("%s_%s", parcel, struct_sym);
-            klass = CFCClass_fetch_by_struct_sym(full_struct_sym);
-            FREEMEM(full_struct_sym);
-        }
-        else if (struct_sym && doc_class) {
-            const char *prefix = CFCClass_get_prefix(doc_class);
-            char *full_struct_sym = CFCUtil_sprintf("%s%s", prefix, struct_sym);
-            klass = CFCClass_fetch_by_struct_sym(full_struct_sym);
-            FREEMEM(full_struct_sym);
-        }
-        else {
-            klass = doc_class;
-        }
+    CFCClass *doc_class = self->doc_class;
+    CFCClass *klass     = NULL;
 
-        if (klass) {
-            self->type  = CFC_URI_CLASS;
-            self->klass = klass;
-            CFCBase_incref((CFCBase*)klass);
+    if (parcel) {
+        char *full_struct_sym = CFCUtil_sprintf("%s_%s", parcel, struct_sym);
+        klass = CFCClass_fetch_by_struct_sym(full_struct_sym);
+        FREEMEM(full_struct_sym);
+    }
+    else if (struct_sym && doc_class) {
+        const char *prefix = CFCClass_get_prefix(doc_class);
+        char *full_struct_sym = CFCUtil_sprintf("%s%s", prefix, struct_sym);
+        klass = CFCClass_fetch_by_struct_sym(full_struct_sym);
+        FREEMEM(full_struct_sym);
+    }
+    else if (callable) {
+        klass = doc_class;
+    }
 
-            if (callable) {
-                if (islower(callable[0])) {
-                    if (!CFCClass_function(klass, callable)) {
-                        CFCUtil_warn("Unknown function '%s' in Clownfish URI: %s",
-                                     callable, self->string);
-                    }
+    if (klass) {
+        self->type  = CFC_URI_CLASS;
+        self->klass = klass;
+        CFCBase_incref((CFCBase*)klass);
 
-                    self->type     = CFC_URI_FUNCTION;
-                    self->callable = CFCUtil_strdup(callable);
+        if (callable) {
+            if (islower(callable[0])) {
+                if (!CFCClass_function(klass, callable)) {
+                    CFCUtil_warn("Unknown function '%s' in Clownfish URI: %s",
+                                 callable, self->string);
                 }
-                else {
-                    if (!CFCClass_method(klass, callable)) {
-                        CFCUtil_warn("Unknown method '%s' in Clownfish URI: %s",
-                                     callable, self->string);
-                    }
-
-                    self->type     = CFC_URI_METHOD;
-                    self->callable = CFCUtil_strdup(callable);
+
+                self->type     = CFC_URI_FUNCTION;
+                self->callable = CFCUtil_strdup(callable);
+            }
+            else {
+                if (!CFCClass_method(klass, callable)) {
+                    CFCUtil_warn("Unknown method '%s' in Clownfish URI: %s",
+                                 callable, self->string);
                 }
+
+                self->type     = CFC_URI_METHOD;
+                self->callable = CFCUtil_strdup(callable);
             }
         }
+
+        return;
     }
 
     // Try to find a CFCDocument.
-    if (self->type == 0 && !parcel && struct_sym && !callable) {
+    if (!parcel && struct_sym && !callable) {
         CFCDocument *doc = CFCDocument_fetch(struct_sym);
 
         if (doc) {
             self->type     = CFC_URI_DOCUMENT;
             self->document = doc;
             CFCBase_incref((CFCBase*)doc);
+            return;
         }
     }
 
-    if (self->type == 0) {
-        CFCUtil_die("Couldn't resolve Clownfish URI: %s", self->string);
-    }
+    S_set_error(self, "Couldn't resolve Clownfish URI");
+}
+
+static void
+S_set_error(CFCUri *self, const char *error) {
+    self->type  = CFC_URI_ERROR;
+    self->error = CFCUtil_sprintf("%s: %s", error, self->string);
+
+    CFCUtil_warn(self->error);
 }
 
 static char*
@@ -231,7 +241,7 @@ CFCUri_get_string(CFCUri *self) {
     return self->string;
 }
 
-int
+CFCUriType
 CFCUri_get_type(CFCUri *self) {
     if (self->type == 0) { S_parse(self); }
     return self->type;
@@ -264,3 +274,12 @@ CFCUri_get_callable_name(CFCUri *self) {
     return self->callable;
 }
 
+const char*
+CFCUri_get_error(CFCUri *self) {
+    if (self->type == 0) { S_parse(self); }
+    if (self->error == NULL) {
+        CFCUtil_die("Not an error URI");
+    }
+    return self->error;
+}
+

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/520a87ef/compiler/src/CFCUri.h
----------------------------------------------------------------------
diff --git a/compiler/src/CFCUri.h b/compiler/src/CFCUri.h
index 9e22d4e..6c05653 100644
--- a/compiler/src/CFCUri.h
+++ b/compiler/src/CFCUri.h
@@ -21,11 +21,14 @@
 extern "C" {
 #endif
 
-#define CFC_URI_NULL        1
-#define CFC_URI_CLASS       2
-#define CFC_URI_FUNCTION    3
-#define CFC_URI_METHOD      4
-#define CFC_URI_DOCUMENT    5
+typedef enum {
+    CFC_URI_NULL     = 1,
+    CFC_URI_CLASS    = 2,
+    CFC_URI_FUNCTION = 3,
+    CFC_URI_METHOD   = 4,
+    CFC_URI_DOCUMENT = 5,
+    CFC_URI_ERROR    = 6
+} CFCUriType;
 
 typedef struct CFCUri CFCUri;
 struct CFCClass;
@@ -46,7 +49,7 @@ CFCUri_destroy(CFCUri *self);
 const char*
 CFCUri_get_string(CFCUri *self);
 
-int
+CFCUriType
 CFCUri_get_type(CFCUri *self);
 
 struct CFCClass*
@@ -58,6 +61,9 @@ CFCUri_get_document(CFCUri *self);
 const char*
 CFCUri_get_callable_name(CFCUri *self);
 
+const char*
+CFCUri_get_error(CFCUri *self);
+
 #ifdef __cplusplus
 }
 #endif