1 unstable release

Uses old Rust 2015

0.1.0 Jul 10, 2017

#16 in #configured

MIT license

13KB
211 lines

nom_config

nom_config is a small crate that allows to write nom parser combinators while carrying parsing configuration around.

Let's look at an artificial example. Suppose we want to pick up "test" tokens from an input, skipping "skip" and replacing "test" with something we don't know upfront (a configurable replacement).

We can first designate a structure that will contain the replacement:

#[derive(Debug, Clone, Copy)]
struct Config {
    replacement: &'static [u8],
}

Now, we can use named_with_config! macro to pick up the "test" tag and retrieve the configuration using the config! macro:

named_with_config!(Config, test, do_parse!(cfg: config!() >> v: tag!(b"test") >> ({cfg.replacement})));

For cases when you want to use parsers that are not aware of the configuration, you can use the lift_config! macro:

named_with_config!(Config, tests<Vec<&[u8]>>, many0!(alt!(lift_config!(tag!("skip")) | test)));

You will typically need to do this when you have errors like this one:

   = note: expected type `nom::IResult<nom_config::Configured<_, &[u8]>, nom_config::Configured<Config, &[u8]>, _>`
              found type `nom::IResult<nom_config::Configured<_, &[u8]>, &[u8], _>`

Now, you can call your parser with your input and configuration wrapped into Configured:

fn main() {
    let (_, result) = tests(Configured::new(Config { replacement: b"TEST" }, b"testskiptest")).unwrap();
    assert_eq!(result, vec!["TEST".as_bytes(), "skip".as_bytes(), "TEST".as_bytes()]);
}

As you can see, the "test" tag was replaced with "TEST", as intended.

You can find the full example in the examples

License

This crate is licensed under the terms of the MIT license, same as nom. For details, see LICENSE.

Status

This crate is an early version. It is likely to be incomplete and have some bugs; it's also possible that a better API can emerge later on.

Dependencies

~1MB
~22K SLoC