use EC_NO_ID instead of NULL
[protos/libecoli.git] / lib / ecoli_node_re_lex.c
index f580b72..d543178 100644 (file)
 #include <ecoli_test.h>
 #include <ecoli_strvec.h>
 #include <ecoli_node.h>
+#include <ecoli_parsed.h>
 #include <ecoli_node_many.h>
 #include <ecoli_node_or.h>
 #include <ecoli_node_str.h>
 #include <ecoli_node_int.h>
 #include <ecoli_node_re_lex.h>
 
+EC_LOG_TYPE_REGISTER(node_re_lex);
+
 struct regexp_pattern {
        char *pattern;
        regex_t r;
@@ -44,7 +47,7 @@ tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
        if (dup == NULL)
                goto fail;
 
-       strvec = ec_strvec_new();
+       strvec = ec_strvec();
        if (strvec == NULL)
                goto fail;
 
@@ -64,7 +67,7 @@ tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
 
                        c = dup[pos.rm_eo + off];
                        dup[pos.rm_eo + off] = '\0';
-                       ec_log(EC_LOG_DEBUG, "re_lex match <%s>\n", &dup[off]);
+                       EC_LOG(EC_LOG_DEBUG, "re_lex match <%s>\n", &dup[off]);
                        if (ec_strvec_add(strvec, &dup[off]) < 0)
                                goto fail;
 
@@ -87,55 +90,49 @@ fail:
        return NULL;
 }
 
-static struct ec_parsed *ec_node_re_lex_parse(const struct ec_node *gen_node,
-       const struct ec_strvec *strvec)
+static int
+ec_node_re_lex_parse(const struct ec_node *gen_node,
+               struct ec_parsed *state,
+               const struct ec_strvec *strvec)
 {
        struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
-       struct ec_strvec *new_vec = NULL, *match_strvec;
-       struct ec_parsed *parsed = NULL, *child_parsed;
+       struct ec_strvec *new_vec = NULL;
+       struct ec_parsed *child_parsed;
        const char *str;
+       int ret;
 
-       parsed = ec_parsed_new();
-       if (parsed == NULL)
-               return NULL;
-
-       if (ec_strvec_len(strvec) == 0)
-               return parsed;
-
-       str = ec_strvec_val(strvec, 0);
-       new_vec = tokenize(node->table, node->len, str);
-       if (new_vec == NULL)
+       if (ec_strvec_len(strvec) == 0) {
+               new_vec = ec_strvec();
+       } else {
+               str = ec_strvec_val(strvec, 0);
+               new_vec = tokenize(node->table, node->len, str);
+       }
+       if (new_vec == NULL) {
+               ret = -ENOMEM;
                goto fail;
+       }
 
-       printf("--------\n");
-       ec_strvec_dump(stdout, new_vec);
-       child_parsed = ec_node_parse_strvec(node->child, new_vec);
-       if (child_parsed == NULL)
+       ret = ec_node_parse_child(node->child, state, new_vec);
+       if (ret < 0)
                goto fail;
 
-       if (!ec_parsed_matches(child_parsed) ||
-                       ec_parsed_len(child_parsed) !=
-                               ec_strvec_len(new_vec)) {
-               ec_strvec_free(new_vec);
+       if ((unsigned)ret == ec_strvec_len(new_vec)) {
+               ret = 1;
+       } else if (ret != EC_PARSED_NOMATCH) {
+               child_parsed = ec_parsed_get_last_child(state);
+               ec_parsed_del_child(state, child_parsed);
                ec_parsed_free(child_parsed);
-               return parsed;
+               ret = EC_PARSED_NOMATCH;
        }
+
        ec_strvec_free(new_vec);
        new_vec = NULL;
 
-       ec_parsed_add_child(parsed, child_parsed);
-       match_strvec = ec_strvec_ndup(strvec, 0, 1);
-       if (match_strvec == NULL)
-               goto fail;
-       ec_parsed_set_match(parsed, gen_node, match_strvec);
-
-       return parsed;
+       return ret;
 
  fail:
        ec_strvec_free(new_vec);
-       ec_parsed_free(parsed);
-
-       return NULL;
+       return ret;
 }
 
 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
@@ -181,7 +178,7 @@ int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
 
        ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
        if (ret != 0) {
-               ec_log(EC_LOG_ERR,
+               EC_LOG(EC_LOG_ERR,
                        "Regular expression <%s> compilation failed: %d\n",
                        pattern, ret);
                if (ret == REG_ESPACE)
@@ -211,7 +208,7 @@ struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
        if (child == NULL)
                return NULL;
 
-       node = (struct ec_node_re_lex *)__ec_node_new(&ec_node_re_lex_type, id);
+       node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
        if (node == NULL) {
                ec_node_free(child);
                return NULL;
@@ -222,23 +219,23 @@ struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
        return &node->gen;
 }
 
-
+/* LCOV_EXCL_START */
 static int ec_node_re_lex_testcase(void)
 {
        struct ec_node *node;
        int ret = 0;
 
-       node = ec_node_re_lex(NULL,
-               ec_node_many(NULL,
-                       EC_NODE_OR(NULL,
-                               ec_node_str(NULL, "foo"),
-                               ec_node_str(NULL, "bar"),
-                               ec_node_int(NULL, 0, 1000, 0)
+       node = ec_node_re_lex(EC_NO_ID,
+               ec_node_many(EC_NO_ID,
+                       EC_NODE_OR(EC_NO_ID,
+                               ec_node_str(EC_NO_ID, "foo"),
+                               ec_node_str(EC_NO_ID, "bar"),
+                               ec_node_int(EC_NO_ID, 0, 1000, 0)
                        ), 0, 0
                )
        );
        if (node == NULL) {
-               ec_log(EC_LOG_ERR, "cannot create node\n");
+               EC_LOG(EC_LOG_ERR, "cannot create node\n");
                return -1;
        }
 
@@ -250,7 +247,7 @@ static int ec_node_re_lex_testcase(void)
        ret |= ec_node_re_lex_add(node, "\\+", 1);
        ret |= ec_node_re_lex_add(node, "[      ]+", 0);
        if (ret != 0) {
-               ec_log(EC_LOG_ERR, "cannot add regexp to node\n");
+               EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
                ec_node_free(node);
                return -1;
        }
@@ -264,6 +261,7 @@ static int ec_node_re_lex_testcase(void)
 
        return ret;
 }
+/* LCOV_EXCL_STOP */
 
 static struct ec_test ec_node_re_lex_test = {
        .name = "node_re_lex",