[][src]Module combine::stream::easy

Stream wrapper which provides an informative and easy to use error type.

Unless you have specific constraints preventing you from using this error type (such as being a no_std environment) you probably want to use this stream type. It can easily be used through the Parser::easy_parse method.

The provided Errors type is roughly the same as ParseError in combine 1.x and 2.x.

#[macro_use]
extern crate combine;
use combine::{easy, Parser, Stream, many1};
use combine::parser::char::letter;
use combine::stream::StreamErrorFor;
use combine::error::{ParseError, StreamError};

fn main() {
    parser!{
       fn parser[I]()(I) -> String
        where [
            I: Stream<Item=char, Error = easy::ParseError<I>>,
            // If we want to use the error type explicitly we need to help rustc infer
            // `StreamError` to `easy::Error` (rust-lang/rust#24159)
            I::Error: ParseError<
                I::Item,
                I::Range,
                I::Position,
                StreamError = easy::Error<I::Item, I::Range>
            >
        ]
        {
            many1(letter()).and_then(|word: String| {
                if word == "combine" {
                    Ok(word)
                } else {
                    Err(easy::Error::Expected(easy::Info::Borrowed("combine")))
                }
            })
        }
    }

    parser!{
       fn parser2[I]()(I) -> String
        where [
            I: Stream<Item=char>,
        ]
        {
            many1(letter()).and_then(|word: String| {
                if word == "combine" {
                    Ok(word)
                } else {
                    // Alternatively it is possible to only use the methods provided by the
                    // `StreamError` trait.
                    // In that case the extra bound is not necessary (and this method will work
                    // for other errors than `easy::Errors`)
                    Err(StreamErrorFor::<I>::expected_static_message("combine"))
                }
            })
        }
    }

    let input = "combin";
    let expected_error = Err(easy::Errors {
        errors: vec![
            easy::Error::Expected("combine".into())
        ],
        position: 0,
    });
    assert_eq!(
        parser().easy_parse(input).map_err(|err| err.map_position(|p| p.translate_position(input))),
        expected_error
    );
    assert_eq!(
        parser2().easy_parse(input).map_err(|err| err.map_position(|p| p.translate_position(input))),
        expected_error
    );
}

Structs

Errors

Struct which hold information about an error that occurred at a specific position. Can hold multiple instances of Error if more that one error occurred in the same position.

Stream

Enums

Error

Enum used to store information about an error that has occurred during parsing.

Info

Enum holding error information. Variants are defined for Stream::Item and Stream::Range as well as string variants holding easy descriptions.

Type Definitions

ParseError

Convenience alias over Errors for StreamOnce types which makes it possible to specify the Errors type from a StreamOnce by writing ParseError<I> instead of Errors<I::Item, I::Range, I::Position>