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 for (i = 0; i < node->len; i++) {
147 ec_free(node->table[i].pattern);
148 regfree(&node->table[i].r);
151 ec_free(node->table);
155 ec_node_re_lex_get_children_count(const struct ec_node *gen_node)
157 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
164 static struct ec_node *
165 ec_node_re_lex_get_child(const struct ec_node *gen_node, size_t i)
167 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
175 static struct ec_node_type ec_node_re_lex_type = {
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,
185 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
187 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
189 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
190 struct regexp_pattern *table;
192 char *pat_dup = NULL;
194 pat_dup = ec_strdup(pattern);
198 table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
202 ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
205 "Regular expression <%s> compilation failed: %d\n",
207 if (ret == REG_ESPACE)
215 table[node->len].pattern = pat_dup;
216 table[node->len].keep = keep;
227 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
229 struct ec_node_re_lex *node = NULL;
234 node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
245 /* LCOV_EXCL_START */
246 static int ec_node_re_lex_testcase(void)
248 struct ec_node *node;
249 int ret, testres = 0;
251 node = ec_node_re_lex(EC_NO_ID,
252 ec_node_many(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)
261 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
278 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
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");
289 testres |= EC_TEST_CHECK_COMPLETE(node,
299 static struct ec_test ec_node_re_lex_test = {
300 .name = "node_re_lex",
301 .test = ec_node_re_lex_testcase,
304 EC_TEST_REGISTER(ec_node_re_lex_test);