8 #include <ecoli_malloc.h>
10 #include <ecoli_test.h>
11 #include <ecoli_strvec.h>
12 #include <ecoli_node.h>
13 #include <ecoli_parsed.h>
14 #include <ecoli_node_many.h>
15 #include <ecoli_node_or.h>
16 #include <ecoli_node_str.h>
17 #include <ecoli_node_int.h>
18 #include <ecoli_node_re_lex.h>
20 struct regexp_pattern {
26 struct ec_node_re_lex {
28 struct ec_node *child;
29 struct regexp_pattern *table;
33 static struct ec_strvec *
34 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
36 struct ec_strvec *strvec = NULL;
54 for (i = 0; i < table_len; i++) {
55 ret = regexec(&table[i].r, &dup[off], 1, &pos, 0);
58 if (pos.rm_so != 0 || pos.rm_eo == 0) {
63 if (table[i].keep == 0)
66 c = dup[pos.rm_eo + off];
67 dup[pos.rm_eo + off] = '\0';
68 ec_log(EC_LOG_DEBUG, "re_lex match <%s>\n", &dup[off]);
69 if (ec_strvec_add(strvec, &dup[off]) < 0)
72 dup[pos.rm_eo + off] = c;
87 ec_strvec_free(strvec);
92 ec_node_re_lex_parse(const struct ec_node *gen_node,
93 struct ec_parsed *state,
94 const struct ec_strvec *strvec)
96 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
97 struct ec_strvec *new_vec = NULL;
98 struct ec_parsed *child_parsed;
102 if (ec_strvec_len(strvec) == 0) {
103 new_vec = ec_strvec();
105 str = ec_strvec_val(strvec, 0);
106 new_vec = tokenize(node->table, node->len, str);
108 if (new_vec == NULL) {
113 ret = ec_node_parse_child(node->child, state, new_vec);
115 if ((unsigned)ret == ec_strvec_len(new_vec)) {
118 child_parsed = ec_parsed_get_last_child(state);
119 ec_parsed_del_child(state, child_parsed);
120 ec_parsed_free(child_parsed);
121 ret = EC_PARSED_NOMATCH;
125 ec_strvec_free(new_vec);
131 ec_strvec_free(new_vec);
135 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
137 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
140 for (i = 0; i < node->len; i++) {
141 ec_free(node->table[i].pattern);
142 regfree(&node->table[i].r);
145 ec_free(node->table);
146 ec_node_free(node->child);
149 static struct ec_node_type ec_node_re_lex_type = {
151 .parse = ec_node_re_lex_parse,
152 //.complete = ec_node_re_lex_complete, //XXX
153 .size = sizeof(struct ec_node_re_lex),
154 .free_priv = ec_node_re_lex_free_priv,
157 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
159 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
161 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
162 struct regexp_pattern *table;
164 char *pat_dup = NULL;
167 pat_dup = ec_strdup(pattern);
172 table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
176 ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
179 "Regular expression <%s> compilation failed: %d\n",
181 if (ret == REG_ESPACE)
189 table[node->len].pattern = pat_dup;
190 table[node->len].keep = keep;
201 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
203 struct ec_node_re_lex *node = NULL;
208 node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
219 /* LCOV_EXCL_START */
220 static int ec_node_re_lex_testcase(void)
222 struct ec_node *node;
225 node = ec_node_re_lex(NULL,
228 ec_node_str(NULL, "foo"),
229 ec_node_str(NULL, "bar"),
230 ec_node_int(NULL, 0, 1000, 0)
235 ec_log(EC_LOG_ERR, "cannot create node\n");
239 /* XXX add ^ automatically ? */
240 ret |= ec_node_re_lex_add(node, "[a-zA-Z]+", 1);
241 ret |= ec_node_re_lex_add(node, "[0-9]+", 1);
242 ret |= ec_node_re_lex_add(node, "=", 1);
243 ret |= ec_node_re_lex_add(node, "-", 1);
244 ret |= ec_node_re_lex_add(node, "\\+", 1);
245 ret |= ec_node_re_lex_add(node, "[ ]+", 0);
247 ec_log(EC_LOG_ERR, "cannot add regexp to node\n");
252 ret |= EC_TEST_CHECK_PARSE(node, 1, " foo bar 324 bar234");
253 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
254 ret |= EC_TEST_CHECK_PARSE(node, 1, "");
255 ret |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
263 static struct ec_test ec_node_re_lex_test = {
264 .name = "node_re_lex",
265 .test = ec_node_re_lex_testcase,
268 EC_TEST_REGISTER(ec_node_re_lex_test);