chonk 0.3.1

A lightweight parser combinator framework.
Documentation

Chonk 0.3

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.