dynamic log types
[protos/libecoli.git] / lib / ecoli_node_re_lex.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <string.h>
5 #include <regex.h>
6 #include <errno.h>
7
8 #include <ecoli_malloc.h>
9 #include <ecoli_log.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>
19
20 EC_LOG_TYPE_REGISTER(node_re_lex);
21
22 struct regexp_pattern {
23         char *pattern;
24         regex_t r;
25         bool keep;
26 };
27
28 struct ec_node_re_lex {
29         struct ec_node gen;
30         struct ec_node *child;
31         struct regexp_pattern *table;
32         size_t len;
33 };
34
35 static struct ec_strvec *
36 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
37 {
38         struct ec_strvec *strvec = NULL;
39         char *dup = NULL;
40         char c;
41         size_t len, off = 0;
42         size_t i;
43         int ret;
44         regmatch_t pos;
45
46         dup = ec_strdup(str);
47         if (dup == NULL)
48                 goto fail;
49
50         strvec = ec_strvec();
51         if (strvec == NULL)
52                 goto fail;
53
54         len = strlen(dup);
55         while (off < len) {
56                 for (i = 0; i < table_len; i++) {
57                         ret = regexec(&table[i].r, &dup[off], 1, &pos, 0);
58                         if (ret != 0)
59                                 continue;
60                         if (pos.rm_so != 0 || pos.rm_eo == 0) {
61                                 ret = -1;
62                                 continue;
63                         }
64
65                         if (table[i].keep == 0)
66                                 break;
67
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)
72                                 goto fail;
73
74                         dup[pos.rm_eo + off] = c;
75                         break;
76                 }
77
78                 if (ret != 0)
79                         goto fail;
80
81                 off += pos.rm_eo;
82         }
83
84         ec_free(dup);
85         return strvec;
86
87 fail:
88         ec_free(dup);
89         ec_strvec_free(strvec);
90         return NULL;
91 }
92
93 static int
94 ec_node_re_lex_parse(const struct ec_node *gen_node,
95                 struct ec_parsed *state,
96                 const struct ec_strvec *strvec)
97 {
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;
101         const char *str;
102         int ret;
103
104         if (ec_strvec_len(strvec) == 0) {
105                 new_vec = ec_strvec();
106         } else {
107                 str = ec_strvec_val(strvec, 0);
108                 new_vec = tokenize(node->table, node->len, str);
109         }
110         if (new_vec == NULL) {
111                 ret = -ENOMEM;
112                 goto fail;
113         }
114
115         ret = ec_node_parse_child(node->child, state, new_vec);
116         if (ret >= 0) {
117                 if ((unsigned)ret == ec_strvec_len(new_vec)) {
118                         ret = 1;
119                 } else {
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;
124                 }
125         }
126
127         ec_strvec_free(new_vec);
128         new_vec = NULL;
129
130         return ret;
131
132  fail:
133         ec_strvec_free(new_vec);
134         return ret;
135 }
136
137 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
138 {
139         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
140         unsigned int i;
141
142         for (i = 0; i < node->len; i++) {
143                 ec_free(node->table[i].pattern);
144                 regfree(&node->table[i].r);
145         }
146
147         ec_free(node->table);
148         ec_node_free(node->child);
149 }
150
151 static struct ec_node_type ec_node_re_lex_type = {
152         .name = "re_lex",
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,
157 };
158
159 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
160
161 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
162 {
163         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
164         struct regexp_pattern *table;
165         int ret;
166         char *pat_dup = NULL;
167
168         ret = -ENOMEM;
169         pat_dup = ec_strdup(pattern);
170         if (pat_dup == NULL)
171                 goto fail;
172
173         ret = -ENOMEM;
174         table = ec_realloc(node->table, sizeof(*table) * (node->len + 1));
175         if (table == NULL)
176                 goto fail;
177
178         ret = regcomp(&table[node->len].r, pattern, REG_EXTENDED);
179         if (ret != 0) {
180                 EC_LOG(EC_LOG_ERR,
181                         "Regular expression <%s> compilation failed: %d\n",
182                         pattern, ret);
183                 if (ret == REG_ESPACE)
184                         ret = -ENOMEM;
185                 else
186                         ret = -EINVAL;
187
188                 goto fail;
189         }
190
191         table[node->len].pattern = pat_dup;
192         table[node->len].keep = keep;
193         node->len++;
194         node->table = table;
195
196         return 0;
197
198 fail:
199         ec_free(pat_dup);
200         return ret;
201 }
202
203 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
204 {
205         struct ec_node_re_lex *node = NULL;
206
207         if (child == NULL)
208                 return NULL;
209
210         node = (struct ec_node_re_lex *)__ec_node(&ec_node_re_lex_type, id);
211         if (node == NULL) {
212                 ec_node_free(child);
213                 return NULL;
214         }
215
216         node->child = child;
217
218         return &node->gen;
219 }
220
221 /* LCOV_EXCL_START */
222 static int ec_node_re_lex_testcase(void)
223 {
224         struct ec_node *node;
225         int ret = 0;
226
227         node = ec_node_re_lex(NULL,
228                 ec_node_many(NULL,
229                         EC_NODE_OR(NULL,
230                                 ec_node_str(NULL, "foo"),
231                                 ec_node_str(NULL, "bar"),
232                                 ec_node_int(NULL, 0, 1000, 0)
233                         ), 0, 0
234                 )
235         );
236         if (node == NULL) {
237                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
238                 return -1;
239         }
240
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);
248         if (ret != 0) {
249                 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
250                 ec_node_free(node);
251                 return -1;
252         }
253
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");
258
259         ec_node_free(node);
260
261         return ret;
262 }
263 /* LCOV_EXCL_STOP */
264
265 static struct ec_test ec_node_re_lex_test = {
266         .name = "node_re_lex",
267         .test = ec_node_re_lex_testcase,
268 };
269
270 EC_TEST_REGISTER(ec_node_re_lex_test);