Hinku is a small library for writing parsers based on token streams. It provides an integration for lexers created with the logos crate but provedes flexible trait based interface for integration with other token streams.
Below you can find a rudimentary parser implemented using this library and the Logos crate.
```rust use logos::Logos; use hinku::{ logos::BufferedLexer, Either, ParseError, ParseResult, TokenStream, TokenStreamExt, };
enum Token { #[token("foo")] Foo,
#[token("bar")]
Bar,
#[error]
#[regex(r"[ \n\r\f\t]+", logos::skip)]
Error,
}
/// A function that either consumes a Foo token or returns an error.
fn foo(stream: &mut dyn TokenStream
/// A function that either consumes a Bar token or returns an error.
fn bar(stream: &mut dyn TokenStream
/// A function that consumes either one of the tokens.
fn fooorbar(mut stream: &mut dyn TokenStream
/// A function that expects a Foo token, followed by a Bar token.
fn foobar(mut stream: &mut dyn TokenStream
Ok((f, b))
}
let lex = Token::lexer("foo bar bar foo bar"); let mut stream = BufferedLexer::new(lex);
asserteq!(stream.take(foo), Ok(Token::Foo)); asserteq!(stream.take(bar), Ok(Token::Bar)); asserteq!(stream.take(fooorbar), Ok(Token::Bar)); asserteq!(stream.take(foobar), Ok((Token::Foo, Token::Bar))); ```