store full token and completion in completed_item
[protos/libecoli.git] / lib / ecoli_node_once.c
index 3142504..b01640d 100644 (file)
@@ -44,6 +44,8 @@
 #include <ecoli_node_many.h>
 #include <ecoli_node_once.h>
 
+EC_LOG_TYPE_REGISTER(node_once);
+
 struct ec_node_once {
        struct ec_node gen;
        struct ec_node *child;
@@ -58,10 +60,10 @@ count_node(struct ec_parsed *parsed, const struct ec_node *node)
        if (parsed == NULL)
                return 0;
 
-       if (parsed->node == node)
+       if (ec_parsed_get_node(parsed) == node)
                count++;
 
-       TAILQ_FOREACH(child, &parsed->children, next)
+       EC_PARSED_FOREACH_CHILD(child, parsed)
                count += count_node(child, node);
 
        return count;
@@ -85,36 +87,28 @@ ec_node_once_parse(const struct ec_node *gen_node,
        return ec_node_parse_child(node->child, state, strvec);
 }
 
-static struct ec_completed *
+static int
 ec_node_once_complete(const struct ec_node *gen_node,
-                       const struct ec_strvec *strvec)
+               struct ec_completed *completed,
+               const struct ec_strvec *strvec)
 {
        struct ec_node_once *node = (struct ec_node_once *)gen_node;
-       struct ec_completed *completed = NULL, *child_completed = NULL;
+       struct ec_parsed *parsed = ec_completed_get_state(completed);
        unsigned int count;
-
-       completed = ec_completed();
-       if (completed == NULL)
-               goto fail;
+       int ret;
 
        /* count the number of occurences of the node: if already parsed,
         * do not match
         */
-       //count = count_node(ec_parsed_get_root(state), node->child); //XXX
-       count = 1;
+       count = count_node(ec_parsed_get_root(parsed), node->child); //XXX
        if (count > 0)
-               return completed;
-
-       child_completed = ec_node_complete_strvec(node->child, strvec);
-       if (child_completed == NULL)
-               goto fail;
+               return 0;
 
-       ec_completed_merge(completed, child_completed);
-       return completed;
+       ret = ec_node_complete_child(node->child, completed, strvec);
+       if (ret < 0)
+               return ret;
 
-fail:
-       ec_completed_free(completed);
-       return NULL;
+       return 0;
 }
 
 static void ec_node_once_free_priv(struct ec_node *gen_node)
@@ -137,14 +131,15 @@ EC_NODE_TYPE_REGISTER(ec_node_once_type);
 int ec_node_once_set(struct ec_node *gen_node, struct ec_node *child)
 {
        struct ec_node_once *node = (struct ec_node_once *)gen_node;
+       int ret;
 
-       // XXX check node type
-
-       assert(node != NULL);
-
-       if (child == NULL)
+       if (gen_node == NULL || child == NULL)
                return -EINVAL;
 
+       ret = ec_node_check_type(gen_node, &ec_node_once_type);
+       if (ret < 0)
+               return ret;
+
        gen_node->flags &= ~EC_NODE_F_BUILT;
 
        node->child = child;
@@ -171,6 +166,7 @@ struct ec_node *ec_node_once(const char *id, struct ec_node *child)
        return gen_node;
 }
 
+/* LCOV_EXCL_START */
 static int ec_node_once_testcase(void)
 {
        struct ec_node *node;
@@ -183,7 +179,7 @@ static int ec_node_once_testcase(void)
                                ), 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;
        }
        ret |= EC_TEST_CHECK_PARSE(node, 0);
@@ -208,21 +204,20 @@ static int ec_node_once_testcase(void)
                ec_node_str(NULL, "titi")
        );
        if (node == NULL) {
-               ec_log(EC_LOG_ERR, "cannot create node\n");
+               EC_LOG(EC_LOG_ERR, "cannot create node\n");
                return -1;
        }
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "", EC_NODE_ENDLIST,
-               "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST,
-               "");
+               "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "f", EC_NODE_ENDLIST,
-               "oo", EC_NODE_ENDLIST,
-               "oo");
+               "foo", EC_NODE_ENDLIST);
        ec_node_free(node);
 #endif
        return ret;
 }
+/* LCOV_EXCL_STOP */
 
 static struct ec_test ec_node_once_test = {
        .name = "node_once",