use logos::Logos;
#[derive(Debug, Logos, PartialEq, Eq)]
#[logos(skip r"[ \t\r\n]")]
pub enum OPBToken {
#[regex("\\*.*")]
Comment,
#[regex("[+-]?[0-9]+")]
Integer,
#[regex("[a-zA-Z_][_a-zA-Z0-9\\-\\^\\[\\]\\{\\}]+")]
Var,
#[token("~")]
Negation,
#[token(">=")]
GreaterEqual,
#[token("<=")]
LessEqual,
#[token("=")]
Equal,
#[token(";")]
Semicolon,
#[token("min:")]
Minimize,
#[token("max:")]
Maximize,
#[regex("@[a-zA-Z0-9_^\\[\\]\\{\\}]+")]
Label,
}
#[cfg(test)]
mod test {
use logos::Logos;
use crate::opb_token::OPBToken;
#[test]
fn integer() {
let mut lex = OPBToken::lexer("424 -424 +424 +004");
assert_eq!(lex.next(), Some(Ok(OPBToken::Integer)));
assert_eq!(lex.next(), Some(Ok(OPBToken::Integer)));
assert_eq!(lex.next(), Some(Ok(OPBToken::Integer)));
assert_eq!(lex.next(), Some(Ok(OPBToken::Integer)));
assert_eq!(lex.slice().parse::<i64>().unwrap(), 4);
assert_eq!(lex.next(), None);
}
#[test]
fn variable() {
let mut lex = OPBToken::lexer("x12 _x12 xaK2-[]{}_^");
assert_eq!(lex.next(), Some(Ok(OPBToken::Var)));
assert_eq!(lex.next(), Some(Ok(OPBToken::Var)));
assert_eq!(lex.next(), Some(Ok(OPBToken::Var)));
assert_eq!(lex.next(), None);
}
#[test]
fn comment() {
let mut lex = OPBToken::lexer("*sdf sdafsd ffsdf sdf asdf dsfsdf 12 sda f\n*\n11");
assert_eq!(lex.next(), Some(Ok(OPBToken::Comment)));
assert_eq!(lex.next(), Some(Ok(OPBToken::Comment)));
assert_eq!(lex.next(), Some(Ok(OPBToken::Integer)));
assert_eq!(lex.next(), None);
}
}