store full token and completion in completed_item
[protos/libecoli.git] / lib / ecoli_parsed.c
index e951671..f7ca38f 100644 (file)
@@ -32,6 +32,7 @@
 #include <assert.h>
 #include <errno.h>
 
+#include <ecoli_assert.h>
 #include <ecoli_malloc.h>
 #include <ecoli_strvec.h>
 #include <ecoli_keyval.h>
 #include <ecoli_node.h>
 #include <ecoli_parsed.h>
 
-int ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
-                       const struct ec_strvec *strvec)
+TAILQ_HEAD(ec_parsed_list, ec_parsed);
+
+/* XXX idea for parse: maintain a "cursor" ?
+struct ec_parsed {
+   struct ec_parsed_tree *root;
+   stuct ec_parsed_tree *cursor;
+};
+*/
+
+struct ec_parsed {
+       TAILQ_ENTRY(ec_parsed) next;
+       struct ec_parsed_list children;
+       struct ec_parsed *parent;
+       const struct ec_node *node;
+       struct ec_strvec *strvec;
+       struct ec_keyval *attrs;
+};
+
+static int __ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
+                               bool is_root, const struct ec_strvec *strvec)
 {
        struct ec_strvec *match_strvec;
        struct ec_parsed *child;
        int ret;
 
-       assert(state != NULL);
-
        /* build the node if required */
        if (node->type->build != NULL) {
                if ((node->flags & EC_NODE_F_BUILT) == 0) {
@@ -61,19 +78,26 @@ int ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
        if (node->type->parse == NULL)
                return -ENOTSUP;
 
-       child = ec_parsed();
-       if (child == NULL)
-               return -ENOMEM;
+       if (!is_root) {
+               child = ec_parsed();
+               if (child == NULL)
+                       return -ENOMEM;
 
-       child->node = node;
-       ec_parsed_add_child(state, child);
+               ec_parsed_add_child(state, child);
+       } else {
+               child = state;
+       }
+       ec_parsed_set_node(child, node);
        ret = node->type->parse(node, child, strvec);
-       if (ret == EC_PARSED_NOMATCH) {
-               ec_parsed_del_child(state, child);
-               assert(TAILQ_EMPTY(&child->children));
-               ec_parsed_free(child);
+       if (ret < 0) // XXX should we free state, child?
                return ret;
-       } else if (ret < 0) {
+
+       if (ret == EC_PARSED_NOMATCH) {
+               if (!is_root) {
+                       ec_parsed_del_child(state, child);
+                       assert(TAILQ_EMPTY(&child->children));
+                       ec_parsed_free(child);
+               }
                return ret;
        }
 
@@ -86,26 +110,26 @@ int ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
        return ret;
 }
 
+int ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
+                       const struct ec_strvec *strvec)
+{
+       assert(state != NULL);
+       return __ec_node_parse_child(node, state, false, strvec);
+}
+
 struct ec_parsed *ec_node_parse_strvec(struct ec_node *node,
                                const struct ec_strvec *strvec)
 {
        struct ec_parsed *parsed = ec_parsed();
-       struct ec_parsed *child_parsed;
        int ret;
 
        if (parsed == NULL)
                return NULL;
 
-       ret = ec_node_parse_child(node, parsed, strvec);
-       if (ret < 0 && ret != EC_PARSED_NOMATCH) {
+       ret = __ec_node_parse_child(node, parsed, true, strvec);
+       if (ret < 0) {
                ec_parsed_free(parsed);
-               parsed = NULL;
-       } else if (ret != EC_PARSED_NOMATCH) {
-               /* remove dummy root node */
-               child_parsed = ec_parsed_get_last_child(parsed);
-               ec_parsed_del_child(parsed, child_parsed);
-               ec_parsed_free(parsed);
-               parsed = child_parsed;
+               return NULL;
        }
 
        return parsed;
@@ -161,6 +185,64 @@ struct ec_parsed *ec_parsed(void)
        return NULL;
 }
 
+static struct ec_parsed *
+__ec_parsed_dup(const struct ec_parsed *root, const struct ec_parsed *ref,
+               struct ec_parsed **new_ref)
+{
+       struct ec_parsed *dup = NULL;
+       struct ec_parsed *child, *dup_child;
+       struct ec_keyval *attrs = NULL;
+
+       if (root == NULL)
+               return NULL;
+
+       dup = ec_parsed();
+       if (dup == NULL)
+               return NULL;
+
+       if (root == ref)
+               *new_ref = dup;
+
+       attrs = ec_keyval_dup(root->attrs);
+       if (attrs == NULL)
+               goto fail;
+       ec_keyval_free(dup->attrs);
+       dup->attrs = attrs;
+       dup->node = root->node;
+
+       if (root->strvec != NULL) {
+               dup->strvec = ec_strvec_dup(root->strvec);
+               if (dup->strvec == NULL)
+                       goto fail;
+       }
+
+       TAILQ_FOREACH(child, &root->children, next) {
+               dup_child = __ec_parsed_dup(child, ref, new_ref);
+               if (dup_child == NULL)
+                       goto fail;
+               ec_parsed_add_child(dup, dup_child);
+       }
+
+       return dup;
+
+fail:
+       ec_parsed_free(dup);
+       return NULL;
+}
+
+struct ec_parsed *ec_parsed_dup(struct ec_parsed *parsed) //XXX const
+{
+       struct ec_parsed *root, *dup_root, *dup = NULL;
+
+       root = ec_parsed_get_root(parsed);
+       dup_root = __ec_parsed_dup(root, parsed, &dup);
+       if (dup_root == NULL)
+               return NULL;
+       assert(dup != NULL);
+
+       return dup;
+}
+
 void ec_parsed_free_children(struct ec_parsed *parsed)
 {
        struct ec_parsed *child;
@@ -180,6 +262,10 @@ void ec_parsed_free(struct ec_parsed *parsed)
        if (parsed == NULL)
                return;
 
+       // assert(parsed->parent == NULL); XXX
+       // or
+       // parsed = ec_parsed_get_root(parsed);
+
        ec_parsed_free_children(parsed);
        ec_strvec_free(parsed->strvec);
        ec_keyval_free(parsed->attrs);
@@ -224,7 +310,12 @@ void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed)
                fprintf(out, "parsed is NULL, error in parse\n");
                return;
        }
-       if (!ec_parsed_matches(parsed)) {
+
+       /* only exist if it does not match (strvec == NULL) and if it
+        * does not have children: an incomplete parse, like those
+        * generated by complete() don't match but have children that
+        * may match. */
+       if (!ec_parsed_matches(parsed) && TAILQ_EMPTY(&parsed->children)) {
                fprintf(out, "no match\n");
                return;
        }
@@ -239,6 +330,7 @@ void ec_parsed_add_child(struct ec_parsed *parsed,
        child->parent = parsed;
 }
 
+// XXX we can remove the first arg ? parsed == child->parent ?
 void ec_parsed_del_child(struct ec_parsed *parsed, // XXX rename del in unlink?
        struct ec_parsed *child)
 {
@@ -247,11 +339,37 @@ void ec_parsed_del_child(struct ec_parsed *parsed, // XXX rename del in unlink?
 }
 
 struct ec_parsed *
-ec_parsed_get_last_child(struct ec_parsed *parsed)
+ec_parsed_get_first_child(const struct ec_parsed *parsed)
+{
+       return TAILQ_FIRST(&parsed->children);
+}
+
+struct ec_parsed *
+ec_parsed_get_last_child(const struct ec_parsed *parsed)
 {
        return TAILQ_LAST(&parsed->children, ec_parsed_list);
 }
 
+struct ec_parsed *ec_parsed_get_next(const struct ec_parsed *parsed)
+{
+       return TAILQ_NEXT(parsed, next);
+}
+
+bool ec_parsed_has_child(const struct ec_parsed *parsed)
+{
+       return !TAILQ_EMPTY(&parsed->children);
+}
+
+const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed)
+{
+       return parsed->node;
+}
+
+void ec_parsed_set_node(struct ec_parsed *parsed, const struct ec_node *node)
+{
+       parsed->node = node;
+}
+
 void ec_parsed_del_last_child(struct ec_parsed *parsed) // rename in free
 {
        struct ec_parsed *child;
@@ -310,7 +428,7 @@ const struct ec_strvec *ec_parsed_strvec(const struct ec_parsed *parsed)
        return parsed->strvec;
 }
 
-/* number of parsed strings in the vector */
+/* number of strings in the parsed vector */
 size_t ec_parsed_len(const struct ec_parsed *parsed)
 {
        if (parsed == NULL || parsed->strvec == NULL)
@@ -324,7 +442,7 @@ size_t ec_parsed_matches(const struct ec_parsed *parsed)
        if (parsed == NULL)
                return 0;
 
-       if (parsed->node == NULL && TAILQ_EMPTY(&parsed->children)) // XXX both needed?
+       if (parsed->strvec == NULL)
                return 0;
 
        return 1;