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