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.
35 #include <ecoli_malloc.h>
36 #include <ecoli_log.h>
37 #include <ecoli_test.h>
38 #include <ecoli_strvec.h>
39 #include <ecoli_node.h>
40 #include <ecoli_parsed.h>
41 #include <ecoli_completed.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_node_option.h>
44 #include <ecoli_node_seq.h>
48 struct ec_node **table;
53 ec_node_seq_parse(const struct ec_node *gen_node,
54 struct ec_parsed *state,
55 const struct ec_strvec *strvec)
57 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
58 struct ec_strvec *childvec = NULL;
63 for (i = 0; i < node->len; i++) {
64 childvec = ec_strvec_ndup(strvec, len,
65 ec_strvec_len(strvec) - len);
66 if (childvec == NULL) {
71 ret = ec_node_parse_child(node->table[i], state, childvec);
72 ec_strvec_free(childvec);
74 if (ret == EC_PARSED_NOMATCH) {
75 ec_parsed_free_children(state);
76 return EC_PARSED_NOMATCH;
87 ec_strvec_free(childvec);
91 static struct ec_completed *
92 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
93 const struct ec_strvec *strvec)
95 struct ec_completed *completed, *child_completed;
96 struct ec_strvec *childvec = NULL;
97 struct ec_parsed *parsed;
100 completed = ec_completed();
101 if (completed == NULL)
108 * Example of completion for a sequence node = [n1,n2] and an
111 * result = complete(n1, [a,b,c,d]) +
112 * complete(n2, [b,c,d]) if n1 matches [a] +
113 * complete(n2, [c,d]) if n1 matches [a,b] +
114 * complete(n2, [d]) if n1 matches [a,b,c] +
115 * complete(n2, []) if n1 matches [a,b,c,d]
118 /* first, try to complete with the first node of the table */
119 child_completed = ec_node_complete_strvec(table[0], strvec);
120 if (child_completed == NULL)
122 ec_completed_merge(completed, child_completed);
123 child_completed = NULL;
125 /* then, if the node matches the beginning of the strvec, try to
126 * complete the rest */
127 for (i = 0; i < ec_strvec_len(strvec); i++) {
128 childvec = ec_strvec_ndup(strvec, 0, i);
129 if (childvec == NULL)
132 parsed = ec_node_parse_strvec(table[0], childvec);
136 ec_strvec_free(childvec);
139 if (!ec_parsed_matches(parsed) || ec_parsed_len(parsed) != i) {
140 ec_parsed_free(parsed);
144 ec_parsed_free(parsed);
147 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
148 if (childvec == NULL)
151 child_completed = __ec_node_seq_complete(&table[1],
154 ec_strvec_free(childvec);
157 if (child_completed == NULL)
160 ec_completed_merge(completed, child_completed);
161 child_completed = NULL;
167 ec_strvec_free(childvec);
168 ec_parsed_free(parsed);
169 ec_completed_free(child_completed);
170 ec_completed_free(completed);
174 static struct ec_completed *ec_node_seq_complete(const struct ec_node *gen_node,
175 const struct ec_strvec *strvec)
177 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
179 return __ec_node_seq_complete(node->table, node->len, strvec);
182 static void ec_node_seq_free_priv(struct ec_node *gen_node)
184 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
187 for (i = 0; i < node->len; i++)
188 ec_node_free(node->table[i]);
189 ec_free(node->table);
192 static struct ec_node_type ec_node_seq_type = {
194 .parse = ec_node_seq_parse,
195 .complete = ec_node_seq_complete,
196 .size = sizeof(struct ec_node_seq),
197 .free_priv = ec_node_seq_free_priv,
200 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
202 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
204 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
205 struct ec_node **table;
207 // XXX check node type
209 assert(node != NULL);
214 gen_node->flags &= ~EC_NODE_F_BUILT;
216 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
223 table[node->len] = child;
226 child->parent = gen_node;
227 TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
232 struct ec_node *__ec_node_seq(const char *id, ...)
234 struct ec_node *gen_node = NULL;
235 struct ec_node_seq *node = NULL;
236 struct ec_node *child;
242 gen_node = __ec_node(&ec_node_seq_type, id);
243 node = (struct ec_node_seq *)gen_node;
247 for (child = va_arg(ap, struct ec_node *);
248 child != EC_NODE_ENDLIST;
249 child = va_arg(ap, struct ec_node *)) {
251 /* on error, don't quit the loop to avoid leaks */
252 if (fail == 1 || child == NULL ||
253 ec_node_seq_add(&node->gen, child) < 0) {
266 ec_node_free(gen_node); /* will also free children */
271 static int ec_node_seq_testcase(void)
273 struct ec_node *node;
276 node = EC_NODE_SEQ(NULL,
277 ec_node_str(NULL, "foo"),
278 ec_node_str(NULL, "bar")
281 ec_log(EC_LOG_ERR, "cannot create node\n");
284 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
285 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
286 ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
287 ret |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
288 ret |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
289 ret |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
290 ret |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
293 /* test completion */
294 node = EC_NODE_SEQ(NULL,
295 ec_node_str(NULL, "foo"),
296 ec_node_option(NULL, ec_node_str(NULL, "toto")),
297 ec_node_str(NULL, "bar")
300 ec_log(EC_LOG_ERR, "cannot create node\n");
303 ret |= EC_TEST_CHECK_COMPLETE(node,
305 "foo", EC_NODE_ENDLIST,
307 ret |= EC_TEST_CHECK_COMPLETE(node,
308 "f", EC_NODE_ENDLIST,
309 "oo", EC_NODE_ENDLIST,
311 ret |= EC_TEST_CHECK_COMPLETE(node,
312 "foo", EC_NODE_ENDLIST,
315 ret |= EC_TEST_CHECK_COMPLETE(node,
316 "foo", "", EC_NODE_ENDLIST,
317 "bar", "toto", EC_NODE_ENDLIST,
319 ret |= EC_TEST_CHECK_COMPLETE(node,
320 "foo", "t", EC_NODE_ENDLIST,
321 "oto", EC_NODE_ENDLIST,
323 ret |= EC_TEST_CHECK_COMPLETE(node,
324 "foo", "b", EC_NODE_ENDLIST,
325 "ar", EC_NODE_ENDLIST,
327 ret |= EC_TEST_CHECK_COMPLETE(node,
328 "foo", "bar", EC_NODE_ENDLIST,
331 ret |= EC_TEST_CHECK_COMPLETE(node,
332 "x", EC_NODE_ENDLIST,
335 ret |= EC_TEST_CHECK_COMPLETE(node,
336 "foobarx", EC_NODE_ENDLIST,
344 static struct ec_test ec_node_seq_test = {
346 .test = ec_node_seq_testcase,
349 EC_TEST_REGISTER(ec_node_seq_test);