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>
37 #include <ecoli_tk_or.h>
38 #include <ecoli_tk_str.h>
39 #include <ecoli_test.h>
41 static struct ec_parsed_tk *parse(const struct ec_tk *gen_tk,
44 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
45 struct ec_parsed_tk *parsed_tk, *child_parsed_tk;
48 parsed_tk = ec_parsed_tk_new(gen_tk);
49 if (parsed_tk == NULL)
52 for (i = 0; i < tk->len; i++) {
53 child_parsed_tk = ec_tk_parse(tk->table[i], str);
54 if (child_parsed_tk != NULL)
58 if (child_parsed_tk == NULL)
61 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
63 parsed_tk->str = ec_strndup(child_parsed_tk->str,
64 strlen(child_parsed_tk->str));
69 ec_parsed_tk_free(parsed_tk);
73 static struct ec_completed_tk *complete(const struct ec_tk *gen_tk,
76 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
77 struct ec_completed_tk *completed_tk = NULL, *child_completed_tk;
80 for (n = 0; n < tk->len; n++) {
81 child_completed_tk = ec_tk_complete(tk->table[n], str);
83 if (child_completed_tk == NULL)
86 completed_tk = ec_completed_tk_merge(completed_tk,
93 static void free_priv(struct ec_tk *gen_tk)
95 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
98 for (i = 0; i < tk->len; i++)
99 ec_tk_free(tk->table[i]);
103 static struct ec_tk_ops or_ops = {
105 .complete = complete,
106 .free_priv = free_priv,
109 struct ec_tk *ec_tk_or_new(const char *id)
111 struct ec_tk_or *tk = NULL;
113 tk = (struct ec_tk_or *)ec_tk_new(id, &or_ops, sizeof(*tk));
123 struct ec_tk *ec_tk_or_new_list(const char *id, ...)
125 struct ec_tk_or *tk = NULL;
131 tk = (struct ec_tk_or *)ec_tk_or_new(id);
135 for (child = va_arg(ap, struct ec_tk *);
136 child != EC_TK_ENDLIST;
137 child = va_arg(ap, struct ec_tk *)) {
141 ec_tk_or_add(&tk->gen, child);
148 ec_tk_free(&tk->gen); /* will also free children */
153 int ec_tk_or_add(struct ec_tk *gen_tk, struct ec_tk *child)
155 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
156 struct ec_tk **table;
159 assert(child != NULL);
161 table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
166 table[tk->len] = child;
172 static int testcase(void)
177 /* all inputs starting with foo should match */
178 tk = ec_tk_or_new_list(NULL,
179 ec_tk_str_new(NULL, "foo"),
180 ec_tk_str_new(NULL, "bar"),
183 ec_log(EC_LOG_ERR, "cannot create tk\n");
186 ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", "foo");
187 ret |= EC_TEST_CHECK_TK_PARSE(tk, "fooxxx", "foo");
188 ret |= EC_TEST_CHECK_TK_PARSE(tk, "bar", "bar");
189 ret |= EC_TEST_CHECK_TK_PARSE(tk, "oo", NULL);
192 /* test completion */
193 tk = ec_tk_or_new_list(NULL,
194 ec_tk_str_new(NULL, "foo"),
195 ec_tk_str_new(NULL, "bar"),
196 ec_tk_str_new(NULL, "bar2"),
197 ec_tk_str_new(NULL, "toto"),
198 ec_tk_str_new(NULL, "titi"),
201 ec_log(EC_LOG_ERR, "cannot create tk\n");
204 ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "", "");
205 ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "f", "oo");
206 ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "b", "ar");
207 ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "t", "");
208 ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "to", "to");
209 ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "x", NULL);
215 static struct ec_test test = {
220 EC_REGISTER_TEST(test);