|
|
<!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 `synom` mod in crate `syn`."><meta name="keywords" content="rust, rustlang, rust-lang, synom"><title>syn::synom - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../syn/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><p class='location'>Module synom</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class='location'><a href='../index.html'>syn</a></p><script>window.sidebarCurrent = {name: 'synom', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><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 class='srclink' href='../../src/syn/synom.rs.html#9-261' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>syn</a>::<wbr><a class="mod" href=''>synom</a></span></h1><div class='docblock'><p>Parsing interface for parsing a token stream into a syntax tree node.</p>
|
|
|
<p>Parsing in Syn is built on parser functions that take in a <a href="../buffer/index.html"><code>Cursor</code></a> and
|
|
|
produce a <a href="type.PResult.html"><code>PResult<T></code></a> where <code>T</code> is some syntax tree node. <code>Cursor</code> is a
|
|
|
cheaply copyable cursor over a range of tokens in a token stream, and
|
|
|
<code>PResult</code> is a result that packages together a parsed syntax tree node <code>T</code>
|
|
|
with a stream of remaining unparsed tokens after <code>T</code> represented as another
|
|
|
<code>Cursor</code>, or a <a href="struct.ParseError.html"><code>ParseError</code></a> if parsing failed.</p>
|
|
|
<p>This <code>Cursor</code>- and <code>PResult</code>-based interface is convenient for parser
|
|
|
combinators and parser implementations, but not necessarily when you just
|
|
|
have some tokens that you want to parse. For that we expose the following
|
|
|
two entry points.</p>
|
|
|
<h2 id="the-synparse-functions" class="section-header"><a href="#the-synparse-functions">The <code>syn::parse*</code> functions</a></h2>
|
|
|
<p>The <a href="../fn.parse.html"><code>syn::parse</code></a>, <a href="../fn.parse2.html"><code>syn::parse2</code></a>, and <a href="../fn.parse_str.html"><code>syn::parse_str</code></a> functions serve
|
|
|
as an entry point for parsing syntax tree nodes that can be parsed in an
|
|
|
obvious default way. These functions can return any syntax tree node that
|
|
|
implements the <a href="trait.Synom.html"><code>Synom</code></a> trait, which includes most types in Syn.</p>
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
<span class="kw">use</span> <span class="ident">syn</span>::<span class="ident">Type</span>;
|
|
|
|
|
|
<span class="kw">let</span> <span class="ident">t</span>: <span class="ident">Type</span> <span class="op">=</span> <span class="ident">syn</span>::<span class="ident">parse_str</span>(<span class="string">"std::collections::HashMap<String, Value>"</span>)<span class="question-mark">?</span>;</pre></div>
|
|
|
<p>The <a href="../macro.parse_quote.html"><code>parse_quote!</code></a> macro also uses this approach.</p>
|
|
|
<h2 id="the-parser-trait" class="section-header"><a href="#the-parser-trait">The <code>Parser</code> trait</a></h2>
|
|
|
<p>Some types can be parsed in several ways depending on context. For example
|
|
|
an <a href="../struct.Attribute.html"><code>Attribute</code></a> can be either "outer" like <code>#[...]</code> or "inner" like
|
|
|
<code>#![...]</code> and parsing the wrong one would be a bug. Similarly <a href="../punctuated/index.html"><code>Punctuated</code></a>
|
|
|
may or may not allow trailing punctuation, and parsing it the wrong way
|
|
|
would either reject valid input or accept invalid input.</p>
|
|
|
<p>The <code>Synom</code> trait is not implemented in these cases because there is no good
|
|
|
behavior to consider the default.</p>
|
|
|
|
|
|
<div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
|
|
|
<span class="comment">// Can't parse `Punctuated` without knowing whether trailing punctuation</span>
|
|
|
<span class="comment">// should be allowed in this context.</span>
|
|
|
<span class="kw">let</span> <span class="ident">path</span>: <span class="ident">Punctuated</span><span class="op"><</span><span class="ident">PathSegment</span>, <span class="macro">Token</span><span class="macro">!</span>[::]<span class="op">></span> <span class="op">=</span> <span class="ident">syn</span>::<span class="ident">parse</span>(<span class="ident">tokens</span>)<span class="question-mark">?</span>;</pre></div>
|
|
|
<p>In these cases the types provide a choice of parser functions rather than a
|
|
|
single <code>Synom</code> implementation, and those parser functions can be invoked
|
|
|
through the <a href="trait.Parser.html"><code>Parser</code></a> trait.</p>
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
<span class="kw">use</span> <span class="ident">syn</span>::<span class="ident">synom</span>::<span class="ident">Parser</span>;
|
|
|
<span class="kw">use</span> <span class="ident">syn</span>::<span class="ident">punctuated</span>::<span class="ident">Punctuated</span>;
|
|
|
<span class="kw">use</span> <span class="ident">syn</span>::{<span class="ident">PathSegment</span>, <span class="ident">Expr</span>, <span class="ident">Attribute</span>};
|
|
|
|
|
|
<span class="comment">// Parse a nonempty sequence of path segments separated by `::` punctuation</span>
|
|
|
<span class="comment">// with no trailing punctuation.</span>
|
|
|
<span class="kw">let</span> <span class="ident">parser</span> <span class="op">=</span> <span class="ident">Punctuated</span>::<span class="op"><</span><span class="ident">PathSegment</span>, <span class="macro">Token</span><span class="macro">!</span>[::]<span class="op">></span>::<span class="ident">parse_separated_nonempty</span>;
|
|
|
<span class="kw">let</span> <span class="ident">path</span> <span class="op">=</span> <span class="ident">parser</span>.<span class="ident">parse</span>(<span class="ident">tokens</span>)<span class="question-mark">?</span>;
|
|
|
|
|
|
<span class="comment">// Parse a possibly empty sequence of expressions terminated by commas with</span>
|
|
|
<span class="comment">// an optional trailing punctuation.</span>
|
|
|
<span class="kw">let</span> <span class="ident">parser</span> <span class="op">=</span> <span class="ident">Punctuated</span>::<span class="op"><</span><span class="ident">Expr</span>, <span class="macro">Token</span><span class="macro">!</span>[,]<span class="op">></span>::<span class="ident">parse_terminated</span>;
|
|
|
<span class="kw">let</span> <span class="ident">args</span> <span class="op">=</span> <span class="ident">parser</span>.<span class="ident">parse</span>(<span class="ident">tokens</span>)<span class="question-mark">?</span>;
|
|
|
|
|
|
<span class="comment">// Parse zero or more outer attributes but not inner attributes.</span>
|
|
|
<span class="macro">named</span><span class="macro">!</span>(<span class="ident">outer_attrs</span> <span class="op">-</span><span class="op">></span> <span class="ident">Vec</span><span class="op"><</span><span class="ident">Attribute</span><span class="op">></span>, <span class="macro">many0</span><span class="macro">!</span>(<span class="ident">Attribute</span>::<span class="ident">parse_outer</span>));
|
|
|
<span class="kw">let</span> <span class="ident">attrs</span> <span class="op">=</span> <span class="ident">outer_attrs</span>.<span class="ident">parse</span>(<span class="ident">tokens</span>)<span class="question-mark">?</span>;</pre></div>
|
|
|
<h1 id="implementing-a-parser-function" class="section-header"><a href="#implementing-a-parser-function">Implementing a parser function</a></h1>
|
|
|
<p>Parser functions are usually implemented using the <a href="https://github.com/Geal/nom"><code>nom</code></a>-style parser
|
|
|
combinator macros provided by Syn, but may also be implemented without
|
|
|
macros be using the low-level <a href="../buffer/index.html"><code>Cursor</code></a> API directly.</p>
|
|
|
<p>The following parser combinator macros are available and a <code>Synom</code> parsing
|
|
|
example is provided for each one.</p>
|
|
|
<ul>
|
|
|
<li><a href="../macro.alt.html"><code>alt!</code></a></li>
|
|
|
<li><a href="../macro.braces.html"><code>braces!</code></a></li>
|
|
|
<li><a href="../macro.brackets.html"><code>brackets!</code></a></li>
|
|
|
<li><a href="../macro.call.html"><code>call!</code></a></li>
|
|
|
<li><a href="../macro.cond.html"><code>cond!</code></a></li>
|
|
|
<li><a href="../macro.cond_reduce.html"><code>cond_reduce!</code></a></li>
|
|
|
<li><a href="../macro.do_parse.html"><code>do_parse!</code></a></li>
|
|
|
<li><a href="../macro.epsilon.html"><code>epsilon!</code></a></li>
|
|
|
<li><a href="../macro.input_end.html"><code>input_end!</code></a></li>
|
|
|
<li><a href="../macro.keyword.html"><code>keyword!</code></a></li>
|
|
|
<li><a href="../macro.many0.html"><code>many0!</code></a></li>
|
|
|
<li><a href="../macro.map.html"><code>map!</code></a></li>
|
|
|
<li><a href="../macro.not.html"><code>not!</code></a></li>
|
|
|
<li><a href="../macro.option.html"><code>option!</code></a></li>
|
|
|
<li><a href="../macro.parens.html"><code>parens!</code></a></li>
|
|
|
<li><a href="../macro.punct.html"><code>punct!</code></a></li>
|
|
|
<li><a href="../macro.reject.html"><code>reject!</code></a></li>
|
|
|
<li><a href="../macro.switch.html"><code>switch!</code></a></li>
|
|
|
<li><a href="../macro.syn.html"><code>syn!</code></a></li>
|
|
|
<li><a href="../macro.tuple.html"><code>tuple!</code></a></li>
|
|
|
<li><a href="../macro.value.html"><code>value!</code></a></li>
|
|
|
</ul>
|
|
|
<p><em>This module is available if Syn is built with the <code>"parsing"</code> feature.</em></p>
|
|
|
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
|
|
<table><tr class='module-item'><td><a class="struct" href="struct.ParseError.html" title='syn::synom::ParseError struct'>ParseError</a></td><td class='docblock-short'><p>Error returned when a <code>Synom</code> parser cannot parse the input tokens.</p>
|
|
|
</td></tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
|
|
|
<table><tr class='module-item'><td><a class="trait" href="trait.Parser.html" title='syn::synom::Parser trait'>Parser</a></td><td class='docblock-short'><p>Parser that can parse Rust tokens into a particular syntax tree node.</p>
|
|
|
</td></tr><tr class='module-item'><td><a class="trait" href="trait.Synom.html" title='syn::synom::Synom trait'>Synom</a></td><td class='docblock-short'><p>Parsing interface implemented by all types that can be parsed in a default
|
|
|
way from a token stream.</p>
|
|
|
</td></tr></table><h2 id='types' class='section-header'><a href="#types">Type Definitions</a></h2>
|
|
|
<table><tr class='module-item'><td><a class="type" href="type.PResult.html" title='syn::synom::PResult type'>PResult</a></td><td class='docblock-short'><p>The result of a <code>Synom</code> parser.</p>
|
|
|
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><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>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "syn";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html> |