I am trying my hand at writing a compiler in Rust for my compilers class. The first project is to write a lexer for our language. So I came across Logos which seems to be a popular lexer crate. In Logos, you specify a pattern to match with procedural macros, for example:
#[regex("[a-zA-Z]+")]
Text,
inside of your Token enum.
Flex, the C(++) lexer-generator, allows you to define macros, like so:
TEXT [a-zA-Z]+
%%
{TEXT} {return 1;}
I'm looking to get this functionality with Logos to help make one of my regular expressions a bit easier to understand. The first thing I have tried is code that looks like this:
const COMMENT : &'static str = "^#.*$";
#[derive(Logos, Debug, PartialEq)]
enum Token {
#[regex(format!("{COMMENT}|[[:space:]]"), logos::skip)]
#[error]
Error,
}
However, I get the error:
error: expected literal
--> src/main.rs:140:13
|
140 | #[regex(format!("{COMMENT}|[[:space:]]"), logos::skip)]
| ^^^^^^
After reading about procedural macros, this error makes sense as I understand that they take just straight, unevaluated streams of tokens and manipulate them. With that, is there any way to get the compiler to evaluate the format! expression to a literal stream of tokens before passing it to the regex macro? Or, does Logos have a better way to do what I'm trying to do?