nom, eating data byte by byte
nom is a parser combinator library with a focus on safe parsing, streaming patterns, and as much as possible zero copy.
Example
extern crate nom;
named!;
named!;
The code is available on Github
There are a few guides with more details about the design of nom, how to write parsers, or the error management system.
Looking for a specific combinator? Read the "choose a combinator" guide
If you are upgrading to nom 2.0, please read the migration document.
If you are upgrading to nom 4.0, please read the migration document.
See also the FAQ.
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 then 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
Here is an example of one such parser, to recognize text between parentheses:
extern crate nom;
#
It defines a function named parens
, which will recognize a sequence of the character (
, the longest byte array not containing )
, then the character )
, and will return the byte array in the middle.
Here is another parser, written without using nom's macros this time:
extern crate nom;
use ;
#
This function takes a byte array as input, and tries to consume 4 bytes. Writing all the parsers manually, like this, is dangerous, despite Rust's safety features. There are still a lot of mistakes one can make. That's why nom provides a list of macros to help in developing parsers.
With macros, you would write it like this:
extern crate nom;
#
A parser in nom is a function which, for an input type I
, an output type O
and an optional error type E
, will have the following signature:
;
Or like this, if you don't want to specify a custom error type (it will be u32
by default):
;
IResult
is an alias for the Result
type:
use ;
type IResult<I, O, E = > = ;
It can have the following values:
- a correct result
Ok((I,O))
with the first element being the remaining of the input (not parsed yet), and the second the output value; - an error
Err(Err::Error(c))
withc
an enum that contains an error code with its position in the input, and optionally a chain of accumulated errors; - an error
Err(Err::Incomplete(Needed))
indicating that more input is necessary.Needed
can indicate how much data is needed - an error
Err(Err::Failure(c))
. It works like theError
case, except it indicates an unrecoverable error: we cannot backtrack and test another parser
Please refer to the "choose a combinator" guide for an exhaustive list of parsers. See also the rest of the documentation here. .
Making new parsers with macros
Macros are the main way to make new parsers by combining other ones. Those macros accept other macros or function names as arguments. You then need to make a function out of that combinator with named!
, or a closure with closure!
. Here is how you would do, with the tag!
and take!
combinators:
# extern crate nom;
#
The named!
macro can take three different syntaxes:
named!;
named!;
named!; // when you know the parser takes &[u8] as input, and returns &[u8] as output
IMPORTANT NOTE: Rust's macros can be very sensitive to the syntax, so you may encounter an error compiling parsers like this one:
# extern crate nom;
#
#
#
#
You will get the following error: error: expected an item keyword
. This
happens because >>
is seen as an operator, so the macro parser does not
recognize what we want. There is a way to avoid it, by inserting a space:
# extern crate nom;
#
#
#
#
This will compile correctly. I am very sorry for this inconvenience.
Combining parsers
There are more high level patterns, like the alt!
combinator, which provides a choice between multiple parsers. If one branch fails, it tries the next, and returns the result of the first parser that succeeds:
# extern crate nom;
#
The pipe |
character is used as separator.
The opt!
combinator makes a parser optional. If the child parser returns an error, opt!
will succeed and return None:
# extern crate nom;
#
many0!
applies a parser 0 or more times, and returns a vector of the aggregated results:
# extern crate nom;
#
#
#
#
Here are some basic combining macros available:
opt!
: will make the parser optional (if it returns theO
type, the new parser returnsOption<O>
)many0!
: will apply the parser 0 or more times (if it returns theO
type, the new parser returnsVec<O>
)many1!
: will apply the parser 1 or more times
There are more complex (and more useful) parsers like do_parse!
and tuple!
, which are used to apply a series of parsers then assemble their results.
Example with tuple!
:
# extern crate nom;
#
Example with do_parse!
:
# extern crate nom;
#
The double right arrow >>
is used as separator between every parser in the sequence, and the last closure can see the variables storing the result of parsers. Unless the specified return type is already a tuple, the final line should be that type wrapped in a tuple.
More examples of do_parse!
and tuple!
usage can be found in the INI file parser example.
Going further: read the guides!