b5dff927dd5d005ff42836f632c1802ad4a2dc19
[protos/libecoli.git] / lib / ecoli_node_many.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
34 #include <ecoli_malloc.h>
35 #include <ecoli_log.h>
36 #include <ecoli_test.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_node_str.h>
42 #include <ecoli_node_option.h>
43 #include <ecoli_node_many.h>
44
45 struct ec_node_many {
46         struct ec_node gen;
47         unsigned int min;
48         unsigned int max;
49         struct ec_node *child;
50 };
51
52 static struct ec_parsed *ec_node_many_parse(const struct ec_node *gen_node,
53         const struct ec_strvec *strvec)
54 {
55         struct ec_node_many *node = (struct ec_node_many *)gen_node;
56         struct ec_parsed *parsed, *child_parsed;
57         struct ec_strvec *match_strvec;
58         struct ec_strvec *childvec = NULL;
59         size_t off = 0, len, count;
60
61         parsed = ec_parsed();
62         if (parsed == NULL)
63                 goto fail;
64
65         for (count = 0; node->max == 0 || count < node->max; count++) {
66                 childvec = ec_strvec_ndup(strvec, off,
67                         ec_strvec_len(strvec) - off);
68                 if (childvec == NULL)
69                         goto fail;
70
71                 child_parsed = ec_node_parse_strvec(node->child, childvec);
72                 if (child_parsed == NULL)
73                         goto fail;
74
75                 ec_strvec_free(childvec);
76                 childvec = NULL;
77
78                 if (!ec_parsed_matches(child_parsed)) {
79                         ec_parsed_free(child_parsed);
80                         break;
81                 }
82
83                 ec_parsed_add_child(parsed, child_parsed);
84
85                 /* it matches an empty strvec, no need to continue */
86                 len = ec_parsed_len(child_parsed);
87                 if (len == 0) {
88                         ec_parsed_free(child_parsed);
89                         break;
90                 }
91
92                 off += len;
93         }
94
95         if (count < node->min) {
96                 ec_parsed_free_children(parsed);
97                 return parsed;
98         }
99
100         match_strvec = ec_strvec_ndup(strvec, 0, off);
101         if (match_strvec == NULL)
102                 goto fail;
103
104         ec_parsed_set_match(parsed, gen_node, match_strvec);
105
106         return parsed;
107
108 fail:
109         ec_strvec_free(childvec);
110         ec_parsed_free(parsed);
111         return NULL;
112 }
113
114 #if 0 //XXX missing node_many_complete
115 static struct ec_completed *ec_node_many_complete(const struct ec_node *gen_node,
116         const struct ec_strvec *strvec)
117 {
118         struct ec_node_many *node = (struct ec_node_many *)gen_node;
119         struct ec_completed *completed, *child_completed;
120         struct ec_strvec *childvec;
121         struct ec_parsed *parsed;
122         size_t len = 0;
123         unsigned int i;
124
125         completed = ec_completed();
126         if (completed == NULL)
127                 return NULL;
128
129         if (node->len == 0)
130                 return completed;
131
132         for (i = 0; i < node->len; i++) {
133                 childvec = ec_strvec_ndup(strvec, len,
134                         ec_strvec_len(strvec) - len);
135                 if (childvec == NULL)
136                         goto fail; // XXX fail ?
137
138                 child_completed = ec_node_complete_strvec(node->table[i],
139                         childvec);
140                 if (child_completed == NULL)
141                         goto fail;
142
143                 ec_completed_merge(completed, child_completed);
144
145                 parsed = ec_node_parse_strvec(node->table[i], childvec);
146                 if (parsed == NULL)
147                         goto fail;
148
149                 ec_strvec_free(childvec);
150                 childvec = NULL;
151
152                 if (!ec_parsed_matches(parsed)) {
153                         ec_parsed_free(parsed);
154                         break;
155                 }
156
157                 len += ec_strvec_len(parsed->strvec);
158                 ec_parsed_free(parsed);
159         }
160
161         return completed;
162
163 fail:
164         ec_strvec_free(childvec);
165         ec_completed_free(completed);
166         return NULL;
167 }
168 #endif
169
170 static void ec_node_many_free_priv(struct ec_node *gen_node)
171 {
172         struct ec_node_many *node = (struct ec_node_many *)gen_node;
173
174         ec_node_free(node->child);
175 }
176
177 static struct ec_node_type ec_node_many_type = {
178         .name = "many",
179         .parse = ec_node_many_parse,
180         .complete = ec_node_default_complete,
181 //XXX   .complete = ec_node_many_complete,
182         .size = sizeof(struct ec_node_many),
183         .free_priv = ec_node_many_free_priv,
184 };
185
186 EC_NODE_TYPE_REGISTER(ec_node_many_type);
187
188 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
189         unsigned int min, unsigned int max)
190 {
191         struct ec_node_many *node = NULL;
192
193         if (child == NULL)
194                 return NULL;
195
196         node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
197         if (node == NULL) {
198                 ec_node_free(child);
199                 return NULL;
200         }
201
202         node->child = child;
203         node->min = min;
204         node->max = max;
205
206         return &node->gen;
207 }
208
209 static int ec_node_many_testcase(void)
210 {
211         struct ec_node *node;
212         int ret = 0;
213
214         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 0, 0);
215         if (node == NULL) {
216                 ec_log(EC_LOG_ERR, "cannot create node\n");
217                 return -1;
218         }
219         ret |= EC_TEST_CHECK_PARSE(node, 0, "bar");
220         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
221         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
222         ret |= EC_TEST_CHECK_PARSE(node, 0);
223         ec_node_free(node);
224
225         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 0);
226         if (node == NULL) {
227                 ec_log(EC_LOG_ERR, "cannot create node\n");
228                 return -1;
229         }
230         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
231         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
232         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
233         ret |= EC_TEST_CHECK_PARSE(node, -1);
234         ec_node_free(node);
235
236         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 2);
237         if (node == NULL) {
238                 ec_log(EC_LOG_ERR, "cannot create node\n");
239                 return -1;
240         }
241         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
242         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
243         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
244         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
245         ret |= EC_TEST_CHECK_PARSE(node, -1);
246         ec_node_free(node);
247
248         /* test completion */
249         /* XXX */
250
251         return ret;
252 }
253
254 static struct ec_test ec_node_many_test = {
255         .name = "many",
256         .test = ec_node_many_testcase,
257 };
258
259 EC_TEST_REGISTER(ec_node_many_test);