Crate peresil

Source
Expand description

A simple and simplistic parsing library

§Example

#[macro_use]
extern crate peresil;

use peresil::{ParseMaster,Progress,Recoverable,Status,StringPoint};

type DemoMaster<'a> = ParseMaster<StringPoint<'a>, DemoError>;
type DemoProgress<'a, T> = Progress<StringPoint<'a>, T, DemoError>;
enum DemoError {
    ExpectedGreeting,
    ExpectedWhitespace,
    ExpectedObject,
}

impl Recoverable for DemoError {
    fn recoverable(&self) -> bool { true }
}

fn parse_basic<'a>(pm: &mut DemoMaster<'a>, pt: StringPoint<'a>)
                  -> DemoProgress<'a, (&'a str, &'a str)>
{
    let tmp = pm.alternate()
        .one(|_| pt.consume_literal("goodbye").map_err(|_| DemoError::ExpectedGreeting))
        .one(|_| pt.consume_literal("hello").map_err(|_| DemoError::ExpectedGreeting))
        .finish();
    let (pt, greeting) = try_parse!(tmp);

    let (pt, _) = try_parse!(pt.consume_literal(" ").map_err(|_| DemoError::ExpectedWhitespace));

    let tmp = pm.alternate()
        .one(|_| pt.consume_literal("world").map_err(|_| DemoError::ExpectedObject))
        .one(|_| pt.consume_literal("moon").map_err(|_| DemoError::ExpectedObject))
        .finish();
    let (pt, object) = try_parse!(tmp);

    Progress::success(pt, (greeting, object))
}

fn main() {
    let mut pm = ParseMaster::new();
    let pt = StringPoint::new("hello world");

    let result = parse_basic(&mut pm, pt);
    let (greeting, object) = match pm.finish(result) {
        Progress { status: Status::Success(v), .. } => v,
        Progress { status: Status::Failure(..), .. } => panic!("Did not parse"),
    };

    println!("Parsed [{}], [{}]", greeting, object);
}

Macros§

try_parse
An analog to try!, but for Progress

Structs§

Alternate
Follows the first successful parsing path.
ParseMaster
The main entrypoint to parsing.
Progress
Tracks where the parser currently is and if it is successful.
StringPoint
Tracks the location of parsing in a string, the most common case.

Enums§

Status
An analog to Result, specialized for parsing.

Traits§

Point
A location in the parsed data
Recoverable
Indicate if an error should terminate all parsing.

Type Aliases§

Identifier
Matches a literal string to a specific type, usually an enum.