#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
+#include <ecoli_parsed.h>
+#include <ecoli_completed.h>
#include <ecoli_node_str.h>
+EC_LOG_TYPE_REGISTER(node_str);
+
struct ec_node_str {
struct ec_node gen;
char *string;
unsigned len;
};
-static struct ec_parsed *ec_node_str_parse(const struct ec_node *gen_node,
- const struct ec_strvec *strvec)
+static int
+ec_node_str_parse(const struct ec_node *gen_node,
+ struct ec_parsed *state,
+ const struct ec_strvec *strvec)
{
struct ec_node_str *node = (struct ec_node_str *)gen_node;
- struct ec_strvec *match_strvec;
- struct ec_parsed *parsed = NULL;
const char *str;
- parsed = ec_parsed_new();
- if (parsed == NULL)
- goto fail;
+ (void)state;
if (ec_strvec_len(strvec) == 0)
- return parsed;
+ return EC_PARSED_NOMATCH;
str = ec_strvec_val(strvec, 0);
if (strcmp(str, node->string) != 0)
- return parsed;
-
- match_strvec = ec_strvec_ndup(strvec, 0, 1);
- if (match_strvec == NULL)
- goto fail;
-
- ec_parsed_set_match(parsed, gen_node, match_strvec);
-
- return parsed;
+ return EC_PARSED_NOMATCH;
- fail:
- ec_parsed_free(parsed);
- return NULL;
+ return 1;
}
-static struct ec_completed *ec_node_str_complete(const struct ec_node *gen_node,
- const struct ec_strvec *strvec)
+static int
+ec_node_str_complete(const struct ec_node *gen_node,
+ struct ec_completed *completed,
+ struct ec_parsed *parsed,
+ const struct ec_strvec *strvec)
{
+ struct ec_completed_item *item = NULL;
struct ec_node_str *node = (struct ec_node_str *)gen_node;
- struct ec_completed *completed;
- struct ec_completed_elt *completed_elt;
- const char *str, *add;
+ const char *str;
size_t n = 0;
+ int ret;
- completed = ec_completed_new();
- if (completed == NULL)
- return NULL;
+ (void)parsed;
if (ec_strvec_len(strvec) != 1)
- return completed;
+ return 0;
str = ec_strvec_val(strvec, 0);
for (n = 0; n < node->len; n++) {
break;
}
+ /* no completion */
if (str[n] != '\0')
- add = NULL;
- else
- add = node->string + n;
-
- completed_elt = ec_completed_elt_new(gen_node, add);
- if (completed_elt == NULL) {
- ec_completed_free(completed);
- return NULL;
- }
+ return 0; // XXX add a no_match instead?
- ec_completed_add_elt(completed, completed_elt);
+ item = ec_completed_item(parsed, gen_node);
+ if (item == NULL)
+ return -ENOMEM;
+ ret = ec_completed_item_set(item, EC_MATCH, node->string);
+ if (ret < 0) {
+ ec_completed_item_free(item);
+ return ret;
+ }
+ ret = ec_completed_item_add(completed, item);
+ if (ret < 0) {
+ ec_completed_item_free(item);
+ return ret;
+ }
- return completed;
+ return 0;
}
static const char *ec_node_str_desc(const struct ec_node *gen_node)
ec_free(node->string);
}
+static size_t ec_node_str_get_max_parse_len(const struct ec_node *gen_node)
+{
+ (void)gen_node;
+ return 1;
+}
+
static struct ec_node_type ec_node_str_type = {
.name = "str",
.parse = ec_node_str_parse,
.complete = ec_node_str_complete,
+ .get_max_parse_len = ec_node_str_get_max_parse_len,
.desc = ec_node_str_desc,
.size = sizeof(struct ec_node_str),
.free_priv = ec_node_str_free_priv,
{
struct ec_node *gen_node = NULL;
- gen_node = __ec_node_new(&ec_node_str_type, id);
+ gen_node = __ec_node(&ec_node_str_type, id);
if (gen_node == NULL)
goto fail;
return NULL;
}
+/* LCOV_EXCL_START */
static int ec_node_str_testcase(void)
{
struct ec_node *node;
/* XXX use EC_NO_ID instead of NULL */
node = ec_node_str(NULL, "foo");
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");
node = ec_node_str(NULL, "Здравствуйте");
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, "Здравствуйте");
/* an empty string node always matches */
node = ec_node_str(NULL, "");
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, "");
/* test completion */
node = ec_node_str(NULL, "foo");
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,
"foo");
ret |= EC_TEST_CHECK_COMPLETE(node,
"f", EC_NODE_ENDLIST,
- "oo", EC_NODE_ENDLIST,
- "oo");
+ "foo", EC_NODE_ENDLIST,
+ "foo");
ret |= EC_TEST_CHECK_COMPLETE(node,
"foo", EC_NODE_ENDLIST,
- "", EC_NODE_ENDLIST,
- "");
+ "foo", EC_NODE_ENDLIST,
+ "foo");
ret |= EC_TEST_CHECK_COMPLETE(node,
"x", EC_NODE_ENDLIST,
EC_NODE_ENDLIST,
return ret;
}
+/* LCOV_EXCL_STOP */
static struct ec_test ec_node_str_test = {
.name = "node_str",