2 * Copyright (c) 2016, 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.
34 #include <ecoli_malloc.h>
35 #include <ecoli_log.h>
36 #include <ecoli_strvec.h>
38 #include <ecoli_tk_or.h>
39 #include <ecoli_tk_str.h>
40 #include <ecoli_test.h>
48 static struct ec_parsed_tk *ec_tk_or_parse(const struct ec_tk *gen_tk,
49 const struct ec_strvec *strvec)
51 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
52 struct ec_parsed_tk *parsed_tk, *child_parsed_tk = NULL;
53 struct ec_strvec *match_strvec;
56 parsed_tk = ec_parsed_tk_new();
57 if (parsed_tk == NULL)
60 for (i = 0; i < tk->len; i++) {
61 child_parsed_tk = ec_tk_parse_tokens(tk->table[i], strvec);
62 if (child_parsed_tk == NULL)
64 if (ec_parsed_tk_matches(child_parsed_tk))
66 ec_parsed_tk_free(child_parsed_tk);
67 child_parsed_tk = NULL;
74 match_strvec = ec_strvec_dup(child_parsed_tk->strvec);
75 if (match_strvec == NULL)
78 ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
79 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
84 ec_parsed_tk_free(child_parsed_tk);
85 ec_parsed_tk_free(parsed_tk);
89 static struct ec_completed_tk *ec_tk_or_complete(const struct ec_tk *gen_tk,
90 const struct ec_strvec *strvec)
92 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
93 struct ec_completed_tk *completed_tk, *child_completed_tk;
96 completed_tk = ec_completed_tk_new();
97 if (completed_tk == NULL)
100 for (n = 0; n < tk->len; n++) {
101 child_completed_tk = ec_tk_complete_tokens(tk->table[n],
104 if (child_completed_tk == NULL) // XXX fail instead?
107 ec_completed_tk_merge(completed_tk, child_completed_tk);
113 static void ec_tk_or_free_priv(struct ec_tk *gen_tk)
115 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
118 for (i = 0; i < tk->len; i++)
119 ec_tk_free(tk->table[i]);
123 static struct ec_tk_ops ec_tk_or_ops = {
125 .parse = ec_tk_or_parse,
126 .complete = ec_tk_or_complete,
127 .free_priv = ec_tk_or_free_priv,
130 struct ec_tk *ec_tk_or_new(const char *id)
132 struct ec_tk_or *tk = NULL;
134 tk = (struct ec_tk_or *)ec_tk_new(id, &ec_tk_or_ops, sizeof(*tk));
144 struct ec_tk *ec_tk_or_new_list(const char *id, ...)
146 struct ec_tk_or *tk = NULL;
153 tk = (struct ec_tk_or *)ec_tk_or_new(id);
157 for (child = va_arg(ap, struct ec_tk *);
158 child != EC_TK_ENDLIST;
159 child = va_arg(ap, struct ec_tk *)) {
160 /* on error, don't quit the loop to avoid leaks */
162 if (child == NULL || ec_tk_or_add(&tk->gen, child) < 0)
173 ec_tk_free(&tk->gen); /* will also free children */
178 int ec_tk_or_add(struct ec_tk *gen_tk, struct ec_tk *child)
180 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
181 struct ec_tk **table;
184 assert(child != NULL);
186 table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
191 table[tk->len] = child;
194 child->parent = gen_tk;
195 TAILQ_INSERT_TAIL(&gen_tk->children, child, next);
200 static int ec_tk_or_testcase(void)
205 tk = ec_tk_or_new_list(NULL,
206 ec_tk_str_new(NULL, "foo"),
207 ec_tk_str_new(NULL, "bar"),
210 ec_log(EC_LOG_ERR, "cannot create tk\n");
213 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", EC_TK_ENDLIST);
214 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "bar", EC_TK_ENDLIST);
215 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "bar", EC_TK_ENDLIST);
216 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " ", EC_TK_ENDLIST);
217 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox", EC_TK_ENDLIST);
218 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "toto", EC_TK_ENDLIST);
219 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "", EC_TK_ENDLIST);
222 /* test completion */
223 tk = ec_tk_or_new_list(NULL,
224 ec_tk_str_new(NULL, "foo"),
225 ec_tk_str_new(NULL, "bar"),
226 ec_tk_str_new(NULL, "bar2"),
227 ec_tk_str_new(NULL, "toto"),
228 ec_tk_str_new(NULL, "titi"),
231 ec_log(EC_LOG_ERR, "cannot create tk\n");
234 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
236 "foo", "bar", "bar2", "toto", "titi", EC_TK_ENDLIST,
238 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
242 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
244 "ar", "ar2", EC_TK_ENDLIST,
246 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
247 "bar", EC_TK_ENDLIST,
248 "", "2", EC_TK_ENDLIST,
250 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
252 "oto", "iti", EC_TK_ENDLIST,
254 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
258 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
267 static struct ec_test ec_tk_or_test = {
269 .test = ec_tk_or_testcase,
272 EC_REGISTER_TEST(ec_tk_or_test);