A generic lexer in Rust using a simple match function or closure
```rust use generic_lexer::{Lexer, LexerInput, MatchError};
enum TokenKind { Int, Float, Name, Plus, Minus, Star, Slash, Semicolon, Equals, }
fn lexint(input: &mut LexerInput) -> TokenKind { input.nextwhile(char::isasciidigit); if input.test('.') { return lex_float(input); } TokenKind::Int }
fn lexfloat(input: &mut LexerInput) -> TokenKind { input.nextwhile(char::isasciidigit); TokenKind::Float }
fn lexname(input: &mut LexerInput) -> TokenKind { input.nextwhile(|&c| c == '' || c.isascii_alphabetic()); TokenKind::Name }
fn lex(firstchar: char, input: &mut LexerInput) -> Result
c if c.is_ascii_digit() => lex_int(input),
c if c.is_ascii_alphabetic() => lex_name(input),
c => return Err(MatchError::Custom(format!("Unexpected '{}'", c)))
};
Ok(kind)
}
fn main() -> Result<(), Box
[
Token {
kind: Name,
text: "a",
},
Token {
kind: Equals,
text: "=",
},
Token {
kind: Int,
text: "420",
},
Token {
kind: Plus,
text: "+",
},
Token {
kind: Int,
text: "69",
},
Token {
kind: Star,
text: "*",
},
Token {
kind: Float,
text: "3.14",
},
Token {
kind: Semicolon,
text: ";",
},
]