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
254 lines (220 loc) · 6.7 KB
/
re.rs
File metadata and controls
254 lines (220 loc) · 6.7 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Regular expressions.
*
* This module fits the python re interface onto the rust regular expression
* system.
*/
use regex::{Match, Regex, RegexBuilder};
use std::fmt;
use crate::function::{Args, OptionalArg};
use crate::obj::objint::PyIntRef;
use crate::obj::objstr::PyStringRef;
use crate::obj::objtype::PyClassRef;
use crate::pyobject::{PyClassImpl, PyObjectRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
use num_traits::ToPrimitive;
// #[derive(Debug)]
#[pyclass(name = "Pattern")]
struct PyPattern {
regex: Regex,
pattern: String,
}
impl fmt::Debug for PyPattern {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Pattern()")
}
}
const IGNORECASE: usize = 2;
const MULTILINE: usize = 8;
const DOTALL: usize = 16;
const VERBOSE: usize = 64;
#[derive(Default)]
struct PyRegexFlags {
ignorecase: bool,
multiline: bool,
dotall: bool,
verbose: bool,
}
impl PyRegexFlags {
fn from_int(bits: usize) -> Self {
// TODO: detect unknown flag bits.
PyRegexFlags {
ignorecase: (bits & IGNORECASE) != 0,
multiline: (bits & MULTILINE) != 0,
dotall: (bits & DOTALL) != 0,
verbose: (bits & VERBOSE) != 0,
}
}
}
impl PyValue for PyPattern {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.class("re", "Pattern")
}
}
/// Inner data for a match object.
#[pyclass(name = "Match")]
struct PyMatch {
start: usize,
end: usize,
// m: Match<'t>,
}
impl fmt::Debug for PyMatch {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Match()")
}
}
impl PyValue for PyMatch {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.class("re", "Match")
}
}
// type PyPatternRef = PyRef<PyPattern>;
// type PyMatchRef = PyRef<PyMatch>;
fn re_match(
pattern: PyStringRef,
string: PyStringRef,
flags: OptionalArg<PyIntRef>,
vm: &VirtualMachine,
) -> PyResult {
let flags = extract_flags(flags);
let regex = make_regex(vm, &pattern.value, flags)?;
do_match(vm, ®ex, &string.value)
}
fn re_search(
pattern: PyStringRef,
string: PyStringRef,
flags: OptionalArg<PyIntRef>,
vm: &VirtualMachine,
) -> PyResult {
let flags = extract_flags(flags);
let regex = make_regex(vm, &pattern.value, flags)?;
do_search(vm, ®ex, &string.value)
}
fn do_match(vm: &VirtualMachine, regex: &PyPattern, search_text: &str) -> PyResult {
// TODO: implement match!
do_search(vm, regex, search_text)
}
fn do_search(vm: &VirtualMachine, regex: &PyPattern, search_text: &str) -> PyResult {
match regex.regex.find(search_text) {
None => Ok(vm.get_none()),
Some(result) => create_match(vm, &result),
}
}
fn make_regex(vm: &VirtualMachine, pattern: &str, flags: PyRegexFlags) -> PyResult<PyPattern> {
let r = RegexBuilder::new(pattern)
.case_insensitive(flags.ignorecase)
.multi_line(flags.multiline)
.dot_matches_new_line(flags.dotall)
.ignore_whitespace(flags.verbose)
.build()
.map_err(|err| vm.new_value_error(format!("Error in regex: {:?}", err)))?;
Ok(PyPattern {
regex: r,
pattern: pattern.to_string(),
})
}
/// Take a found regular expression and convert it to proper match object.
fn create_match(vm: &VirtualMachine, match_value: &Match) -> PyResult {
// 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);
Ok(PyMatch {
start: match_value.start(),
end: match_value.end(),
}
.into_ref(vm)
.into_object())
}
fn extract_flags(flags: OptionalArg<PyIntRef>) -> PyRegexFlags {
match flags {
OptionalArg::Present(flags) => {
PyRegexFlags::from_int(flags.as_bigint().to_usize().unwrap())
}
OptionalArg::Missing => Default::default(),
}
}
fn re_compile(
pattern: PyStringRef,
flags: OptionalArg<PyIntRef>,
vm: &VirtualMachine,
) -> PyResult<PyPattern> {
let flags = extract_flags(flags);
make_regex(vm, &pattern.value, flags)
}
fn re_escape(pattern: PyStringRef, _vm: &VirtualMachine) -> String {
regex::escape(&pattern.value)
}
fn re_purge(_vm: &VirtualMachine) {}
#[pyimpl]
impl PyPattern {
#[pymethod(name = "match")]
fn match_(&self, text: PyStringRef, vm: &VirtualMachine) -> PyResult {
do_match(vm, self, &text.value)
}
#[pymethod(name = "search")]
fn search(&self, text: PyStringRef, vm: &VirtualMachine) -> PyResult {
do_search(vm, self, &text.value)
}
#[pymethod(name = "sub")]
fn sub(&self, repl: PyStringRef, text: PyStringRef, vm: &VirtualMachine) -> PyResult {
// let replacer: &Replacer = ;
let replaced_text: String = self
.regex
.replace_all(&text.value, { repl.value.as_str() })
.into_owned();
Ok(vm.ctx.new_str(replaced_text))
}
#[pymethod(name = "subn")]
fn subn(&self, repl: PyStringRef, text: PyStringRef, vm: &VirtualMachine) -> PyResult {
self.sub(repl, text, vm)
}
#[pyproperty(name = "pattern")]
fn pattern(&self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_str(self.pattern.clone()))
}
}
#[pyimpl]
impl PyMatch {
#[pymethod(name = "start")]
fn start(&self, _group: OptionalArg<PyObjectRef>, _vm: &VirtualMachine) -> usize {
self.start
}
#[pymethod(name = "end")]
fn end(&self, _group: OptionalArg<PyObjectRef>, _vm: &VirtualMachine) -> usize {
self.end
}
#[pymethod(name = "group")]
fn group(&self, _groups: Args, _vm: &VirtualMachine) -> usize {
/*
let groups = groups.into_iter().collect();
if groups.len() == 1 {
} else {
}
*/
// println!("{:?}", groups);
self.start
}
}
/// Create the python `re` module with all its members.
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
let match_type = PyMatch::make_class(ctx);
let pattern_type = PyPattern::make_class(ctx);
py_module!(vm, "re", {
"compile" => ctx.new_rustfunc(re_compile),
"escape" => ctx.new_rustfunc(re_escape),
"purge" => ctx.new_rustfunc(re_purge),
"Match" => match_type,
"match" => ctx.new_rustfunc(re_match),
"Pattern" => pattern_type,
"search" => ctx.new_rustfunc(re_search),
"IGNORECASE" => ctx.new_int(IGNORECASE),
"I" => ctx.new_int(IGNORECASE),
"MULTILINE" => ctx.new_int(MULTILINE),
"M" => ctx.new_int(MULTILINE),
"VERBOSE" => ctx.new_int(VERBOSE),
"X" => ctx.new_int(VERBOSE),
"DOTALL" => ctx.new_int(DOTALL),
"S" => ctx.new_int(DOTALL),
})
}