From f4a9b5acce6de30a5c972bafcedf4447636cb74e Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Thu, 15 Jun 2017 19:34:20 +0200 Subject: [PATCH] save --- lib/ecoli_tk.h | 1 + lib/ecoli_tk_cmd.c | 18 ++++++++++++------ lib/ecoli_tk_empty.c | 6 +++--- lib/ecoli_tk_empty.h | 6 +++++- lib/ecoli_tk_subset.c | 12 ++++++++---- lib/todo.txt | 2 +- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/ecoli_tk.h b/lib/ecoli_tk.h index f420a52..6aa1c77 100644 --- a/lib/ecoli_tk.h +++ b/lib/ecoli_tk.h @@ -118,6 +118,7 @@ struct ec_tk { struct ec_tk *ec_tk_new(const char *id, const struct ec_tk_type *type, size_t priv_size); + void ec_tk_free(struct ec_tk *tk); /* XXX add more accessors */ diff --git a/lib/ecoli_tk_cmd.c b/lib/ecoli_tk_cmd.c index f5f1575..2f702ee 100644 --- a/lib/ecoli_tk_cmd.c +++ b/lib/ecoli_tk_cmd.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -168,9 +169,12 @@ ec_tk_cmd_eval_bin_op(void **result, void *userctx, void *operand1, ec_tk_free(in2); *result = out; } else if (!strcmp(ec_strvec_val(vec, 0), ",")) { + out = EC_TK_SUBSET(NULL, ec_tk_clone(in1), ec_tk_clone(in2)); + if (out == NULL) + return -EINVAL; ec_tk_free(in1); ec_tk_free(in2); - *result = NULL; //XXX + *result = out; } else { return -EINVAL; } @@ -201,6 +205,8 @@ ec_tk_cmd_eval_parenthesis(void **result, void *userctx, if (out == NULL) return -EINVAL; ec_tk_free(in); + } else if (!strcmp(ec_strvec_val(vec, 0), "(")) { + out = in; } else { return -EINVAL; } @@ -471,7 +477,7 @@ static int ec_tk_cmd_testcase(void) int ret = 0; tk = EC_TK_CMD(NULL, - "add [toto] x | y", + "command [option] (subset1, subset2) x | y", ec_tk_int("x", 0, 10, 10), ec_tk_int("y", 20, 30, 10) ); @@ -479,10 +485,10 @@ static int ec_tk_cmd_testcase(void) ec_log(EC_LOG_ERR, "cannot create tk\n"); return -1; } - ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "add", "1"); - ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "add", "23"); - ret |= EC_TEST_CHECK_TK_PARSE(tk, 3, "add", "toto", "23"); - ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "add", "15"); + ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "command", "1"); + ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "command", "23"); + ret |= EC_TEST_CHECK_TK_PARSE(tk, 3, "command", "option", "23"); + ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "command", "15"); ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo"); ec_tk_free(tk); diff --git a/lib/ecoli_tk_empty.c b/lib/ecoli_tk_empty.c index 9077c44..be487d0 100644 --- a/lib/ecoli_tk_empty.c +++ b/lib/ecoli_tk_empty.c @@ -73,7 +73,7 @@ static struct ec_tk_type ec_tk_empty_type = { EC_TK_TYPE_REGISTER(ec_tk_empty_type); -struct ec_tk *ec_tk_empty_new(const char *id) +struct ec_tk *ec_tk_empty(const char *id) { return ec_tk_new(id, &ec_tk_empty_type, sizeof(struct ec_tk_empty)); @@ -84,7 +84,7 @@ static int ec_tk_empty_testcase(void) struct ec_tk *tk; int ret = 0; - tk = ec_tk_empty_new(NULL); + tk = ec_tk_empty(NULL); if (tk == NULL) { ec_log(EC_LOG_ERR, "cannot create tk\n"); return -1; @@ -95,7 +95,7 @@ static int ec_tk_empty_testcase(void) ec_tk_free(tk); /* never completes */ - tk = ec_tk_empty_new(NULL); + tk = ec_tk_empty(NULL); if (tk == NULL) { ec_log(EC_LOG_ERR, "cannot create tk\n"); return -1; diff --git a/lib/ecoli_tk_empty.h b/lib/ecoli_tk_empty.h index 37c0eef..6482c4b 100644 --- a/lib/ecoli_tk_empty.h +++ b/lib/ecoli_tk_empty.h @@ -25,9 +25,13 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/** + * This token always matches an empty string vector + */ + #ifndef ECOLI_TK_EMPTY_ #define ECOLI_TK_EMPTY_ -struct ec_tk *ec_tk_empty_new(const char *id); +struct ec_tk *ec_tk_empty(const char *id); #endif diff --git a/lib/ecoli_tk_subset.c b/lib/ecoli_tk_subset.c index b6773d2..b73eb04 100644 --- a/lib/ecoli_tk_subset.c +++ b/lib/ecoli_tk_subset.c @@ -173,8 +173,13 @@ static struct ec_parsed_tk *ec_tk_subset_parse(const struct ec_tk *gen_tk, if (ret < 0) goto fail; + /* if no child tk matches, return a matching empty strvec */ if (result.parsed_table_len == 0) { ec_free(result.parsed_table); + match_strvec = ec_strvec_new(); + if (match_strvec == NULL) + goto fail; + ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec); return parsed_tk; } @@ -416,6 +421,7 @@ static int ec_tk_subset_testcase(void) ec_log(EC_LOG_ERR, "cannot create tk\n"); return -1; } + ret |= EC_TEST_CHECK_TK_PARSE(tk, 0); ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo"); ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "bar"); ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar", "titi"); @@ -423,10 +429,8 @@ static int ec_tk_subset_testcase(void) ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "foo"); ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "bar", "bar"); ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "bar", "foo"); - ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " "); - ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox"); - ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "titi"); - ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, ""); + ret |= EC_TEST_CHECK_TK_PARSE(tk, 0, " "); + ret |= EC_TEST_CHECK_TK_PARSE(tk, 0, "foox"); ec_tk_free(tk); /* test completion */ diff --git a/lib/todo.txt b/lib/todo.txt index a82ba6c..f90f008 100644 --- a/lib/todo.txt +++ b/lib/todo.txt @@ -4,7 +4,7 @@ tk_cmd X evaluate expression tree in ec_tk_expr - cmd token - example -- tk_re +X tk_re cleanup ======= -- 2.39.5