Trait chomp::types::Input [] [src]

pub trait Input: Sized {
    type Token: Copy + PartialEq;
    type Buffer: Buffer<Token = Self::Token>;
    fn ret<T, E>(self, t: T) -> ParseResult<Self, T, E> { ... }
    fn err<T, E>(self, e: E) -> ParseResult<Self, T, E> { ... }
    fn from_result<T, E>(self, r: Result<T, E>) -> ParseResult<Self, T, E> { ... }
}

Linear type containing the parser state, this type is threaded though bind and is also the initial type passed to a parser.

Coupled with the ParseResult type it forms the parser monad:

Fn*<I: Input>(I, ...) -> ParseResult<I, T, E>;

where Fn* is the appropriate closure/function trait, I the input type (can be something like [u8]), ... additional parameters to the parser, T the carried success type and E the potential error type.

Associated Types

The token type of the input.

The buffer type yielded by this input when multiple tokens are consumed in sequence.

Can eg. provide zero-copy parsing if the input type is built to support it.

Provided Methods

Returns t as a success value in the parsing context.

Equivalent to Haskell's return function in the Monad typeclass.

Example

use chomp::prelude::{Input, parse_only};

let r = parse_only(|i|
    // Annotate the error type
    i.ret::<_, ()>("Wohoo, success!"),
    b"some input");

assert_eq!(r, Ok("Wohoo, success!"));

Returns e as an error value in the parsing context.

A more general version of Haskell's fail function in the Monad typeclass.

Example

use chomp::prelude::{Input, parse_only};

let r = parse_only(|i|
    // Annotate the value type
    i.err::<(), _>("Something went wrong"),
    b"some input");

assert_eq!(r, Err((&b"some input"[..], "Something went wrong")));

Converts a Result into a ParseResult, preserving parser state.

To convert an Option into a ParseResult it is recommended to use Option::ok_or or Option::ok_or_else in combination with this method.

Examples

use chomp::prelude::{Input, parse_only};

let r = parse_only(|i| i.from_result::<_, ()>(Ok("foo")), b"test");

assert_eq!(r, Ok("foo"));

let r = parse_only(|i| i.from_result::<(), _>(Err("error message")), b"test");

assert_eq!(r, Err((&b"test"[..], "error message")));

Implementors