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