1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
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>
25 EC_LOG_TYPE_REGISTER(node_re_lex);
27 struct regexp_pattern {
33 struct ec_node_re_lex {
35 struct ec_node *child;
36 struct regexp_pattern *table;
40 static struct ec_strvec *
41 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
43 struct ec_strvec *strvec = NULL;
61 for (i = 0; i < table_len; i++) {
62 ret = regexec(&table[i].r, &dup[off], 1, &pos, 0);
65 if (pos.rm_so != 0 || pos.rm_eo == 0) {
70 if (table[i].keep == 0)
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)
79 dup[pos.rm_eo + off] = c;
94 ec_strvec_free(strvec);
99 ec_node_re_lex_parse(const struct ec_node *gen_node,
100 struct ec_parse *state,
101 const struct ec_strvec *strvec)
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;
109 if (ec_strvec_len(strvec) == 0) {
110 new_vec = ec_strvec();
112 str = ec_strvec_val(strvec, 0);
113 new_vec = tokenize(node->table, node->len, str);
118 ret = ec_node_parse_child(node->child, state, new_vec);
122 if ((unsigned)ret == ec_strvec_len(new_vec)) {
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;
131 ec_strvec_free(new_vec);
137 ec_strvec_free(new_vec);
141 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
143 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
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);
152 ec_free(node->table);
156 ec_node_re_lex_get_children_count(const struct ec_node *gen_node)
158 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
166 ec_node_re_lex_get_child(const struct ec_node *gen_node, size_t i,
167 struct ec_node **child, unsigned int *refs)
169 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
174 *child = node->child;
179 static struct ec_node_type ec_node_re_lex_type = {
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,
189 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
191 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
193 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
194 struct regexp_pattern *table;
196 char *pat_dup = NULL;
198 pat_dup = ec_strdup(pattern);
202 table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
206 ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
209 "Regular expression <%s> compilation failed: %d\n",
211 if (ret == REG_ESPACE)
219 table[node->len].pattern = pat_dup;
220 table[node->len].keep = keep;
231 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
233 struct ec_node_re_lex *node = NULL;
238 node = (struct ec_node_re_lex *)ec_node_from_type(&ec_node_re_lex_type, id);
249 /* LCOV_EXCL_START */
250 static int ec_node_re_lex_testcase(void)
252 struct ec_node *node;
253 int ret, testres = 0;
255 node = ec_node_re_lex(EC_NO_ID,
256 ec_node_many(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)
265 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
282 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
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");
293 testres |= EC_TEST_CHECK_COMPLETE(node,
303 static struct ec_test ec_node_re_lex_test = {
304 .name = "node_re_lex",
305 .test = ec_node_re_lex_testcase,
308 EC_TEST_REGISTER(ec_node_re_lex_test);