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