nom
Rust parser combinator framework
A Rust library for building parsers by composing small reusable functions, no grammar files, no code generation needed. Works on binary data, plain text, and streaming input that arrives in chunks.
nom is a library for writing parsers in the Rust programming language. A parser, in this context, is a piece of code that reads raw data and extracts structured information from it. For example, a parser might read a chunk of bytes and recognize that it contains an HTTP request, a JSON document, or a custom binary file format.
Instead of requiring you to write grammar files in a separate language and run a code generator, nom takes a different approach called parser combinators. You build small, focused functions that each handle one tiny task, such as "match the word HTTP" or "read exactly four bytes", and then combine them into larger parsers. The resulting code stays in Rust, can be tested piece by piece, and closely mirrors what the format actually looks like.
nom works on binary data, plain text, and streaming input. For streaming, it is designed to handle situations where data arrives in chunks rather than all at once, such as network traffic or large files. If there is not enough data yet to make a decision, nom signals that it needs more rather than producing a wrong result. It also avoids copying data wherever possible, returning slices of the original input instead of allocating new buffers.
The library has been used to build parsers for HTTP, video containers like FLV and Matroska, archive formats like tar, configuration languages like TOML, and prototype programming language compilers. It supports custom error types so that when a parse fails, you can report the exact location and reason rather than a generic failure message.
nom is available as a package on crates.io, the standard package registry for Rust, and works with Rust version 1.65.0 or newer. Documentation, a beginner guide called the Nominomicon, and a list of available combinators are all linked from the repository.
Where it fits
- Parse a custom binary file format in Rust by combining small, independently testable parsing functions.
- Build a streaming network protocol parser that handles data arriving in chunks without producing wrong results mid-stream.
- Write a parser for a configuration language like TOML in pure Rust without a separate grammar file or code generator.
- Build a prototype compiler front end by composing nom combinators to tokenize and parse source code.