#[macro_use] extern crate flexer; use flexer::Flexer; #[derive(Debug, PartialEq)] enum Token { OpeningParenthesis, ClosingParenthesis, IntegerLiteral(i64), FloatLiteral(f64), Id(String), Operator(String), Unknown(String), } #[test] fn main() { let lexer: Flexer = flexer!( "opening_parenthesis" => r"\(" => |_| Token::OpeningParenthesis "closing_parenthesis" => r"\)" => |_| Token::ClosingParenthesis "float_literal" => r"[\+\-]?\d+\.\d+" => |token| Token::FloatLiteral(token.parse().unwrap()) "integer_literal" => r"[\+\-]?\d+" => |token| Token::IntegerLiteral(token.parse().unwrap()) "id" => r"[\w_]+" => |token| Token::Id(token) "operator" => r"[\+\-\*/]" => |token| Token::Operator(token) "unknown" => r"\S.*\S" => |token| Token::Unknown(token) ); let source_code = "(def variable 12)\n(def result (+ 1.5 variable))\n"; let tokens: Vec = lexer.tokens(source_code).collect(); let want = vec![ Token::OpeningParenthesis, Token::Id(String::from("def")), Token::Id(String::from("variable")), Token::IntegerLiteral(12), Token::ClosingParenthesis, Token::OpeningParenthesis, Token::Id(String::from("def")), Token::Id(String::from("result")), Token::OpeningParenthesis, Token::Operator(String::from("+")), Token::FloatLiteral(1.5), Token::Id(String::from("variable")), Token::ClosingParenthesis, Token::ClosingParenthesis, ]; assert_eq!(want, tokens); }