parseme

Crates.ioparseme
lib.rsparseme
version0.2.0-alpha
sourcesrc
created_at2023-02-16 18:05:32.804899
updated_at2023-02-16 19:36:15.880745
descriptionA framework for building robust parsers.
homepagehttps://github.com/pzipper/parseme
repositoryhttps://github.com/pzipper/parseme
max_upload_size
id786948
size16,134
Zack (trimorphdev)

documentation

README

parseme

Parseme is a simple parser combinator framework which takes advantage of Rust's type system.

Examples

A basic identifier parser using Unicode XID via parseme::xid (requires the xid feature, enabled by default).

use parseme::Source;

fn parse_iden<'a>(input: &mut Source<'a>) -> Result<&'a str, ()> {
    let start_pos = input.pos();

    parseme::iter::next_if(input, parseme::xid::is_start).ok_or(())?;
    parseme::iter::advance_while(input, parseme::xid::is_continue);

    Ok(&input.src()[start_pos..input.pos()])
}

fn main() {
    let ident1 = "hello";
    let ident2 = "hello123";

    let bad1 = "123";
    let bad2 = "_ident";

    assert_eq!(parse_iden(&mut Source::new(ident1)), Ok("hello"));
    assert_eq!(parse_iden(&mut Source::new(ident2)), Ok("hello123"));
    assert_eq!(parse_iden(&mut Source::new(bad1)), Err(()));
    assert_eq!(parse_iden(&mut Source::new(bad2)), Err(()));
}
Commit count: 17

cargo fmt