add missing child ops
[protos/libecoli.git] / lib / ecoli_node_re_lex.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <string.h>
9 #include <regex.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_parse.h>
19 #include <ecoli_node_many.h>
20 #include <ecoli_node_or.h>
21 #include <ecoli_node_str.h>
22 #include <ecoli_node_int.h>
23 #include <ecoli_node_re_lex.h>
24
25 EC_LOG_TYPE_REGISTER(node_re_lex);
26
27 struct regexp_pattern {
28         char *pattern;
29         regex_t r;
30         bool keep;
31 };
32
33 struct ec_node_re_lex {
34         struct ec_node gen;
35         struct ec_node *child;
36         struct regexp_pattern *table;
37         size_t len;
38 };
39
40 static struct ec_strvec *
41 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
42 {
43         struct ec_strvec *strvec = NULL;
44         char *dup = NULL;
45         char c;
46         size_t len, off = 0;
47         size_t i;
48         int ret;
49         regmatch_t pos;
50
51         dup = ec_strdup(str);
52         if (dup == NULL)
53                 goto fail;
54
55         strvec = ec_strvec();
56         if (strvec == NULL)
57                 goto fail;
58
59         len = strlen(dup);
60         while (off < len) {
61                 for (i = 0; i < table_len; i++) {
62                         ret = regexec(&table[i].r, &dup[off], 1, &pos, 0);
63                         if (ret != 0)
64                                 continue;
65                         if (pos.rm_so != 0 || pos.rm_eo == 0) {
66                                 ret = -1;
67                                 continue;
68                         }
69
70                         if (table[i].keep == 0)
71                                 break;
72
73                         c = dup[pos.rm_eo + off];
74                         dup[pos.rm_eo + off] = '\0';
75                         EC_LOG(EC_LOG_DEBUG, "re_lex match <%s>\n", &dup[off]);
76                         if (ec_strvec_add(strvec, &dup[off]) < 0)
77                                 goto fail;
78
79                         dup[pos.rm_eo + off] = c;
80                         break;
81                 }
82
83                 if (ret != 0)
84                         goto fail;
85
86                 off += pos.rm_eo;
87         }
88
89         ec_free(dup);
90         return strvec;
91
92 fail:
93         ec_free(dup);
94         ec_strvec_free(strvec);
95         return NULL;
96 }
97
98 static int
99 ec_node_re_lex_parse(const struct ec_node *gen_node,
100                 struct ec_parse *state,
101                 const struct ec_strvec *strvec)
102 {
103         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
104         struct ec_strvec *new_vec = NULL;
105         struct ec_parse *child_parse;
106         const char *str;
107         int ret;
108
109         if (ec_strvec_len(strvec) == 0) {
110                 new_vec = ec_strvec();
111         } else {
112                 str = ec_strvec_val(strvec, 0);
113                 new_vec = tokenize(node->table, node->len, str);
114         }
115         if (new_vec == NULL)
116                 goto fail;
117
118         ret = ec_node_parse_child(node->child, state, new_vec);
119         if (ret < 0)
120                 goto fail;
121
122         if ((unsigned)ret == ec_strvec_len(new_vec)) {
123                 ret = 1;
124         } else if (ret != EC_PARSE_NOMATCH) {
125                 child_parse = ec_parse_get_last_child(state);
126                 ec_parse_unlink_child(state, child_parse);
127                 ec_parse_free(child_parse);
128                 ret = EC_PARSE_NOMATCH;
129         }
130
131         ec_strvec_free(new_vec);
132         new_vec = NULL;
133
134         return ret;
135
136  fail:
137         ec_strvec_free(new_vec);
138         return -1;
139 }
140
141 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
142 {
143         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
144         unsigned int i;
145
146         for (i = 0; i < node->len; i++) {
147                 ec_free(node->table[i].pattern);
148                 regfree(&node->table[i].r);
149         }
150
151         ec_free(node->table);
152         ec_node_free(node->child);
153 }
154
155 static size_t
156 ec_node_re_lex_get_children_count(const struct ec_node *gen_node)
157 {
158         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
159
160         if (node->child)
161                 return 1;
162         return 0;
163 }
164
165 static struct ec_node *
166 ec_node_re_lex_get_child(const struct ec_node *gen_node, size_t i)
167 {
168         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
169
170         if (i >= 1)
171                 return NULL;
172
173         return node->child;
174 }
175
176 static struct ec_node_type ec_node_re_lex_type = {
177         .name = "re_lex",
178         .parse = ec_node_re_lex_parse,
179         .complete = ec_node_complete_unknown,
180         .size = sizeof(struct ec_node_re_lex),
181         .free_priv = ec_node_re_lex_free_priv,
182         .get_children_count = ec_node_re_lex_get_children_count,
183         .get_child = ec_node_re_lex_get_child,
184 };
185
186 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
187
188 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
189 {
190         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
191         struct regexp_pattern *table;
192         int ret;
193         char *pat_dup = NULL;
194
195         pat_dup = ec_strdup(pattern);
196         if (pat_dup == NULL)
197                 goto fail;
198
199         table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
200         if (table == NULL)
201                 goto fail;
202
203         ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
204         if (ret != 0) {
205                 EC_LOG(EC_LOG_ERR,
206                         "Regular expression <%s> compilation failed: %d\n",
207                         pattern, ret);
208                 if (ret == REG_ESPACE)
209                         errno = ENOMEM;
210                 else
211                         errno = EINVAL;
212
213                 goto fail;
214         }
215
216         table[node->len].pattern = pat_dup;
217         table[node->len].keep = keep;
218         node->len++;
219         node->table = table;
220
221         return 0;
222
223 fail:
224         ec_free(pat_dup);
225         return -1;
226 }
227
228 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
229 {
230         struct ec_node_re_lex *node = NULL;
231
232         if (child == NULL)
233                 return NULL;
234
235         node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
236         if (node == NULL) {
237                 ec_node_free(child);
238                 return NULL;
239         }
240
241         node->child = child;
242
243         return &node->gen;
244 }
245
246 /* LCOV_EXCL_START */
247 static int ec_node_re_lex_testcase(void)
248 {
249         struct ec_node *node;
250         int ret, testres = 0;
251
252         node = ec_node_re_lex(EC_NO_ID,
253                 ec_node_many(EC_NO_ID,
254                         EC_NODE_OR(EC_NO_ID,
255                                 ec_node_str(EC_NO_ID, "foo"),
256                                 ec_node_str(EC_NO_ID, "bar"),
257                                 ec_node_int(EC_NO_ID, 0, 1000, 0)
258                         ), 0, 0
259                 )
260         );
261         if (node == NULL) {
262                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
263                 return -1;
264         }
265
266         ret = ec_node_re_lex_add(node, "[a-zA-Z]+", 1);
267         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
268         ret = ec_node_re_lex_add(node, "[0-9]+", 1);
269         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
270         ret = ec_node_re_lex_add(node, "=", 1);
271         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
272         ret = ec_node_re_lex_add(node, "-", 1);
273         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
274         ret = ec_node_re_lex_add(node, "\\+", 1);
275         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
276         ret = ec_node_re_lex_add(node, "[       ]+", 0);
277         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
278         if (ret != 0) {
279                 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
280                 ec_node_free(node);
281                 return -1;
282         }
283
284         testres |= EC_TEST_CHECK_PARSE(node, 1, "  foo bar  324 bar234");
285         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
286         testres |= EC_TEST_CHECK_PARSE(node, 1, "");
287         testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
288
289         /* no completion */
290         testres |= EC_TEST_CHECK_COMPLETE(node,
291                 "", EC_NODE_ENDLIST,
292                 EC_NODE_ENDLIST);
293
294         ec_node_free(node);
295
296         return testres;
297 }
298 /* LCOV_EXCL_STOP */
299
300 static struct ec_test ec_node_re_lex_test = {
301         .name = "node_re_lex",
302         .test = ec_node_re_lex_testcase,
303 };
304
305 EC_TEST_REGISTER(ec_node_re_lex_test);