#include <ecoli_log.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
+#include <ecoli_parsed.h>
+#include <ecoli_completed.h>
#include <ecoli_node_or.h>
#include <ecoli_node_str.h>
#include <ecoli_test.h>
+EC_LOG_TYPE_REGISTER(node_or);
+
struct ec_node_or {
struct ec_node gen;
struct ec_node **table;
unsigned int len;
};
-static struct ec_parsed *ec_node_or_parse(const struct ec_node *gen_node,
- const struct ec_strvec *strvec)
+static int
+ec_node_or_parse(const struct ec_node *gen_node,
+ struct ec_parsed *state,
+ const struct ec_strvec *strvec)
{
struct ec_node_or *node = (struct ec_node_or *)gen_node;
- struct ec_parsed *parsed, *child_parsed = NULL;
- struct ec_strvec *match_strvec;
unsigned int i;
-
- parsed = ec_parsed();
- if (parsed == NULL)
- goto fail;
+ int ret;
for (i = 0; i < node->len; i++) {
- child_parsed = ec_node_parse_strvec(node->table[i], strvec);
- if (child_parsed == NULL)
- goto fail;
- if (ec_parsed_matches(child_parsed))
- break;
- ec_parsed_free(child_parsed);
- child_parsed = NULL;
+ ret = ec_node_parse_child(node->table[i], state, strvec);
+ if (ret == EC_PARSED_NOMATCH)
+ continue;
+ return ret;
}
- /* no match */
- if (i == node->len)
- return parsed;
-
- match_strvec = ec_strvec_dup(child_parsed->strvec);
- if (match_strvec == NULL)
- goto fail;
-
- ec_parsed_set_match(parsed, gen_node, match_strvec);
- ec_parsed_add_child(parsed, child_parsed);
-
- return parsed;
-
- fail:
- ec_parsed_free(child_parsed);
- ec_parsed_free(parsed);
- return NULL;
+ return EC_PARSED_NOMATCH;
}
-static struct ec_completed *ec_node_or_complete(const struct ec_node *gen_node,
- const struct ec_strvec *strvec)
+static int
+ec_node_or_complete(const struct ec_node *gen_node,
+ struct ec_completed *completed,
+ const struct ec_strvec *strvec)
{
struct ec_node_or *node = (struct ec_node_or *)gen_node;
- struct ec_completed *completed, *child_completed;
+ int ret;
size_t n;
- completed = ec_completed();
- if (completed == NULL)
- return NULL;
-
for (n = 0; n < node->len; n++) {
- child_completed = ec_node_complete_strvec(node->table[n],
- strvec);
+ ret = ec_node_complete_child(node->table[n],
+ completed, strvec);
+ if (ret < 0)
+ return ret;
+ }
- if (child_completed == NULL) // XXX fail instead?
- continue;
+ return 0;
+}
- ec_completed_merge(completed, child_completed);
+static size_t ec_node_or_get_max_parse_len(const struct ec_node *gen_node)
+{
+ struct ec_node_or *node = (struct ec_node_or *)gen_node;
+ size_t i, ret = 0, len;
+
+ for (i = 0; i < node->len; i++) {
+ len = ec_node_get_max_parse_len(node->table[i]);
+ if (len > ret)
+ ret = len;
}
- return completed;
+ return ret;
}
static void ec_node_or_free_priv(struct ec_node *gen_node)
if (child == NULL)
return -EINVAL;
- gen_node->flags &= ~EC_NODE_F_BUILT;
-
table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
if (table == NULL) {
ec_node_free(child);
.name = "or",
.parse = ec_node_or_parse,
.complete = ec_node_or_complete,
+ .get_max_parse_len = ec_node_or_get_max_parse_len,
.size = sizeof(struct ec_node_or),
.free_priv = ec_node_or_free_priv,
};
return NULL;
}
+/* LCOV_EXCL_START */
static int ec_node_or_testcase(void)
{
struct ec_node *node;
int ret = 0;
- node = EC_NODE_OR(NULL,
- ec_node_str(NULL, "foo"),
- ec_node_str(NULL, "bar")
+ node = EC_NODE_OR(EC_NO_ID,
+ ec_node_str(EC_NO_ID, "foo"),
+ ec_node_str(EC_NO_ID, "bar")
);
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, 1, "foo");
ec_node_free(node);
/* test completion */
- node = EC_NODE_OR(NULL,
- ec_node_str(NULL, "foo"),
- ec_node_str(NULL, "bar"),
- ec_node_str(NULL, "bar2"),
- ec_node_str(NULL, "toto"),
- ec_node_str(NULL, "titi")
+ node = EC_NODE_OR(EC_NO_ID,
+ ec_node_str(EC_NO_ID, "foo"),
+ ec_node_str(EC_NO_ID, "bar"),
+ ec_node_str(EC_NO_ID, "bar2"),
+ ec_node_str(EC_NO_ID, "toto"),
+ ec_node_str(EC_NO_ID, "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);
ret |= EC_TEST_CHECK_COMPLETE(node,
"b", EC_NODE_ENDLIST,
- "ar", "ar2", EC_NODE_ENDLIST,
- "ar");
+ "bar", "bar2", EC_NODE_ENDLIST);
ret |= EC_TEST_CHECK_COMPLETE(node,
"bar", EC_NODE_ENDLIST,
- "", "2", EC_NODE_ENDLIST,
- "");
+ "bar", "bar2", EC_NODE_ENDLIST);
ret |= EC_TEST_CHECK_COMPLETE(node,
"t", EC_NODE_ENDLIST,
- "oto", "iti", EC_NODE_ENDLIST,
- "");
+ "toto", "titi", EC_NODE_ENDLIST);
ret |= EC_TEST_CHECK_COMPLETE(node,
"to", EC_NODE_ENDLIST,
- "to", EC_NODE_ENDLIST,
- "to");
+ "toto", EC_NODE_ENDLIST);
ret |= EC_TEST_CHECK_COMPLETE(node,
"x", EC_NODE_ENDLIST,
- EC_NODE_ENDLIST,
- "");
+ EC_NODE_ENDLIST);
ec_node_free(node);
return ret;
}
+/* LCOV_EXCL_STOP */
static struct ec_test ec_node_or_test = {
.name = "node_or",