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);
119 if ((unsigned)ret == ec_strvec_len(new_vec)) {
121 } else if (ret != EC_PARSED_NOMATCH) {
122 child_parsed = ec_parsed_get_last_child(state);
123 ec_parsed_del_child(state, child_parsed);
124 ec_parsed_free(child_parsed);
125 ret = EC_PARSED_NOMATCH;
128 ec_strvec_free(new_vec);
134 ec_strvec_free(new_vec);
138 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
140 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
143 for (i = 0; i < node->len; i++) {
144 ec_free(node->table[i].pattern);
145 regfree(&node->table[i].r);
148 ec_free(node->table);
149 ec_node_free(node->child);
152 static struct ec_node_type ec_node_re_lex_type = {
154 .parse = ec_node_re_lex_parse,
155 //.complete = ec_node_re_lex_complete, //XXX
156 .size = sizeof(struct ec_node_re_lex),
157 .free_priv = ec_node_re_lex_free_priv,
160 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
162 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
164 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
165 struct regexp_pattern *table;
167 char *pat_dup = NULL;
170 pat_dup = ec_strdup(pattern);
175 table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
179 ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
182 "Regular expression <%s> compilation failed: %d\n",
184 if (ret == REG_ESPACE)
192 table[node->len].pattern = pat_dup;
193 table[node->len].keep = keep;
204 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
206 struct ec_node_re_lex *node = NULL;
211 node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
222 /* LCOV_EXCL_START */
223 static int ec_node_re_lex_testcase(void)
225 struct ec_node *node;
228 node = ec_node_re_lex(NULL,
231 ec_node_str(NULL, "foo"),
232 ec_node_str(NULL, "bar"),
233 ec_node_int(NULL, 0, 1000, 0)
238 EC_LOG(EC_LOG_ERR, "cannot create node\n");
242 /* XXX add ^ automatically ? */
243 ret |= ec_node_re_lex_add(node, "[a-zA-Z]+", 1);
244 ret |= ec_node_re_lex_add(node, "[0-9]+", 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, "\\+", 1);
248 ret |= ec_node_re_lex_add(node, "[ ]+", 0);
250 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
255 ret |= EC_TEST_CHECK_PARSE(node, 1, " foo bar 324 bar234");
256 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
257 ret |= EC_TEST_CHECK_PARSE(node, 1, "");
258 ret |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
266 static struct ec_test ec_node_re_lex_test = {
267 .name = "node_re_lex",
268 .test = ec_node_re_lex_testcase,
271 EC_TEST_REGISTER(ec_node_re_lex_test);