KPGBRXJOHTJ2FM7X7PH26SNBXOKWRUQU6FVQAACX6A5LO75LKWLQC
mod tokenizer {
#[derive(Debug, PartialEq, Eq)]
pub enum Token {
Char(u8), // A-Z a-z
Dot, // .
Alt, // |
Star, // *
Plus, // +
Quest, // ?
Lpar, // (
Rpar, // )
}
#[derive(Debug, PartialEq)]
pub enum TokenError {
NotAccepted(u8),
Miscellaneous(String),
}
pub fn map(input: u8) -> Result<Token, TokenError> {
if input.is_ascii() {
Ok(match input {
b'.' => Token::Dot,
b'|' => Token::Alt,
b'*' => Token::Star,
b'+' => Token::Plus,
b'?' => Token::Quest,
b'(' => Token::Lpar,
b')' => Token::Rpar,
x => Token::Char(x)
})
} else {
Err(TokenError::NotAccepted(input))
}
}
pub fn scan(input: &[u8]) -> Result<Vec<Token>, (TokenError, usize)> {
input
.iter()
.cloned()
.enumerate()
.map(|(pos, inp)| map(inp).map_err(|err| (err, pos)))
.collect()
}
#[cfg(test)]
mod tests {
use super::{map, Token};
#[test]
fn match_dot() {
assert_eq!(map(b'.'), Ok(Token::Dot))
}
#[test]
fn match_alternation() {
assert_eq!(map(b'|'), Ok(Token::Alt))
}
#[test]
fn match_star() {
assert_eq!(map(b'*'), Ok(Token::Star))
}
#[test]
fn match_plus() {
assert_eq!(map(b'+'), Ok(Token::Plus))
}
#[test]
fn match_question() {
assert_eq!(map(b'?'), Ok(Token::Quest))
}
#[test]
fn match_left_parenthesis() {
assert_eq!(map(b'('), Ok(Token::Lpar))
}
#[test]
fn match_right_parenthesis() {
assert_eq!(map(b')'), Ok(Token::Rpar))
}
#[test]
fn match_ascii() {
assert_eq!(map(b'a'), Ok(Token::Char(b'a')))
}
}
}
mod tokenizer {
#[derive(Debug, PartialEq, Eq)]
pub enum Token {
Char(u8), // A-Z a-z
Dot, // .
Alt, // |
Star, // *
Plus, // +
Quest, // ?
Lpar, // (
Rpar, // )
}
#[derive(Debug, PartialEq)]
pub enum TokenError {
NotAccepted(u8),
Miscellaneous(String),
}
pub fn map(input: u8) -> Result<Token, TokenError> {
if input.is_ascii() {
Ok(match input {
b'.' => Token::Dot,
b'|' => Token::Alt,
b'*' => Token::Star,
b'+' => Token::Plus,
b'?' => Token::Quest,
b'(' => Token::Lpar,
b')' => Token::Rpar,
x => Token::Char(x)
})
} else {
Err(TokenError::NotAccepted(input))
}
}
pub fn scan(input: &[u8]) -> Result<Vec<Token>, (TokenError, usize)> {
input
.iter()
.cloned()
.enumerate()
.map(|(pos, inp)| map(inp).map_err(|err| (err, pos)))
.collect()
}
#[cfg(test)]
mod tests {
use super::{map, Token};
#[test]
fn match_dot() {
assert_eq!(map(b'.'), Ok(Token::Dot))
}
#[test]
fn match_alternation() {
assert_eq!(map(b'|'), Ok(Token::Alt))
}
#[test]
fn match_star() {
assert_eq!(map(b'*'), Ok(Token::Star))
}
#[test]
fn match_plus() {
assert_eq!(map(b'+'), Ok(Token::Plus))
}
#[test]
fn match_question() {
assert_eq!(map(b'?'), Ok(Token::Quest))
}
#[test]
fn match_left_parenthesis() {
assert_eq!(map(b'('), Ok(Token::Lpar))
}
#[test]
fn match_right_parenthesis() {
assert_eq!(map(b')'), Ok(Token::Rpar))
}
#[test]
fn match_ascii() {
assert_eq!(map(b'a'), Ok(Token::Char(b'a')))
}
}
}