chonk

Crates.iochonk
lib.rschonk
version0.5.0
sourcesrc
created_at2020-07-22 10:04:28.878007
updated_at2020-09-17 02:14:38.138325
descriptionA lightweight parser combinator framework.
homepage
repositoryhttps://github.com/jasmineknight/chonk
max_upload_size
id267991
size62,821
(wtfsophia)

documentation

README

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.

Commit count: 0

cargo fmt