GithubHelp home page GithubHelp logo

jcrevier / nom-derive Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rust-bakery/nom-derive

0.0 0.0 0.0 233 KB

Declarative parsing for Rust, using a custom derive and nom

License: Apache License 2.0

Rust 100.00%

nom-derive's Introduction

nom-derive

License: MIT Apache License 2.0 docs.rs Build Status Crates.io Version

Overview

nom-derive is a custom derive attribute, to derive nom parsers automatically from the structure definition.

It is not meant to replace nom, but to provide a quick and easy way to generate parsers for structures, especially for simple structures. This crate aims at simplifying common cases. In some cases, writing the parser manually will remain more efficient.

  • API documentation
  • The [docs::Nom] pseudo-module. This is the main documentation for the Nom attribute, with all possible options and many examples.

Feedback welcome !

#[derive(Nom)]

This crate exposes a single custom-derive macro Nom which implements parse for the struct it is applied to.

The goal of this project is that:

  • derive(Nom) should be enough for you to derive nom parsers for simple structures easily, without having to write it manually
  • it allows overriding any parsing method by your own
  • it allows using generated parsing functions along with handwritten parsers and combining them without efforts
  • it remains as fast as nom

nom-derive adds declarative parsing to nom. It also allows mixing with procedural parsing easily, making writing parsers for byte-encoded formats very easy.

For example:

use nom_derive::*;

#[derive(Nom)]
struct S {
  a: u32,
  b: u16,
  c: u16
}

This adds static method parse to S. The generated code looks like:

impl S {
    pub fn parse(i: &[u8]) -> nom::IResult(&[u8], S) {
        let (i, a) = be_u32(i)?;
        let (i, b) = be_u16(i)?;
        let (i, c) = be_u16(i)?;
        Ok((i, S{ a, b, c }))
    }
}

To parse input, just call let res = S::parse(input);.

For extensive documentation of all attributes and examples, see the documentation of [docs::Nom] custom derive attribute.

Many examples are provided, and more can be found in the project tests.

Combinators visibility

All inferred parsers will generate code with absolute type path, so there is no need to add use statements for them. However, if you use any combinator directly (or in a Parse statement, for ex.), it has to be imported as usual.

That is probably not going to change, since

  • a proc_macro cannot export items other than functions tagged with #[proc_macro_derive]
  • there are variants of combinators with the same names (complete/streaming, bits/bytes), so re-exporting them would create side-effects.

Debug tips

  • If the generated parser does not compile, add #[nom(DebugDerive)] to the structure. It will dump the generated parser to stderr.
  • If the generated parser fails at runtime, try adding #[nom(Debug)] to the structure or to fields. It wraps subparsers in dbg_dmp and will print the field name and input to stderr if the parser fails.

Changes

See CHANGELOG.md, and UPGRADING.md for instructions for upgrading major versions.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

nom-derive's People

Contributors

chifflier avatar katyo avatar pc-anssi avatar r4v3n6101 avatar kcking avatar stephanvanschaik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.