2 * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <ecoli_malloc.h>
37 #include <ecoli_log.h>
38 #include <ecoli_strvec.h>
40 #include <ecoli_tk_subset.h>
41 #include <ecoli_tk_str.h>
42 #include <ecoli_tk_or.h>
43 #include <ecoli_test.h>
52 struct ec_parsed_tk **parsed_table; /* list of parsed tk */
53 size_t parsed_table_len; /* number of parsed tk */
54 size_t len; /* consumed strings */
57 static int __ec_tk_subset_parse(struct ec_tk **table, size_t table_len,
58 const struct ec_strvec *strvec,
59 struct parse_result *out)
61 struct ec_tk **child_table;
62 struct ec_parsed_tk *child_parsed_tk = NULL;
63 struct ec_strvec *childvec = NULL;
65 struct parse_result best_result, result;
71 memset(&best_result, 0, sizeof(best_result));
72 best_result.parsed_table =
73 ec_calloc(table_len, sizeof(*best_result.parsed_table[0]));
74 if (best_result.parsed_table == NULL)
77 child_table = ec_calloc(table_len - 1, sizeof(*child_table));
78 if (child_table == NULL)
81 for (i = 0; i < table_len; i++) {
82 /* try to parse elt i */
83 child_parsed_tk = ec_tk_parse_tokens(table[i], strvec);
84 if (child_parsed_tk == NULL)
87 if (!ec_parsed_tk_matches(child_parsed_tk)) {
88 ec_parsed_tk_free(child_parsed_tk);
89 child_parsed_tk = NULL;
93 /* build a new table without elt i */
94 for (j = 0; j < table_len; j++) {
96 child_table[j] = table[j];
98 child_table[j - 1] = table[j];
101 /* build a new strvec */
102 len = ec_parsed_tk_len(child_parsed_tk);
103 childvec = ec_strvec_ndup(strvec, len,
104 ec_strvec_len(strvec) - len);
105 if (childvec == NULL)
108 memset(&result, 0, sizeof(result));
110 ret = __ec_tk_subset_parse(child_table, table_len - 1,
112 ec_strvec_free(childvec);
117 /* if result is not the best, ignore */
118 if (result.parsed_table_len + 1 <=
119 best_result.parsed_table_len) {
120 ec_parsed_tk_free(child_parsed_tk);
121 child_parsed_tk = NULL;
122 for (j = 0; j < result.parsed_table_len; j++)
123 ec_parsed_tk_free(result.parsed_table[j]);
124 ec_free(result.parsed_table);
125 memset(&result, 0, sizeof(result));
129 /* replace the previous best result */
130 for (j = 0; j < best_result.parsed_table_len; j++)
131 ec_parsed_tk_free(best_result.parsed_table[j]);
132 best_result.parsed_table[0] = child_parsed_tk;
133 child_parsed_tk = NULL;
134 for (j = 0; j < result.parsed_table_len; j++)
135 best_result.parsed_table[j+1] = result.parsed_table[j];
136 best_result.parsed_table_len = result.parsed_table_len + 1;
137 best_result.len = len + result.len;
138 ec_free(result.parsed_table);
142 ec_free(child_table);
147 ec_parsed_tk_free(child_parsed_tk);
148 ec_strvec_free(childvec);
149 for (j = 0; j < best_result.parsed_table_len; j++)
150 ec_parsed_tk_free(best_result.parsed_table[j]);
151 ec_free(best_result.parsed_table);
152 ec_free(child_table);
156 static struct ec_parsed_tk *ec_tk_subset_parse(const struct ec_tk *gen_tk,
157 const struct ec_strvec *strvec)
159 struct ec_tk_subset *tk = (struct ec_tk_subset *)gen_tk;
160 struct ec_parsed_tk *parsed_tk = NULL;
161 struct parse_result result;
162 struct ec_strvec *match_strvec;
166 memset(&result, 0, sizeof(result));
168 parsed_tk = ec_parsed_tk_new();
169 if (parsed_tk == NULL)
172 ret = __ec_tk_subset_parse(tk->table, tk->len, strvec, &result);
176 if (result.parsed_table_len == 0) {
177 ec_free(result.parsed_table);
181 for (i = 0; i < result.parsed_table_len; i++) {
182 ec_parsed_tk_add_child(parsed_tk, result.parsed_table[i]);
183 result.parsed_table[i] = NULL;
185 ec_free(result.parsed_table);
186 result.parsed_table = NULL;
188 match_strvec = ec_strvec_ndup(strvec, 0, result.len);
189 if (match_strvec == NULL)
192 ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
197 for (i = 0; i < result.parsed_table_len; i++)
198 ec_parsed_tk_free(result.parsed_table[i]);
199 ec_free(result.parsed_table);
200 ec_parsed_tk_free(parsed_tk);
205 static struct ec_completed_tk *
206 __ec_tk_subset_complete(struct ec_tk **table, size_t table_len,
207 const struct ec_strvec *strvec)
209 struct ec_completed_tk *completed_tk = NULL;
210 struct ec_completed_tk *child_completed_tk = NULL;
211 struct ec_strvec *childvec = NULL;
212 struct ec_parsed_tk *parsed_tk = NULL;
217 * example with table = [a, b, c]
218 * subset_complete([a,b,c], strvec) returns:
219 * complete(a, strvec) + complete(b, strvec) + complete(c, strvec) +
220 * + __subset_complete([b, c], childvec) if a matches
221 * + __subset_complete([a, c], childvec) if b matches
222 * + __subset_complete([a, b], childvec) if c matches
225 completed_tk = ec_completed_tk_new();
226 if (completed_tk == NULL)
229 /* first, try to complete with each token of the table */
230 for (i = 0; i < table_len; i++) {
231 if (table[i] == NULL)
234 child_completed_tk = ec_tk_complete_tokens(table[i],
237 if (child_completed_tk == NULL)
240 ec_completed_tk_merge(completed_tk, child_completed_tk);
241 child_completed_tk = NULL;
244 /* then, if a token matches, advance in strvec and try to complete with
245 * all the other tokens */
246 for (i = 0; i < table_len; i++) {
247 if (table[i] == NULL)
250 parsed_tk = ec_tk_parse_tokens(table[i], strvec);
251 if (parsed_tk == NULL)
254 if (!ec_parsed_tk_matches(parsed_tk)) {
255 ec_parsed_tk_free(parsed_tk);
260 len = ec_parsed_tk_len(parsed_tk);
261 childvec = ec_strvec_ndup(strvec, len,
262 ec_strvec_len(strvec) - len);
263 if (childvec == NULL)
268 child_completed_tk = __ec_tk_subset_complete(table,
269 table_len, childvec);
271 ec_strvec_free(childvec);
274 if (child_completed_tk == NULL)
277 ec_completed_tk_merge(completed_tk, child_completed_tk);
278 child_completed_tk = NULL;
280 ec_parsed_tk_free(parsed_tk);
286 ec_parsed_tk_free(parsed_tk);
287 ec_completed_tk_free(child_completed_tk);
288 ec_completed_tk_free(completed_tk);
293 static struct ec_completed_tk *ec_tk_subset_complete(const struct ec_tk *gen_tk,
294 const struct ec_strvec *strvec)
296 struct ec_tk_subset *tk = (struct ec_tk_subset *)gen_tk;
298 return __ec_tk_subset_complete(tk->table, tk->len, strvec);
301 static void ec_tk_subset_free_priv(struct ec_tk *gen_tk)
303 struct ec_tk_subset *tk = (struct ec_tk_subset *)gen_tk;
306 for (i = 0; i < tk->len; i++)
307 ec_tk_free(tk->table[i]);
311 int ec_tk_subset_add(struct ec_tk *gen_tk, struct ec_tk *child)
313 struct ec_tk_subset *tk = (struct ec_tk_subset *)gen_tk;
314 struct ec_tk **table;
321 gen_tk->flags &= ~EC_TK_F_BUILT;
323 table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
330 table[tk->len] = child;
333 child->parent = gen_tk;
334 TAILQ_INSERT_TAIL(&gen_tk->children, child, next);
339 static struct ec_tk_type ec_tk_subset_type = {
341 .parse = ec_tk_subset_parse,
342 .complete = ec_tk_subset_complete,
343 .free_priv = ec_tk_subset_free_priv,
346 EC_TK_TYPE_REGISTER(ec_tk_subset_type);
348 struct ec_tk *ec_tk_subset(const char *id)
350 struct ec_tk *gen_tk = NULL;
351 struct ec_tk_subset *tk = NULL;
353 gen_tk = ec_tk_new(id, &ec_tk_subset_type, sizeof(*tk));
357 tk = (struct ec_tk_subset *)gen_tk;
364 struct ec_tk *__ec_tk_subset(const char *id, ...)
366 struct ec_tk *gen_tk = NULL;
367 struct ec_tk_subset *tk = NULL;
374 gen_tk = ec_tk_subset(id);
375 tk = (struct ec_tk_subset *)gen_tk;
379 for (child = va_arg(ap, struct ec_tk *);
380 child != EC_TK_ENDLIST;
381 child = va_arg(ap, struct ec_tk *)) {
383 /* on error, don't quit the loop to avoid leaks */
384 if (fail == 1 || child == NULL ||
385 ec_tk_subset_add(gen_tk, child) < 0) {
398 ec_tk_free(gen_tk); /* will also free children */
403 static int ec_tk_subset_testcase(void)
408 tk = EC_TK_SUBSET(NULL,
410 ec_tk_str(NULL, "foo"),
411 ec_tk_str(NULL, "bar")),
412 ec_tk_str(NULL, "bar"),
413 ec_tk_str(NULL, "toto")
416 ec_log(EC_LOG_ERR, "cannot create tk\n");
419 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo");
420 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "bar");
421 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar", "titi");
422 ret |= EC_TEST_CHECK_TK_PARSE(tk, 3, "bar", "foo", "toto");
423 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "foo");
424 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "bar", "bar");
425 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "bar", "foo");
426 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " ");
427 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox");
428 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "titi");
429 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "");
432 /* test completion */
433 tk = EC_TK_SUBSET(NULL,
434 ec_tk_str(NULL, "foo"),
435 ec_tk_str(NULL, "bar"),
436 ec_tk_str(NULL, "bar2"),
437 ec_tk_str(NULL, "toto"),
438 ec_tk_str(NULL, "titi")
441 ec_log(EC_LOG_ERR, "cannot create tk\n");
444 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
446 "foo", "bar", "bar2", "toto", "titi", EC_TK_ENDLIST,
448 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
449 "bar", "bar2", "", EC_TK_ENDLIST,
450 "foo", "toto", "titi", EC_TK_ENDLIST,
452 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
456 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
458 "ar", "ar2", EC_TK_ENDLIST,
460 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
461 "bar", EC_TK_ENDLIST,
462 "", "2", EC_TK_ENDLIST,
464 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
465 "bar", "b", EC_TK_ENDLIST,
466 "ar2", EC_TK_ENDLIST,
468 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
470 "oto", "iti", EC_TK_ENDLIST,
472 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
476 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
485 static struct ec_test ec_tk_subset_test = {
487 .test = ec_tk_subset_testcase,
490 EC_TEST_REGISTER(ec_tk_subset_test);