Crates.io | parseme |
lib.rs | parseme |
version | 0.2.0-alpha |
source | src |
created_at | 2023-02-16 18:05:32.804899 |
updated_at | 2023-02-16 19:36:15.880745 |
description | A framework for building robust parsers. |
homepage | https://github.com/pzipper/parseme |
repository | https://github.com/pzipper/parseme |
max_upload_size | |
id | 786948 |
size | 16,134 |
Parseme is a simple parser combinator framework which takes advantage of Rust's type system.
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(()));
}