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
use std::fmt;

use crate::tokens::{tokenize, TokenType};

type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Matcher {
    /// Match any token.
    Any,
    /// Match numbers only.
    Number,
}

#[derive(Debug, Clone)]
pub enum Error {
    MatchError,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::MatchError => write!(f, "unable to match specifiers with input"),
        }
    }
}

impl std::error::Error for Error {}

/// Match string with matchers.
///
/// # Arguments
///
/// * `s` - String slice to match.
/// * `matchers` - Slice of matchers to match with.
///
/// # Returns
///
/// A `Result` containing a `Vec` of indices pointing to the start of each match.
pub fn match_all(s: &str, matchers: &[Matcher]) -> Result<Vec<usize>> {
    let (token_indices, token_types) = tokenize(s);
    let indices = match_token(s, &token_indices, &token_types, matchers);
    if indices.len() != matchers.len() {
        return Err(Error::MatchError);
    }
    Ok(indices)
}

/// Check if token type matches matcher.
fn is_match(token_type: TokenType, matcher: &Matcher) -> bool {
    match matcher {
        Matcher::Any => true,
        Matcher::Number => token_type == TokenType::Number,
    }
}

/// Match token.
fn match_token(
    s: &str,
    token_indices: &[usize],
    token_types: &[TokenType],
    matchers: &[Matcher],
) -> Vec<usize> {
    if token_types.is_empty()
        || matchers.is_empty()
        || !is_match(token_types[0], &matchers[0])
        || (matchers.len() == 1 && token_indices.len() != 1 && matchers[0] != Matcher::Any)
    {
        return vec![];
    }
    let mut indices = vec![token_indices[0]];
    if matchers.len() > 1 {
        let next = match matchers[0] {
            Matcher::Any => match_any(s, &token_indices[1..], &token_types[1..], &matchers[1..]),
            _ => match_token(s, &token_indices[1..], &token_types[1..], &matchers[1..]),
        };
        if next.is_empty() {
            return vec![];
        }
        indices.extend(next);
    }
    indices
}

/// Match any matcher.
fn match_any(
    s: &str,
    token_indices: &[usize],
    token_types: &[TokenType],
    matchers: &[Matcher],
) -> Vec<usize> {
    if token_indices.is_empty() || matchers.is_empty() {
        return vec![];
    }
    let mut next = vec![];
    let mut next_token_idx = 0;
    while next.is_empty() {
        if next_token_idx == token_indices.len() {
            return vec![];
        }
        next = match_token(
            s,
            &token_indices[next_token_idx..],
            &token_types[next_token_idx..],
            &matchers,
        );
        next_token_idx += 1;
    }
    next
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! match_tests {
        ($($name:ident: $value:expr,)*) => {
            $(
                #[test]
                fn $name() {
                    let (s, matchers, expected) = $value;
                    assert_eq!(match_all(s, matchers).unwrap(), expected);
                }
            )*
        }
    }

    match_tests!(
        match_1: ("abc", &[Matcher::Any], &[0]),
        match_2: ("abc123", &[Matcher::Any, Matcher::Any], &[0, 3]),
        match_2_to_1: ("abc123", &[Matcher::Any], &[0]),
        match_4_to_2: (
            "abc def456",
            &[Matcher::Any, Matcher::Any],
            &[0, 3],
        ),
        match_number: (
            "abc def456",
            &[Matcher::Any, Matcher::Number],
            &[0, 7],
        ),
        match_skip: (
            "abc123def456",
            &[Matcher::Any, Matcher::Number],
            &[0, 9],
        ),
    );

    macro_rules! match_fail_tests {
        ($($name:ident: $value:expr,)*) => {
            $(
                #[test]
                fn $name() {
                    let (s, specs) = $value;
                    assert!(match_all(s, specs).is_err());
                }
            )*
        }
    }

    match_fail_tests!(
        match_no_match: (
            "abc123def456",
            &[Matcher::Any, Matcher::Number, Matcher::Number],
        ),
    );
}