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

Expand description

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 EasyParser::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, EasyParser, Stream, many1};
use combine::parser::char::letter;
use combine::stream::StreamErrorFor;
use combine::error::{ParseError, StreamError};

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

    parser!{
       fn parser2[Input]()(Input) -> String
        where [
            Input: Stream<Token = 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::<Input>::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

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.

Enums

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

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

Type Definitions

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