f5a6c370df2982aa9e25c2f752b4be67b1e52513
[protos/libecoli.git] / libecoli / 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         ec_node_free(node->child);
147         for (i = 0; i < node->len; i++) {
148                 ec_free(node->table[i].pattern);
149                 regfree(&node->table[i].r);
150         }
151
152         ec_free(node->table);
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 int
166 ec_node_re_lex_get_child(const struct ec_node *gen_node, size_t i,
167                         struct ec_node **child, unsigned int *refs)
168 {
169         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
170
171         if (i >= 1)
172                 return -1;
173
174         *child = node->child;
175         *refs = 1;
176         return 0;
177 }
178
179 static struct ec_node_type ec_node_re_lex_type = {
180         .name = "re_lex",
181         .parse = ec_node_re_lex_parse,
182         .complete = ec_node_complete_unknown,
183         .size = sizeof(struct ec_node_re_lex),
184         .free_priv = ec_node_re_lex_free_priv,
185         .get_children_count = ec_node_re_lex_get_children_count,
186         .get_child = ec_node_re_lex_get_child,
187 };
188
189 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
190
191 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
192 {
193         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
194         struct regexp_pattern *table;
195         int ret;
196         char *pat_dup = NULL;
197
198         pat_dup = ec_strdup(pattern);
199         if (pat_dup == NULL)
200                 goto fail;
201
202         table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
203         if (table == NULL)
204                 goto fail;
205
206         ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
207         if (ret != 0) {
208                 EC_LOG(EC_LOG_ERR,
209                         "Regular expression <%s> compilation failed: %d\n",
210                         pattern, ret);
211                 if (ret == REG_ESPACE)
212                         errno = ENOMEM;
213                 else
214                         errno = EINVAL;
215
216                 goto fail;
217         }
218
219         table[node->len].pattern = pat_dup;
220         table[node->len].keep = keep;
221         node->len++;
222         node->table = table;
223
224         return 0;
225
226 fail:
227         ec_free(pat_dup);
228         return -1;
229 }
230
231 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
232 {
233         struct ec_node_re_lex *node = NULL;
234
235         if (child == NULL)
236                 return NULL;
237
238         node = (struct ec_node_re_lex *)ec_node_from_type(&ec_node_re_lex_type, id);
239         if (node == NULL) {
240                 ec_node_free(child);
241                 return NULL;
242         }
243
244         node->child = child;
245
246         return &node->gen;
247 }
248
249 /* LCOV_EXCL_START */
250 static int ec_node_re_lex_testcase(void)
251 {
252         struct ec_node *node;
253         int ret, testres = 0;
254
255         node = ec_node_re_lex(EC_NO_ID,
256                 ec_node_many(EC_NO_ID,
257                         EC_NODE_OR(EC_NO_ID,
258                                 ec_node_str(EC_NO_ID, "foo"),
259                                 ec_node_str(EC_NO_ID, "bar"),
260                                 ec_node_int(EC_NO_ID, 0, 1000, 0)
261                         ), 0, 0
262                 )
263         );
264         if (node == NULL) {
265                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
266                 return -1;
267         }
268
269         ret = ec_node_re_lex_add(node, "[a-zA-Z]+", 1);
270         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
271         ret = ec_node_re_lex_add(node, "[0-9]+", 1);
272         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
273         ret = ec_node_re_lex_add(node, "=", 1);
274         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
275         ret = ec_node_re_lex_add(node, "-", 1);
276         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
277         ret = ec_node_re_lex_add(node, "\\+", 1);
278         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
279         ret = ec_node_re_lex_add(node, "[       ]+", 0);
280         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
281         if (ret != 0) {
282                 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
283                 ec_node_free(node);
284                 return -1;
285         }
286
287         testres |= EC_TEST_CHECK_PARSE(node, 1, "  foo bar  324 bar234");
288         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
289         testres |= EC_TEST_CHECK_PARSE(node, 1, "");
290         testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
291
292         /* no completion */
293         testres |= EC_TEST_CHECK_COMPLETE(node,
294                 "", EC_NODE_ENDLIST,
295                 EC_NODE_ENDLIST);
296
297         ec_node_free(node);
298
299         return testres;
300 }
301 /* LCOV_EXCL_STOP */
302
303 static struct ec_test ec_node_re_lex_test = {
304         .name = "node_re_lex",
305         .test = ec_node_re_lex_testcase,
306 };
307
308 EC_TEST_REGISTER(ec_node_re_lex_test);