5 releases (3 breaking)

Uses old Rust 2015

0.4.0-alpha.0 Apr 22, 2018
0.3.0 Jan 15, 2016
0.2.1 Aug 19, 2015
0.2.0 Apr 23, 2015
0.1.0 Feb 22, 2015

#153 in Parser tooling

Download history 6561/week @ 2024-04-08 6425/week @ 2024-04-15 6423/week @ 2024-04-22 7035/week @ 2024-04-29 7291/week @ 2024-05-06 6939/week @ 2024-05-13 7373/week @ 2024-05-20 7228/week @ 2024-05-27 7647/week @ 2024-06-03 6969/week @ 2024-06-10 5241/week @ 2024-06-17 5792/week @ 2024-06-24 4988/week @ 2024-07-01 5704/week @ 2024-07-08 7377/week @ 2024-07-15 5286/week @ 2024-07-22

23,597 downloads per month
Used in 60 crates (4 directly)

MIT license

39KB
886 lines

Peresil

Build Status

A simple and simplistic string parsing library in Rust.

Check the docs for a quick introduction.

Contributing

  1. Fork it ( https://github.com/shepmaster/peresil/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Add a failing test.
  4. Add code to pass the test.
  5. Commit your changes (git commit -am 'Add some feature')
  6. Ensure tests pass.
  7. Push to the branch (git push origin my-new-feature)
  8. Create a new Pull Request

lib.rs:

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(pt)
        .one(|_, pt| pt.consume_literal("goodbye").map_err(|_| DemoError::ExpectedGreeting))
        .one(|_, pt| 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(pt)
        .one(|_, pt| pt.consume_literal("world").map_err(|_| DemoError::ExpectedObject))
        .one(|_, pt| 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);
}

No runtime deps

Features