6 releases (breaking)
0.5.0 | Sep 17, 2020 |
---|---|
0.4.0 | Jul 29, 2020 |
0.3.1 | Jul 29, 2020 |
0.2.0 | Jul 24, 2020 |
0.1.0 | Jul 22, 2020 |
#213 in Parser tooling
Used in intext
51KB
1K
SLoC
Chonk 0.5
A lightweight parser combinator framework.
Usage
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.