use log::*;
use logos::Lexer;
use logos::Logos;
struct LexerCallbacks;
impl LexerCallbacks {
fn code(lex: &mut Lexer<Token>) -> (String, String) {
debug!("Running lexer callback: code");
let content: &str = lex.slice().trim();
let mut split = content.split(r" ");
let (name, args) = (
split
.next()
.expect("Parsing code unexpectedly failed.")
.to_owned(),
split.next().unwrap_or("").to_owned(),
);
trace!("name: {}", name);
trace!("args: {}", args);
(name, args)
}
}
#[derive(Logos, Debug, PartialEq)]
pub enum Token {
#[error]
#[regex(r"[ \t\r\n\f]+", logos::skip)]
Error,
#[regex(r#"[a-zA-Z0-9-_]+([ ][a-zA-Z0-9-_]+)*"#, LexerCallbacks::code)]
Code((String, String)),
#[regex("[//|#](.*)", logos::skip)]
Comment,
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::*;
#[rstest]
fn lexer() {
let script: &str = include_str!("../resources/test/example_script.txt");
let mut lex = Token::lexer(script);
assert_eq!(
lex.next(),
Some(Token::Code(("cl_hello".to_owned(), "".to_owned())))
);
assert_eq!(
lex.next(),
Some(Token::Code(("cl_hello".to_owned(), "Eray".to_owned())))
);
assert_eq!(
lex.next(),
Some(Token::Code(("cl_hello".to_owned(), "".to_owned())))
);
assert_eq!(
lex.next(),
Some(Token::Code(("cl_hello".to_owned(), "Eray".to_owned())))
);
assert_eq!(
lex.next(),
Some(Token::Code(("cl_hello".to_owned(), "Eray".to_owned())))
);
}
}