Module nom8::input

source ·
Expand description

Input capability for nom combinators to parse

Input types include:

  • &str and &[u8] are the standard input types
  • Located can track the location within the original buffer to report spans
  • Stateful to thread global state through your parsers
  • Streaming can mark an input as partial buffer that is being streamed into

How do a parse a custom input type?

While historically, nom has worked mainly on &[u8] and &str, it can actually use any type as input, as long as they follow a specific set of traits. Those traits were developed first to abstract away the differences between &[u8] and &str, but were then employed for more interesting types, like nom_locate, a wrapper type that can carry line and column information, or to parse a list of tokens.

Implementing a custom type

Let’s assume we have an input type we’ll call MyInput. MyInput is a sequence of MyItem type. The goal is to define nom parsers with this signature: MyInput -> IResult<MyInput, Output>.

fn parser(i: MyInput) -> IResult<MyInput, Output> {
    tag("test")(i)
}

Here are the traits we have to implement for MyInput:

traitusage
InputIsStreamingMarks the input as being the complete buffer or a partial buffer for streaming input
AsBytesCasts the input type to a byte slice
CompareCharacter comparison operations
ExtendIntoAbstracts something which can extend an Extend
FindSubstringLook for a substring in self
FindTokenLook for self in the given input stream
InputIterCommon iteration operations on the input type
InputLengthCalculate the input length
IntoOutputAdapt a captired Input into an appropriate type
LocationCalculate location within initial input
InputTakeSlicing operations
InputTakeAtPositionLook for a specific token and split at its position
OffsetCalculate the offset between slices
ParseToUsed to integrate &str’s parse() method
SliceSlicing operations using ranges

Here are the traits we have to implement for MyItem:

traitusage
AsCharTransforms common types to a char for basic token parsing

Structs

  • Allow collecting the span of a parsed token
  • Thread global state through your parsers
  • Mark the input as a partial buffer for streaming input.

Enums

  • Indicates whether a comparison was successful, an error, or if more data was needed

Traits

  • Helper trait for types that can be viewed as a byte slice
  • Transforms common types to a char for basic token parsing
  • Abstracts comparison operations
  • Equivalent From implementation to avoid orphan rules in bits parsers
  • Abstracts something which can extend an Extend. Used to build modified input slices in escaped_transform
  • Look for a substring in self
  • Check if a token in in a set of possible tokens
  • Helper trait to show a byte slice as a hex dump
  • Marks the input as being the complete buffer or a partial buffer for streaming input
  • Abstracts common iteration operations on the input type
  • Abstract method to calculate the input length
  • Abstracts slicing operations
  • Methods to take as much input as possible until the provided function returns true for the current element.
  • Convert an Input into an appropriate Output type
  • Number of indices input has advanced since start of parsing
  • Useful functions to calculate the offset between slices and show a hexdump of a slice
  • Used to integrate str’s parse() method
  • Slicing operations using ranges.
  • Helper trait to convert numbers to usize.
  • Dummy trait used for default implementations (currently only used for InputTakeAtPosition and Compare).