1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2014 Paul Sokolovsky
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <stdio.h>
28 #include <assert.h>
29 #include <string.h>
30 
31 #include "py/runtime.h"
32 #include "py/binary.h"
33 #include "py/objstr.h"
34 #include "py/stackctrl.h"
35 
36 #if MICROPY_PY_URE
37 
38 #define re1_5_stack_chk() MP_STACK_CHECK()
39 
40 #include "lib/re1.5/re1.5.h"
41 
42 #define FLAG_DEBUG 0x1000
43 
44 typedef struct _mp_obj_re_t {
45     mp_obj_base_t base;
46     ByteProg re;
47 } mp_obj_re_t;
48 
49 typedef struct _mp_obj_match_t {
50     mp_obj_base_t base;
51     int num_matches;
52     mp_obj_t str;
53     const char *caps[0];
54 } mp_obj_match_t;
55 
56 STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args);
57 #if !MICROPY_ENABLE_DYNRUNTIME
58 STATIC const mp_obj_type_t re_type;
59 #endif
60 
match_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)61 STATIC void match_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
62     (void)kind;
63     mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
64     mp_printf(print, "<match num=%d>", self->num_matches);
65 }
66 
match_group(mp_obj_t self_in,mp_obj_t no_in)67 STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
68     mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
69     mp_int_t no = mp_obj_get_int(no_in);
70     if (no < 0 || no >= self->num_matches) {
71         mp_raise_type_arg(&mp_type_IndexError, no_in);
72     }
73 
74     const char *start = self->caps[no * 2];
75     if (start == NULL) {
76         // no match for this group
77         return mp_const_none;
78     }
79     return mp_obj_new_str_of_type(mp_obj_get_type(self->str),
80         (const byte *)start, self->caps[no * 2 + 1] - start);
81 }
82 MP_DEFINE_CONST_FUN_OBJ_2(match_group_obj, match_group);
83 
84 #if MICROPY_PY_URE_MATCH_GROUPS
85 
match_groups(mp_obj_t self_in)86 STATIC mp_obj_t match_groups(mp_obj_t self_in) {
87     mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
88     if (self->num_matches <= 1) {
89         return mp_const_empty_tuple;
90     }
91     mp_obj_tuple_t *groups = MP_OBJ_TO_PTR(mp_obj_new_tuple(self->num_matches - 1, NULL));
92     for (int i = 1; i < self->num_matches; ++i) {
93         groups->items[i - 1] = match_group(self_in, MP_OBJ_NEW_SMALL_INT(i));
94     }
95     return MP_OBJ_FROM_PTR(groups);
96 }
97 MP_DEFINE_CONST_FUN_OBJ_1(match_groups_obj, match_groups);
98 
99 #endif
100 
101 #if MICROPY_PY_URE_MATCH_SPAN_START_END
102 
match_span_helper(size_t n_args,const mp_obj_t * args,mp_obj_t span[2])103 STATIC void match_span_helper(size_t n_args, const mp_obj_t *args, mp_obj_t span[2]) {
104     mp_obj_match_t *self = MP_OBJ_TO_PTR(args[0]);
105 
106     mp_int_t no = 0;
107     if (n_args == 2) {
108         no = mp_obj_get_int(args[1]);
109         if (no < 0 || no >= self->num_matches) {
110             mp_raise_type_arg(&mp_type_IndexError, args[1]);
111         }
112     }
113 
114     mp_int_t s = -1;
115     mp_int_t e = -1;
116     const char *start = self->caps[no * 2];
117     if (start != NULL) {
118         // have a match for this group
119         const char *begin = mp_obj_str_get_str(self->str);
120         s = start - begin;
121         e = self->caps[no * 2 + 1] - begin;
122     }
123 
124     span[0] = mp_obj_new_int(s);
125     span[1] = mp_obj_new_int(e);
126 }
127 
match_span(size_t n_args,const mp_obj_t * args)128 STATIC mp_obj_t match_span(size_t n_args, const mp_obj_t *args) {
129     mp_obj_t span[2];
130     match_span_helper(n_args, args, span);
131     return mp_obj_new_tuple(2, span);
132 }
133 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(match_span_obj, 1, 2, match_span);
134 
match_start(size_t n_args,const mp_obj_t * args)135 STATIC mp_obj_t match_start(size_t n_args, const mp_obj_t *args) {
136     mp_obj_t span[2];
137     match_span_helper(n_args, args, span);
138     return span[0];
139 }
140 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(match_start_obj, 1, 2, match_start);
141 
match_end(size_t n_args,const mp_obj_t * args)142 STATIC mp_obj_t match_end(size_t n_args, const mp_obj_t *args) {
143     mp_obj_t span[2];
144     match_span_helper(n_args, args, span);
145     return span[1];
146 }
147 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(match_end_obj, 1, 2, match_end);
148 
149 #endif
150 
151 #if !MICROPY_ENABLE_DYNRUNTIME
152 STATIC const mp_rom_map_elem_t match_locals_dict_table[] = {
153     { MP_ROM_QSTR(MP_QSTR_group), MP_ROM_PTR(&match_group_obj) },
154     #if MICROPY_PY_URE_MATCH_GROUPS
155     { MP_ROM_QSTR(MP_QSTR_groups), MP_ROM_PTR(&match_groups_obj) },
156     #endif
157     #if MICROPY_PY_URE_MATCH_SPAN_START_END
158     { MP_ROM_QSTR(MP_QSTR_span), MP_ROM_PTR(&match_span_obj) },
159     { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&match_start_obj) },
160     { MP_ROM_QSTR(MP_QSTR_end), MP_ROM_PTR(&match_end_obj) },
161     #endif
162 };
163 
164 STATIC MP_DEFINE_CONST_DICT(match_locals_dict, match_locals_dict_table);
165 
166 STATIC const mp_obj_type_t match_type = {
167     { &mp_type_type },
168     .name = MP_QSTR_match,
169     .print = match_print,
170     .locals_dict = (void *)&match_locals_dict,
171 };
172 #endif
173 
re_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)174 STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
175     (void)kind;
176     mp_obj_re_t *self = MP_OBJ_TO_PTR(self_in);
177     mp_printf(print, "<re %p>", self);
178 }
179 
ure_exec(bool is_anchored,uint n_args,const mp_obj_t * args)180 STATIC mp_obj_t ure_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
181     (void)n_args;
182     mp_obj_re_t *self;
183     if (mp_obj_is_type(args[0], &re_type)) {
184         self = MP_OBJ_TO_PTR(args[0]);
185     } else {
186         self = MP_OBJ_TO_PTR(mod_re_compile(1, args));
187     }
188     Subject subj;
189     size_t len;
190     subj.begin = mp_obj_str_get_data(args[1], &len);
191     subj.end = subj.begin + len;
192     int caps_num = (self->re.sub + 1) * 2;
193     mp_obj_match_t *match = m_new_obj_var(mp_obj_match_t, char *, caps_num);
194     // cast is a workaround for a bug in msvc: it treats const char** as a const pointer instead of a pointer to pointer to const char
195     memset((char *)match->caps, 0, caps_num * sizeof(char *));
196     int res = re1_5_recursiveloopprog(&self->re, &subj, match->caps, caps_num, is_anchored);
197     if (res == 0) {
198         m_del_var(mp_obj_match_t, char *, caps_num, match);
199         return mp_const_none;
200     }
201 
202     match->base.type = &match_type;
203     match->num_matches = caps_num / 2; // caps_num counts start and end pointers
204     match->str = args[1];
205     return MP_OBJ_FROM_PTR(match);
206 }
207 
re_match(size_t n_args,const mp_obj_t * args)208 STATIC mp_obj_t re_match(size_t n_args, const mp_obj_t *args) {
209     return ure_exec(true, n_args, args);
210 }
211 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_match_obj, 2, 4, re_match);
212 
re_search(size_t n_args,const mp_obj_t * args)213 STATIC mp_obj_t re_search(size_t n_args, const mp_obj_t *args) {
214     return ure_exec(false, n_args, args);
215 }
216 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_search_obj, 2, 4, re_search);
217 
re_split(size_t n_args,const mp_obj_t * args)218 STATIC mp_obj_t re_split(size_t n_args, const mp_obj_t *args) {
219     mp_obj_re_t *self = MP_OBJ_TO_PTR(args[0]);
220     Subject subj;
221     size_t len;
222     const mp_obj_type_t *str_type = mp_obj_get_type(args[1]);
223     subj.begin = mp_obj_str_get_data(args[1], &len);
224     subj.end = subj.begin + len;
225     int caps_num = (self->re.sub + 1) * 2;
226 
227     int maxsplit = 0;
228     if (n_args > 2) {
229         maxsplit = mp_obj_get_int(args[2]);
230     }
231 
232     mp_obj_t retval = mp_obj_new_list(0, NULL);
233     const char **caps = mp_local_alloc(caps_num * sizeof(char *));
234     while (true) {
235         // cast is a workaround for a bug in msvc: it treats const char** as a const pointer instead of a pointer to pointer to const char
236         memset((char **)caps, 0, caps_num * sizeof(char *));
237         int res = re1_5_recursiveloopprog(&self->re, &subj, caps, caps_num, false);
238 
239         // if we didn't have a match, or had an empty match, it's time to stop
240         if (!res || caps[0] == caps[1]) {
241             break;
242         }
243 
244         mp_obj_t s = mp_obj_new_str_of_type(str_type, (const byte *)subj.begin, caps[0] - subj.begin);
245         mp_obj_list_append(retval, s);
246         if (self->re.sub > 0) {
247             mp_raise_NotImplementedError(MP_ERROR_TEXT("splitting with sub-captures"));
248         }
249         subj.begin = caps[1];
250         if (maxsplit > 0 && --maxsplit == 0) {
251             break;
252         }
253     }
254     // cast is a workaround for a bug in msvc (see above)
255     mp_local_free((char **)caps);
256 
257     mp_obj_t s = mp_obj_new_str_of_type(str_type, (const byte *)subj.begin, subj.end - subj.begin);
258     mp_obj_list_append(retval, s);
259     return retval;
260 }
261 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_split_obj, 2, 3, re_split);
262 
263 #if MICROPY_PY_URE_SUB
264 
re_sub_helper(size_t n_args,const mp_obj_t * args)265 STATIC mp_obj_t re_sub_helper(size_t n_args, const mp_obj_t *args) {
266     mp_obj_re_t *self;
267     if (mp_obj_is_type(args[0], &re_type)) {
268         self = MP_OBJ_TO_PTR(args[0]);
269     } else {
270         self = MP_OBJ_TO_PTR(mod_re_compile(1, args));
271     }
272     mp_obj_t replace = args[1];
273     mp_obj_t where = args[2];
274     mp_int_t count = 0;
275     if (n_args > 3) {
276         count = mp_obj_get_int(args[3]);
277         // Note: flags are currently ignored
278     }
279 
280     size_t where_len;
281     const char *where_str = mp_obj_str_get_data(where, &where_len);
282     Subject subj;
283     subj.begin = where_str;
284     subj.end = subj.begin + where_len;
285     int caps_num = (self->re.sub + 1) * 2;
286 
287     vstr_t vstr_return;
288     vstr_return.buf = NULL; // We'll init the vstr after the first match
289     mp_obj_match_t *match = mp_local_alloc(sizeof(mp_obj_match_t) + caps_num * sizeof(char *));
290     match->base.type = &match_type;
291     match->num_matches = caps_num / 2; // caps_num counts start and end pointers
292     match->str = where;
293 
294     for (;;) {
295         // cast is a workaround for a bug in msvc: it treats const char** as a const pointer instead of a pointer to pointer to const char
296         memset((char *)match->caps, 0, caps_num * sizeof(char *));
297         int res = re1_5_recursiveloopprog(&self->re, &subj, match->caps, caps_num, false);
298 
299         // If we didn't have a match, or had an empty match, it's time to stop
300         if (!res || match->caps[0] == match->caps[1]) {
301             break;
302         }
303 
304         // Initialise the vstr if it's not already
305         if (vstr_return.buf == NULL) {
306             vstr_init(&vstr_return, match->caps[0] - subj.begin);
307         }
308 
309         // Add pre-match string
310         vstr_add_strn(&vstr_return, subj.begin, match->caps[0] - subj.begin);
311 
312         // Get replacement string
313         const char *repl = mp_obj_str_get_str((mp_obj_is_callable(replace) ? mp_call_function_1(replace, MP_OBJ_FROM_PTR(match)) : replace));
314 
315         // Append replacement string to result, substituting any regex groups
316         while (*repl != '\0') {
317             if (*repl == '\\') {
318                 ++repl;
319                 bool is_g_format = false;
320                 if (*repl == 'g' && repl[1] == '<') {
321                     // Group specified with syntax "\g<number>"
322                     repl += 2;
323                     is_g_format = true;
324                 }
325 
326                 if ('0' <= *repl && *repl <= '9') {
327                     // Group specified with syntax "\g<number>" or "\number"
328                     unsigned int match_no = 0;
329                     do {
330                         match_no = match_no * 10 + (*repl++ - '0');
331                     } while ('0' <= *repl && *repl <= '9');
332                     if (is_g_format && *repl == '>') {
333                         ++repl;
334                     }
335 
336                     if (match_no >= (unsigned int)match->num_matches) {
337                         mp_raise_type_arg(&mp_type_IndexError, MP_OBJ_NEW_SMALL_INT(match_no));
338                     }
339 
340                     const char *start_match = match->caps[match_no * 2];
341                     if (start_match != NULL) {
342                         // Add the substring matched by group
343                         const char *end_match = match->caps[match_no * 2 + 1];
344                         vstr_add_strn(&vstr_return, start_match, end_match - start_match);
345                     }
346                 } else if (*repl == '\\') {
347                     // Add the \ character
348                     vstr_add_byte(&vstr_return, *repl++);
349                 }
350             } else {
351                 // Just add the current byte from the replacement string
352                 vstr_add_byte(&vstr_return, *repl++);
353             }
354         }
355 
356         // Move start pointer to end of last match
357         subj.begin = match->caps[1];
358 
359         // Stop substitutions if count was given and gets to 0
360         if (count > 0 && --count == 0) {
361             break;
362         }
363     }
364 
365     mp_local_free(match);
366 
367     if (vstr_return.buf == NULL) {
368         // Optimisation for case of no substitutions
369         return where;
370     }
371 
372     // Add post-match string
373     vstr_add_strn(&vstr_return, subj.begin, subj.end - subj.begin);
374 
375     return mp_obj_new_str_from_vstr(mp_obj_get_type(where), &vstr_return);
376 }
377 
378 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_sub_obj, 3, 5, re_sub_helper);
379 
380 #endif
381 
382 #if !MICROPY_ENABLE_DYNRUNTIME
383 STATIC const mp_rom_map_elem_t re_locals_dict_table[] = {
384     { MP_ROM_QSTR(MP_QSTR_match), MP_ROM_PTR(&re_match_obj) },
385     { MP_ROM_QSTR(MP_QSTR_search), MP_ROM_PTR(&re_search_obj) },
386     { MP_ROM_QSTR(MP_QSTR_split), MP_ROM_PTR(&re_split_obj) },
387     #if MICROPY_PY_URE_SUB
388     { MP_ROM_QSTR(MP_QSTR_sub), MP_ROM_PTR(&re_sub_obj) },
389     #endif
390 };
391 
392 STATIC MP_DEFINE_CONST_DICT(re_locals_dict, re_locals_dict_table);
393 
394 STATIC const mp_obj_type_t re_type = {
395     { &mp_type_type },
396     .name = MP_QSTR_ure,
397     .print = re_print,
398     .locals_dict = (void *)&re_locals_dict,
399 };
400 #endif
401 
mod_re_compile(size_t n_args,const mp_obj_t * args)402 STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) {
403     (void)n_args;
404     const char *re_str = mp_obj_str_get_str(args[0]);
405     int size = re1_5_sizecode(re_str);
406     if (size == -1) {
407         goto error;
408     }
409     mp_obj_re_t *o = m_new_obj_var(mp_obj_re_t, char, size);
410     o->base.type = &re_type;
411     #if MICROPY_PY_URE_DEBUG
412     int flags = 0;
413     if (n_args > 1) {
414         flags = mp_obj_get_int(args[1]);
415     }
416     #endif
417     int error = re1_5_compilecode(&o->re, re_str);
418     if (error != 0) {
419     error:
420         mp_raise_ValueError(MP_ERROR_TEXT("error in regex"));
421     }
422     #if MICROPY_PY_URE_DEBUG
423     if (flags & FLAG_DEBUG) {
424         re1_5_dumpcode(&o->re);
425     }
426     #endif
427     return MP_OBJ_FROM_PTR(o);
428 }
429 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_compile_obj, 1, 2, mod_re_compile);
430 
431 #if !MICROPY_ENABLE_DYNRUNTIME
432 STATIC const mp_rom_map_elem_t mp_module_re_globals_table[] = {
433     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ure) },
434     { MP_ROM_QSTR(MP_QSTR_compile), MP_ROM_PTR(&mod_re_compile_obj) },
435     { MP_ROM_QSTR(MP_QSTR_match), MP_ROM_PTR(&re_match_obj) },
436     { MP_ROM_QSTR(MP_QSTR_search), MP_ROM_PTR(&re_search_obj) },
437     #if MICROPY_PY_URE_SUB
438     { MP_ROM_QSTR(MP_QSTR_sub), MP_ROM_PTR(&re_sub_obj) },
439     #endif
440     #if MICROPY_PY_URE_DEBUG
441     { MP_ROM_QSTR(MP_QSTR_DEBUG), MP_ROM_INT(FLAG_DEBUG) },
442     #endif
443 };
444 
445 STATIC MP_DEFINE_CONST_DICT(mp_module_re_globals, mp_module_re_globals_table);
446 
447 const mp_obj_module_t mp_module_ure = {
448     .base = { &mp_type_module },
449     .globals = (mp_obj_dict_t *)&mp_module_re_globals,
450 };
451 #endif
452 
453 // Source files #include'd here to make sure they're compiled in
454 // only if module is enabled by config setting.
455 
456 #define re1_5_fatal(x) assert(!x)
457 #include "lib/re1.5/compilecode.c"
458 #if MICROPY_PY_URE_DEBUG
459 #include "lib/re1.5/dumpcode.c"
460 #endif
461 #include "lib/re1.5/recursiveloop.c"
462 #include "lib/re1.5/charclass.c"
463 
464 #endif // MICROPY_PY_URE
465