Macro titanic::accept[][src]

macro_rules! accept {
    ($parser:expr, $toktype:ident) => { ... };
    ($parser:expr, $toktype:ident, $tokdata:ident) => { ... };
}

Accept only the given TokenKind.

The first arg has to have an accessible field lexer: iter::Peekable<_>. The second arg is the name of a TokenKind variant, e.g. StringLiteral. If the variant contains data, a third arg is needed and can be any ident.

Ok(Some(TokenKind)) is returned if it is the correct token without data. Ok(Some(data)) is returned if it is the correct token with data. Ok(None) is returned if another token is found. If an error is found, it will be returned.

This example is not tested
// In a method of GgaParser. The next bytes to parse are b",doc*".
assert_eq!(accept!(self, CommaSeparator), Ok(Some(TokenKind::CommaSeparator)));
assert_eq!(accept!(self, CommaSeparator), Ok(None));
match accept!(self, StringLiteral, s) {
    Ok(Some(s)) => assert_eq!(s.len(), 3),
    _ => unreachable!(),
}
assert!(accept!(self, Checksum, c).is_err());