百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

nom

> 编程语言
开源

Rust 解析器组合器框架

10.4K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Rust 解析器组合器框架

nom, eating data byte by byte

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.

nom will happily take a byte out of your files :)

  • Example
  • Documentation
  • Why use nom?
    • Binary format parsers
    • Text format parsers
    • Programming language parsers
    • Streaming formats
  • Parser combinators
  • Technical features
  • Rust version requirements
  • Installation
  • Related projects
  • Parsers written with nom
  • Contributors

Example

Hexadecimal color parser:

…

Documentation

  • Reference documentation
  • The Nominomicon: A Guide To Using Nom
  • Various design documents and tutorials
  • List of combinators and their behaviour

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.

Why use nom

If you want to write:

Binary format parsers

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
  • Bit level parsing
  • Hexadecimal viewer in the debugging macros for easy data analysis
  • Streaming parsers for network formats and huge files

Example projects:

  • FLV parser
  • Matroska parser
  • tar parser

Text format parsers

While nom was made for binary format at first, it soon grew to work just as 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:

  • 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

Example projects:

  • HTTP proxy
  • TOML parser

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 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
  • xshade shading language

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. nom has been designed for a correct behaviour with partial data: If there is 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:

  • HTTP proxy
  • Using nom with generators

Parser combinators

Parser combinators are an approach to parsers that is very different from software like lex and 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 "recognize the word 'HTTP'", and assemble them in meaningful patterns 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:

  • 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

Technical features

nom parsers are for:

  • 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)
  • bit-oriented: nom can address a byte slice as a bit stream
  • string-oriented: The same kind of combinators can apply on UTF-8 strings as well
  • zero-copy: If a parser returns a subset of its input data, it will return a slice of that input, without copying
  • streaming: nom can work on partial data and detect when it needs more data to produce a correct result
  • 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.
  • custom error types: You can provide a specific type to improve errors returned by parsers
  • 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
  • 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

Some benchmarks are available on GitHub.

Rust version requirements (MSRV)

The 8.0 series of nom supports Rustc version 1.65 or greater.

The current policy is that this will only be updated in the next major nom release.

Installation

nom is available on crates.io and can be included in your Cargo enabled project like this:

[dependencies]
nom = "8"

There are a few compilation features:

  • 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

You can configure those features like this:

[dependencies.nom]
version = "8"
default-features = false
features = ["alloc"]

Related projects

  • Get line and column info in nom's input type
  • Using nom as lexer and parser

Parsers written with nom

Here is a (non exhaustive) list of known projects using nom:

  • Text file formats: Ceph Crush, Cronenberg, Email, XFS Runtime Stats, CSV, FASTA, FASTQ, INI, ISO 8601 dates, libconfig-like configuration file format, Web archive, PDB, proto files, Fountain screenplay markup, vimwiki, vimwiki_macros, Kconfig language, Askama templates, LP files
  • Programming languages: PHP, Basic Calculator, GLSL Lua, Python, SQL, Elm, SystemVerilog, Turtle, CSML, Wasm, Pseudocode, Filter for MeiliSearch, PotterScript, R
  • Interface definition formats: Thrift
  • Audio, video and image formats: GIF, MagicaVoxel .vox, MIDI, SWF, WAVE, Matroska (MKV), Exif/Metadata parser for JPEG/HEIF/HEIC/MOV/MP4
  • Document formats: TAR, GZ, GDSII
  • Cryptographic formats: X.509
  • Network protocol formats: Bencode, D-Bus, DHCP, HTTP, URI, IMAP (alt), IRC, Pcap-NG, Pcap, Pcap + PcapNG, IKEv2, NTP, SNMP, Kerberos v5, DER, TLS, V5, V7, V9, IPFIX / Netflow v10, GTP, SIP, SMTP

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •Documentation
  • •Why use nom?
  • •Binary format parsers
  • •Text format parsers
  • •Programming language parsers
  • •Streaming formats
  • •Parser combinators
  • •Technical features
  • •Rust version requirements
  • •Installation

> 标签

Rustbyte-arraygrammarnomparse

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言