Skip to main content

Module stream

Module stream 

Source
Available on crate feature unstable-doc only.
Expand description

§Custom Stream

winnow is batteries included with support for

  • Basic inputs like &str, newtypes with
  • Improved debug output like Bytes
  • Stateful for passing state through your parser, like tracking recursion depth
  • LocatingSlice for looking up the absolute position of a token

§Implementing a custom token

The first level of customization is parsing &[MyItem] or TokenSlice<MyItem>.

The basic traits you may want for a custom token type are:

traitusage
AsCharTransforms common types to a char for basic token parsing
ContainsTokenLook for the token in the given set

See also TokenSlice<MyItem>, lexing.

§Implementing a custom stream

Let’s assume we have an input type we’ll call MyStream. MyStream is a sequence of MyItem tokens.

The goal is to define parsers with this signature: &mut MyStream -> ModalResult<Output>.

fn parser<'s>(i: &mut MyStream<'s>) -> ModalResult<Output<'s>> {
    "test".parse_next(i)
}

Like above, you’ll need to implement the related token traits for MyItem.

The traits you may want to implement for MyStream include:

traitusage
StreamCore trait for driving parsing
StreamIsPartialMarks the input as being the complete buffer or a partial buffer for streaming input
AsBytesCasts the input type to a byte slice
AsBStrCasts the input type to a slice of ASCII / UTF-8-like bytes
CompareCharacter comparison operations
FindSliceLook for a substring in self
LocationCalculate location within initial input
OffsetCalculate the offset between slices

And for &[MyItem] (slices returned by Stream):

traitusage
SliceLenCalculate the input length
ParseSliceUsed to integrate &str’s parse() method