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

#3 in #simplistic

Download history 6318/week @ 2024-08-07 5849/week @ 2024-08-14 6841/week @ 2024-08-21 5923/week @ 2024-08-28 6984/week @ 2024-09-04 5797/week @ 2024-09-11 7279/week @ 2024-09-18 5915/week @ 2024-09-25 5970/week @ 2024-10-02 6150/week @ 2024-10-09 8482/week @ 2024-10-16 8446/week @ 2024-10-23 8595/week @ 2024-10-30 7646/week @ 2024-11-06 8610/week @ 2024-11-13 7343/week @ 2024-11-20

34,284 downloads per month
Used in 64 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