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
//! This crate provides a Rust implementation of VerbalExpressions in order to build regex
//! strings without knowing the minutiae of regex syntax.
//!
//! It uses the `Regex` crate to compile the created regex strings.
//!
//! # Examples
//!
//! A simple example to show the usage:
//!
//! ```rust
//! # extern crate verex;
//! use verex::Verex;
//! use verex::find;
//!
//! # fn main() {
//!     // You can either use a mutable Verex to define different regexes
//!     let mut verex = Verex::new();
//!     let regex1 = verex.find("a")
//!                       .compile()
//!                       .unwrap();
//!
//!     let regex2 = verex.or_find("b")
//!                       .compile()
//!                       .unwrap();
//!
//!     // Or just use it for building one (you can use the funcitons directly as constructors)
//!     let regex3 = find("a") // or: Verex::new().find("a")
//!                  .or_find("b")
//!                  .compile()
//!                  .unwrap();
//!
//!     // Test whether the regexes match correctly
//!     assert!(!regex1.is_match("b"));
//!     assert!(regex2.is_match("b"));
//!     assert!(regex3.is_match("b"));
//!
//!     // Test the generated regex strings
//!     assert_eq!(regex1.as_str(), r"(?:(?:a))");
//!     assert_eq!(regex2.as_str(), r"(?:(?:a)|(?:b))");
//!     assert_eq!(regex3.as_str(), r"(?:(?:a)|(?:b))");
//! # }
//! ```
//!
//!
//! Here's a URL testing example shamelessly stolen from the python Verex readme:
//!
//! ```rust
//! # extern crate verex;
//! use verex::start_of_line;
//!
//! # fn main() {
//!     // Create an example of how to test for correctly formed URLs
//!     let verex = start_of_line()
//!                 .find("http")
//!                 .maybe("s")
//!                 .find("://")
//!                 .maybe("www.")
//!                 .anything_but(" ")
//!                 .end_of_line()
//!                 .clone();
//!     let regex = verex.compile().unwrap();
//!
//!     // Create an example URL
//!     let test_url = r"https://www.google.com";
//!
//!     // Test if the URL is valid
//!     assert!(regex.is_match(test_url));
//!
//!     // Test the generated regex string
//!     assert_eq!(verex.source(), r"(?:^(?:http)(?:s)?(?:://)(?:www\.)?(?:[^ ]*)$)");
//! # }
//! ```
//!
//! Example usage of the `or!` macro:
//!
//! ```rust
//! #[macro_use(or)]
//! extern crate verex;
//!
//! # fn main() {
//!     let regex = or!("foo", "bar", "baz")
//!                 .compile()
//!                 .unwrap();
//!
//!     // Test if the regex matches correctly
//!     assert!(regex.is_match("foo"));
//!     assert!(regex.is_match("bar"));
//!     assert!(regex.is_match("baz"));
//!     assert!(!regex.is_match("bum"));
//!
//!     // Test the generated regex string
//!     assert_eq!(regex.as_str(), r"(?:(?:foo)|(?:bar)|(?:baz))");
//! # }
//! ```
//!
//! Example usage of the `or_expr!` macro:
//!
//! ```rust
//! #[macro_use(or_expr)]
//! extern crate verex;
//! extern crate regex;
//! use verex::Expression as E;
//! use verex::Verex;
//! use regex::Regex;
//!
//! # fn main() {
//!     let sub_verex = Verex::from_str("Darth(Vader)*?");
//!     let sub_regex = Regex::new("(?P<robot>C3PO)").unwrap();
//!     let regex = or_expr!(
//!                     E::String("([RD]2){2}"),
//!                     E::Verex(&sub_verex),
//!                     E::Regex(&sub_regex))
//!                 .compile()
//!                 .unwrap();
//!
//!     // Test if the regex matches correctly
//!     assert!(regex.is_match("R2D2"));
//!     assert!(regex.is_match("Darth"));
//!     assert!(regex.is_match("C3PO"));
//!     assert!(!regex.is_match("Anakin"));
//!
//!     // Test the generated regex string
//!     assert_eq!(regex.as_str(), r"(?:(?:([RD]2){2})|(?:(?:Darth(Vader)*?))|(?:(?P<robot>C3PO)))");
//! # }
//! ```

#![warn(missing_docs)]

#[macro_use]
extern crate bitflags;
extern crate regex;

pub use verex::Verex;
pub use verex::Expression;

mod verex;

// standalone functions
/// Any of the given characters
pub fn any(chars: &str) -> Verex {
    Verex::new().any(chars).clone()
}

/// See any()
pub fn any_of(chars: &str) -> Verex {
    any(chars)
}

/// Any character zero or more times
pub fn anything() -> Verex {
    Verex::new().anything().clone()
}

/// Any character zero or more times except the provided characters
pub fn anything_but(chars: &str) -> Verex {
    Verex::new().anything_but(chars).clone()
}

/// A line break!
pub fn br() -> Verex {
    line_break()
}

/// Find a specific string and capture it (will get escaped)
pub fn capture(value: &str) -> Verex {
    Verex::new().capture(value).clone()
}

/// Find an expression and capture it
pub fn capture_expr(expr: Expression) -> Verex {
    Verex::new().capture_expr(expr).clone()
}

/// Add the token for matching digits
pub fn digit() -> Verex {
    Verex::new().digit().clone()
}

/// Add a token for the end of a line
pub fn end_of_line() -> Verex {
    Verex::new().end_of_line().clone()
}

/// Find a specific string
pub fn find(value: &str) -> Verex {
    Verex::new().find(value).clone()
}

/// Find an expression
pub fn find_expr(expr: Expression) -> Verex {
    Verex::new().find_expr(expr).clone()
}

/// A line break!
pub fn line_break() -> Verex {
    Verex::new().line_break().clone()
}

/// Any string either one or zero times
pub fn maybe(value: &str) -> Verex {
    Verex::new().maybe(value).clone()
}

/// Any string either one or zero times
pub fn maybe_expr(expr: Expression) -> Verex {
    Verex::new().maybe_expr(expr).clone()
}

/// Match any of the given values
#[macro_export]
macro_rules! or {
    ( $first_string:expr, $( $string:expr ),* ) => {
        {
            let mut verex = $crate::Verex::new();
            verex.find($first_string);
            $(
                verex.or_find($string);
            )*
            verex
        }
    };
}

/// Match any of the given sub-expressions
#[macro_export]
macro_rules! or_expr {
    ( $first_e:expr, $( $e:expr ),* ) => {
        {
            let mut verex = $crate::Verex::new();
            verex.find_expr($first_e);
            $(
                verex.or_find_expr($e);
            )*
            verex
        }
    };
}

/// A range of characters e.g. [A-Z]
/// Usage example: verex.range(vec![('a', 'z'),('A', 'Z')])
pub fn range(range: Vec<(char, char)>) -> Verex {
    Verex::new().range(range).clone()
}

/// Toggle whether ^ and $ match line start and end or string start and end
pub fn search_one_line(enable: bool) -> Verex {
    Verex::new().search_one_line(enable).clone()
}

/// Any character at least one time
pub fn something() -> Verex {
    Verex::new().something().clone()
}

/// Any character at least one time except for these characters
pub fn something_but(chars: &str) -> Verex {
    Verex::new().something_but(chars).clone()
}

/// Add a token for the start of a line
pub fn start_of_line() -> Verex {
    Verex::new().start_of_line().clone()
}

/// Add a token for a tab
pub fn tab() -> Verex {
    Verex::new().tab().clone()
}

/// Toggle whether to match case-sensitively or not
pub fn with_any_case(enable: bool) -> Verex {
    Verex::new().with_any_case(enable).clone()
}

/// Any alphanumeric characters
pub fn word() -> Verex {
    Verex::new().word().clone()
}