2019-09-11 06:23:15 +08:00
|
|
|
use logos::Logos;
|
2019-09-11 05:37:51 +08:00
|
|
|
use super::session::ReportMode;
|
|
|
|
|
|
|
|
#[derive(Logos, Debug, PartialEq)]
|
|
|
|
enum Token {
|
|
|
|
#[end]
|
|
|
|
End,
|
|
|
|
#[error]
|
|
|
|
Error,
|
|
|
|
|
|
|
|
#[token = "Quit"]
|
|
|
|
Quit,
|
|
|
|
#[token = "show"]
|
|
|
|
Show,
|
|
|
|
#[token = "channel"]
|
|
|
|
Channel,
|
|
|
|
#[token = "report"]
|
|
|
|
Report,
|
|
|
|
#[token = "mode"]
|
|
|
|
Mode,
|
|
|
|
#[token = "off"]
|
|
|
|
Off,
|
|
|
|
#[token = "once"]
|
|
|
|
Once,
|
|
|
|
#[token = "continuous"]
|
|
|
|
Continuous,
|
|
|
|
|
|
|
|
#[regex = "[0-9]+"]
|
|
|
|
Number,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Parser,
|
|
|
|
UnexpectedEnd,
|
|
|
|
UnexpectedToken(Token),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2019-09-11 06:23:15 +08:00
|
|
|
pub enum ShowCommand {
|
2019-09-11 05:37:51 +08:00
|
|
|
ReportMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Command {
|
|
|
|
Quit,
|
2019-09-11 06:23:15 +08:00
|
|
|
Show(ShowCommand),
|
2019-09-11 05:37:51 +08:00
|
|
|
Report(ReportMode),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
pub fn parse(input: &str) -> Result<Self, Error> {
|
|
|
|
let mut lexer = Token::lexer(input);
|
|
|
|
|
|
|
|
macro_rules! choice {
|
|
|
|
[$($token: tt => $block: stmt,)*] => {
|
|
|
|
match lexer.token {
|
|
|
|
$(
|
|
|
|
Token::$token => {
|
|
|
|
lexer.advance();
|
|
|
|
$block
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
Token::End => Err(Error::UnexpectedEnd),
|
|
|
|
_ => Err(Error::UnexpectedToken(lexer.token))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
choice![
|
|
|
|
Quit => Ok(Command::Quit),
|
|
|
|
Report => choice![
|
|
|
|
Mode => choice![
|
2019-09-11 06:23:15 +08:00
|
|
|
End => Ok(Command::Show(ShowCommand::ReportMode)),
|
2019-09-11 05:37:51 +08:00
|
|
|
Off => Ok(Command::Report(ReportMode::Off)),
|
|
|
|
Once => Ok(Command::Report(ReportMode::Once)),
|
|
|
|
Continuous => Ok(Command::Report(ReportMode::Continuous)),
|
|
|
|
],
|
|
|
|
End => Ok(Command::Report(ReportMode::Once)),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|