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_completed.h>
18 #include <ecoli_parsed.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_parsed *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_parsed *child_parsed;
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);
115 if (new_vec == NULL) {
120 ret = ec_node_parse_child(node->child, state, new_vec);
124 if ((unsigned)ret == ec_strvec_len(new_vec)) {
126 } else if (ret != EC_PARSED_NOMATCH) {
127 child_parsed = ec_parsed_get_last_child(state);
128 ec_parsed_del_child(state, child_parsed);
129 ec_parsed_free(child_parsed);
130 ret = EC_PARSED_NOMATCH;
133 ec_strvec_free(new_vec);
139 ec_strvec_free(new_vec);
143 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
145 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
148 for (i = 0; i < node->len; i++) {
149 ec_free(node->table[i].pattern);
150 regfree(&node->table[i].r);
153 ec_free(node->table);
154 ec_node_free(node->child);
157 static struct ec_node_type ec_node_re_lex_type = {
159 .parse = ec_node_re_lex_parse,
160 .complete = ec_node_default_complete,
161 .size = sizeof(struct ec_node_re_lex),
162 .free_priv = ec_node_re_lex_free_priv,
165 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
167 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
169 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
170 struct regexp_pattern *table;
172 char *pat_dup = NULL;
175 pat_dup = ec_strdup(pattern);
180 table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
184 ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
187 "Regular expression <%s> compilation failed: %d\n",
189 if (ret == REG_ESPACE)
197 table[node->len].pattern = pat_dup;
198 table[node->len].keep = keep;
209 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
211 struct ec_node_re_lex *node = NULL;
216 node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
227 /* LCOV_EXCL_START */
228 static int ec_node_re_lex_testcase(void)
230 struct ec_node *node;
231 int ret, testres = 0;
233 node = ec_node_re_lex(EC_NO_ID,
234 ec_node_many(EC_NO_ID,
236 ec_node_str(EC_NO_ID, "foo"),
237 ec_node_str(EC_NO_ID, "bar"),
238 ec_node_int(EC_NO_ID, 0, 1000, 0)
243 EC_LOG(EC_LOG_ERR, "cannot create node\n");
247 ret = ec_node_re_lex_add(node, "[a-zA-Z]+", 1);
248 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
249 ret = ec_node_re_lex_add(node, "[0-9]+", 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, "-", 1);
254 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
255 ret = ec_node_re_lex_add(node, "\\+", 1);
256 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
257 ret = ec_node_re_lex_add(node, "[ ]+", 0);
258 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
260 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
265 testres |= EC_TEST_CHECK_PARSE(node, 1, " foo bar 324 bar234");
266 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
267 testres |= EC_TEST_CHECK_PARSE(node, 1, "");
268 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
271 testres |= EC_TEST_CHECK_COMPLETE(node,
281 static struct ec_test ec_node_re_lex_test = {
282 .name = "node_re_lex",
283 .test = ec_node_re_lex_testcase,
286 EC_TEST_REGISTER(ec_node_re_lex_test);