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);
152 ec_node_free(node->child);
155 static struct ec_node_type ec_node_re_lex_type = {
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,
163 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
165 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
167 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
168 struct regexp_pattern *table;
170 char *pat_dup = NULL;
172 pat_dup = ec_strdup(pattern);
176 table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
180 ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
183 "Regular expression <%s> compilation failed: %d\n",
185 if (ret == REG_ESPACE)
193 table[node->len].pattern = pat_dup;
194 table[node->len].keep = keep;
205 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
207 struct ec_node_re_lex *node = NULL;
212 node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
223 /* LCOV_EXCL_START */
224 static int ec_node_re_lex_testcase(void)
226 struct ec_node *node;
227 int ret, testres = 0;
229 node = ec_node_re_lex(EC_NO_ID,
230 ec_node_many(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)
239 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
256 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
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");
267 testres |= EC_TEST_CHECK_COMPLETE(node,
277 static struct ec_test ec_node_re_lex_test = {
278 .name = "node_re_lex",
279 .test = ec_node_re_lex_testcase,
282 EC_TEST_REGISTER(ec_node_re_lex_test);