save
[protos/libecoli.git] / lib / ecoli_node_seq.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <stdarg.h>
33 #include <errno.h>
34
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>
45
46 struct ec_node_seq {
47         struct ec_node gen;
48         struct ec_node **table;
49         unsigned int len;
50 };
51
52 static int
53 ec_node_seq_parse(const struct ec_node *gen_node,
54                 struct ec_parsed *state,
55                 const struct ec_strvec *strvec)
56 {
57         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
58         struct ec_strvec *childvec = NULL;
59         size_t len = 0;
60         unsigned int i;
61         int ret;
62
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) {
67                         ret = -ENOMEM;
68                         goto fail;
69                 }
70
71                 ret = ec_node_parse_child(node->table[i], state, childvec);
72                 ec_strvec_free(childvec);
73                 childvec = NULL;
74                 if (ret == EC_PARSED_NOMATCH) {
75                         ec_parsed_free_children(state);
76                         return EC_PARSED_NOMATCH;
77                 } else if (ret < 0) {
78                         goto fail;
79                 }
80
81                 len += ret;
82         }
83
84         return len;
85
86 fail:
87         ec_strvec_free(childvec);
88         return ret;
89 }
90
91 static struct ec_completed *
92 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
93                 struct ec_parsed *state, const struct ec_strvec *strvec)
94 {
95         struct ec_completed *completed, *child_completed;
96         struct ec_strvec *childvec = NULL;
97         unsigned int i;
98         int ret;
99
100         completed = ec_completed();
101         if (completed == NULL)
102                 return NULL;
103
104         if (table_len == 0)
105                 return completed;
106
107         /*
108          * Example of completion for a sequence node = [n1,n2] and an
109          * input = [a,b,c,d]:
110          *
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]
116          */
117
118         /* first, try to complete with the first node of the table */
119         child_completed = ec_node_complete_child(table[0], state, strvec);
120         if (child_completed == NULL)
121                 goto fail;
122
123         ec_completed_merge(completed, child_completed);
124         child_completed = NULL;
125
126         /* then, if the first node of the table matches the beginning of the
127          * strvec, try to complete the rest */
128         for (i = 0; i < ec_strvec_len(strvec); i++) {
129                 childvec = ec_strvec_ndup(strvec, 0, i);
130                 if (childvec == NULL)
131                         goto fail;
132
133                 ret = ec_node_parse_child(table[0], state, childvec);
134                 if (ret < 0 && ret != EC_PARSED_NOMATCH)
135                         goto fail;
136
137                 ec_strvec_free(childvec);
138                 childvec = NULL;
139
140                 if ((unsigned int)ret != i) {
141                         if (ret != EC_PARSED_NOMATCH)
142                                 ec_parsed_del_last_child(state);
143                         continue;
144                 }
145
146                 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
147                 if (childvec == NULL) {
148                         ec_parsed_del_last_child(state);
149                         goto fail;
150                 }
151
152                 child_completed = __ec_node_seq_complete(&table[1],
153                                                         table_len - 1,
154                                                         state, childvec);
155                 ec_parsed_del_last_child(state);
156                 ec_strvec_free(childvec);
157                 childvec = NULL;
158
159                 if (child_completed == NULL)
160                         goto fail;
161
162                 ec_completed_merge(completed, child_completed);
163                 child_completed = NULL;
164         }
165
166         return completed;
167
168 fail:
169         ec_strvec_free(childvec);
170         ec_completed_free(child_completed);
171         ec_completed_free(completed);
172         return NULL;
173 }
174
175 static struct ec_completed *
176 ec_node_seq_complete(const struct ec_node *gen_node,
177                 struct ec_parsed *state,
178                 const struct ec_strvec *strvec)
179 {
180         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
181
182         return __ec_node_seq_complete(node->table, node->len, state, strvec);
183 }
184
185 static void ec_node_seq_free_priv(struct ec_node *gen_node)
186 {
187         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
188         unsigned int i;
189
190         for (i = 0; i < node->len; i++)
191                 ec_node_free(node->table[i]);
192         ec_free(node->table);
193 }
194
195 static struct ec_node_type ec_node_seq_type = {
196         .name = "seq",
197         .parse = ec_node_seq_parse,
198         .complete = ec_node_seq_complete,
199         .size = sizeof(struct ec_node_seq),
200         .free_priv = ec_node_seq_free_priv,
201 };
202
203 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
204
205 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
206 {
207         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
208         struct ec_node **table;
209
210         // XXX check node type
211
212         assert(node != NULL);
213
214         if (child == NULL)
215                 return -EINVAL;
216
217         gen_node->flags &= ~EC_NODE_F_BUILT;
218
219         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
220         if (table == NULL) {
221                 ec_node_free(child);
222                 return -1;
223         }
224
225         node->table = table;
226         table[node->len] = child;
227         node->len++;
228
229         child->parent = gen_node;
230         TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
231
232         return 0;
233 }
234
235 struct ec_node *__ec_node_seq(const char *id, ...)
236 {
237         struct ec_node *gen_node = NULL;
238         struct ec_node_seq *node = NULL;
239         struct ec_node *child;
240         va_list ap;
241         int fail = 0;
242
243         va_start(ap, id);
244
245         gen_node = __ec_node(&ec_node_seq_type, id);
246         node = (struct ec_node_seq *)gen_node;
247         if (node == NULL)
248                 fail = 1;;
249
250         for (child = va_arg(ap, struct ec_node *);
251              child != EC_NODE_ENDLIST;
252              child = va_arg(ap, struct ec_node *)) {
253
254                 /* on error, don't quit the loop to avoid leaks */
255                 if (fail == 1 || child == NULL ||
256                                 ec_node_seq_add(&node->gen, child) < 0) {
257                         fail = 1;
258                         ec_node_free(child);
259                 }
260         }
261
262         if (fail == 1)
263                 goto fail;
264
265         va_end(ap);
266         return gen_node;
267
268 fail:
269         ec_node_free(gen_node); /* will also free children */
270         va_end(ap);
271         return NULL;
272 }
273
274 /* LCOV_EXCL_START */
275 static int ec_node_seq_testcase(void)
276 {
277         struct ec_node *node;
278         int ret = 0;
279
280         node = EC_NODE_SEQ(NULL,
281                 ec_node_str(NULL, "foo"),
282                 ec_node_str(NULL, "bar")
283         );
284         if (node == NULL) {
285                 ec_log(EC_LOG_ERR, "cannot create node\n");
286                 return -1;
287         }
288         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
289         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
290         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
291         ret |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
292         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
293         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
294         ret |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
295         ec_node_free(node);
296
297         /* test completion */
298         node = EC_NODE_SEQ(NULL,
299                 ec_node_str(NULL, "foo"),
300                 ec_node_option(NULL, ec_node_str(NULL, "toto")),
301                 ec_node_str(NULL, "bar")
302         );
303         if (node == NULL) {
304                 ec_log(EC_LOG_ERR, "cannot create node\n");
305                 return -1;
306         }
307         ret |= EC_TEST_CHECK_COMPLETE(node,
308                 "", EC_NODE_ENDLIST,
309                 "foo", EC_NODE_ENDLIST,
310                 "foo");
311         ret |= EC_TEST_CHECK_COMPLETE(node,
312                 "f", EC_NODE_ENDLIST,
313                 "oo", EC_NODE_ENDLIST,
314                 "oo");
315         ret |= EC_TEST_CHECK_COMPLETE(node,
316                 "foo", EC_NODE_ENDLIST,
317                 "", EC_NODE_ENDLIST,
318                 "");
319         ret |= EC_TEST_CHECK_COMPLETE(node,
320                 "foo", "", EC_NODE_ENDLIST,
321                 "bar", "toto", EC_NODE_ENDLIST,
322                 "");
323         ret |= EC_TEST_CHECK_COMPLETE(node,
324                 "foo", "t", EC_NODE_ENDLIST,
325                 "oto", EC_NODE_ENDLIST,
326                 "oto");
327         ret |= EC_TEST_CHECK_COMPLETE(node,
328                 "foo", "b", EC_NODE_ENDLIST,
329                 "ar", EC_NODE_ENDLIST,
330                 "ar");
331         ret |= EC_TEST_CHECK_COMPLETE(node,
332                 "foo", "bar", EC_NODE_ENDLIST,
333                 "", EC_NODE_ENDLIST,
334                 "");
335         ret |= EC_TEST_CHECK_COMPLETE(node,
336                 "x", EC_NODE_ENDLIST,
337                 EC_NODE_ENDLIST,
338                 "");
339         ret |= EC_TEST_CHECK_COMPLETE(node,
340                 "foobarx", EC_NODE_ENDLIST,
341                 EC_NODE_ENDLIST,
342                 "");
343         ec_node_free(node);
344
345         return ret;
346 }
347 /* LCOV_EXCL_STOP */
348
349 static struct ec_test ec_node_seq_test = {
350         .name = "node_seq",
351         .test = ec_node_seq_testcase,
352 };
353
354 EC_TEST_REGISTER(ec_node_seq_test);