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 EC_LOG_TYPE_REGISTER(node_re_lex);
22 struct regexp_pattern {
28 struct ec_node_re_lex {
30 struct ec_node *child;
31 struct regexp_pattern *table;
35 static struct ec_strvec *
36 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
38 struct ec_strvec *strvec = NULL;
56 for (i = 0; i < table_len; i++) {
57 ret = regexec(&table[i].r, &dup[off], 1, &pos, 0);
60 if (pos.rm_so != 0 || pos.rm_eo == 0) {
65 if (table[i].keep == 0)
68 c = dup[pos.rm_eo + off];
69 dup[pos.rm_eo + off] = '\0';
70 EC_LOG(EC_LOG_DEBUG, "re_lex match <%s>\n", &dup[off]);
71 if (ec_strvec_add(strvec, &dup[off]) < 0)
74 dup[pos.rm_eo + off] = c;
89 ec_strvec_free(strvec);
94 ec_node_re_lex_parse(const struct ec_node *gen_node,
95 struct ec_parsed *state,
96 const struct ec_strvec *strvec)
98 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
99 struct ec_strvec *new_vec = NULL;
100 struct ec_parsed *child_parsed;
104 if (ec_strvec_len(strvec) == 0) {
105 new_vec = ec_strvec();
107 str = ec_strvec_val(strvec, 0);
108 new_vec = tokenize(node->table, node->len, str);
110 if (new_vec == NULL) {
115 ret = ec_node_parse_child(node->child, state, new_vec);
117 if ((unsigned)ret == ec_strvec_len(new_vec)) {
120 child_parsed = ec_parsed_get_last_child(state);
121 ec_parsed_del_child(state, child_parsed);
122 ec_parsed_free(child_parsed);
123 ret = EC_PARSED_NOMATCH;
127 ec_strvec_free(new_vec);
133 ec_strvec_free(new_vec);
137 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
139 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
142 for (i = 0; i < node->len; i++) {
143 ec_free(node->table[i].pattern);
144 regfree(&node->table[i].r);
147 ec_free(node->table);
148 ec_node_free(node->child);
151 static struct ec_node_type ec_node_re_lex_type = {
153 .parse = ec_node_re_lex_parse,
154 //.complete = ec_node_re_lex_complete, //XXX
155 .size = sizeof(struct ec_node_re_lex),
156 .free_priv = ec_node_re_lex_free_priv,
159 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
161 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
163 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
164 struct regexp_pattern *table;
166 char *pat_dup = NULL;
169 pat_dup = ec_strdup(pattern);
174 table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
178 ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
181 "Regular expression <%s> compilation failed: %d\n",
183 if (ret == REG_ESPACE)
191 table[node->len].pattern = pat_dup;
192 table[node->len].keep = keep;
203 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
205 struct ec_node_re_lex *node = NULL;
210 node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
221 /* LCOV_EXCL_START */
222 static int ec_node_re_lex_testcase(void)
224 struct ec_node *node;
227 node = ec_node_re_lex(NULL,
230 ec_node_str(NULL, "foo"),
231 ec_node_str(NULL, "bar"),
232 ec_node_int(NULL, 0, 1000, 0)
237 EC_LOG(EC_LOG_ERR, "cannot create node\n");
241 /* XXX add ^ automatically ? */
242 ret |= ec_node_re_lex_add(node, "[a-zA-Z]+", 1);
243 ret |= ec_node_re_lex_add(node, "[0-9]+", 1);
244 ret |= ec_node_re_lex_add(node, "=", 1);
245 ret |= ec_node_re_lex_add(node, "-", 1);
246 ret |= ec_node_re_lex_add(node, "\\+", 1);
247 ret |= ec_node_re_lex_add(node, "[ ]+", 0);
249 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
254 ret |= EC_TEST_CHECK_PARSE(node, 1, " foo bar 324 bar234");
255 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
256 ret |= EC_TEST_CHECK_PARSE(node, 1, "");
257 ret |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
265 static struct ec_test ec_node_re_lex_test = {
266 .name = "node_re_lex",
267 .test = ec_node_re_lex_testcase,
270 EC_TEST_REGISTER(ec_node_re_lex_test);