308 lines
No EOL
37 KiB
HTML
308 lines
No EOL
37 KiB
HTML
<!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="nom, eating data byte by byte"><title>nom - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Bold-a2c9cd1067f8b328.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-fa3bb1812debf86c.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="nom" data-themes="" data-resource-suffix="" data-rustdoc-version="1.74.0 (79e9716c9 2023-11-13)" data-channel="1.74.0" data-search-js="search-8be46b629f5f14a8.js" data-settings-js="settings-74424d7eec62a23e.js" ><script src="../static.files/storage-fec3eaa3851e447d.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-c5bd66d33317d69f.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-5d8b3c7633ad77ba.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="logo-container" href="../nom/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a></nav><nav class="sidebar"><a class="logo-container" href="../nom/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2 class="location"><a href="#">Crate nom</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 7.1.3</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li><li><a href="#types">Type Aliases</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">nom</a><button id="copy-path" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../src/nom/lib.rs.html#1-464">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><h2 id="nom-eating-data-byte-by-byte"><a href="#nom-eating-data-byte-by-byte">nom, eating data byte by byte</a></h2>
|
||
<p>nom is a parser combinator library with a focus on safe parsing,
|
||
streaming patterns, and as much as possible zero copy.</p>
|
||
<h3 id="example"><a href="#example">Example</a></h3>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{
|
||
IResult,
|
||
bytes::complete::{tag, take_while_m_n},
|
||
combinator::map_res,
|
||
sequence::tuple};
|
||
|
||
<span class="attr">#[derive(Debug,PartialEq)]
|
||
</span><span class="kw">pub struct </span>Color {
|
||
<span class="kw">pub </span>red: u8,
|
||
<span class="kw">pub </span>green: u8,
|
||
<span class="kw">pub </span>blue: u8,
|
||
}
|
||
|
||
<span class="kw">fn </span>from_hex(input: <span class="kw-2">&</span>str) -> <span class="prelude-ty">Result</span><u8, std::num::ParseIntError> {
|
||
u8::from_str_radix(input, <span class="number">16</span>)
|
||
}
|
||
|
||
<span class="kw">fn </span>is_hex_digit(c: char) -> bool {
|
||
c.is_digit(<span class="number">16</span>)
|
||
}
|
||
|
||
<span class="kw">fn </span>hex_primary(input: <span class="kw-2">&</span>str) -> IResult<<span class="kw-2">&</span>str, u8> {
|
||
map_res(
|
||
take_while_m_n(<span class="number">2</span>, <span class="number">2</span>, is_hex_digit),
|
||
from_hex
|
||
)(input)
|
||
}
|
||
|
||
<span class="kw">fn </span>hex_color(input: <span class="kw-2">&</span>str) -> IResult<<span class="kw-2">&</span>str, Color> {
|
||
<span class="kw">let </span>(input, <span class="kw">_</span>) = tag(<span class="string">"#"</span>)(input)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>(input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)<span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>((input, Color { red, green, blue }))
|
||
}
|
||
|
||
<span class="kw">fn </span>main() {
|
||
<span class="macro">assert_eq!</span>(hex_color(<span class="string">"#2F14DF"</span>), <span class="prelude-val">Ok</span>((<span class="string">""</span>, Color {
|
||
red: <span class="number">47</span>,
|
||
green: <span class="number">20</span>,
|
||
blue: <span class="number">223</span>,
|
||
})));
|
||
}</code></pre></div>
|
||
<p>The code is available on <a href="https://github.com/Geal/nom">Github</a></p>
|
||
<p>There are a few <a href="https://github.com/Geal/nom/tree/main/doc">guides</a> with more details
|
||
about <a href="https://github.com/Geal/nom/blob/main/doc/making_a_new_parser_from_scratch.md">how to write parsers</a>,
|
||
or the <a href="https://github.com/Geal/nom/blob/main/doc/error_management.md">error management system</a>.
|
||
You can also check out the [recipes] module that contains examples of common patterns.</p>
|
||
<p><strong>Looking for a specific combinator? Read the
|
||
<a href="https://github.com/Geal/nom/blob/main/doc/choosing_a_combinator.md">“choose a combinator” guide</a></strong></p>
|
||
<p>If you are upgrading to nom 5.0, please read the
|
||
<a href="https://github.com/Geal/nom/blob/main/doc/upgrading_to_nom_5.md">migration document</a>.</p>
|
||
<h3 id="parser-combinators"><a href="#parser-combinators">Parser combinators</a></h3>
|
||
<p>Parser combinators are an approach to parsers that is very different from
|
||
software like <a href="https://en.wikipedia.org/wiki/Lex_(software)">lex</a> and
|
||
<a href="https://en.wikipedia.org/wiki/Yacc">yacc</a>. Instead of writing the grammar
|
||
in a separate syntax and generating the corresponding code, you use very small
|
||
functions with very specific purposes, 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.</p>
|
||
<p>This gives us a few advantages:</p>
|
||
<ul>
|
||
<li>The parsers are small and easy to write</li>
|
||
<li>The parsers components are easy to reuse (if they’re general enough, please add them to nom!)</li>
|
||
<li>The parsers components are easy to test separately (unit tests and property-based tests)</li>
|
||
<li>The parser combination code looks close to the grammar you would have written</li>
|
||
<li>You can build partial parsers, specific to the data you need at the moment, and ignore the rest</li>
|
||
</ul>
|
||
<p>Here is an example of one such parser, to recognize text between parentheses:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{
|
||
IResult,
|
||
sequence::delimited,
|
||
<span class="comment">// see the "streaming/complete" paragraph lower for an explanation of these submodules
|
||
</span>character::complete::char,
|
||
bytes::complete::is_not
|
||
};
|
||
|
||
<span class="kw">fn </span>parens(input: <span class="kw-2">&</span>str) -> IResult<<span class="kw-2">&</span>str, <span class="kw-2">&</span>str> {
|
||
delimited(char(<span class="string">'('</span>), is_not(<span class="string">")"</span>), char(<span class="string">')'</span>))(input)
|
||
}</code></pre></div>
|
||
<p>It defines a function named <code>parens</code> which will recognize a sequence of the
|
||
character <code>(</code>, the longest byte array not containing <code>)</code>, then the character
|
||
<code>)</code>, and will return the byte array in the middle.</p>
|
||
<p>Here is another parser, written without using nom’s combinators this time:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{IResult, <span class="prelude-val">Err</span>, Needed};
|
||
|
||
<span class="kw">fn </span>take4(i: <span class="kw-2">&</span>[u8]) -> IResult<<span class="kw-2">&</span>[u8], <span class="kw-2">&</span>[u8]>{
|
||
<span class="kw">if </span>i.len() < <span class="number">4 </span>{
|
||
<span class="prelude-val">Err</span>(Err::Incomplete(Needed::new(<span class="number">4</span>)))
|
||
} <span class="kw">else </span>{
|
||
<span class="prelude-val">Ok</span>((<span class="kw-2">&</span>i[<span class="number">4</span>..], <span class="kw-2">&</span>i[<span class="number">0</span>..<span class="number">4</span>]))
|
||
}
|
||
}</code></pre></div>
|
||
<p>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 functions to help in developing parsers.</p>
|
||
<p>With functions, you would write it like this:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{IResult, bytes::streaming::take};
|
||
<span class="kw">fn </span>take4(input: <span class="kw-2">&</span>str) -> IResult<<span class="kw-2">&</span>str, <span class="kw-2">&</span>str> {
|
||
take(<span class="number">4u8</span>)(input)
|
||
}</code></pre></div>
|
||
<p>A parser in nom is a function which, for an input type <code>I</code>, an output type <code>O</code>
|
||
and an optional error type <code>E</code>, will have the following signature:</p>
|
||
|
||
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>parser(input: I) -> IResult<I, O, E>;</code></pre></div>
|
||
<p>Or like this, if you don’t want to specify a custom error type (it will be <code>(I, ErrorKind)</code> by default):</p>
|
||
|
||
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>parser(input: I) -> IResult<I, O>;</code></pre></div>
|
||
<p><code>IResult</code> is an alias for the <code>Result</code> type:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{Needed, error::Error};
|
||
|
||
<span class="kw">type </span>IResult<I, O, E = Error<I>> = <span class="prelude-ty">Result</span><(I, O), <span class="prelude-val">Err</span><E>>;
|
||
|
||
<span class="kw">enum </span><span class="prelude-val">Err</span><E> {
|
||
Incomplete(Needed),
|
||
Error(E),
|
||
Failure(E),
|
||
}</code></pre></div>
|
||
<p>It can have the following values:</p>
|
||
<ul>
|
||
<li>A correct result <code>Ok((I,O))</code> with the first element being the remaining of the input (not parsed yet), and the second the output value;</li>
|
||
<li>An error <code>Err(Err::Error(c))</code> with <code>c</code> an error that can be built from the input position and a parser specific error</li>
|
||
<li>An error <code>Err(Err::Incomplete(Needed))</code> indicating that more input is necessary. <code>Needed</code> can indicate how much data is needed</li>
|
||
<li>An error <code>Err(Err::Failure(c))</code>. It works like the <code>Error</code> case, except it indicates an unrecoverable error: We cannot backtrack and test another parser</li>
|
||
</ul>
|
||
<p>Please refer to the <a href="https://github.com/Geal/nom/blob/main/doc/choosing_a_combinator.md">“choose a combinator” guide</a> for an exhaustive list of parsers.
|
||
See also the rest of the documentation <a href="https://github.com/Geal/nom/blob/main/doc">here</a>.</p>
|
||
<h3 id="making-new-parsers-with-function-combinators"><a href="#making-new-parsers-with-function-combinators">Making new parsers with function combinators</a></h3>
|
||
<p>nom is based on functions that generate parsers, with a signature like
|
||
this: <code>(arguments) -> impl Fn(Input) -> IResult<Input, Output, Error></code>.
|
||
The arguments of a combinator can be direct values (like <code>take</code> which uses
|
||
a number of bytes or character as argument) or even other parsers (like
|
||
<code>delimited</code> which takes as argument 3 parsers, and returns the result of
|
||
the second one if all are successful).</p>
|
||
<p>Here are some examples:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::IResult;
|
||
<span class="kw">use </span>nom::bytes::complete::{tag, take};
|
||
<span class="kw">fn </span>abcd_parser(i: <span class="kw-2">&</span>str) -> IResult<<span class="kw-2">&</span>str, <span class="kw-2">&</span>str> {
|
||
tag(<span class="string">"abcd"</span>)(i) <span class="comment">// will consume bytes if the input begins with "abcd"
|
||
</span>}
|
||
|
||
<span class="kw">fn </span>take_10(i: <span class="kw-2">&</span>[u8]) -> IResult<<span class="kw-2">&</span>[u8], <span class="kw-2">&</span>[u8]> {
|
||
take(<span class="number">10u8</span>)(i) <span class="comment">// will consume and return 10 bytes of input
|
||
</span>}</code></pre></div>
|
||
<h3 id="combining-parsers"><a href="#combining-parsers">Combining parsers</a></h3>
|
||
<p>There are higher level patterns, like the <strong><code>alt</code></strong> 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:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::IResult;
|
||
<span class="kw">use </span>nom::branch::alt;
|
||
<span class="kw">use </span>nom::bytes::complete::tag;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>alt_tags = alt((tag(<span class="string">"abcd"</span>), tag(<span class="string">"efgh"</span>)));
|
||
|
||
<span class="macro">assert_eq!</span>(alt_tags(<span class="kw-2">&</span><span class="string">b"abcdxxx"</span>[..]), <span class="prelude-val">Ok</span>((<span class="kw-2">&</span><span class="string">b"xxx"</span>[..], <span class="kw-2">&</span><span class="string">b"abcd"</span>[..])));
|
||
<span class="macro">assert_eq!</span>(alt_tags(<span class="kw-2">&</span><span class="string">b"efghxxx"</span>[..]), <span class="prelude-val">Ok</span>((<span class="kw-2">&</span><span class="string">b"xxx"</span>[..], <span class="kw-2">&</span><span class="string">b"efgh"</span>[..])));
|
||
<span class="macro">assert_eq!</span>(alt_tags(<span class="kw-2">&</span><span class="string">b"ijklxxx"</span>[..]), <span class="prelude-val">Err</span>(nom::Err::Error((<span class="kw-2">&</span><span class="string">b"ijklxxx"</span>[..], nom::error::ErrorKind::Tag))));</code></pre></div>
|
||
<p>The <strong><code>opt</code></strong> combinator makes a parser optional. If the child parser returns
|
||
an error, <strong><code>opt</code></strong> will still succeed and return None:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{IResult, combinator::opt, bytes::complete::tag};
|
||
<span class="kw">fn </span>abcd_opt(i: <span class="kw-2">&</span>[u8]) -> IResult<<span class="kw-2">&</span>[u8], <span class="prelude-ty">Option</span><<span class="kw-2">&</span>[u8]>> {
|
||
opt(tag(<span class="string">"abcd"</span>))(i)
|
||
}
|
||
|
||
<span class="macro">assert_eq!</span>(abcd_opt(<span class="kw-2">&</span><span class="string">b"abcdxxx"</span>[..]), <span class="prelude-val">Ok</span>((<span class="kw-2">&</span><span class="string">b"xxx"</span>[..], <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="string">b"abcd"</span>[..]))));
|
||
<span class="macro">assert_eq!</span>(abcd_opt(<span class="kw-2">&</span><span class="string">b"efghxxx"</span>[..]), <span class="prelude-val">Ok</span>((<span class="kw-2">&</span><span class="string">b"efghxxx"</span>[..], <span class="prelude-val">None</span>)));</code></pre></div>
|
||
<p><strong><code>many0</code></strong> applies a parser 0 or more times, and returns a vector of the aggregated results:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{IResult, multi::many0, bytes::complete::tag};
|
||
<span class="kw">use </span>std::str;
|
||
|
||
<span class="kw">fn </span>multi(i: <span class="kw-2">&</span>str) -> IResult<<span class="kw-2">&</span>str, Vec<<span class="kw-2">&</span>str>> {
|
||
many0(tag(<span class="string">"abcd"</span>))(i)
|
||
}
|
||
|
||
<span class="kw">let </span>a = <span class="string">"abcdef"</span>;
|
||
<span class="kw">let </span>b = <span class="string">"abcdabcdef"</span>;
|
||
<span class="kw">let </span>c = <span class="string">"azerty"</span>;
|
||
<span class="macro">assert_eq!</span>(multi(a), <span class="prelude-val">Ok</span>((<span class="string">"ef"</span>, <span class="macro">vec!</span>[<span class="string">"abcd"</span>])));
|
||
<span class="macro">assert_eq!</span>(multi(b), <span class="prelude-val">Ok</span>((<span class="string">"ef"</span>, <span class="macro">vec!</span>[<span class="string">"abcd"</span>, <span class="string">"abcd"</span>])));
|
||
<span class="macro">assert_eq!</span>(multi(c), <span class="prelude-val">Ok</span>((<span class="string">"azerty"</span>, Vec::new())));</code></pre></div>
|
||
<p>Here are some basic combinators available:</p>
|
||
<ul>
|
||
<li><strong><code>opt</code></strong>: Will make the parser optional (if it returns the <code>O</code> type, the new parser returns <code>Option<O></code>)</li>
|
||
<li><strong><code>many0</code></strong>: Will apply the parser 0 or more times (if it returns the <code>O</code> type, the new parser returns <code>Vec<O></code>)</li>
|
||
<li><strong><code>many1</code></strong>: Will apply the parser 1 or more times</li>
|
||
</ul>
|
||
<p>There are more complex (and more useful) parsers like <code>tuple</code>, which is
|
||
used to apply a series of parsers then assemble their results.</p>
|
||
<p>Example with <code>tuple</code>:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{error::ErrorKind, Needed,
|
||
number::streaming::be_u16,
|
||
bytes::streaming::{tag, take},
|
||
sequence::tuple};
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>tpl = tuple((be_u16, take(<span class="number">3u8</span>), tag(<span class="string">"fg"</span>)));
|
||
|
||
<span class="macro">assert_eq!</span>(
|
||
tpl(<span class="kw-2">&</span><span class="string">b"abcdefgh"</span>[..]),
|
||
<span class="prelude-val">Ok</span>((
|
||
<span class="kw-2">&</span><span class="string">b"h"</span>[..],
|
||
(<span class="number">0x6162u16</span>, <span class="kw-2">&</span><span class="string">b"cde"</span>[..], <span class="kw-2">&</span><span class="string">b"fg"</span>[..])
|
||
))
|
||
);
|
||
<span class="macro">assert_eq!</span>(tpl(<span class="kw-2">&</span><span class="string">b"abcde"</span>[..]), <span class="prelude-val">Err</span>(nom::Err::Incomplete(Needed::new(<span class="number">2</span>))));
|
||
<span class="kw">let </span>input = <span class="kw-2">&</span><span class="string">b"abcdejk"</span>[..];
|
||
<span class="macro">assert_eq!</span>(tpl(input), <span class="prelude-val">Err</span>(nom::Err::Error((<span class="kw-2">&</span>input[<span class="number">5</span>..], ErrorKind::Tag))));</code></pre></div>
|
||
<p>But you can also use a sequence of combinators written in imperative style,
|
||
thanks to the <code>?</code> operator:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{IResult, bytes::complete::tag};
|
||
|
||
<span class="attr">#[derive(Debug, PartialEq)]
|
||
</span><span class="kw">struct </span>A {
|
||
a: u8,
|
||
b: u8
|
||
}
|
||
|
||
<span class="kw">fn </span>ret_int1(i:<span class="kw-2">&</span>[u8]) -> IResult<<span class="kw-2">&</span>[u8], u8> { <span class="prelude-val">Ok</span>((i,<span class="number">1</span>)) }
|
||
<span class="kw">fn </span>ret_int2(i:<span class="kw-2">&</span>[u8]) -> IResult<<span class="kw-2">&</span>[u8], u8> { <span class="prelude-val">Ok</span>((i,<span class="number">2</span>)) }
|
||
|
||
<span class="kw">fn </span>f(i: <span class="kw-2">&</span>[u8]) -> IResult<<span class="kw-2">&</span>[u8], A> {
|
||
<span class="comment">// if successful, the parser returns `Ok((remaining_input, output_value))` that we can destructure
|
||
</span><span class="kw">let </span>(i, <span class="kw">_</span>) = tag(<span class="string">"abcd"</span>)(i)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>(i, a) = ret_int1(i)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>(i, <span class="kw">_</span>) = tag(<span class="string">"efgh"</span>)(i)<span class="question-mark">?</span>;
|
||
<span class="kw">let </span>(i, b) = ret_int2(i)<span class="question-mark">?</span>;
|
||
|
||
<span class="prelude-val">Ok</span>((i, A { a, b }))
|
||
}
|
||
|
||
<span class="kw">let </span>r = f(<span class="string">b"abcdefghX"</span>);
|
||
<span class="macro">assert_eq!</span>(r, <span class="prelude-val">Ok</span>((<span class="kw-2">&</span><span class="string">b"X"</span>[..], A{a: <span class="number">1</span>, b: <span class="number">2</span>})));</code></pre></div>
|
||
<h3 id="streaming--complete"><a href="#streaming--complete">Streaming / Complete</a></h3>
|
||
<p>Some of nom’s modules have <code>streaming</code> or <code>complete</code> submodules. They hold
|
||
different variants of the same combinators.</p>
|
||
<p>A streaming parser assumes that we might not have all of the input data.
|
||
This can happen with some network protocol or large file parsers, where the
|
||
input buffer can be full and need to be resized or refilled.</p>
|
||
<p>A complete parser assumes that we already have all of the input data.
|
||
This will be the common case with small files that can be read entirely to
|
||
memory.</p>
|
||
<p>Here is how it works in practice:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>nom::{IResult, <span class="prelude-val">Err</span>, Needed, error::{Error, ErrorKind}, bytes, character};
|
||
|
||
<span class="kw">fn </span>take_streaming(i: <span class="kw-2">&</span>[u8]) -> IResult<<span class="kw-2">&</span>[u8], <span class="kw-2">&</span>[u8]> {
|
||
bytes::streaming::take(<span class="number">4u8</span>)(i)
|
||
}
|
||
|
||
<span class="kw">fn </span>take_complete(i: <span class="kw-2">&</span>[u8]) -> IResult<<span class="kw-2">&</span>[u8], <span class="kw-2">&</span>[u8]> {
|
||
bytes::complete::take(<span class="number">4u8</span>)(i)
|
||
}
|
||
|
||
<span class="comment">// both parsers will take 4 bytes as expected
|
||
</span><span class="macro">assert_eq!</span>(take_streaming(<span class="kw-2">&</span><span class="string">b"abcde"</span>[..]), <span class="prelude-val">Ok</span>((<span class="kw-2">&</span><span class="string">b"e"</span>[..], <span class="kw-2">&</span><span class="string">b"abcd"</span>[..])));
|
||
<span class="macro">assert_eq!</span>(take_complete(<span class="kw-2">&</span><span class="string">b"abcde"</span>[..]), <span class="prelude-val">Ok</span>((<span class="kw-2">&</span><span class="string">b"e"</span>[..], <span class="kw-2">&</span><span class="string">b"abcd"</span>[..])));
|
||
|
||
<span class="comment">// if the input is smaller than 4 bytes, the streaming parser
|
||
// will return `Incomplete` to indicate that we need more data
|
||
</span><span class="macro">assert_eq!</span>(take_streaming(<span class="kw-2">&</span><span class="string">b"abc"</span>[..]), <span class="prelude-val">Err</span>(Err::Incomplete(Needed::new(<span class="number">1</span>))));
|
||
|
||
<span class="comment">// but the complete parser will return an error
|
||
</span><span class="macro">assert_eq!</span>(take_complete(<span class="kw-2">&</span><span class="string">b"abc"</span>[..]), <span class="prelude-val">Err</span>(Err::Error(Error::new(<span class="kw-2">&</span><span class="string">b"abc"</span>[..], ErrorKind::Eof))));
|
||
|
||
<span class="comment">// the alpha0 function recognizes 0 or more alphabetic characters
|
||
</span><span class="kw">fn </span>alpha0_streaming(i: <span class="kw-2">&</span>str) -> IResult<<span class="kw-2">&</span>str, <span class="kw-2">&</span>str> {
|
||
character::streaming::alpha0(i)
|
||
}
|
||
|
||
<span class="kw">fn </span>alpha0_complete(i: <span class="kw-2">&</span>str) -> IResult<<span class="kw-2">&</span>str, <span class="kw-2">&</span>str> {
|
||
character::complete::alpha0(i)
|
||
}
|
||
|
||
<span class="comment">// if there's a clear limit to the recognized characters, both parsers work the same way
|
||
</span><span class="macro">assert_eq!</span>(alpha0_streaming(<span class="string">"abcd;"</span>), <span class="prelude-val">Ok</span>((<span class="string">";"</span>, <span class="string">"abcd"</span>)));
|
||
<span class="macro">assert_eq!</span>(alpha0_complete(<span class="string">"abcd;"</span>), <span class="prelude-val">Ok</span>((<span class="string">";"</span>, <span class="string">"abcd"</span>)));
|
||
|
||
<span class="comment">// but when there's no limit, the streaming version returns `Incomplete`, because it cannot
|
||
// know if more input data should be recognized. The whole input could be "abcd;", or
|
||
// "abcde;"
|
||
</span><span class="macro">assert_eq!</span>(alpha0_streaming(<span class="string">"abcd"</span>), <span class="prelude-val">Err</span>(Err::Incomplete(Needed::new(<span class="number">1</span>))));
|
||
|
||
<span class="comment">// while the complete version knows that all of the data is there
|
||
</span><span class="macro">assert_eq!</span>(alpha0_complete(<span class="string">"abcd"</span>), <span class="prelude-val">Ok</span>((<span class="string">""</span>, <span class="string">"abcd"</span>)));</code></pre></div>
|
||
<p><strong>Going further:</strong> Read the <a href="https://github.com/Geal/nom/tree/main/doc">guides</a>,
|
||
check out the [recipes]!</p>
|
||
</div></details><h2 id="reexports" class="small-section-header"><a href="#reexports">Re-exports</a></h2><ul class="item-table"><li><div class="item-name"><code>pub use self::<a class="mod" href="bits/index.html" title="mod nom::bits">bits</a>::*;</code></div></li></ul><h2 id="modules" class="small-section-header"><a href="#modules">Modules</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="bits/index.html" title="mod nom::bits">bits</a></div><div class="desc docblock-short">Bit level parsers</div></li><li><div class="item-name"><a class="mod" href="branch/index.html" title="mod nom::branch">branch</a></div><div class="desc docblock-short">Choice combinators</div></li><li><div class="item-name"><a class="mod" href="bytes/index.html" title="mod nom::bytes">bytes</a></div><div class="desc docblock-short">Parsers recognizing bytes streams</div></li><li><div class="item-name"><a class="mod" href="character/index.html" title="mod nom::character">character</a></div><div class="desc docblock-short">Character specific parsers and combinators</div></li><li><div class="item-name"><a class="mod" href="combinator/index.html" title="mod nom::combinator">combinator</a></div><div class="desc docblock-short">General purpose combinators</div></li><li><div class="item-name"><a class="mod" href="error/index.html" title="mod nom::error">error</a></div><div class="desc docblock-short">Error management</div></li><li><div class="item-name"><a class="mod" href="lib/index.html" title="mod nom::lib">lib</a></div><div class="desc docblock-short">Lib module to re-export everything needed from <code>std</code> or <code>core</code>/<code>alloc</code>. This is how <code>serde</code> does
|
||
it, albeit there it is not public.</div></li><li><div class="item-name"><a class="mod" href="multi/index.html" title="mod nom::multi">multi</a></div><div class="desc docblock-short">Combinators applying their child parser multiple times</div></li><li><div class="item-name"><a class="mod" href="number/index.html" title="mod nom::number">number</a></div><div class="desc docblock-short">Parsers recognizing numbers</div></li><li><div class="item-name"><a class="mod" href="sequence/index.html" title="mod nom::sequence">sequence</a></div><div class="desc docblock-short">Combinators applying parsers in sequence</div></li></ul><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2><ul class="item-table"><li><div class="item-name"><a class="macro" href="macro.error_node_position.html" title="macro nom::error_node_position">error_node_position</a></div><div class="desc docblock-short">Creates a parse error from a <code>nom::ErrorKind</code>,
|
||
the position in the input and the next error in
|
||
the parsing tree</div></li><li><div class="item-name"><a class="macro" href="macro.error_position.html" title="macro nom::error_position">error_position</a></div><div class="desc docblock-short">Creates a parse error from a <code>nom::ErrorKind</code>
|
||
and the position in the input</div></li></ul><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.And.html" title="struct nom::And">And</a></div><div class="desc docblock-short">Implementation of <code>Parser::and</code></div></li><li><div class="item-name"><a class="struct" href="struct.AndThen.html" title="struct nom::AndThen">AndThen</a></div><div class="desc docblock-short">Implementation of <code>Parser::and_then</code></div></li><li><div class="item-name"><a class="struct" href="struct.FlatMap.html" title="struct nom::FlatMap">FlatMap</a></div><div class="desc docblock-short">Implementation of <code>Parser::flat_map</code></div></li><li><div class="item-name"><a class="struct" href="struct.Into.html" title="struct nom::Into">Into</a></div><div class="desc docblock-short">Implementation of <code>Parser::into</code></div></li><li><div class="item-name"><a class="struct" href="struct.Map.html" title="struct nom::Map">Map</a></div><div class="desc docblock-short">Implementation of <code>Parser::map</code></div></li><li><div class="item-name"><a class="struct" href="struct.Or.html" title="struct nom::Or">Or</a></div><div class="desc docblock-short">Implementation of <code>Parser::or</code></div></li></ul><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2><ul class="item-table"><li><div class="item-name"><a class="enum" href="enum.CompareResult.html" title="enum nom::CompareResult">CompareResult</a></div><div class="desc docblock-short">Indicates whether a comparison was successful, an error, or
|
||
if more data was needed</div></li><li><div class="item-name"><a class="enum" href="enum.Err.html" title="enum nom::Err">Err</a></div><div class="desc docblock-short">The <code>Err</code> enum indicates the parser was not successful</div></li><li><div class="item-name"><a class="enum" href="enum.Needed.html" title="enum nom::Needed">Needed</a></div><div class="desc docblock-short">Contains information on needed data if a parser returned <code>Incomplete</code></div></li></ul><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.AsBytes.html" title="trait nom::AsBytes">AsBytes</a></div><div class="desc docblock-short">Helper trait for types that can be viewed as a byte slice</div></li><li><div class="item-name"><a class="trait" href="trait.AsChar.html" title="trait nom::AsChar">AsChar</a></div><div class="desc docblock-short">Transforms common types to a char for basic token parsing</div></li><li><div class="item-name"><a class="trait" href="trait.Compare.html" title="trait nom::Compare">Compare</a></div><div class="desc docblock-short">Abstracts comparison operations</div></li><li><div class="item-name"><a class="trait" href="trait.ErrorConvert.html" title="trait nom::ErrorConvert">ErrorConvert</a></div><div class="desc docblock-short">Equivalent From implementation to avoid orphan rules in bits parsers</div></li><li><div class="item-name"><a class="trait" href="trait.ExtendInto.html" title="trait nom::ExtendInto">ExtendInto</a></div><div class="desc docblock-short">Abstracts something which can extend an <code>Extend</code>.
|
||
Used to build modified input slices in <code>escaped_transform</code></div></li><li><div class="item-name"><a class="trait" href="trait.FindSubstring.html" title="trait nom::FindSubstring">FindSubstring</a></div><div class="desc docblock-short">Look for a substring in self</div></li><li><div class="item-name"><a class="trait" href="trait.FindToken.html" title="trait nom::FindToken">FindToken</a></div><div class="desc docblock-short">Look for a token in self</div></li><li><div class="item-name"><a class="trait" href="trait.Finish.html" title="trait nom::Finish">Finish</a></div><div class="desc docblock-short">Helper trait to convert a parser’s result to a more manageable type</div></li><li><div class="item-name"><a class="trait" href="trait.HexDisplay.html" title="trait nom::HexDisplay">HexDisplay</a></div><div class="desc docblock-short">Helper trait to show a byte slice as a hex dump</div></li><li><div class="item-name"><a class="trait" href="trait.InputIter.html" title="trait nom::InputIter">InputIter</a></div><div class="desc docblock-short">Abstracts common iteration operations on the input type</div></li><li><div class="item-name"><a class="trait" href="trait.InputLength.html" title="trait nom::InputLength">InputLength</a></div><div class="desc docblock-short">Abstract method to calculate the input length</div></li><li><div class="item-name"><a class="trait" href="trait.InputTake.html" title="trait nom::InputTake">InputTake</a></div><div class="desc docblock-short">Abstracts slicing operations</div></li><li><div class="item-name"><a class="trait" href="trait.InputTakeAtPosition.html" title="trait nom::InputTakeAtPosition">InputTakeAtPosition</a></div><div class="desc docblock-short">Methods to take as much input as possible until the provided function returns true for the current element.</div></li><li><div class="item-name"><a class="trait" href="trait.Offset.html" title="trait nom::Offset">Offset</a></div><div class="desc docblock-short">Useful functions to calculate the offset between slices and show a hexdump of a slice</div></li><li><div class="item-name"><a class="trait" href="trait.ParseTo.html" title="trait nom::ParseTo">ParseTo</a></div><div class="desc docblock-short">Used to integrate <code>str</code>’s <code>parse()</code> method</div></li><li><div class="item-name"><a class="trait" href="trait.Parser.html" title="trait nom::Parser">Parser</a></div><div class="desc docblock-short">All nom parsers implement this trait</div></li><li><div class="item-name"><a class="trait" href="trait.Slice.html" title="trait nom::Slice">Slice</a></div><div class="desc docblock-short">Slicing operations using ranges.</div></li><li><div class="item-name"><a class="trait" href="trait.ToUsize.html" title="trait nom::ToUsize">ToUsize</a></div><div class="desc docblock-short">Helper trait to convert numbers to usize.</div></li><li><div class="item-name"><a class="trait" href="trait.UnspecializedInput.html" title="trait nom::UnspecializedInput">UnspecializedInput</a></div><div class="desc docblock-short">Dummy trait used for default implementations (currently only used for <code>InputTakeAtPosition</code> and <code>Compare</code>).</div></li></ul><h2 id="types" class="small-section-header"><a href="#types">Type Aliases</a></h2><ul class="item-table"><li><div class="item-name"><a class="type" href="type.IResult.html" title="type nom::IResult">IResult</a></div><div class="desc docblock-short">Holds the result of parsing functions</div></li></ul></section></div></main></body></html> |