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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
pub use regex::Regex;
use regex::Error;
#[cfg(test)] pub mod test;
bitflags! {
flags Modifiers: u8 {
const MULTI_LINE = 0b00000001,
const CASE_INSENSITIVE = 0b00000010,
}
}
impl Modifiers {
pub fn new() -> Modifiers {
Modifiers::empty()
}
}
#[derive(Debug, Clone)]
pub struct Verex {
string: String,
modifiers: Modifiers,
source: String
}
impl Verex {
pub fn new() -> Verex {
Verex::from_string(String::new())
}
pub fn from_string(string: String) -> Verex {
let mut verex = Verex {
string: string,
modifiers: Modifiers::new(),
source: String::new()
};
verex.update_source_with_modifiers();
verex
}
pub fn from_str(string: &str) -> Verex {
Verex::from_string(string.to_string())
}
pub fn add(&mut self, value: &str) -> &mut Verex {
self.string.push_str(value);
self
}
pub fn update_source_with_modifiers(&mut self) -> &mut Verex {
self.source.clear();
self.source.push_str(r"(?");
if self.modifiers.contains(CASE_INSENSITIVE) {
self.source.push('i');
}
if self.modifiers.contains(MULTI_LINE) {
self.source.push('m');
}
self.source.push(':');
self.source.push_str(self.string.as_ref());
self.source.push(')');
self
}
pub fn compile(& self) -> Result<Regex, Error> {
Regex::new(self.source.as_ref())
}
pub fn raw(& self) -> &str {
self.source()
}
pub fn regex(& self) -> Result<Regex, Error> {
self.compile()
}
pub fn source(& self) -> &str {
self.source.as_ref()
}
pub fn value(& self) -> &str {
self.source()
}
pub fn open_class(&mut self) -> &mut Verex {
self.add(r"[")
}
pub fn close_class(&mut self) -> &mut Verex {
self.add(r"]")
}
pub fn open_group(&mut self) -> &mut Verex {
self.add(r"(?:")
}
pub fn open_capturing_group(&mut self) -> &mut Verex {
self.add(r"(")
}
pub fn close_group(&mut self) -> &mut Verex {
self.add(r")")
}
pub fn any(&mut self, chars: &str) -> &mut Verex {
self.open_class()
.add(chars)
.close_class();
self.update_source_with_modifiers()
}
pub fn any_of(&mut self, chars: &str) -> &mut Verex {
self.any(chars)
}
pub fn anything(&mut self) -> &mut Verex {
self.add(r"(.*)");
self.update_source_with_modifiers()
}
pub fn anything_but(&mut self, value: &str) -> &mut Verex {
self.open_group()
.open_class()
.add(r"^")
.add(value)
.close_class()
.add(r"*")
.close_group();
self.update_source_with_modifiers()
}
pub fn br(&mut self) -> &mut Verex {
self.line_break()
}
pub fn capture(&mut self, value: &str) -> &mut Verex {
self.open_capturing_group()
.add(value)
.close_group();
self.update_source_with_modifiers()
}
pub fn digit(&mut self) -> &mut Verex {
self.add(r"\d");
self.update_source_with_modifiers()
}
pub fn end_of_line(&mut self) -> &mut Verex {
self.add(r"$");
self.update_source_with_modifiers()
}
pub fn find(&mut self, value: &str) -> &mut Verex {
self.open_group()
.add(value)
.close_group();
self.update_source_with_modifiers()
}
pub fn line_break(&mut self) -> &mut Verex {
self.open_group()
.add(r"\n")
.or_find(r"\r\n")
.close_group();
self.update_source_with_modifiers()
}
pub fn maybe(&mut self, value: &str) -> &mut Verex {
self.open_group()
.add(value)
.close_group()
.add(r"?");
self.update_source_with_modifiers()
}
pub fn or(&mut self) -> &mut Verex {
self.add(r"|");
self.update_source_with_modifiers()
}
pub fn or_find(&mut self, value: &str) -> &mut Verex {
self.or()
.find(value)
}
pub fn range(&mut self, range: Vec<(char, char)>) -> &mut Verex {
let mut string = r"[".to_string();
for tuple in range {
let from = tuple.0;
let to = tuple.1;
string.push(from);
string.push('-');
string.push(to);
}
string.push(']');
self.add(string.as_ref());
self.update_source_with_modifiers()
}
pub fn replace(& self, text: &str, replacement: &str) -> Result<String, Error> {
let regex = try!(self.compile());
Ok(regex.replace(text, replacement))
}
pub fn search_one_line(&mut self, enable: bool) -> &mut Verex {
if enable {
self.modifiers.remove(MULTI_LINE);
}
else {
self.modifiers.insert(MULTI_LINE);
}
self.update_source_with_modifiers()
}
pub fn something(&mut self) -> &mut Verex {
self.add(r"(.+)");
self.update_source_with_modifiers()
}
pub fn something_but(&mut self, value: &str) -> &mut Verex {
self.open_group()
.open_class()
.add(r"^")
.add(value)
.close_class()
.add(r"+")
.close_group();
self.update_source_with_modifiers()
}
pub fn start_of_line(&mut self) -> &mut Verex {
self.add(r"^");
self.update_source_with_modifiers()
}
pub fn tab(&mut self) -> &mut Verex {
self.add(r"\t");
self.update_source_with_modifiers()
}
pub fn then(&mut self, value: &str) -> &mut Verex {
self.find(value)
}
pub fn with_any_case(&mut self, enable: bool) -> &mut Verex {
if enable {
self.modifiers.insert(CASE_INSENSITIVE);
}
else {
self.modifiers.remove(CASE_INSENSITIVE);
}
self.update_source_with_modifiers()
}
pub fn word(&mut self) -> &mut Verex {
self.find(r"\w+")
}
}
use std::fmt;
impl fmt::Display for Verex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.string)
}
}
impl Eq for Verex {}
use std::str::FromStr;
pub enum Void {}
impl FromStr for Verex {
type Err = Void;
fn from_str(s: &str) -> Result<Verex, Void> {
Ok(Verex::from_str(s))
}
}
impl PartialEq for Verex {
fn eq(&self, other: &Verex) -> bool {
self.string == other.string
}
}