replace generic child table by a node ops
[protos/libecoli.git] / lib / ecoli_node_seq.c
index e32432e..34512b6 100644 (file)
@@ -1,5 +1,5 @@
 /* SPDX-License-Identifier: BSD-3-Clause
- * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
  */
 
 #include <stdio.h>
@@ -15,8 +15,8 @@
 #include <ecoli_test.h>
 #include <ecoli_strvec.h>
 #include <ecoli_node.h>
-#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_parse.h>
+#include <ecoli_complete.h>
 #include <ecoli_node_str.h>
 #include <ecoli_node_option.h>
 #include <ecoli_node_or.h>
@@ -33,7 +33,7 @@ struct ec_node_seq {
 
 static int
 ec_node_seq_parse(const struct ec_node *gen_node,
-               struct ec_parsed *state,
+               struct ec_parse *state,
                const struct ec_strvec *strvec)
 {
        struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
@@ -45,10 +45,8 @@ ec_node_seq_parse(const struct ec_node *gen_node,
        for (i = 0; i < node->len; i++) {
                childvec = ec_strvec_ndup(strvec, len,
                        ec_strvec_len(strvec) - len);
-               if (childvec == NULL) {
-                       ret = -ENOMEM;
+               if (childvec == NULL)
                        goto fail;
-               }
 
                ret = ec_node_parse_child(node->table[i], state, childvec);
                if (ret < 0)
@@ -57,9 +55,9 @@ ec_node_seq_parse(const struct ec_node *gen_node,
                ec_strvec_free(childvec);
                childvec = NULL;
 
-               if (ret == EC_PARSED_NOMATCH) {
-                       ec_parsed_free_children(state);
-                       return EC_PARSED_NOMATCH;
+               if (ret == EC_PARSE_NOMATCH) {
+                       ec_parse_free_children(state);
+                       return EC_PARSE_NOMATCH;
                }
 
                len += ret;
@@ -69,15 +67,15 @@ ec_node_seq_parse(const struct ec_node *gen_node,
 
 fail:
        ec_strvec_free(childvec);
-       return ret;
+       return -1;
 }
 
 static int
 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
-               struct ec_completed *completed,
+               struct ec_comp *comp,
                const struct ec_strvec *strvec)
 {
-       struct ec_parsed *parsed = ec_completed_get_state(completed);
+       struct ec_parse *parse = ec_comp_get_state(comp);
        struct ec_strvec *childvec = NULL;
        unsigned int i;
        int ret;
@@ -97,7 +95,7 @@ __ec_node_seq_complete(struct ec_node **table, size_t table_len,
         */
 
        /* first, try to complete with the first node of the table */
-       ret = ec_node_complete_child(table[0], completed, strvec);
+       ret = ec_node_complete_child(table[0], comp, strvec);
        if (ret < 0)
                goto fail;
 
@@ -108,7 +106,7 @@ __ec_node_seq_complete(struct ec_node **table, size_t table_len,
                if (childvec == NULL)
                        goto fail;
 
-               ret = ec_node_parse_child(table[0], parsed, childvec);
+               ret = ec_node_parse_child(table[0], parse, childvec);
                if (ret < 0)
                        goto fail;
 
@@ -116,21 +114,21 @@ __ec_node_seq_complete(struct ec_node **table, size_t table_len,
                childvec = NULL;
 
                if ((unsigned int)ret != i) {
-                       if (ret != EC_PARSED_NOMATCH)
-                               ec_parsed_del_last_child(parsed);
+                       if (ret != EC_PARSE_NOMATCH)
+                               ec_parse_del_last_child(parse);
                        continue;
                }
 
                childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
                if (childvec == NULL) {
-                       ec_parsed_del_last_child(parsed);
+                       ec_parse_del_last_child(parse);
                        goto fail;
                }
 
                ret = __ec_node_seq_complete(&table[1],
                                        table_len - 1,
-                                       completed, childvec);
-               ec_parsed_del_last_child(parsed);
+                                       comp, childvec);
+               ec_parse_del_last_child(parse);
                ec_strvec_free(childvec);
                childvec = NULL;
 
@@ -147,12 +145,12 @@ fail:
 
 static int
 ec_node_seq_complete(const struct ec_node *gen_node,
-               struct ec_completed *completed,
+               struct ec_comp *comp,
                const struct ec_strvec *strvec)
 {
        struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
 
-       return __ec_node_seq_complete(node->table, node->len, completed,
+       return __ec_node_seq_complete(node->table, node->len, comp,
                                strvec);
 }
 
@@ -166,12 +164,32 @@ static void ec_node_seq_free_priv(struct ec_node *gen_node)
        ec_free(node->table);
 }
 
+static size_t
+ec_node_seq_get_children_count(const struct ec_node *gen_node)
+{
+       struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
+       return node->len;
+}
+
+static struct ec_node *
+ec_node_seq_get_child(const struct ec_node *gen_node, size_t i)
+{
+       struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
+
+       if (i >= node->len)
+               return NULL;
+
+       return node->table[i];
+}
+
 static struct ec_node_type ec_node_seq_type = {
        .name = "seq",
        .parse = ec_node_seq_parse,
        .complete = ec_node_seq_complete,
        .size = sizeof(struct ec_node_seq),
        .free_priv = ec_node_seq_free_priv,
+       .get_children_count = ec_node_seq_get_children_count,
+       .get_child = ec_node_seq_get_child,
 };
 
 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
@@ -196,10 +214,6 @@ int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
                goto fail;
 
        node->table = table;
-
-       if (ec_node_add_child(gen_node, child) < 0)
-               goto fail;
-
        table[node->len] = child;
        node->len++;