rename default complete as complete unknown
[protos/libecoli.git] / lib / ecoli_parsed.c
index b4b59e8..c2e7c8e 100644 (file)
@@ -1,28 +1,5 @@
-/*
- * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in the
- *       documentation and/or other materials provided with the distribution.
- *     * Neither the name of the University of California, Berkeley nor the
- *       names of its contributors may be used to endorse or promote products
- *       derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
  */
 
 #include <stdio.h>
 
 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;
@@ -74,7 +44,7 @@ static int __ec_node_parse_child(const struct ec_node *node,
                if (child == NULL)
                        return -ENOMEM;
 
-               ec_parsed_add_child(state, child);
+               ec_parsed_link_child(state, child);
        } else {
                child = state;
        }
@@ -94,7 +64,7 @@ static int __ec_node_parse_child(const struct ec_node *node,
 
 free:
        if (!is_root) {
-               ec_parsed_del_child(state, child);
+               ec_parsed_unlink_child(state, child);
                ec_parsed_free(child);
        }
        return ret;
@@ -210,7 +180,7 @@ __ec_parsed_dup(const struct ec_parsed *root, const struct ec_parsed *ref,
                dup_child = __ec_parsed_dup(child, ref, new_ref);
                if (dup_child == NULL)
                        goto fail;
-               ec_parsed_add_child(dup, dup_child);
+               ec_parsed_link_child(dup, dup_child);
        }
 
        return dup;
@@ -220,9 +190,10 @@ fail:
        return NULL;
 }
 
-struct ec_parsed *ec_parsed_dup(struct ec_parsed *parsed) //XXX const
+struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed)
 {
-       struct ec_parsed *root, *dup_root, *dup = NULL;
+       const struct ec_parsed *root;
+       struct ec_parsed *dup_root, *dup = NULL;
 
        root = ec_parsed_get_root(parsed);
        dup_root = __ec_parsed_dup(root, parsed, &dup);
@@ -243,6 +214,7 @@ void ec_parsed_free_children(struct ec_parsed *parsed)
        while (!TAILQ_EMPTY(&parsed->children)) {
                child = TAILQ_FIRST(&parsed->children);
                TAILQ_REMOVE(&parsed->children, child, next);
+               child->parent = NULL;
                ec_parsed_free(child);
        }
 }
@@ -252,9 +224,8 @@ 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_assert_print(parsed->parent == NULL,
+                       "parent not NULL in ec_parsed_free()");
 
        ec_parsed_free_children(parsed);
        ec_strvec_free(parsed->strvec);
@@ -267,34 +238,26 @@ static void __ec_parsed_dump(FILE *out,
 {
        struct ec_parsed *child;
        const struct ec_strvec *vec;
-       size_t i;
-       const char *id = "none", *typename = "none";
+       const char *id, *typename = "none";
 
+       /* node can be null when parsing is incomplete */
        if (parsed->node != NULL) {
-               if (parsed->node->id != NULL)
-                       id = parsed->node->id;
+               id = parsed->node->id;
                typename = parsed->node->type->name;
        }
 
-       /* XXX enhance */
-       for (i = 0; i < indent; i++) {
-               if (i % 2)
-                       fprintf(out, " ");
-               else
-                       fprintf(out, "|");
-       }
-
-       fprintf(out, "node_type=%s id=%s vec=", typename, id);
+       fprintf(out, "%*s" "type=%s id=%s vec=",
+               (int)indent * 4, "", typename, id);
        vec = ec_parsed_strvec(parsed);
        ec_strvec_dump(out, vec);
 
        TAILQ_FOREACH(child, &parsed->children, next)
-               __ec_parsed_dump(out, child, indent + 2);
+               __ec_parsed_dump(out, child, indent + 1);
 }
 
 void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed)
 {
-       fprintf(out, "------------------- parsed dump:\n"); //XXX
+       fprintf(out, "------------------- parsed dump:\n");
 
        if (parsed == NULL) {
                fprintf(out, "parsed is NULL, error in parse\n");
@@ -313,15 +276,14 @@ void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed)
        __ec_parsed_dump(out, parsed, 0);
 }
 
-void ec_parsed_add_child(struct ec_parsed *parsed,
+void ec_parsed_link_child(struct ec_parsed *parsed,
        struct ec_parsed *child)
 {
        TAILQ_INSERT_TAIL(&parsed->children, child, next);
        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?
+void ec_parsed_unlink_child(struct ec_parsed *parsed,
        struct ec_parsed *child)
 {
        TAILQ_REMOVE(&parsed->children, child, next);
@@ -355,16 +317,16 @@ const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed)
        return parsed->node;
 }
 
-void ec_parsed_del_last_child(struct ec_parsed *parsed) // rename in free
+void ec_parsed_del_last_child(struct ec_parsed *parsed)
 {
        struct ec_parsed *child;
 
        child = ec_parsed_get_last_child(parsed);
-       ec_parsed_del_child(parsed, child);
+       ec_parsed_unlink_child(parsed, child);
        ec_parsed_free(child);
 }
 
-struct ec_parsed *ec_parsed_get_root(struct ec_parsed *parsed)
+struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed)
 {
        if (parsed == NULL)
                return NULL;
@@ -375,7 +337,7 @@ struct ec_parsed *ec_parsed_get_root(struct ec_parsed *parsed)
        return parsed;
 }
 
-struct ec_parsed *ec_parsed_get_parent(struct ec_parsed *parsed)
+struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed)
 {
        if (parsed == NULL)
                return NULL;