forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathre.rs
More file actions
210 lines (181 loc) · 6.07 KB
/
re.rs
File metadata and controls
210 lines (181 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Regular expressions.
*
* This module fits the python re interface onto the rust regular expression
* system.
*/
// extern crate regex;
use crate::import;
use regex::{Match, Regex};
use std::path::PathBuf;
use crate::obj::objstr;
use crate::pyobject::{
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
};
use crate::VirtualMachine;
/// Create the python `re` module with all its members.
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let match_type = py_class!(ctx, "Match", ctx.object(), {
"start" => ctx.new_rustfunc(match_start),
"end" => ctx.new_rustfunc(match_end)
});
let pattern_type = py_class!(ctx, "Pattern", ctx.object(), {
"match" => ctx.new_rustfunc(pattern_match),
"search" => ctx.new_rustfunc(pattern_search)
});
py_module!(ctx, "re", {
"compile" => ctx.new_rustfunc(re_compile),
"Match" => match_type,
"match" => ctx.new_rustfunc(re_match),
"Pattern" => pattern_type,
"search" => ctx.new_rustfunc(re_search)
})
}
/// Implement re.match
/// See also:
/// https://docs.python.org/3/library/re.html#re.match
fn re_match(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(pattern, Some(vm.ctx.str_type())),
(string, Some(vm.ctx.str_type()))
]
);
let regex = make_regex(vm, pattern)?;
let search_text = objstr::get_value(string);
do_match(vm, ®ex, search_text)
}
/// Implement re.search
/// See also:
/// https://docs.python.org/3/library/re.html#re.search
fn re_search(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(pattern, Some(vm.ctx.str_type())),
(string, Some(vm.ctx.str_type()))
]
);
// let pattern_str = objstr::get_value(&pattern);
let regex = make_regex(vm, pattern)?;
let search_text = objstr::get_value(string);
do_search(vm, ®ex, search_text)
}
fn do_match(vm: &mut VirtualMachine, regex: &Regex, search_text: String) -> PyResult {
// TODO: implement match!
do_search(vm, regex, search_text)
}
fn do_search(vm: &mut VirtualMachine, regex: &Regex, search_text: String) -> PyResult {
match regex.find(&search_text) {
None => Ok(vm.get_none()),
Some(result) => create_match(vm, &result),
}
}
fn make_regex(vm: &mut VirtualMachine, pattern: &PyObjectRef) -> PyResult<Regex> {
let pattern_str = objstr::get_value(pattern);
match Regex::new(&pattern_str) {
Ok(regex) => Ok(regex),
Err(err) => Err(vm.new_value_error(format!("Error in regex: {:?}", err))),
}
}
/// Inner data for a match object.
struct PyMatch {
start: usize,
end: usize,
}
/// Take a found regular expression and convert it to proper match object.
fn create_match(vm: &mut VirtualMachine, match_value: &Match) -> PyResult {
// Return match object:
// TODO: implement match object
// TODO: how to refer to match object defined in this
let module = import::import_module(vm, PathBuf::default(), "re").unwrap();
let match_class = vm.ctx.get_attr(&module, "Match").unwrap();
// let mo = vm.invoke(match_class, PyFuncArgs::default())?;
// let txt = vm.ctx.new_str(result.as_str().to_string());
// vm.ctx.set_attr(&mo, "str", txt);
let match_value = PyMatch {
start: match_value.start(),
end: match_value.end(),
};
Ok(PyObject::new(
PyObjectPayload::AnyRustValue {
value: Box::new(match_value),
},
match_class.clone(),
))
}
/// Compile a regular expression into a Pattern object.
/// See also:
/// https://docs.python.org/3/library/re.html#re.compile
fn re_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(pattern, Some(vm.ctx.str_type()))] // TODO: flags=0
);
let regex = make_regex(vm, pattern)?;
// TODO: retrieval of this module is akward:
let module = import::import_module(vm, PathBuf::default(), "re").unwrap();
let pattern_class = vm.ctx.get_attr(&module, "Pattern").unwrap();
Ok(PyObject::new(
PyObjectPayload::AnyRustValue {
value: Box::new(regex),
},
pattern_class.clone(),
))
}
fn pattern_match(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(zelf, None), (text, Some(vm.ctx.str_type()))]
);
let regex = get_regex(zelf);
let search_text = objstr::get_value(text);
do_match(vm, ®ex, search_text)
}
fn pattern_search(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(zelf, None), (text, Some(vm.ctx.str_type()))]
);
let regex = get_regex(zelf);
let search_text = objstr::get_value(text);
do_search(vm, ®ex, search_text)
}
/// Returns start of match
/// see: https://docs.python.org/3/library/re.html#re.Match.start
fn match_start(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, None)]);
// TODO: implement groups
let m = get_match(zelf);
Ok(vm.new_int(m.start))
}
fn match_end(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, None)]);
// TODO: implement groups
let m = get_match(zelf);
Ok(vm.new_int(m.end))
}
/// Retrieve inner rust regex from python object:
fn get_regex<'a>(obj: &'a PyObjectRef) -> &'a Regex {
if let PyObjectPayload::AnyRustValue { ref value } = obj.payload {
if let Some(regex) = value.downcast_ref::<Regex>() {
return regex;
}
}
panic!("Inner error getting regex {:?}", obj);
}
/// Retrieve inner rust match from python object:
fn get_match<'a>(obj: &'a PyObjectRef) -> &'a PyMatch {
if let PyObjectPayload::AnyRustValue { ref value } = obj.payload {
if let Some(value) = value.downcast_ref::<PyMatch>() {
return value;
}
}
panic!("Inner error getting match {:?}", obj);
}