rex_regex/
lib.rs

1#![allow(dead_code)]
2
3mod compile;
4mod matcher;
5mod matching;
6mod optimize;
7mod parse;
8mod repr;
9mod state;
10
11mod tests;
12
13use std::iter::FromIterator;
14
15/// Easily take a substring from a match tuple.
16pub fn substring(s: &str, (from, len): (usize, usize)) -> String {
17    String::from_iter(s.chars().skip(from).take(len))
18}
19
20/// Render the state machine generated from `re` as graphviz `dot` input. The result can be pasted
21/// into `visualize.sh`, which renders a PNG image from it.
22pub fn render_graph(re: &str) -> String {
23    return format!(
24        "digraph st {{ {} }}",
25        state::dot(&compile::start_compile(parse::parse(re).as_ref().unwrap()))
26    );
27}
28
29/// Translate a regular expression string into an unoptimized `Pattern`. This is useful for
30/// inspecting (Pattern implements `Debug`) the parser output if there are unexpected effects.
31fn parse(re: &str) -> Result<repr::Pattern, String> {
32    return parse::parse(re);
33}
34
35/// Compiles a parsed regular expression into the internal state graph and matches s against it.
36/// Returns whether the string matched as well as a list of submatches. The first submatch is the
37/// entire matched string. A submatch is a tuple of (start, end), where end is the index of the
38/// first character that isn't part of the submatch anymore (i.e. [start, end)).
39fn compile_and_match(re: &repr::Pattern, s: &str) -> (bool, Vec<(usize, usize)>) {
40    let compiled = compile::start_compile(re);
41    matching::do_match(&compiled, s)
42}
43
44/// Parse, compile, and match a regular expression. Not recommended for repeated use, as the
45/// regular expression will be compiled every time. Use `compile()` and `match_re()` to make this
46/// more efficient (about 3x faster).
47pub fn match_re_str(re: &str, s: &str) -> Result<(bool, Vec<(usize, usize)>), String> {
48    return Ok(compile_and_match(&optimize::optimize(parse::parse(re)?), s));
49}
50
51/// Optimize and compile a regular expression into a representation that can be directly used for
52/// matching with `match_re()`.
53pub fn compile(re: &str) -> Result<state::CompiledRE, String> {
54    Ok(state::CompiledRE(compile::start_compile(
55        &optimize::optimize(parse(re)?),
56    )))
57}
58
59/// Match a regular expression compiled with `compile()` against a string. Returns a tuple of a
60/// boolean (whether there was a match or partial match) and a vector of `(position, length)`
61/// tuples for all submatches, where the first element describes the match by the whole regular
62/// expression.
63pub fn match_re(re: &state::CompiledRE, s: &str) -> (bool, Vec<(usize, usize)>) {
64    matching::do_match(&re.0, s)
65}