14 unstable releases (3 breaking)

0.4.1 Feb 8, 2021
0.4.0 Feb 7, 2021
0.3.4 Feb 7, 2021
0.2.1 Feb 2, 2021
0.1.4 Jan 28, 2021

#2254 in Rust patterns

31 downloads per month

MIT license

45KB
1K SLoC

nommy

A type based parsing library with convenient macros

use nommy::{parse, text::*, Parse};

type Letters = AnyOf1<"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ">;

#[derive(Debug, Parse, PartialEq)]
#[nommy(prefix = Tag<"struct">)]
#[nommy(ignore = WhiteSpace)]
struct StructNamed {
    #[nommy(parser = Letters)]
    name: String,

    #[nommy(prefix = Tag<"{">, suffix = Tag<"}">)]
    fields: Vec<NamedField>,
}

#[derive(Debug, Parse, PartialEq)]
#[nommy(suffix = Tag<",">)]
#[nommy(ignore = WhiteSpace)]
struct NamedField {
    #[nommy(parser = Letters)]
    name: String,

    #[nommy(prefix = Tag<":">, parser = Letters)]
    ty: String,
}
let input = "struct Foo {
    bar: Abc,
    baz: Xyz,
}";

let struct_: StructNamed = parse(input.chars()).unwrap();
assert_eq!(
    struct_,
    StructNamed {
        name: "Foo".to_string(),
        fields: vec![
            NamedField {
                name: "bar".to_string(),
                ty: "Abc".to_string(),
            },
            NamedField {
                name: "baz".to_string(),
                ty: "Xyz".to_string(),
            },
        ]
    }
);

Dependencies

~1.5MB
~39K SLoC