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