<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="rustdoc">
<meta name="description" content="API documentation for the Rust `nom` crate.">
<meta name="keywords" content="rust, rustlang, rust-lang, nom">
<title>nom - Rust</title>
<link rel="stylesheet" type="text/css" href="../main.css">
</head>
<body class="rustdoc">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<section class="sidebar">
<p class='location'></p><script>window.sidebarCurrent = {name: 'nom', ty: 'mod', relpath: '../'};</script>
</section>
<nav class="sub">
<form class="search-form js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press 'S' to search, '?' for more options..."
type="search">
</div>
</form>
</nav>
<section id='main' class="content mod">
<h1 class='fqn'><span class='in-band'>Crate <a class='mod' href=''>nom</a></span><span class='out-of-band'><span id='render-detail'>
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
[<span class='inner'>−</span>]
</a>
</span><a id='src-0' class='srclink' href='../src/nom/lib.rs.html#1-143' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>Nom, eating data byte by byte</p>
<p>The goal is to make a parser combinator library that is safe,
supports streaming (push and pull), and as much as possible zero copy.</p>
<p>The code is available on <a href="https://github.com/Geal/nom">Github</a></p>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><pre class='rust rust-example-rendered'>
<span class='attribute'>#[<span class='ident'>macro_use</span>]</span>
<span class='kw'>extern</span> <span class='kw'>crate</span> <span class='ident'>nom</span>;
<span class='kw'>use</span> <span class='ident'>nom</span>::{<span class='ident'>Consumer</span>,<span class='ident'>ConsumerState</span>,<span class='ident'>MemProducer</span>,<span class='ident'>IResult</span>};
<span class='kw'>use</span> <span class='ident'>nom</span>::<span class='ident'>IResult</span>::<span class='op'>*</span>;
<span class='comment'>// Parser definition</span>
<span class='macro'>named</span><span class='macro'>!</span>( <span class='ident'>om_parser</span>, <span class='macro'>tag</span><span class='macro'>!</span>( <span class='string'>"om"</span> ) );
<span class='macro'>named</span><span class='macro'>!</span>( <span class='ident'>nomnom_parser</span><span class='op'><</span> <span class='kw-2'>&</span>[<span class='ident'>u8</span>], <span class='ident'>Vec</span><span class='op'><</span><span class='kw-2'>&</span>[<span class='ident'>u8</span>]<span class='op'>></span> <span class='op'>></span>, <span class='macro'>many1</span><span class='macro'>!</span>( <span class='macro'>tag</span><span class='macro'>!</span>( <span class='string'>"nom"</span> ) ) );
<span class='macro'>named</span><span class='macro'>!</span>( <span class='ident'>end_parser</span>, <span class='macro'>tag</span><span class='macro'>!</span>( <span class='string'>"kthxbye"</span>) );
<span class='comment'>// Streaming parsing and state machine</span>
<span class='attribute'>#[<span class='ident'>derive</span>(<span class='ident'>PartialEq</span>,<span class='ident'>Eq</span>,<span class='ident'>Debug</span>)]</span>
<span class='kw'>enum</span> <span class='ident'>State</span> {
<span class='ident'>Beginning</span>,
<span class='ident'>Middle</span>,
<span class='ident'>End</span>,
<span class='ident'>Done</span>
}
<span class='kw'>struct</span> <span class='ident'>TestConsumer</span> {
<span class='ident'>state</span>: <span class='ident'>State</span>,
<span class='ident'>counter</span>: <span class='ident'>usize</span>
}
<span class='kw'>impl</span> <span class='ident'>Consumer</span> <span class='kw'>for</span> <span class='ident'>TestConsumer</span> {
<span class='kw'>fn</span> <span class='ident'>consume</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='self'>self</span>, <span class='ident'>input</span>: <span class='kw-2'>&</span>[<span class='ident'>u8</span>]) <span class='op'>-></span> <span class='ident'>ConsumerState</span> {
<span class='kw'>match</span> <span class='self'>self</span>.<span class='ident'>state</span> {
<span class='ident'>State</span>::<span class='ident'>Beginning</span> <span class='op'>=></span> {
<span class='kw'>match</span> <span class='ident'>om_parser</span>(<span class='ident'>input</span>) {
<span class='ident'>Error</span>(_) <span class='op'>=></span> <span class='ident'>ConsumerState</span>::<span class='ident'>ConsumerError</span>(<span class='number'>0</span>),
<span class='ident'>Incomplete</span>(_) <span class='op'>=></span> <span class='ident'>ConsumerState</span>::<span class='ident'>Await</span>(<span class='number'>0</span>, <span class='number'>2</span>),
<span class='ident'>Done</span>(_,_) <span class='op'>=></span> {
<span class='comment'>// "om" was recognized, get to the next state</span>
<span class='self'>self</span>.<span class='ident'>state</span> <span class='op'>=</span> <span class='ident'>State</span>::<span class='ident'>Middle</span>;
<span class='ident'>ConsumerState</span>::<span class='ident'>Await</span>(<span class='number'>2</span>, <span class='number'>3</span>)
}
}
},
<span class='ident'>State</span>::<span class='ident'>Middle</span> <span class='op'>=></span> {
<span class='kw'>match</span> <span class='ident'>nomnom_parser</span>(<span class='ident'>input</span>) {
<span class='ident'>Error</span>(<span class='ident'>a</span>) <span class='op'>=></span> {
<span class='comment'>// the "nom" parser failed, let's get to the next state</span>
<span class='self'>self</span>.<span class='ident'>state</span> <span class='op'>=</span> <span class='ident'>State</span>::<span class='ident'>End</span>;
<span class='ident'>ConsumerState</span>::<span class='ident'>Await</span>(<span class='number'>0</span>, <span class='number'>7</span>)
},
<span class='ident'>Incomplete</span>(_) <span class='op'>=></span> <span class='ident'>ConsumerState</span>::<span class='ident'>Await</span>(<span class='number'>0</span>, <span class='number'>3</span>),
<span class='ident'>Done</span>(<span class='ident'>i</span>,<span class='ident'>noms_vec</span>) <span class='op'>=></span> {
<span class='comment'>// we got a few noms, let's count them and continue</span>
<span class='self'>self</span>.<span class='ident'>counter</span> <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>counter</span> <span class='op'>+</span> <span class='ident'>noms_vec</span>.<span class='ident'>len</span>();
<span class='ident'>ConsumerState</span>::<span class='ident'>Await</span>(<span class='ident'>input</span>.<span class='ident'>len</span>() <span class='op'>-</span> <span class='ident'>i</span>.<span class='ident'>len</span>(), <span class='number'>3</span>)
}
}
},
<span class='ident'>State</span>::<span class='ident'>End</span> <span class='op'>=></span> {
<span class='kw'>match</span> <span class='ident'>end_parser</span>(<span class='ident'>input</span>) {
<span class='ident'>Error</span>(_) <span class='op'>=></span> <span class='ident'>ConsumerState</span>::<span class='ident'>ConsumerError</span>(<span class='number'>0</span>),
<span class='ident'>Incomplete</span>(_) <span class='op'>=></span> <span class='ident'>ConsumerState</span>::<span class='ident'>Await</span>(<span class='number'>0</span>, <span class='number'>7</span>),
<span class='ident'>Done</span>(_,_) <span class='op'>=></span> {
<span class='comment'>// we recognized the suffix, everything was parsed correctly</span>
<span class='self'>self</span>.<span class='ident'>state</span> <span class='op'>=</span> <span class='ident'>State</span>::<span class='ident'>Done</span>;
<span class='ident'>ConsumerState</span>::<span class='ident'>ConsumerDone</span>
}
}
},
<span class='ident'>State</span>::<span class='ident'>Done</span> <span class='op'>=></span> {
<span class='comment'>// this should not be called</span>
<span class='ident'>ConsumerState</span>::<span class='ident'>ConsumerError</span>(<span class='number'>42</span>)
}
}
}
<span class='kw'>fn</span> <span class='ident'>end</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='self'>self</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"we counted {} noms"</span>, <span class='self'>self</span>.<span class='ident'>counter</span>);
}
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>p</span> <span class='op'>=</span> <span class='ident'>MemProducer</span>::<span class='ident'>new</span>(<span class='string'>b"omnomnomnomkthxbye"</span>, <span class='number'>4</span>);
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>c</span> <span class='op'>=</span> <span class='ident'>TestConsumer</span>{<span class='ident'>state</span>: <span class='ident'>State</span>::<span class='ident'>Beginning</span>, <span class='ident'>counter</span>: <span class='number'>0</span>};
<span class='ident'>c</span>.<span class='ident'>run</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>p</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>c</span>.<span class='ident'>counter</span>, <span class='number'>3</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>c</span>.<span class='ident'>state</span>, <span class='ident'>State</span>::<span class='ident'>Done</span>);
}
</pre>
</div><h2 id='reexports' class='section-header'><a href="#reexports">Reexports</a></h2>
<table><tr><td><code>pub use self::<a class='mod' href='../nom/util/index.html' title='nom::util'>util</a>::*;</code></td></tr><tr><td><code>pub use self::<a class='mod' href='../nom/internal/index.html' title='nom::internal'>internal</a>::*;</code></td></tr><tr><td><code>pub use self::<a class='mod' href='../nom/macros/index.html' title='nom::macros'>macros</a>::*;</code></td></tr><tr><td><code>pub use self::<a class='mod' href='../nom/producer/index.html' title='nom::producer'>producer</a>::*;</code></td></tr><tr><td><code>pub use self::<a class='mod' href='../nom/consumer/index.html' title='nom::consumer'>consumer</a>::*;</code></td></tr><tr><td><code>pub use self::<a class='mod' href='../nom/nom/index.html' title='nom::nom'>nom</a>::*;</code></td></tr></table><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
<table>
<tr class=' module-item'>
<td><a class='mod' href='consumer/index.html'
title='nom::consumer'>consumer</a></td>
<td class='docblock short'>
<p>Data consumers</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='mod' href='internal/index.html'
title='nom::internal'>internal</a></td>
<td class='docblock short'>
<p>Basic types to build the parsers</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='mod' href='macros/index.html'
title='nom::macros'>macros</a></td>
<td class='docblock short'>
<p>Macro combinators</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='mod' href='nom/index.html'
title='nom::nom'>nom</a></td>
<td class='docblock short'>
<p>Useful parser combinators</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='mod' href='producer/index.html'
title='nom::producer'>producer</a></td>
<td class='docblock short'>
<p>Data producers</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='mod' href='util/index.html'
title='nom::util'>util</a></td>
<td class='docblock short'>
</td>
</tr>
</table><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
<table>
<tr class=' module-item'>
<td><a class='macro' href='macro.alt!.html'
title='nom::alt!'>alt!</a></td>
<td class='docblock short'>
<p>try a list of parser, return the result of the first successful one</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.alt_parser!.html'
title='nom::alt_parser!'>alt_parser!</a></td>
<td class='docblock short'>
<p>Internal parser, do not use directly</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.apply!.html'
title='nom::apply!'>apply!</a></td>
<td class='docblock short'>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.call!.html'
title='nom::call!'>call!</a></td>
<td class='docblock short'>
<p>Used to wrap common expressions and function as macros</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.chain!.html'
title='nom::chain!'>chain!</a></td>
<td class='docblock short'>
<p>chains parsers and assemble the results through a closure</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.chaining_parser!.html'
title='nom::chaining_parser!'>chaining_parser!</a></td>
<td class='docblock short'>
<p>Internal parser, do not use directly</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.closure!.html'
title='nom::closure!'>closure!</a></td>
<td class='docblock short'>
<p>Wraps a parser in a closure</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.cond!.html'
title='nom::cond!'>cond!</a></td>
<td class='docblock short'>
<p>Conditional combinator</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.count!.html'
title='nom::count!'>count!</a></td>
<td class='docblock short'>
<p>Applies the child parser a specified number of times</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.dbg!.html'
title='nom::dbg!'>dbg!</a></td>
<td class='docblock short'>
<p>Prints a message if the parser fails</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.dbg_dmp!.html'
title='nom::dbg_dmp!'>dbg_dmp!</a></td>
<td class='docblock short'>
<p>Prints a message and the input if the parser fails</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.delimited!.html'
title='nom::delimited!'>delimited!</a></td>
<td class='docblock short'>
<p>delimited(opening, X, closing) returns X</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.error!.html'
title='nom::error!'>error!</a></td>
<td class='docblock short'>
<p>Prevents backtracking if the child parser fails</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.filter!.html'
title='nom::filter!'>filter!</a></td>
<td class='docblock short'>
<p>returns the longest list of bytes until the provided parser fails</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.flat_map!.html'
title='nom::flat_map!'>flat_map!</a></td>
<td class='docblock short'>
<p>flat_map! combines a parser R -> IResult<R,S> and
a parser S -> IResult<S,T> to return another
parser R -> IResult<R,T></p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.is_a!.html'
title='nom::is_a!'>is_a!</a></td>
<td class='docblock short'>
<p>returns the longest list of bytes that appear in the provided array</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.is_not!.html'
title='nom::is_not!'>is_not!</a></td>
<td class='docblock short'>
<p>returns the longest list of bytes that do not appear in the provided array</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.length_value!.html'
title='nom::length_value!'>length_value!</a></td>
<td class='docblock short'>
<p>returns</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.many0!.html'
title='nom::many0!'>many0!</a></td>
<td class='docblock short'>
<p>Applies the parser 0 or more times and returns the list of results in a Vec</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.many1!.html'
title='nom::many1!'>many1!</a></td>
<td class='docblock short'>
<p>Applies the parser 1 or more times and returns the list of results in a Vec</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.map!.html'
title='nom::map!'>map!</a></td>
<td class='docblock short'>
<p>maps a function on the result of a parser</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.map_opt!.html'
title='nom::map_opt!'>map_opt!</a></td>
<td class='docblock short'>
<p>maps a function returning an Option on the output of a parser</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.map_res!.html'
title='nom::map_res!'>map_res!</a></td>
<td class='docblock short'>
<p>maps a function returning a Result on the output of a parser</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.named!.html'
title='nom::named!'>named!</a></td>
<td class='docblock short'>
<p>Makes a function from a parser combination</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.opt!.html'
title='nom::opt!'>opt!</a></td>
<td class='docblock short'>
<p>make the underlying parser optional</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.pair!.html'
title='nom::pair!'>pair!</a></td>
<td class='docblock short'>
<p>pair(X,Y), returns (x,y)</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.peek!.html'
title='nom::peek!'>peek!</a></td>
<td class='docblock short'>
<p>returns a result without consuming the input</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.preceded!.html'
title='nom::preceded!'>preceded!</a></td>
<td class='docblock short'>
<p>preceded(opening, X) returns X</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.pusher!.html'
title='nom::pusher!'>pusher!</a></td>
<td class='docblock short'>
<p>Prepares a parser function for a push pipeline</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.separated_list!.html'
title='nom::separated_list!'>separated_list!</a></td>
<td class='docblock short'>
<p>separated_list(sep, X) returns Vec<X></p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.separated_nonempty_list!.html'
title='nom::separated_nonempty_list!'>separated_nonempty_list!</a></td>
<td class='docblock short'>
<p>separated_nonempty_list(sep, X) returns Vec<X></p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.separated_pair!.html'
title='nom::separated_pair!'>separated_pair!</a></td>
<td class='docblock short'>
<p>separated_pair(X,sep,Y) returns (x,y)</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.tag!.html'
title='nom::tag!'>tag!</a></td>
<td class='docblock short'>
<p>declares a byte array as a suite to recognize</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.take!.html'
title='nom::take!'>take!</a></td>
<td class='docblock short'>
<p>generates a parser consuming the specified number of bytes</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.take_str!.html'
title='nom::take_str!'>take_str!</a></td>
<td class='docblock short'>
<p>same as take! but returning a &str</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.take_until!.html'
title='nom::take_until!'>take_until!</a></td>
<td class='docblock short'>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.take_until_and_consume!.html'
title='nom::take_until_and_consume!'>take_until_and_consume!</a></td>
<td class='docblock short'>
<p>generates a parser consuming bytes until the specified byte sequence is found</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.take_until_either!.html'
title='nom::take_until_either!'>take_until_either!</a></td>
<td class='docblock short'>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.take_until_either_and_consume!.html'
title='nom::take_until_either_and_consume!'>take_until_either_and_consume!</a></td>
<td class='docblock short'>
</td>
</tr>
<tr class=' module-item'>
<td><a class='macro' href='macro.terminated!.html'
title='nom::terminated!'>terminated!</a></td>
<td class='docblock short'>
<p>terminated(X, closing) returns X</p>
</td>
</tr>
</table></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<div id="help" class="hidden">
<div class="shortcuts">
<h1>Keyboard shortcuts</h1>
<dl>
<dt>?</dt>
<dd>Show this help dialog</dd>
<dt>S</dt>
<dd>Focus the search field</dd>
<dt>⇤</dt>
<dd>Move up in search results</dd>
<dt>⇥</dt>
<dd>Move down in search results</dd>
<dt>⏎</dt>
<dd>Go to active search result</dd>
</dl>
</div>
<div class="infos">
<h1>Search tricks</h1>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>typedef</code> (or
<code>tdef</code>).
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code>)
</p>
</div>
</div>
<script>
window.rootPath = "../";
window.currentCrate = "nom";
window.playgroundUrl = "";
</script>
<script src="../jquery.js"></script>
<script src="../main.js"></script>
<script async src="../search-index.js"></script>
</body>
</html>