2014-09-12 11:48:07 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 15:35:21 -05:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
2014-09-12 11:48:07 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_URE
|
|
|
|
|
2014-10-14 21:43:13 -04:00
|
|
|
#include "re1.5/re1.5.h"
|
2014-09-12 11:48:07 -04:00
|
|
|
|
|
|
|
#define FLAG_DEBUG 0x1000
|
|
|
|
|
|
|
|
typedef struct _mp_obj_re_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
ByteProg re;
|
|
|
|
} mp_obj_re_t;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_match_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
int num_matches;
|
|
|
|
mp_obj_t str;
|
|
|
|
const char *caps[0];
|
|
|
|
} mp_obj_match_t;
|
|
|
|
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void match_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2014-09-12 11:48:07 -04:00
|
|
|
mp_obj_match_t *self = self_in;
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<match num=%d>", self->num_matches);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
|
|
|
|
mp_obj_match_t *self = self_in;
|
2015-05-12 18:05:53 -04:00
|
|
|
mp_int_t no = mp_obj_get_int(no_in);
|
2015-02-18 09:47:14 -05:00
|
|
|
if (no < 0 || no >= self->num_matches) {
|
2014-09-12 11:48:07 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, no_in));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *start = self->caps[no * 2];
|
2015-03-04 08:51:32 -05:00
|
|
|
if (start == NULL) {
|
|
|
|
// no match for this group
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-09-12 11:48:07 -04:00
|
|
|
return mp_obj_new_str(start, self->caps[no * 2 + 1] - start, false);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(match_group_obj, match_group);
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t match_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_group), (mp_obj_t) &match_group_obj },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(match_locals_dict, match_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t match_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_match,
|
|
|
|
.print = match_print,
|
|
|
|
.locals_dict = (mp_obj_t)&match_locals_dict,
|
|
|
|
};
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2014-09-12 11:48:07 -04:00
|
|
|
mp_obj_re_t *self = self_in;
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<re %p>", self);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)n_args;
|
2014-09-12 11:48:07 -04:00
|
|
|
mp_obj_re_t *self = args[0];
|
|
|
|
Subject subj;
|
|
|
|
mp_uint_t len;
|
|
|
|
subj.begin = mp_obj_str_get_data(args[1], &len);
|
|
|
|
subj.end = subj.begin + len;
|
|
|
|
int caps_num = (self->re.sub + 1) * 2;
|
|
|
|
mp_obj_match_t *match = m_new_obj_var(mp_obj_match_t, char*, caps_num);
|
2015-03-16 04:27:46 -04:00
|
|
|
// 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
|
|
|
|
memset((char*)match->caps, 0, caps_num * sizeof(char*));
|
2014-09-12 11:48:07 -04:00
|
|
|
int res = re1_5_recursiveloopprog(&self->re, &subj, match->caps, caps_num, is_anchored);
|
|
|
|
if (res == 0) {
|
|
|
|
m_del_var(mp_obj_match_t, char*, caps_num, match);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
match->base.type = &match_type;
|
2015-02-18 09:47:14 -05:00
|
|
|
match->num_matches = caps_num / 2; // caps_num counts start and end pointers
|
2014-09-12 11:48:07 -04:00
|
|
|
match->str = args[1];
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2015-11-23 10:05:58 -05:00
|
|
|
STATIC mp_obj_t re_match(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-09-12 11:48:07 -04:00
|
|
|
return re_exec(true, n_args, args);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_match_obj, 2, 4, re_match);
|
|
|
|
|
2015-11-23 10:05:58 -05:00
|
|
|
STATIC mp_obj_t re_search(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-09-12 11:48:07 -04:00
|
|
|
return re_exec(false, n_args, args);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_search_obj, 2, 4, re_search);
|
|
|
|
|
2015-11-23 10:05:58 -05:00
|
|
|
STATIC mp_obj_t re_split(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-09-12 11:48:07 -04:00
|
|
|
mp_obj_re_t *self = args[0];
|
|
|
|
Subject subj;
|
|
|
|
mp_uint_t len;
|
|
|
|
subj.begin = mp_obj_str_get_data(args[1], &len);
|
|
|
|
subj.end = subj.begin + len;
|
|
|
|
int caps_num = (self->re.sub + 1) * 2;
|
|
|
|
|
|
|
|
int maxsplit = 0;
|
|
|
|
if (n_args > 2) {
|
2015-05-12 18:05:53 -04:00
|
|
|
maxsplit = mp_obj_get_int(args[2]);
|
2014-09-12 11:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t retval = mp_obj_new_list(0, NULL);
|
2014-11-16 04:56:22 -05:00
|
|
|
const char **caps = alloca(caps_num * sizeof(char*));
|
2014-09-12 11:48:07 -04:00
|
|
|
while (true) {
|
2015-03-16 04:27:46 -04:00
|
|
|
// 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
|
|
|
|
memset((char**)caps, 0, caps_num * sizeof(char*));
|
2014-09-12 11:48:07 -04:00
|
|
|
int res = re1_5_recursiveloopprog(&self->re, &subj, caps, caps_num, false);
|
|
|
|
|
|
|
|
// if we didn't have a match, or had an empty match, it's time to stop
|
|
|
|
if (!res || caps[0] == caps[1]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t s = mp_obj_new_str(subj.begin, caps[0] - subj.begin, false);
|
|
|
|
mp_obj_list_append(retval, s);
|
|
|
|
if (self->re.sub > 0) {
|
|
|
|
mp_not_implemented("Splitting with sub-captures");
|
|
|
|
}
|
|
|
|
subj.begin = caps[1];
|
|
|
|
if (maxsplit > 0 && --maxsplit == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t s = mp_obj_new_str(subj.begin, subj.end - subj.begin, false);
|
|
|
|
mp_obj_list_append(retval, s);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_split_obj, 2, 3, re_split);
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t re_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_match), (mp_obj_t) &re_match_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_search), (mp_obj_t) &re_search_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_split), (mp_obj_t) &re_split_obj },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(re_locals_dict, re_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t re_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_ure,
|
|
|
|
.print = re_print,
|
|
|
|
.locals_dict = (mp_obj_t)&re_locals_dict,
|
|
|
|
};
|
|
|
|
|
2015-11-23 10:05:58 -05:00
|
|
|
STATIC mp_obj_t mod_re_compile(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-09-12 11:48:07 -04:00
|
|
|
const char *re_str = mp_obj_str_get_str(args[0]);
|
|
|
|
int size = re1_5_sizecode(re_str);
|
2015-10-31 17:38:12 -04:00
|
|
|
if (size == -1) {
|
|
|
|
goto error;
|
|
|
|
}
|
2014-09-12 11:48:07 -04:00
|
|
|
mp_obj_re_t *o = m_new_obj_var(mp_obj_re_t, char, size);
|
|
|
|
o->base.type = &re_type;
|
|
|
|
int flags = 0;
|
|
|
|
if (n_args > 1) {
|
|
|
|
flags = mp_obj_get_int(args[1]);
|
|
|
|
}
|
|
|
|
int error = re1_5_compilecode(&o->re, re_str);
|
|
|
|
if (error != 0) {
|
2015-10-31 17:38:12 -04:00
|
|
|
error:
|
2014-09-12 11:48:07 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Error in regex"));
|
|
|
|
}
|
|
|
|
if (flags & FLAG_DEBUG) {
|
|
|
|
re1_5_dumpcode(&o->re);
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_compile_obj, 1, 2, mod_re_compile);
|
|
|
|
|
|
|
|
STATIC mp_obj_t mod_re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)n_args;
|
2014-09-12 11:48:07 -04:00
|
|
|
mp_obj_re_t *self = mod_re_compile(1, args);
|
|
|
|
|
|
|
|
const mp_obj_t args2[] = {self, args[1]};
|
|
|
|
mp_obj_match_t *match = re_exec(is_anchored, 2, args2);
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2015-11-23 10:05:58 -05:00
|
|
|
STATIC mp_obj_t mod_re_match(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-09-12 11:48:07 -04:00
|
|
|
return mod_re_exec(true, n_args, args);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_match_obj, 2, 4, mod_re_match);
|
|
|
|
|
2015-11-23 10:05:58 -05:00
|
|
|
STATIC mp_obj_t mod_re_search(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-09-12 11:48:07 -04:00
|
|
|
return mod_re_exec(false, n_args, args);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_search_obj, 2, 4, mod_re_search);
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t mp_module_re_globals_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ure) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_compile), (mp_obj_t)&mod_re_compile_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_match), (mp_obj_t)&mod_re_match_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_search), (mp_obj_t)&mod_re_search_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_DEBUG), MP_OBJ_NEW_SMALL_INT(FLAG_DEBUG) },
|
|
|
|
};
|
|
|
|
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_re_globals, mp_module_re_globals_table);
|
2014-09-12 11:48:07 -04:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_ure = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.name = MP_QSTR_ure,
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_re_globals,
|
|
|
|
};
|
|
|
|
|
2014-10-11 20:12:58 -04:00
|
|
|
// Source files #include'd here to make sure they're compiled in
|
|
|
|
// only if module is enabled by config setting.
|
|
|
|
|
|
|
|
#define re1_5_fatal(x) assert(!x)
|
|
|
|
#include "re1.5/compilecode.c"
|
|
|
|
#include "re1.5/dumpcode.c"
|
|
|
|
#include "re1.5/recursiveloop.c"
|
2014-10-14 21:43:13 -04:00
|
|
|
#include "re1.5/charclass.c"
|
2014-10-11 20:12:58 -04:00
|
|
|
|
2014-09-12 11:48:07 -04:00
|
|
|
#endif //MICROPY_PY_URE
|