Change URI syntax
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/fcbfaca9 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/fcbfaca9 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/fcbfaca9 Branch: refs/heads/markdown_v2 Commit: fcbfaca9800c66a37c5ad4739eb9c6834d5e36c0 Parents: 27681c2 Author: Nick Wellnhofer <[email protected]> Authored: Thu Dec 4 16:49:40 2014 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Thu Dec 4 16:49:40 2014 +0100 ---------------------------------------------------------------------- compiler/src/CFCUri.c | 184 ++++++++++++++++++++++----------------------- compiler/src/CFCUri.h | 3 +- 2 files changed, 94 insertions(+), 93 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/fcbfaca9/compiler/src/CFCUri.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUri.c b/compiler/src/CFCUri.c index 0f466a6..ce8b2cd 100644 --- a/compiler/src/CFCUri.c +++ b/compiler/src/CFCUri.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include <ctype.h> #include <stdlib.h> #include <string.h> @@ -38,12 +39,15 @@ static const CFCMeta CFCURI_META = { (CFCBase_destroy_t)CFCUri_destroy }; -static char* -S_next_component(const char *uri, const char **comp_ptr, int *num_comps); +static void +S_parse_uri(CFCUri *self, const char *str, CFCClass *klass); + +static char** +S_split(const char *str, char c, int *num_parts_ptr); int CFCUri_is_clownfish_uri(const char *uri) { - return strncmp(uri, "clownfish:", 10) == 0; + return strncmp(uri, "cfish:", 6) == 0; } CFCUri* @@ -56,82 +60,15 @@ CFCUri* CFCUri_init(CFCUri *self, const char *uri, CFCClass *klass) { CFCUTIL_NULL_CHECK(uri); - if (strncmp(uri, "clownfish:", 10) != 0) { + if (strncmp(uri, "cfish:", 6) != 0) { CFCUtil_die("Invalid clownfish URI: %s", uri); } - const char *comp = uri + 10; - - int num_components = 0; - - char *type_str = S_next_component(uri, &comp, &num_components); - char *comp2 = S_next_component(uri, &comp, &num_components); - char *comp3 = S_next_component(uri, &comp, &num_components); - char *comp4 = S_next_component(uri, &comp, &num_components); - if (num_components == 0 || comp[0] != '\0') { - CFCUtil_die("Invalid clownfish URI: %s", uri); - } - - if (strcmp(type_str, "null") == 0) { + if (strcmp(uri + 6, "@null") == 0) { self->type = CFC_URI_NULL; - - if (num_components != 1) { - CFCUtil_die("Invalid clownfish URI: %s", uri); - } - } - else if (strcmp(type_str, "class") == 0) { - self->type = CFC_URI_CLASS; - - if (num_components == 2) { - if (!klass) { - CFCUtil_die("Class needed to complete URI: %s", uri); - } - self->prefix = CFCUtil_strdup(CFCClass_get_prefix(klass)); - self->struct_sym = comp2; - } - else if (num_components == 3) { - self->prefix = CFCUtil_sprintf("%s_", comp2); - self->struct_sym = comp3; - FREEMEM(comp2); - } - else { - CFCUtil_die("Invalid clownfish URI: %s", uri); - } - } - else if (strcmp(type_str, "function") == 0 - || strcmp(type_str, "method") == 0 - ) { - self->type = strcmp(type_str, "function") == 0 - ? CFC_URI_FUNCTION : CFC_URI_METHOD; - - if (num_components == 2) { - if (!klass) { - CFCUtil_die("Class needed to complete URI: %s", uri); - } - self->prefix = CFCUtil_strdup(CFCClass_get_prefix(klass)); - self->struct_sym = CFCUtil_strdup(CFCClass_get_struct_sym(klass)); - self->func_sym = comp2; - } - else if (num_components == 3) { - if (!klass) { - CFCUtil_die("Class needed to complete URI: %s", uri); - } - self->prefix = CFCUtil_strdup(CFCClass_get_prefix(klass)); - self->struct_sym = comp2; - self->func_sym = comp3; - } - else if (num_components == 4) { - self->prefix = CFCUtil_sprintf("%s_", comp2); - self->struct_sym = comp3; - self->func_sym = comp4; - FREEMEM(comp2); - } - else { - CFCUtil_die("Invalid clownfish URI: %s", uri); - } } else { - CFCUtil_die("Invalid clownfish URI: %s", uri); + S_parse_uri(self, uri, klass); } if (self->prefix && self->struct_sym) { @@ -162,8 +99,6 @@ CFCUri_init(CFCUri *self, const char *uri, CFCClass *klass) { } } - FREEMEM(type_str); - return self; } @@ -176,33 +111,98 @@ CFCUri_destroy(CFCUri *self) { CFCBase_destroy((CFCBase*)self); } -static char* -S_next_component(const char *uri, const char **comp_ptr, int *num_comps) { - const char *comp = *comp_ptr; +static void +S_parse_uri(CFCUri *self, const char *uri, CFCClass *klass) { + size_t num_components = 0; + char **components = S_split(uri + 6, '.', &num_components); + size_t i = 0; + + self->type = CFC_URI_PARCEL; - if (comp[0] == '\0') { - return NULL; + if (islower(components[i][0])) { + // Parcel + self->prefix = CFCUtil_sprintf("%s_", components[i]); + ++i; + } + else { + if (!klass) { + CFCUtil_die("Class needed to complete URI: %s", uri); + } + self->prefix = CFCUtil_strdup(CFCClass_get_prefix(klass)); } - const char *colon = strchr(comp, ':'); - char *component; + if (i < num_components) { + self->type = CFC_URI_CLASS; - if (colon) { - size_t len = colon - comp; - if (len == 0) { + if (isupper(components[i][0])) { + // Class + self->struct_sym = components[i]; + components[i] = NULL; + } + else if (i == 0 && components[i][0] == '\0') { + if (!klass) { + CFCUtil_die("Class needed to complete URI: %s", uri); + } + self->struct_sym = CFCUtil_strdup(CFCClass_get_struct_sym(klass)); + } + else { CFCUtil_die("Invalid clownfish URI: %s", uri); } - component = CFCUtil_strndup(comp, len); - *comp_ptr += len + 1; + + ++i; } - else { - component = CFCUtil_strdup(comp); - *comp_ptr += strlen(comp); + + if (i < num_components) { + if (isupper(components[i][0])) { + self->type = CFC_URI_METHOD; + } + else if (islower(components[i][0])) { + self->type = CFC_URI_FUNCTION; + } + else { + CFCUtil_die("Invalid clownfish URI: %s", uri); + } + + self->func_sym = components[i]; + components[i] = NULL; + ++i; + } + + if (i != num_components) { + CFCUtil_die("Invalid clownfish URI: %s", uri); + } + + for (i = 0; i < num_components; ++i) { + FREEMEM(components[i]); + } + FREEMEM(components); +} + +static char** +S_split(const char *str, char c, int *num_parts_ptr) { + const char *ptr; + int num_parts = 1; + + for (ptr = str; *ptr != '\0'; ++ptr) { + if (*ptr == c) { ++num_parts; } + } + + char **parts = (char**)MALLOCATE((num_parts + 1) * sizeof(char*)); + const char *start = str; + size_t i = 0; + + for (ptr = str; *ptr != '\0'; ++ptr) { + if (*ptr == c) { + parts[i++] = CFCUtil_strndup(start, ptr - start); + start = ptr + 1; + } } + parts[i++] = CFCUtil_strndup(start, ptr - start); + parts[i] = NULL; - ++*num_comps; + *num_parts_ptr = num_parts; - return component; + return parts; } int http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/fcbfaca9/compiler/src/CFCUri.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUri.h b/compiler/src/CFCUri.h index 51784e2..004fc3c 100644 --- a/compiler/src/CFCUri.h +++ b/compiler/src/CFCUri.h @@ -21,10 +21,11 @@ extern "C" { #endif -#define CFC_URI_NULL 1 +#define CFC_URI_PARCEL 1 #define CFC_URI_CLASS 2 #define CFC_URI_FUNCTION 3 #define CFC_URI_METHOD 4 +#define CFC_URI_NULL 5 typedef struct CFCUri CFCUri; struct CFCClass;
