This crate builds on the interfaces from yap
to allow simple parsing of streams.
Why
There already exist many crates that intend to help with parsing.
Of that list nom
, winnow
, chumsky
, combine
support parsing streams of values.
nom
:
- No obvious way to signal the end of a stream to a parser.
- The user of the library has to implement a streaming parser noticeably differently from a non-streaming parser.
- Parsing occurs on chunks. Parsing dynamically sized chunks can require re-parsing the chunk from scratch and redoing work.
winnow
:
- Parsing occurs on chunks. Parsing dynamically sized chunks can require re-parsing the chunk from scratch and redoing work.
chumsky
is not designed for speed.
combine
is complicated.
This crate allows using an already written yap
parser by simply changing the initial tokens declaration.
use ;
use ;
// Write parser
// =========================================
// The `Tokens` trait builds on `Iterator`, so we get a `next` method.
// We also get other useful functions..
+ '_
// Now we've parsed our input into OpOrDigits, let's calculate the result..
// Use parser
// =========================================
// Get our input and convert into something implementing `Tokens`
let mut tokens = "10 + 2 x 12-4,foobar".into_tokens;
// Parse
assert_eq!;
// Instead of parsing an in-memory buffer we can use `yap_streaming` to parse a stream.
// While we could [`std::io::Read::read_to_end()`] here, what if the file was too large
// to fit in memory? What if we were parsing from a network socket?
let mut io_err = None;
let file_chars = new
.bytes
.map_while;
// Convert to something implementing `Tokens`.
// If parsing a stream not of `char` use [`yap_streaming::StreamTokens`] instead.
let mut tokens = new;
// Parse
assert_eq!;
// Check that parse encountered no io errors.
assert!;