Crates.io | chonk |
lib.rs | chonk |
version | 0.5.0 |
source | src |
created_at | 2020-07-22 10:04:28.878007 |
updated_at | 2020-09-17 02:14:38.138325 |
description | A lightweight parser combinator framework. |
homepage | |
repository | https://github.com/jasmineknight/chonk |
max_upload_size | |
id | 267991 |
size | 62,821 |
A lightweight parser combinator framework.
Use the test
method to see if your input matches a parser:
use chonk::prelude::*;
fn parser<'a>() -> impl Parser<'a, &'a str, ()> {
move |ctx| {
take(1.., is(alphabetic)).parse(ctx)
}
}
if parser().test("abcd") {
println!("One or more alphabetic characters found!");
}
Use the parse
method to extract information from your input:
assert_eq!(parser().parse("foobar"), Ok((
ParserContext {
input: "foobar",
bounds: 0..6,
},
"foobar"
)))
Write your own parser functions with custom result types:
use chonk::prelude::*;
#[derive(Debug, PartialEq)]
enum Token<'a> {
Identifier(&'a str),
}
#[derive(Debug, PartialEq)]
enum Message {
ExpectedIdentifier
}
fn identifier<'a>() -> impl Parser<'a, Token<'a>, Message> {
move |ctx| {
take(1.., is(alphabetic)).parse(ctx)
.map_result(|token| Token::Identifier(token))
.map_error(|error| error.with_message(Message::ExpectedIdentifier))
}
}
assert_eq!(identifier().parse("foobar"), Ok((
ParserContext {
input: "foobar",
bounds: 0..6,
},
Token::Identifier("foobar")
)));
For more information, look in the examples directory in the git repository.