Macro parsit::token

source ·
macro_rules! token {
    ($obj:expr => $($matcher:pat $(if $pred:expr)* => $result:expr),*) => { ... };
    ($obj:expr => $($matcher:pat $(if $pred:expr)*),*) => { ... };
}
Expand description

Helps to parse a token on the lexer level

  • Typically, it takes a token from the parser(defined in the structure of Logos)
  • The structure should implement PartialEq

Examples

  • create a pattern matching for the given tokens
    use logos::Logos;
    use crate::parsit::parser::Parsit;
    use crate::parsit::token;
    use crate::parsit::step::Step;
    use crate::parsit::parser::EmptyToken;
    #[derive(Logos,PartialEq)]
    pub enum TFQ {
        #[token("true")]
        True,
        #[token("false")]
        False,

        #[token("?")]
        Question,
    }

    let p:Parsit<TFQ> = Parsit::new("true?").unwrap();
    // create a pattern matching for the given tokens
     token!(
        p.token(0) =>
            TFQ::True => true,
            TFQ::False => false
     );
    // create a matching for only one token without a result
    // it is used oftenly with then().
    // The EmptyToken will be return
     token!(p.token(1) => TFQ::Question);