#include <assert.h>
#include <errno.h>
+#include <ecoli_assert.h>
#include <ecoli_malloc.h>
#include <ecoli_strvec.h>
#include <ecoli_keyval.h>
struct ec_keyval *attrs;
};
-int ec_node_parse_child(struct ec_node *node, struct ec_parsed *state,
- const struct ec_strvec *strvec)
+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) {
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;
+ ec_parsed_add_child(state, child);
+ } else {
+ child = state;
+ }
ec_parsed_set_node(child, node);
- ec_parsed_add_child(state, child);
ret = node->type->parse(node, child, strvec);
- if (ret < 0)
+ if (ret < 0) // XXX should we free state, child?
return ret;
if (ret == EC_PARSED_NOMATCH) {
- ec_parsed_del_child(state, child);
- assert(TAILQ_EMPTY(&child->children));
- ec_parsed_free(child);
+ if (!is_root) {
+ ec_parsed_del_child(state, child);
+ assert(TAILQ_EMPTY(&child->children));
+ ec_parsed_free(child);
+ }
return ret;
}
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);
+ ret = __ec_node_parse_child(node, parsed, true, strvec);
if (ret < 0) {
ec_parsed_free(parsed);
return NULL;
}
- 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 parsed;
}
return NULL;
}
-struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed)
+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 (parsed == NULL)
+ if (root == NULL)
return NULL;
dup = ec_parsed();
if (dup == NULL)
return NULL;
- attrs = ec_keyval_dup(parsed->attrs);
+ 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 (parsed->strvec != NULL) {
- dup->strvec = ec_strvec_dup(parsed->strvec);
+ if (root->strvec != NULL) {
+ dup->strvec = ec_strvec_dup(root->strvec);
if (dup->strvec == NULL)
goto fail;
}
- TAILQ_FOREACH(child, &parsed->children, next) {
- dup_child = ec_parsed_dup(child);
+ 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 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;
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);
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;
}
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)
{
if (parsed == NULL)
return 0;
- if (parsed->node == NULL && TAILQ_EMPTY(&parsed->children)) // XXX both needed?
+ if (parsed->strvec == NULL)
return 0;
return 1;