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
34,284 downloads per month
Used in 64 crates
(4 directly)
39KB
886 lines
Peresil
A simple and simplistic string parsing library in Rust.
Check the docs for a quick introduction.
Contributing
- Fork it ( https://github.com/shepmaster/peresil/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Add a failing test.
- Add code to pass the test.
- Commit your changes (
git commit -am 'Add some feature'
) - Ensure tests pass.
- Push to the branch (
git push origin my-new-feature
) - 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);
}