rslexer 1.0.0

Simple lexer written in Rust!
Documentation

RSLexer

Simple lexer written in Rust!

Examples

#[derive(Debug, PartialEq)]
enum Token {
	Number,
	Word{word: String, line: usize, character: usize},
}

#[test]
fn str_test() {
	// The result of the lex function is a Result<Vec<Token>, String>!
	let ts: Vec<Token> = lex(
		
		// The &str to be lexed!
		"test 12 21 text\n w2\n3",

		// A Vec of Rules, jus use the rules! macro!
		// rules! expects a Type (Token) and a list of rules:
		// &str (regex) => |string, line, character| Option<Token>
		rules!(Token;

			r"\s+" => |_,_,_| None,
			r"\d+" => |_,_,_| Some(Token::Number),
			r"[^\s\d]+" => |s, l, c| Some(Token::Word{ word: s.to_string(), line: l, character: c }),
		)

	).expect("Ok([...])");

	// test if equal!
	assert_eq!(
		vec![
			Token::Word{ word: String::from("test"), line: 1, character: 1 },
			Token::Number,
			Token::Number,
			Token::Word{ word: String::from("text"), line: 1, character: 12 },
			Token::Word{ word: String::from("w"), line: 2, character: 2 },
			Token::Number,
			Token::Number,
		], ts
	);
}

License

This software is available under the MIT license