Skip to content

Commit 792c6a1

Browse files
committed
Convert re to new style args
1 parent 149aefe commit 792c6a1

File tree

1 file changed

+56
-128
lines changed

1 file changed

+56
-128
lines changed

vm/src/stdlib/re.rs

+56-128
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
*/
77
use regex::{Match, Regex};
88

9-
use crate::function::PyFuncArgs;
10-
use crate::obj::objstr;
119
use crate::obj::objstr::PyStringRef;
1210
use crate::obj::objtype::PyClassRef;
13-
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
11+
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
1412
use crate::vm::VirtualMachine;
1513

1614
impl PyValue for Regex {
@@ -19,73 +17,39 @@ impl PyValue for Regex {
1917
}
2018
}
2119

22-
/// Create the python `re` module with all its members.
23-
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
24-
let match_type = py_class!(ctx, "Match", ctx.object(), {
25-
"start" => ctx.new_rustfunc(match_start),
26-
"end" => ctx.new_rustfunc(match_end)
27-
});
28-
29-
let pattern_type = py_class!(ctx, "Pattern", ctx.object(), {
30-
"match" => ctx.new_rustfunc(pattern_match),
31-
"search" => ctx.new_rustfunc(pattern_search)
32-
});
20+
/// Inner data for a match object.
21+
#[derive(Debug)]
22+
struct PyMatch {
23+
start: usize,
24+
end: usize,
25+
}
3326

34-
py_module!(ctx, "re", {
35-
"compile" => ctx.new_rustfunc(re_compile),
36-
"Match" => match_type,
37-
"match" => ctx.new_rustfunc(re_match),
38-
"Pattern" => pattern_type,
39-
"search" => ctx.new_rustfunc(re_search)
40-
})
27+
impl PyValue for PyMatch {
28+
fn class(vm: &VirtualMachine) -> PyClassRef {
29+
vm.class("re", "Match")
30+
}
4131
}
4232

43-
/// Implement re.match
44-
/// See also:
45-
/// https://docs.python.org/3/library/re.html#re.match
46-
fn re_match(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
47-
arg_check!(
48-
vm,
49-
args,
50-
required = [
51-
(pattern, Some(vm.ctx.str_type())),
52-
(string, Some(vm.ctx.str_type()))
53-
]
54-
);
55-
let pattern_str = objstr::get_value(&pattern);
56-
let regex = make_regex(vm, &pattern_str)?;
57-
let search_text = objstr::get_value(string);
58-
59-
do_match(vm, &regex, search_text)
33+
type PyRegexRef = PyRef<Regex>;
34+
type PyMatchRef = PyRef<PyMatch>;
35+
36+
fn re_match(pattern: PyStringRef, string: PyStringRef, vm: &VirtualMachine) -> PyResult {
37+
let regex = make_regex(vm, &pattern.value)?;
38+
do_match(vm, &regex, &string.value)
6039
}
6140

62-
/// Implement re.search
63-
/// See also:
64-
/// https://docs.python.org/3/library/re.html#re.search
65-
fn re_search(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
66-
arg_check!(
67-
vm,
68-
args,
69-
required = [
70-
(pattern, Some(vm.ctx.str_type())),
71-
(string, Some(vm.ctx.str_type()))
72-
]
73-
);
74-
75-
let pattern_str = objstr::get_value(&pattern);
76-
let regex = make_regex(vm, &pattern_str)?;
77-
let search_text = objstr::get_value(string);
78-
79-
do_search(vm, &regex, search_text)
41+
fn re_search(pattern: PyStringRef, string: PyStringRef, vm: &VirtualMachine) -> PyResult {
42+
let regex = make_regex(vm, &pattern.value)?;
43+
do_search(vm, &regex, &string.value)
8044
}
8145

82-
fn do_match(vm: &VirtualMachine, regex: &Regex, search_text: String) -> PyResult {
46+
fn do_match(vm: &VirtualMachine, regex: &Regex, search_text: &str) -> PyResult {
8347
// TODO: implement match!
8448
do_search(vm, regex, search_text)
8549
}
8650

87-
fn do_search(vm: &VirtualMachine, regex: &Regex, search_text: String) -> PyResult {
88-
match regex.find(&search_text) {
51+
fn do_search(vm: &VirtualMachine, regex: &Regex, search_text: &str) -> PyResult {
52+
match regex.find(search_text) {
8953
None => Ok(vm.get_none()),
9054
Some(result) => create_match(vm, &result),
9155
}
@@ -98,19 +62,6 @@ fn make_regex(vm: &VirtualMachine, pattern: &str) -> PyResult<Regex> {
9862
}
9963
}
10064

101-
/// Inner data for a match object.
102-
#[derive(Debug)]
103-
struct PyMatch {
104-
start: usize,
105-
end: usize,
106-
}
107-
108-
impl PyValue for PyMatch {
109-
fn class(vm: &VirtualMachine) -> PyClassRef {
110-
vm.class("re", "Match")
111-
}
112-
}
113-
11465
/// Take a found regular expression and convert it to proper match object.
11566
fn create_match(vm: &VirtualMachine, match_value: &Match) -> PyResult {
11667
// let mo = vm.invoke(match_class, PyFuncArgs::default())?;
@@ -124,68 +75,45 @@ fn create_match(vm: &VirtualMachine, match_value: &Match) -> PyResult {
12475
.into_object())
12576
}
12677

127-
/// Compile a regular expression into a Pattern object.
128-
/// See also:
129-
/// https://docs.python.org/3/library/re.html#re.compile
130-
fn re_compile(pattern: PyStringRef, vm: &VirtualMachine) -> PyResult<PyRef<Regex>> {
131-
let regex = make_regex(vm, &pattern.value)?;
132-
133-
Ok(regex.into_ref(vm))
78+
fn re_compile(pattern: PyStringRef, vm: &VirtualMachine) -> PyResult<Regex> {
79+
make_regex(vm, &pattern.value)
13480
}
13581

136-
fn pattern_match(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
137-
arg_check!(
138-
vm,
139-
args,
140-
required = [(zelf, None), (text, Some(vm.ctx.str_type()))]
141-
);
142-
143-
let regex = get_regex(zelf);
144-
let search_text = objstr::get_value(text);
145-
do_match(vm, &regex, search_text)
146-
}
147-
148-
fn pattern_search(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
149-
arg_check!(
150-
vm,
151-
args,
152-
required = [(zelf, None), (text, Some(vm.ctx.str_type()))]
153-
);
154-
155-
let regex = get_regex(zelf);
156-
let search_text = objstr::get_value(text);
157-
do_search(vm, &regex, search_text)
82+
impl PyRegexRef {
83+
fn match_(self, text: PyStringRef, vm: &VirtualMachine) -> PyResult {
84+
do_match(vm, &self, &text.value)
85+
}
86+
fn search(self, text: PyStringRef, vm: &VirtualMachine) -> PyResult {
87+
do_search(vm, &self, &text.value)
88+
}
15889
}
15990

160-
/// Returns start of match
161-
/// see: https://docs.python.org/3/library/re.html#re.Match.start
162-
fn match_start(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
163-
arg_check!(vm, args, required = [(zelf, None)]);
164-
// TODO: implement groups
165-
let m = get_match(zelf);
166-
Ok(vm.new_int(m.start))
91+
impl PyMatchRef {
92+
fn start(self, _vm: &VirtualMachine) -> usize {
93+
self.start
94+
}
95+
fn end(self, _vm: &VirtualMachine) -> usize {
96+
self.end
97+
}
16798
}
16899

169-
fn match_end(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
170-
arg_check!(vm, args, required = [(zelf, None)]);
171-
// TODO: implement groups
172-
let m = get_match(zelf);
173-
Ok(vm.new_int(m.end))
174-
}
100+
/// Create the python `re` module with all its members.
101+
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
102+
let match_type = py_class!(ctx, "Match", ctx.object(), {
103+
"start" => ctx.new_rustfunc(PyMatchRef::start),
104+
"end" => ctx.new_rustfunc(PyMatchRef::end)
105+
});
175106

176-
/// Retrieve inner rust regex from python object:
177-
fn get_regex(obj: &PyObjectRef) -> &Regex {
178-
// TODO: Regex shouldn't be stored in payload directly, create newtype wrapper
179-
if let Some(regex) = obj.payload::<Regex>() {
180-
return regex;
181-
}
182-
panic!("Inner error getting regex {:?}", obj);
183-
}
107+
let pattern_type = py_class!(ctx, "Pattern", ctx.object(), {
108+
"match" => ctx.new_rustfunc(PyRegexRef::match_),
109+
"search" => ctx.new_rustfunc(PyRegexRef::search)
110+
});
184111

185-
/// Retrieve inner rust match from python object:
186-
fn get_match(obj: &PyObjectRef) -> &PyMatch {
187-
if let Some(value) = obj.payload::<PyMatch>() {
188-
return value;
189-
}
190-
panic!("Inner error getting match {:?}", obj);
112+
py_module!(ctx, "re", {
113+
"compile" => ctx.new_rustfunc(re_compile),
114+
"Match" => match_type,
115+
"match" => ctx.new_rustfunc(re_match),
116+
"Pattern" => pattern_type,
117+
"search" => ctx.new_rustfunc(re_search)
118+
})
191119
}

0 commit comments

Comments
 (0)