2015-02-23 20:11:27 +00:00
# nom, eating data byte by byte
2016-07-23 21:46:47 +00:00
[![LICENSE ](https://img.shields.io/badge/license-MIT-blue.svg )](LICENSE)
2015-02-26 11:25:48 +00:00
[![Join the chat at https://gitter.im/Geal/nom ](https://badges.gitter.im/Join%20Chat.svg )](https://gitter.im/Geal/nom?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2021-08-21 11:59:54 +00:00
[![Build Status ](https://github.com/Geal/nom/actions/workflows/ci.yml/badge.svg )](https://github.com/Geal/nom/actions/workflows/ci.yml)
2021-12-20 20:33:39 +00:00
[![Coverage Status ](https://coveralls.io/repos/github/Geal/nom/badge.svg?branch=main )](https://coveralls.io/github/Geal/nom?branch=main)
2016-10-31 20:17:35 +00:00
[![Crates.io Version ](https://img.shields.io/crates/v/nom.svg )](https://crates.io/crates/nom)
2022-01-14 18:00:55 +00:00
[![Minimum rustc version ](https://img.shields.io/badge/rustc-1.48.0+-lightgray.svg )](#rust-version-requirements-msrv)
2015-02-26 11:25:48 +00:00
2019-05-13 15:04:28 +00:00
nom is a parser combinators library written in Rust. Its goal is to provide tools
to build safe parsers without compromising the speed or memory consumption. To
that end, it uses extensively Rust's *strong typing* and *memory safety* to produce
fast and correct parsers, and provides functions, macros and traits to abstract most of the
error prone plumbing.
2015-02-23 20:11:27 +00:00
2021-12-20 20:33:39 +00:00
![nom logo in CC0 license, by Ange Albertini ](https://raw.githubusercontent.com/Geal/nom/main/assets/nom.png )
2018-05-14 11:56:33 +00:00
*nom will happily take a byte out of your files :)*
2022-03-17 10:51:43 +00:00
<!-- toc -->
- [Example ](#example )
- [Documentation ](#documentation )
- [Why use nom? ](#why-use-nom )
- [Binary format parsers ](#binary-format-parsers )
- [Text format parsers ](#text-format-parsers )
- [Programming language parsers ](#programming-language-parsers )
- [Streaming formats ](#streaming-formats )
- [Parser combinators ](#parser-combinators )
- [Technical features ](#technical-features )
- [Rust version requirements ](#rust-version-requirements-msrv )
- [Installation ](#installation )
- [Related projects ](#related-projects )
- [Parsers written with nom ](#parsers-written-with-nom )
- [Contributors ](#contributors )
<!-- tocstop -->
2018-02-11 14:31:33 +00:00
## Example
[Hexadecimal color ](https://developer.mozilla.org/en-US/docs/Web/CSS/color ) parser:
```rust
extern crate nom;
2019-05-13 15:04:28 +00:00
use nom::{
IResult,
bytes::complete::{tag, take_while_m_n},
combinator::map_res,
sequence::tuple
};
2018-02-11 14:31:33 +00:00
#[derive(Debug,PartialEq)]
pub struct Color {
pub red: u8,
pub green: u8,
pub blue: u8,
}
2018-02-11 20:50:04 +00:00
fn from_hex(input: & str) -> Result< u8 , std::num::ParseIntError > {
u8::from_str_radix(input, 16)
}
fn is_hex_digit(c: char) -> bool {
2018-09-24 19:05:19 +00:00
c.is_digit(16)
2018-02-11 20:50:04 +00:00
}
2019-05-13 15:04:28 +00:00
fn hex_primary(input: & str) -> IResult< & str, u8> {
map_res(
take_while_m_n(2, 2, is_hex_digit),
from_hex
)(input)
}
2018-02-11 14:31:33 +00:00
2019-05-13 15:04:28 +00:00
fn hex_color(input: & str) -> IResult< & str, Color> {
let (input, _) = tag("#")(input)?;
let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?;
Ok((input, Color { red, green, blue }))
}
2018-02-11 14:31:33 +00:00
2019-04-28 10:13:53 +00:00
fn main() {}
2018-02-11 14:31:33 +00:00
#[test]
fn parse_color() {
2018-02-11 20:50:04 +00:00
assert_eq!(hex_color("#2F14DF"), Ok(("", Color {
2018-02-11 14:31:33 +00:00
red: 47,
green: 20,
blue: 223,
})));
}
```
## Documentation
2018-05-14 12:25:54 +00:00
- [Reference documentation ](https://docs.rs/nom )
2021-12-20 20:33:39 +00:00
- [Various design documents and tutorials ](https://github.com/Geal/nom/tree/main/doc )
- [List of combinators and their behaviour ](https://github.com/Geal/nom/blob/main/doc/choosing_a_combinator.md )
2015-02-23 20:11:27 +00:00
2021-08-21 10:13:43 +00:00
If you need any help developing your parsers, please ping `geal` on IRC (libera, geeknode, oftc), go to `#nom-parsers` on Libera IRC, or on the [Gitter chat room ](https://gitter.im/Geal/nom ).
2015-02-16 13:39:29 +00:00
2018-02-11 14:31:33 +00:00
## Why use nom
If you want to write:
2020-05-30 09:53:44 +00:00
### Binary format parsers
2018-02-11 14:31:33 +00:00
nom was designed to properly parse binary formats from the beginning. Compared
to the usual handwritten C parsers, nom parsers are just as fast, free from
buffer overflow vulnerabilities, and handle common patterns for you:
- [TLV ](https://en.wikipedia.org/wiki/Type-length-value )
2020-05-30 09:53:44 +00:00
- Bit level parsing
- Hexadecimal viewer in the debugging macros for easy data analysis
- Streaming parsers for network formats and huge files
2018-02-11 14:31:33 +00:00
Example projects:
- [FLV parser ](https://github.com/rust-av/flavors )
- [Matroska parser ](https://github.com/rust-av/matroska )
- [tar parser ](https://github.com/Keruspe/tar-parser.rs )
### Text format parsers
2019-05-13 15:04:28 +00:00
While nom was made for binary format at first, it soon grew to work just as
2018-02-11 14:31:33 +00:00
well with text formats. From line based formats like CSV, to more complex, nested
formats such as JSON, nom can manage it, and provides you with useful tools:
2020-05-30 09:53:44 +00:00
- Fast case insensitive comparison
- Recognizers for escaped strings
- Regular expressions can be embedded in nom parsers to represent complex character patterns succinctly
- Special care has been given to managing non ASCII characters properly
2016-09-11 22:31:28 +00:00
2018-02-11 14:31:33 +00:00
Example projects:
2016-09-11 22:31:28 +00:00
2021-12-16 11:49:29 +00:00
- [HTTP proxy ](https://github.com/sozu-proxy/sozu/tree/main/lib/src/protocol/http/parser )
2018-02-11 14:31:33 +00:00
- [TOML parser ](https://github.com/joelself/tomllib )
2015-01-03 13:53:20 +00:00
2018-02-11 14:31:33 +00:00
### Programming language parsers
While programming language parsers are usually written manually for more
flexibility and performance, nom can be (and has been successfully) used
as a prototyping parser for a language.
nom will get you started quickly with powerful custom error types, that you
can leverage with [nom_locate ](https://github.com/fflorent/nom_locate ) to
pinpoint the exact line and column of the error. No need for separate
tokenizing, lexing and parsing phases: nom can automatically handle whitespace
parsing, and construct an AST in place.
Example projects:
- [PHP VM ](https://github.com/tagua-vm/parser )
- eve language prototype
2018-02-14 20:12:28 +00:00
- [xshade shading language ](https://github.com/xshade-lang/xshade/ )
2018-02-11 14:31:33 +00:00
### Streaming formats
While a lot of formats (and the code handling them) assume that they can fit
the complete data in memory, there are formats for which we only get a part
of the data at once, like network formats, or huge files.
2020-05-30 09:53:44 +00:00
nom has been designed for a correct behaviour with partial data: If there is
2018-02-11 14:31:33 +00:00
not enough data to decide, nom will tell you it needs more instead of silently
returning a wrong result. Whether your data comes entirely or in chunks, the
result should be the same.
It allows you to build powerful, deterministic state machines for your protocols.
Example projects:
2022-01-14 15:58:27 +00:00
- [HTTP proxy ](https://github.com/sozu-proxy/sozu/tree/main/lib/src/protocol/http/parser )
2020-05-30 09:53:44 +00:00
- [Using nom with generators ](https://github.com/Geal/generator_nom )
2018-02-11 14:31:33 +00:00
2018-02-13 10:56:41 +00:00
## Parser combinators
Parser combinators are an approach to parsers that is very different from
software like [lex ](https://en.wikipedia.org/wiki/Lex_(software )) and
[yacc ](https://en.wikipedia.org/wiki/Yacc ). Instead of writing the grammar
in a separate file and generating the corresponding code, you use very
small functions with very specific purpose, like "take 5 bytes", or
2021-06-13 07:58:12 +00:00
"recognize the word 'HTTP'", and assemble them in meaningful patterns
2018-02-13 10:56:41 +00:00
like "recognize 'HTTP', then a space, then a version".
The resulting code is small, and looks like the grammar you would have
written with other parser approaches.
This has a few advantages:
2020-05-30 09:53:44 +00:00
- The parsers are small and easy to write
- The parsers components are easy to reuse (if they're general enough, please add them to nom!)
- The parsers components are easy to test separately (unit tests and property-based tests)
- The parser combination code looks close to the grammar you would have written
- You can build partial parsers, specific to the data you need at the moment, and ignore the rest
2018-02-13 10:56:41 +00:00
2018-02-11 14:31:33 +00:00
## Technical features
nom parsers are for:
2020-05-30 09:53:44 +00:00
- [x] **byte-oriented** : The basic type is `&[u8]` and parsers will work as much as possible on byte array slices (but are not limited to them)
2015-09-08 16:15:45 +00:00
- [x] **bit-oriented** : nom can address a byte slice as a bit stream
2020-05-30 09:53:44 +00:00
- [x] **string-oriented** : The same kind of combinators can apply on UTF-8 strings as well
- [x] **zero-copy** : If a parser returns a subset of its input data, it will return a slice of that input, without copying
2018-02-03 16:18:12 +00:00
- [x] **streaming** : nom can work on partial data and detect when it needs more data to produce a correct result
2020-05-30 09:53:44 +00:00
- [x] **descriptive errors** : The parsers can aggregate a list of error codes with pointers to the incriminated input slice. Those error lists can be pattern matched to provide useful messages.
- [x] **custom error types** : You can provide a specific type to improve errors returned by parsers
2015-11-07 19:46:43 +00:00
- [x] **safe parsing** : nom leverages Rust's safe memory handling and powerful types, and parsers are routinely fuzzed and tested with real world data. So far, the only flaws found by fuzzing were in code written outside of nom
2020-05-30 09:53:44 +00:00
- [x] **speed** : Benchmarks have shown that nom parsers often outperform many parser combinators library like Parsec and attoparsec, some regular expression engines and even handwritten C parsers
2015-02-23 22:15:27 +00:00
2015-03-10 18:06:32 +00:00
Some benchmarks are available on [Github ](https://github.com/Geal/nom_benchmarks ).
2022-01-14 18:00:55 +00:00
## Rust version requirements (MSRV)
2018-10-12 17:14:49 +00:00
2021-08-21 10:13:43 +00:00
The 7.0 series of nom supports **Rustc version 1.48 or greater** . It is known to work properly on Rust 1.41.1 but there is no guarantee it will stay the case through this major release.
2018-10-12 17:14:49 +00:00
The current policy is that this will only be updated in the next major nom release.
2015-02-23 22:15:27 +00:00
## Installation
nom is available on [crates.io ](https://crates.io/crates/nom ) and can be included in your Cargo enabled project like this:
```toml
[dependencies]
2021-08-21 10:13:43 +00:00
nom = "7"
2015-02-23 22:15:27 +00:00
```
2015-09-08 15:07:32 +00:00
There are a few compilation features:
2021-08-21 10:13:43 +00:00
* `alloc` : (activated by default) if disabled, nom can work in `no_std` builds without memory allocators. If enabled, combinators that allocate (like `many0` ) will be available
* `std` : (activated by default, activates `alloc` too) if disabled, nom can work in `no_std` builds
2015-02-23 22:15:27 +00:00
2021-08-21 10:13:43 +00:00
You can configure those features like this:
2015-10-30 17:23:33 +00:00
```toml
[dependencies.nom]
2021-08-21 10:13:43 +00:00
version = "7"
default-features = false
features = ["alloc"]
2015-10-30 17:23:33 +00:00
```
2018-05-14 12:25:54 +00:00
# Related projects
2020-05-30 09:53:44 +00:00
- [Get line and column info in nom's input type ](https://github.com/fflorent/nom_locate )
- [Using nom as lexer and parser ](https://github.com/Rydgel/monkey-rust )
2018-05-14 12:25:54 +00:00
2016-03-04 15:13:14 +00:00
# Parsers written with nom
2019-05-13 15:04:28 +00:00
Here is a (non exhaustive) list of known projects using nom:
2016-03-04 15:13:14 +00:00
2020-10-24 09:39:45 +00:00
- Text file formats: [Ceph Crush ](https://github.com/cholcombe973/crushtool ),
[Cronenberg ](https://github.com/ayrat555/cronenberg ),
[XFS Runtime Stats ](https://github.com/ChrisMacNaughton/xfs-rs ),
[CSV ](https://github.com/GuillaumeGomez/csv-parser ),
[FASTA ](https://github.com/TianyiShi2001/nom-fasta ),
[FASTQ ](https://github.com/elij/fastq.rs ),
2021-12-20 20:33:39 +00:00
[INI ](https://github.com/Geal/nom/blob/main/tests/ini.rs ),
2020-10-24 09:39:45 +00:00
[ISO 8601 dates ](https://github.com/badboy/iso8601 ),
[libconfig-like configuration file format ](https://github.com/filipegoncalves/rust-config ),
[Web archive ](https://github.com/sbeckeriv/warc_nom_parser ),
[PDB ](https://github.com/TianyiShi2001/nom-pdb ),
[proto files ](https://github.com/tafia/protobuf-parser ),
[Fountain screenplay markup ](https://github.com/adamchalmers/fountain-rs ),
[vimwiki ](https://github.com/chipsenkbeil/vimwiki-server/tree/master/vimwiki ) & [vimwiki_macros ](https://github.com/chipsenkbeil/vimwiki-server/tree/master/vimwiki_macros )
2017-08-03 00:56:53 +00:00
- Programming languages:
2020-10-24 09:39:45 +00:00
[PHP ](https://github.com/tagua-vm/parser ),
[Basic Calculator ](https://github.com/balajisivaraman/basic_calculator_rs ),
[GLSL ](https://github.com/phaazon/glsl ),
[Lua ](https://github.com/doomrobo/nom-lua53 ),
[Python ](https://github.com/ProgVal/rust-python-parser ),
[SQL ](https://github.com/ms705/nom-sql ),
[Elm ](https://github.com/cout970/Elm-interpreter ),
[SystemVerilog ](https://github.com/dalance/sv-parser ),
[Turtle ](https://github.com/vandenoever/rome/tree/master/src/io/turtle ),
2021-09-09 20:44:58 +00:00
[CSML ](https://github.com/CSML-by-Clevy/csml-interpreter ),
2021-09-25 14:28:36 +00:00
[Wasm ](https://github.com/Strytyp/wasm-nom ),
[Pseudocode ](https://github.com/Gungy2/pseudocode )
2021-12-16 13:23:52 +00:00
[Filter for MeiliSearch ](https://github.com/meilisearch/meilisearch )
2020-10-24 09:39:45 +00:00
- Interface definition formats: [Thrift ](https://github.com/thehydroimpulse/thrust )
2016-03-04 15:13:14 +00:00
- Audio, video and image formats:
2020-10-24 09:39:45 +00:00
[GIF ](https://github.com/Geal/gif.rs ),
[MagicaVoxel .vox ](https://github.com/davidedmonds/dot_vox ),
[midi ](https://github.com/derekdreery/nom-midi-rs ),
[SWF ](https://github.com/open-flash/swf-parser ),
2020-10-24 09:50:08 +00:00
[WAVE ](http://github.com/noise-Labs/wave ),
[Matroska (MKV) ](https://github.com/rust-av/matroska )
2016-03-04 15:13:14 +00:00
- Document formats:
2020-10-24 09:39:45 +00:00
[TAR ](https://github.com/Keruspe/tar-parser.rs ),
2022-02-05 15:35:33 +00:00
[GZ ](https://github.com/nharward/nom-gzip ),
2022-02-05 15:28:57 +00:00
[GDSII ](https://github.com/erihsu/gds2-io )
2020-03-27 08:58:50 +00:00
- Cryptographic formats:
2020-10-24 09:39:45 +00:00
[X.509 ](https://github.com/rusticata/x509-parser )
2016-10-16 09:51:23 +00:00
- Network protocol formats:
2020-10-24 09:39:45 +00:00
[Bencode ](https://github.com/jbaum98/bencode.rs ),
2020-11-11 06:44:13 +00:00
[D-Bus ](https://github.com/toshokan/misato ),
2020-10-24 09:39:45 +00:00
[DHCP ](https://github.com/rusticata/dhcp-parser ),
2022-01-14 15:58:27 +00:00
[HTTP ](https://github.com/sozu-proxy/sozu/tree/main/lib/src/protocol/http ),
2020-10-24 09:39:45 +00:00
[URI ](https://github.com/santifa/rrp/blob/master/src/uri.rs ),
[IMAP ](https://github.com/djc/tokio-imap ),
[IRC ](https://github.com/Detegr/RBot-parser ),
[Pcap-NG ](https://github.com/richo/pcapng-rs ),
[Pcap ](https://github.com/ithinuel/pcap-rs ),
[Pcap + PcapNG ](https://github.com/rusticata/pcap-parser ),
[IKEv2 ](https://github.com/rusticata/ipsec-parser ),
[NTP ](https://github.com/rusticata/ntp-parser ),
[SNMP ](https://github.com/rusticata/snmp-parser ),
[Kerberos v5 ](https://github.com/rusticata/kerberos-parser ),
[DER ](https://github.com/rusticata/der-parser ),
[TLS ](https://github.com/rusticata/tls-parser ),
[IPFIX / Netflow v10 ](https://github.com/dominotree/rs-ipfix ),
[GTP ](https://github.com/fuerstenau/gorrosion-gtp ),
2020-11-12 01:31:41 +00:00
[SIP ](https://github.com/armatusmiles/sipcore/tree/master/crates/sipmsg ),
[Prometheus ](https://github.com/timberio/vector/blob/master/lib/prometheus-parser/src/line.rs )
2017-10-28 22:57:43 +00:00
- Language specifications:
2020-10-24 09:39:45 +00:00
[BNF ](https://github.com/snewt/bnf )
2020-03-27 09:17:34 +00:00
- Misc formats:
2020-10-24 09:39:45 +00:00
[Gameboy ROM ](https://github.com/MarkMcCaskey/gameboy-rom-parser ),
[ANT FIT ](https://github.com/stadelmanma/fitparse-rs ),
[Version Numbers ](https://github.com/fosskers/rs-versions ),
[Telcordia/Bellcore SR-4731 SOR OTDR files ](https://github.com/JamesHarrison/otdrs ),
2020-10-24 16:23:10 +00:00
[MySQL binary log ](https://github.com/PrivateRookie/boxercrab ),
2020-12-04 08:56:52 +00:00
[URI ](https://github.com/Skasselbard/nom-uri ),
2022-03-30 18:24:43 +00:00
[Furigana ](https://github.com/sachaarbonel/furigana.rs ),
[Wordle Result ](https://github.com/Fyko/wordle-stats/tree/main/parser )
2016-03-04 15:13:14 +00:00
Want to create a new parser using `nom` ? A list of not yet implemented formats is available [here ](https://github.com/Geal/nom/issues/14 ).
Want to add your parser here? Create a pull request for it!
2020-11-24 10:43:03 +00:00
# Contributors
nom is the fruit of the work of many contributors over the years, many thanks for your help!
< a href = "https://github.com/geal/nom/graphs/contributors" >
< img src = "https://contributors-img.web.app/image?repo=geal/nom" / >
< / a >