Now a REAL lib
This commit is contained in:
3
doc/syn/parse/discouraged/index.html
Normal file
3
doc/syn/parse/discouraged/index.html
Normal file
@ -0,0 +1,3 @@
|
||||
<!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="Extensions to the parsing API with niche applicability."><meta name="keywords" content="rust, rustlang, rust-lang, discouraged"><title>syn::parse::discouraged - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></head><body class="rustdoc mod"><!--[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="sidebar-logo" href="../../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module discouraged</a></h2><div class="sidebar-elems"><section><div class="block"><ul><li><a href="#traits">Traits</a></li></ul></div></section></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../syn/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Module <a href="../../index.html">syn</a>::<wbr><a href="../index.html">parse</a>::<wbr><a class="mod" href="#">discouraged</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../../../src/syn/discouraged.rs.html#1-194">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Extensions to the parsing API with niche applicability.</p>
|
||||
</div></details><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Speculative.html" title="syn::parse::discouraged::Speculative trait">Speculative</a></div><div class="item-right docblock-short"><p>Extensions to the <code>ParseStream</code> API to support speculative parsing.</p>
|
||||
</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="syn" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
1
doc/syn/parse/discouraged/sidebar-items.js
Normal file
1
doc/syn/parse/discouraged/sidebar-items.js
Normal file
@ -0,0 +1 @@
|
||||
window.SIDEBAR_ITEMS = {"trait":[["Speculative","Extensions to the `ParseStream` API to support speculative parsing."]]};
|
106
doc/syn/parse/discouraged/trait.Speculative.html
Normal file
106
doc/syn/parse/discouraged/trait.Speculative.html
Normal file
@ -0,0 +1,106 @@
|
||||
<!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="Extensions to the `ParseStream` API to support speculative parsing."><meta name="keywords" content="rust, rustlang, rust-lang, Speculative"><title>Speculative in syn::parse::discouraged - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><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="../../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg"></head><body class="rustdoc trait"><!--[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="sidebar-logo" href="../../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Speculative</a></h2><div class="sidebar-elems"><section><div class="block"><h3 class="sidebar-title"><a href="#required-methods">Required Methods</a></h3><ul><li><a href="#tymethod.advance_to">advance_to</a></li></ul></div><div class="block"><h3 class="sidebar-title"><a href="#implementors">Implementors</a></h3></div></section><h2 class="location"><a href="index.html">In syn::parse::discouraged</a></h2></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../../syn/index.html"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Trait <a href="../../index.html">syn</a>::<wbr><a href="../index.html">parse</a>::<wbr><a href="index.html">discouraged</a>::<wbr><a class="trait" href="#">Speculative</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../../../src/syn/discouraged.rs.html#6-158">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="docblock item-decl"><pre class="rust trait"><code>pub trait Speculative {
|
||||
fn <a href="#tymethod.advance_to" class="fnname">advance_to</a>(&self, fork: <a class="primitive" href="https://doc.rust-lang.org/1.64.0/std/primitive.reference.html">&</a>Self);
|
||||
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Extensions to the <code>ParseStream</code> API to support speculative parsing.</p>
|
||||
</div></details><h2 id="required-methods" class="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle" open><summary><div id="tymethod.advance_to" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../../src/syn/discouraged.rs.html#157">source</a></div><h4 class="code-header">fn <a href="#tymethod.advance_to" class="fnname">advance_to</a>(&self, fork: <a class="primitive" href="https://doc.rust-lang.org/1.64.0/std/primitive.reference.html">&</a>Self)</h4></div></summary><div class="docblock"><p>Advance this parse stream to the position of a forked parse stream.</p>
|
||||
<p>This is the opposite operation to <a href="../struct.ParseBuffer.html#method.fork"><code>ParseStream::fork</code></a>. You can fork a
|
||||
parse stream, perform some speculative parsing, then join the original
|
||||
stream to the fork to “commit” the parsing from the fork to the main
|
||||
stream.</p>
|
||||
<p>If you can avoid doing this, you should, as it limits the ability to
|
||||
generate useful errors. That said, it is often the only way to parse
|
||||
syntax of the form <code>A* B*</code> for arbitrary syntax <code>A</code> and <code>B</code>. The problem
|
||||
is that when the fork fails to parse an <code>A</code>, it’s impossible to tell
|
||||
whether that was because of a syntax error and the user meant to provide
|
||||
an <code>A</code>, or that the <code>A</code>s are finished and it’s time to start parsing
|
||||
<code>B</code>s. Use with care.</p>
|
||||
<p>Also note that if <code>A</code> is a subset of <code>B</code>, <code>A* B*</code> can be parsed by
|
||||
parsing <code>B*</code> and removing the leading members of <code>A</code> from the
|
||||
repetition, bypassing the need to involve the downsides associated with
|
||||
speculative parsing.</p>
|
||||
<h5 id="example"><a href="#example">Example</a></h5>
|
||||
<p>There has been chatter about the possibility of making the colons in the
|
||||
turbofish syntax like <code>path::to::<T></code> no longer required by accepting
|
||||
<code>path::to<T></code> in expression position. Specifically, according to <a href="https://github.com/rust-lang/rfcs/pull/2544">RFC
|
||||
2544</a>, <a href="../../struct.PathSegment.html"><code>PathSegment</code></a> parsing should always try to consume a following
|
||||
<code><</code> token as the start of generic arguments, and reset to the <code><</code> if
|
||||
that fails (e.g. the token is acting as a less-than operator).</p>
|
||||
<p>This is the exact kind of parsing behavior which requires the “fork,
|
||||
try, commit” behavior that <a href="../struct.ParseBuffer.html#method.fork"><code>ParseStream::fork</code></a> discourages. With
|
||||
<code>advance_to</code>, we can avoid having to parse the speculatively parsed
|
||||
content a second time.</p>
|
||||
<p>This change in behavior can be implemented in syn by replacing just the
|
||||
<code>Parse</code> implementation for <code>PathSegment</code>:</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">syn::parse::discouraged::Speculative</span>;
|
||||
|
||||
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">PathSegment</span> {
|
||||
<span class="kw">pub</span> <span class="ident">ident</span>: <span class="ident">Ident</span>,
|
||||
<span class="kw">pub</span> <span class="ident">arguments</span>: <span class="ident">PathArguments</span>,
|
||||
}
|
||||
|
||||
<span class="kw">impl</span> <span class="ident">Parse</span> <span class="kw">for</span> <span class="ident">PathSegment</span> {
|
||||
<span class="kw">fn</span> <span class="ident">parse</span>(<span class="ident">input</span>: <span class="ident">ParseStream</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="self">Self</span><span class="op">></span> {
|
||||
<span class="kw">if</span> <span class="ident">input</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[<span class="kw">super</span>])
|
||||
<span class="op">|</span><span class="op">|</span> <span class="ident">input</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[<span class="self">self</span>])
|
||||
<span class="op">|</span><span class="op">|</span> <span class="ident">input</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[<span class="self">Self</span>])
|
||||
<span class="op">|</span><span class="op">|</span> <span class="ident">input</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[<span class="kw">crate</span>])
|
||||
{
|
||||
<span class="kw">let</span> <span class="ident">ident</span> <span class="op">=</span> <span class="ident">input</span>.<span class="ident">call</span>(<span class="ident">Ident::parse_any</span>)<span class="question-mark">?</span>;
|
||||
<span class="kw">return</span> <span class="prelude-val">Ok</span>(<span class="ident">PathSegment::from</span>(<span class="ident">ident</span>));
|
||||
}
|
||||
|
||||
<span class="kw">let</span> <span class="ident">ident</span> <span class="op">=</span> <span class="ident">input</span>.<span class="ident">parse</span>()<span class="question-mark">?</span>;
|
||||
<span class="kw">if</span> <span class="ident">input</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[::]) <span class="op">&&</span> <span class="ident">input</span>.<span class="ident">peek3</span>(<span class="macro">Token!</span>[<span class="op"><</span>]) {
|
||||
<span class="kw">return</span> <span class="prelude-val">Ok</span>(<span class="ident">PathSegment</span> {
|
||||
<span class="ident">ident</span>,
|
||||
<span class="ident">arguments</span>: <span class="ident">PathArguments::AngleBracketed</span>(<span class="ident">input</span>.<span class="ident">parse</span>()<span class="question-mark">?</span>),
|
||||
});
|
||||
}
|
||||
<span class="kw">if</span> <span class="ident">input</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[<span class="op"><</span>]) <span class="op">&&</span> <span class="op">!</span><span class="ident">input</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[<span class="op"><</span><span class="op">=</span>]) {
|
||||
<span class="kw">let</span> <span class="ident">fork</span> <span class="op">=</span> <span class="ident">input</span>.<span class="ident">fork</span>();
|
||||
<span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Ok</span>(<span class="ident">arguments</span>) <span class="op">=</span> <span class="ident">fork</span>.<span class="ident">parse</span>() {
|
||||
<span class="ident">input</span>.<span class="ident">advance_to</span>(<span class="kw-2">&</span><span class="ident">fork</span>);
|
||||
<span class="kw">return</span> <span class="prelude-val">Ok</span>(<span class="ident">PathSegment</span> {
|
||||
<span class="ident">ident</span>,
|
||||
<span class="ident">arguments</span>: <span class="ident">PathArguments::AngleBracketed</span>(<span class="ident">arguments</span>),
|
||||
});
|
||||
}
|
||||
}
|
||||
<span class="prelude-val">Ok</span>(<span class="ident">PathSegment::from</span>(<span class="ident">ident</span>))
|
||||
}
|
||||
}
|
||||
</code></pre></div>
|
||||
<h5 id="drawbacks"><a href="#drawbacks">Drawbacks</a></h5>
|
||||
<p>The main drawback of this style of speculative parsing is in error
|
||||
presentation. Even if the lookahead is the “correct” parse, the error
|
||||
that is shown is that of the “fallback” parse. To use the same example
|
||||
as the turbofish above, take the following unfinished “turbofish”:</p>
|
||||
<div class="example-wrap"><pre class="language-text"><code>let _ = f<&'a fn(), for<'a> serde::>();</code></pre></div>
|
||||
<p>If this is parsed as generic arguments, we can provide the error message</p>
|
||||
<div class="example-wrap"><pre class="language-text"><code>error: expected identifier
|
||||
--> src.rs:L:C
|
||||
|
|
||||
L | let _ = f<&'a fn(), for<'a> serde::>();
|
||||
| ^</code></pre></div>
|
||||
<p>but if parsed using the above speculative parsing, it falls back to
|
||||
assuming that the <code><</code> is a less-than when it fails to parse the generic
|
||||
arguments, and tries to interpret the <code>&'a</code> as the start of a labelled
|
||||
loop, resulting in the much less helpful error</p>
|
||||
<div class="example-wrap"><pre class="language-text"><code>error: expected `:`
|
||||
--> src.rs:L:C
|
||||
|
|
||||
L | let _ = f<&'a fn(), for<'a> serde::>();
|
||||
| ^^</code></pre></div>
|
||||
<p>This can be mitigated with various heuristics (two examples: show both
|
||||
forks’ parse errors, or show the one that consumed more tokens), but
|
||||
when you can control the grammar, sticking to something that can be
|
||||
parsed LL(3) and without the LL(*) speculative parsing this makes
|
||||
possible, displaying reasonable errors becomes much more simple.</p>
|
||||
<h5 id="performance"><a href="#performance">Performance</a></h5>
|
||||
<p>This method performs a cheap fixed amount of work that does not depend
|
||||
on how far apart the two streams are positioned.</p>
|
||||
<h5 id="panics"><a href="#panics">Panics</a></h5>
|
||||
<p>The forked stream in the argument of <code>advance_to</code> must have been
|
||||
obtained by forking <code>self</code>. Attempting to advance to any other stream
|
||||
will cause a panic.</p>
|
||||
</div></details></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div class="item-list" id="implementors-list"><section id="impl-Speculative-for-ParseBuffer%3C%27a%3E" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../../src/syn/discouraged.rs.html#160-194">source</a></span><a href="#impl-Speculative-for-ParseBuffer%3C%27a%3E" class="anchor"></a><h3 class="code-header in-band">impl<'a> <a class="trait" href="trait.Speculative.html" title="trait syn::parse::discouraged::Speculative">Speculative</a> for <a class="struct" href="../struct.ParseBuffer.html" title="struct syn::parse::ParseBuffer">ParseBuffer</a><'a></h3></section></div><script type="text/javascript" src="../../../implementors/syn/parse/discouraged/trait.Speculative.js" data-ignore-extern-crates="" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="syn" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
131
doc/syn/parse/index.html
Normal file
131
doc/syn/parse/index.html
Normal file
@ -0,0 +1,131 @@
|
||||
<!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="Parsing interface for parsing a token stream into a syntax tree node."><meta name="keywords" content="rust, rustlang, rust-lang, parse"><title>syn::parse - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc mod"><!--[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="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module parse</a></h2><div class="sidebar-elems"><section><div class="block"><ul><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#types">Type Definitions</a></li></ul></div></section></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../syn/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Module <a href="../index.html">syn</a>::<wbr><a class="mod" href="#">parse</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../../src/syn/parse.rs.html#1-1314">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><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="type.ParseStream.html" title="ParseStream"><code>ParseStream</code></a>
|
||||
and produce a <a href="type.Result.html"><code>Result<T></code></a> where <code>T</code> is some syntax tree node. Underlying
|
||||
these parser functions is a lower level mechanism built around the
|
||||
<a href="../buffer/struct.Cursor.html"><code>Cursor</code></a> type. <code>Cursor</code> is a cheaply copyable cursor over a range of
|
||||
tokens in a token stream.</p>
|
||||
<h2 id="example"><a href="#example">Example</a></h2>
|
||||
<p>Here is a snippet of parsing code to get a feel for the style of the
|
||||
library. We define data structures for a subset of Rust syntax including
|
||||
enums (not shown) and structs, then provide implementations of the <a href="trait.Parse.html" title="Parse"><code>Parse</code></a>
|
||||
trait to parse these syntax tree data structures from a token stream.</p>
|
||||
<p>Once <code>Parse</code> impls have been defined, they can be called conveniently from a
|
||||
procedural macro through <a href="../macro.parse_macro_input.html"><code>parse_macro_input!</code></a> as shown at the bottom of
|
||||
the snippet. If the caller provides syntactically invalid input to the
|
||||
procedural macro, they will receive a helpful compiler error message
|
||||
pointing out the exact token that triggered the failure to parse.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">proc_macro::TokenStream</span>;
|
||||
<span class="kw">use</span> <span class="ident">syn</span>::{<span class="ident">braced</span>, <span class="ident">parse_macro_input</span>, <span class="ident">token</span>, <span class="ident">Field</span>, <span class="ident">Ident</span>, <span class="prelude-ty">Result</span>, <span class="ident">Token</span>};
|
||||
<span class="kw">use</span> <span class="ident">syn::parse</span>::{<span class="ident">Parse</span>, <span class="ident">ParseStream</span>};
|
||||
<span class="kw">use</span> <span class="ident">syn::punctuated::Punctuated</span>;
|
||||
|
||||
<span class="kw">enum</span> <span class="ident">Item</span> {
|
||||
<span class="ident">Struct</span>(<span class="ident">ItemStruct</span>),
|
||||
<span class="ident">Enum</span>(<span class="ident">ItemEnum</span>),
|
||||
}
|
||||
|
||||
<span class="kw">struct</span> <span class="ident">ItemStruct</span> {
|
||||
<span class="ident">struct_token</span>: <span class="macro">Token!</span>[<span class="kw">struct</span>],
|
||||
<span class="ident">ident</span>: <span class="ident">Ident</span>,
|
||||
<span class="ident">brace_token</span>: <span class="ident">token::Brace</span>,
|
||||
<span class="ident">fields</span>: <span class="ident">Punctuated</span><span class="op"><</span><span class="ident">Field</span>, <span class="macro">Token!</span>[,]<span class="op">></span>,
|
||||
}
|
||||
|
||||
<span class="kw">impl</span> <span class="ident">Parse</span> <span class="kw">for</span> <span class="ident">Item</span> {
|
||||
<span class="kw">fn</span> <span class="ident">parse</span>(<span class="ident">input</span>: <span class="ident">ParseStream</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="self">Self</span><span class="op">></span> {
|
||||
<span class="kw">let</span> <span class="ident">lookahead</span> <span class="op">=</span> <span class="ident">input</span>.<span class="ident">lookahead1</span>();
|
||||
<span class="kw">if</span> <span class="ident">lookahead</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[<span class="kw">struct</span>]) {
|
||||
<span class="ident">input</span>.<span class="ident">parse</span>().<span class="ident">map</span>(<span class="ident">Item::Struct</span>)
|
||||
} <span class="kw">else</span> <span class="kw">if</span> <span class="ident">lookahead</span>.<span class="ident">peek</span>(<span class="macro">Token!</span>[<span class="kw">enum</span>]) {
|
||||
<span class="ident">input</span>.<span class="ident">parse</span>().<span class="ident">map</span>(<span class="ident">Item::Enum</span>)
|
||||
} <span class="kw">else</span> {
|
||||
<span class="prelude-val">Err</span>(<span class="ident">lookahead</span>.<span class="ident">error</span>())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<span class="kw">impl</span> <span class="ident">Parse</span> <span class="kw">for</span> <span class="ident">ItemStruct</span> {
|
||||
<span class="kw">fn</span> <span class="ident">parse</span>(<span class="ident">input</span>: <span class="ident">ParseStream</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="self">Self</span><span class="op">></span> {
|
||||
<span class="kw">let</span> <span class="ident">content</span>;
|
||||
<span class="prelude-val">Ok</span>(<span class="ident">ItemStruct</span> {
|
||||
<span class="ident">struct_token</span>: <span class="ident">input</span>.<span class="ident">parse</span>()<span class="question-mark">?</span>,
|
||||
<span class="ident">ident</span>: <span class="ident">input</span>.<span class="ident">parse</span>()<span class="question-mark">?</span>,
|
||||
<span class="ident">brace_token</span>: <span class="macro">braced!</span>(<span class="ident">content</span> <span class="kw">in</span> <span class="ident">input</span>),
|
||||
<span class="ident">fields</span>: <span class="ident">content</span>.<span class="ident">parse_terminated</span>(<span class="ident">Field::parse_named</span>)<span class="question-mark">?</span>,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
<span class="attribute">#[<span class="ident">proc_macro</span>]</span>
|
||||
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">my_macro</span>(<span class="ident">tokens</span>: <span class="ident">TokenStream</span>) -> <span class="ident">TokenStream</span> {
|
||||
<span class="kw">let</span> <span class="ident">input</span> <span class="op">=</span> <span class="macro">parse_macro_input!</span>(<span class="ident">tokens</span> <span class="kw">as</span> <span class="ident">Item</span>);
|
||||
|
||||
<span class="comment">/* ... */</span>
|
||||
}</code></pre></div>
|
||||
<h2 id="the-synparse-functions"><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.Parse.html" title="Parse"><code>Parse</code></a> trait, which includes most types in Syn.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">syn::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::parse_str</span>(<span class="string">"std::collections::HashMap<String, Value>"</span>)<span class="question-mark">?</span>;</code></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"><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>Parse</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 compile_fail'>ⓘ</div></div><div class="example-wrap"><pre class="rust rust-example-rendered compile_fail"><code><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="op">></span> <span class="op">=</span> <span class="ident">syn::parse</span>(<span class="ident">tokens</span>)<span class="question-mark">?</span>;</code></pre></div>
|
||||
<p>In these cases the types provide a choice of parser functions rather than a
|
||||
single <code>Parse</code> implementation, and those parser functions can be invoked
|
||||
through the <a href="trait.Parser.html" title="Parser"><code>Parser</code></a> trait.</p>
|
||||
|
||||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">proc_macro::TokenStream</span>;
|
||||
<span class="kw">use</span> <span class="ident">syn::parse::Parser</span>;
|
||||
<span class="kw">use</span> <span class="ident">syn::punctuated::Punctuated</span>;
|
||||
<span class="kw">use</span> <span class="ident">syn</span>::{<span class="ident">Attribute</span>, <span class="ident">Expr</span>, <span class="ident">PathSegment</span>, <span class="prelude-ty">Result</span>, <span class="ident">Token</span>};
|
||||
|
||||
<span class="kw">fn</span> <span class="ident">call_some_parser_methods</span>(<span class="ident">input</span>: <span class="ident">TokenStream</span>) -> <span class="prelude-ty">Result</span><span class="op"><</span>()<span class="op">></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">tokens</span> <span class="op">=</span> <span class="ident">input</span>.<span class="ident">clone</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="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">tokens</span> <span class="op">=</span> <span class="ident">input</span>.<span class="ident">clone</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="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="kw">let</span> <span class="ident">tokens</span> <span class="op">=</span> <span class="ident">input</span>.<span class="ident">clone</span>();
|
||||
<span class="kw">let</span> <span class="ident">parser</span> <span class="op">=</span> <span class="ident">Attribute::parse_outer</span>;
|
||||
<span class="kw">let</span> <span class="ident">_attrs</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="prelude-val">Ok</span>(())
|
||||
}</code></pre></div>
|
||||
<hr />
|
||||
<p><em>This module is available only if Syn is built with the <code>"parsing"</code> feature.</em></p>
|
||||
</div></details><h2 id="modules" class="small-section-header"><a href="#modules">Modules</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="mod" href="discouraged/index.html" title="syn::parse::discouraged mod">discouraged</a></div><div class="item-right docblock-short"><p>Extensions to the parsing API with niche applicability.</p>
|
||||
</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Error.html" title="syn::parse::Error struct">Error</a></div><div class="item-right docblock-short"><p>Error returned when a Syn parser cannot parse the input tokens.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Lookahead1.html" title="syn::parse::Lookahead1 struct">Lookahead1</a></div><div class="item-right docblock-short"><p>Support for checking the next token in a stream to decide how to parse.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Nothing.html" title="syn::parse::Nothing struct">Nothing</a></div><div class="item-right docblock-short"><p>An empty syntax tree node that consumes no tokens when parsed.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ParseBuffer.html" title="syn::parse::ParseBuffer struct">ParseBuffer</a></div><div class="item-right docblock-short"><p>Cursor position within a buffered token stream.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.StepCursor.html" title="syn::parse::StepCursor struct">StepCursor</a></div><div class="item-right docblock-short"><p>Cursor state associated with speculative parsing.</p>
|
||||
</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Parse.html" title="syn::parse::Parse trait">Parse</a></div><div class="item-right docblock-short"><p>Parsing interface implemented by all types that can be parsed in a default
|
||||
way from a token stream.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Parser.html" title="syn::parse::Parser trait">Parser</a></div><div class="item-right docblock-short"><p>Parser that can parse Rust tokens into a particular syntax tree node.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Peek.html" title="syn::parse::Peek trait">Peek</a></div><div class="item-right docblock-short"><p>Types that can be parsed by looking at just one token.</p>
|
||||
</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.ParseStream.html" title="syn::parse::ParseStream type">ParseStream</a></div><div class="item-right docblock-short"><p>Input to a Syn parser function.</p>
|
||||
</div></div><div class="item-row"><div class="item-left module-item"><a class="type" href="type.Result.html" title="syn::parse::Result type">Result</a></div><div class="item-right docblock-short"><p>The result of a Syn parser.</p>
|
||||
</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="syn" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
1
doc/syn/parse/sidebar-items.js
Normal file
1
doc/syn/parse/sidebar-items.js
Normal file
@ -0,0 +1 @@
|
||||
window.SIDEBAR_ITEMS = {"mod":[["discouraged","Extensions to the parsing API with niche applicability."]],"struct":[["Error","Error returned when a Syn parser cannot parse the input tokens."],["Lookahead1","Support for checking the next token in a stream to decide how to parse."],["Nothing","An empty syntax tree node that consumes no tokens when parsed."],["ParseBuffer","Cursor position within a buffered token stream."],["StepCursor","Cursor state associated with speculative parsing."]],"trait":[["Parse","Parsing interface implemented by all types that can be parsed in a default way from a token stream."],["Parser","Parser that can parse Rust tokens into a particular syntax tree node."],["Peek","Types that can be parsed by looking at just one token."]],"type":[["ParseStream","Input to a Syn parser function."],["Result","The result of a Syn parser."]]};
|
130
doc/syn/parse/struct.Error.html
Normal file
130
doc/syn/parse/struct.Error.html
Normal file
File diff suppressed because one or more lines are too long
71
doc/syn/parse/struct.Lookahead1.html
Normal file
71
doc/syn/parse/struct.Lookahead1.html
Normal file
File diff suppressed because one or more lines are too long
30
doc/syn/parse/struct.Nothing.html
Normal file
30
doc/syn/parse/struct.Nothing.html
Normal file
File diff suppressed because one or more lines are too long
414
doc/syn/parse/struct.ParseBuffer.html
Normal file
414
doc/syn/parse/struct.ParseBuffer.html
Normal file
File diff suppressed because one or more lines are too long
46
doc/syn/parse/struct.StepCursor.html
Normal file
46
doc/syn/parse/struct.StepCursor.html
Normal file
File diff suppressed because one or more lines are too long
7
doc/syn/parse/trait.Parse.html
Normal file
7
doc/syn/parse/trait.Parse.html
Normal file
File diff suppressed because one or more lines are too long
25
doc/syn/parse/trait.Parser.html
Normal file
25
doc/syn/parse/trait.Parser.html
Normal file
@ -0,0 +1,25 @@
|
||||
<!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="Parser that can parse Rust tokens into a particular syntax tree node."><meta name="keywords" content="rust, rustlang, rust-lang, Parser"><title>Parser in syn::parse - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc trait"><!--[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="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Parser</a></h2><div class="sidebar-elems"><section><div class="block"><h3 class="sidebar-title"><a href="#required-associated-types">Required Associated Types</a></h3><ul><li><a href="#associatedtype.Output">Output</a></li></ul></div><div class="block"><h3 class="sidebar-title"><a href="#required-methods">Required Methods</a></h3><ul><li><a href="#tymethod.parse2">parse2</a></li></ul></div><div class="block"><h3 class="sidebar-title"><a href="#provided-methods">Provided Methods</a></h3><ul><li><a href="#method.parse">parse</a></li><li><a href="#method.parse_str">parse_str</a></li></ul></div><div class="block"><h3 class="sidebar-title"><a href="#implementors">Implementors</a></h3></div></section><h2 class="location"><a href="index.html">In syn::parse</a></h2></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../syn/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Trait <a href="../index.html">syn</a>::<wbr><a href="index.html">parse</a>::<wbr><a class="trait" href="#">Parser</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../../src/syn/parse.rs.html#1143-1194">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="docblock item-decl"><pre class="rust trait"><code>pub trait Parser: <a class="trait" href="https://doc.rust-lang.org/1.64.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
|
||||
type <a href="#associatedtype.Output" class="associatedtype">Output</a>;
|
||||
|
||||
fn <a href="#tymethod.parse2" class="fnname">parse2</a>(self, tokens: <a class="struct" href="../../proc_macro2/struct.TokenStream.html" title="struct proc_macro2::TokenStream">TokenStream</a>) -> <a class="type" href="type.Result.html" title="type syn::parse::Result">Result</a><Self::<a class="associatedtype" href="trait.Parser.html#associatedtype.Output" title="type syn::parse::Parser::Output">Output</a>>;
|
||||
|
||||
fn <a href="#method.parse" class="fnname">parse</a>(self, tokens: <a class="struct" href="https://doc.rust-lang.org/1.64.0/proc_macro/struct.TokenStream.html" title="struct proc_macro::TokenStream">TokenStream</a>) -> <a class="type" href="type.Result.html" title="type syn::parse::Result">Result</a><Self::<a class="associatedtype" href="trait.Parser.html#associatedtype.Output" title="type syn::parse::Parser::Output">Output</a>> { ... }
|
||||
<span class="item-spacer"></span> fn <a href="#method.parse_str" class="fnname">parse_str</a>(self, s: &<a class="primitive" href="https://doc.rust-lang.org/1.64.0/std/primitive.str.html">str</a>) -> <a class="type" href="type.Result.html" title="type syn::parse::Result">Result</a><Self::<a class="associatedtype" href="trait.Parser.html#associatedtype.Output" title="type syn::parse::Parser::Output">Output</a>> { ... }
|
||||
}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Parser that can parse Rust tokens into a particular syntax tree node.</p>
|
||||
<p>Refer to the <a href="index.html">module documentation</a> for details about parsing in Syn.</p>
|
||||
<p><em>This trait is available only if Syn is built with the <code>"parsing"</code> feature.</em></p>
|
||||
</div></details><h2 id="required-associated-types" class="small-section-header">Required Associated Types<a href="#required-associated-types" class="anchor"></a></h2><div class="methods"><div id="associatedtype.Output" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/syn/parse.rs.html#1144">source</a></div><h4 class="code-header">type <a href="#associatedtype.Output" class="associatedtype">Output</a></h4></div></div><h2 id="required-methods" class="small-section-header">Required Methods<a href="#required-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle" open><summary><div id="tymethod.parse2" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/syn/parse.rs.html#1150">source</a></div><h4 class="code-header">fn <a href="#tymethod.parse2" class="fnname">parse2</a>(self, tokens: <a class="struct" href="../../proc_macro2/struct.TokenStream.html" title="struct proc_macro2::TokenStream">TokenStream</a>) -> <a class="type" href="type.Result.html" title="type syn::parse::Result">Result</a><Self::<a class="associatedtype" href="trait.Parser.html#associatedtype.Output" title="type syn::parse::Parser::Output">Output</a>></h4></div></summary><div class="docblock"><p>Parse a proc-macro2 token stream into the chosen syntax tree node.</p>
|
||||
<p>This function will check that the input is fully parsed. If there are
|
||||
any unparsed tokens at the end of the stream, an error is returned.</p>
|
||||
</div></details></div><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle" open><summary><div id="method.parse" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/syn/parse.rs.html#1163-1165">source</a></div><h4 class="code-header">fn <a href="#method.parse" class="fnname">parse</a>(self, tokens: <a class="struct" href="https://doc.rust-lang.org/1.64.0/proc_macro/struct.TokenStream.html" title="struct proc_macro::TokenStream">TokenStream</a>) -> <a class="type" href="type.Result.html" title="type syn::parse::Result">Result</a><Self::<a class="associatedtype" href="trait.Parser.html#associatedtype.Output" title="type syn::parse::Parser::Output">Output</a>></h4></div></summary><div class="docblock"><p>Parse tokens of source code into the chosen syntax tree node.</p>
|
||||
<p>This function will check that the input is fully parsed. If there are
|
||||
any unparsed tokens at the end of the stream, an error is returned.</p>
|
||||
<p><em>This method is available only if Syn is built with both the <code>"parsing"</code> and
|
||||
<code>"proc-macro"</code> features.</em></p>
|
||||
</div></details><details class="rustdoc-toggle" open><summary><div id="method.parse_str" class="method has-srclink"><div class="rightside"><a class="srclink" href="../../src/syn/parse.rs.html#1176-1178">source</a></div><h4 class="code-header">fn <a href="#method.parse_str" class="fnname">parse_str</a>(self, s: &<a class="primitive" href="https://doc.rust-lang.org/1.64.0/std/primitive.str.html">str</a>) -> <a class="type" href="type.Result.html" title="type syn::parse::Result">Result</a><Self::<a class="associatedtype" href="trait.Parser.html#associatedtype.Output" title="type syn::parse::Parser::Output">Output</a>></h4></div></summary><div class="docblock"><p>Parse a string of Rust code into the chosen syntax tree node.</p>
|
||||
<p>This function will check that the input is fully parsed. If there are
|
||||
any unparsed tokens at the end of the string, an error is returned.</p>
|
||||
<h5 id="hygiene"><a href="#hygiene">Hygiene</a></h5>
|
||||
<p>Every span in the resulting syntax tree will be set to resolve at the
|
||||
macro call site.</p>
|
||||
</div></details></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div class="item-list" id="implementors-list"><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Parser-for-F" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/syn/parse.rs.html#1203-1240">source</a></span><a href="#impl-Parser-for-F" class="anchor"></a><h3 class="code-header in-band">impl<F, T> <a class="trait" href="trait.Parser.html" title="trait syn::parse::Parser">Parser</a> for F <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/1.64.0/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(<a class="type" href="type.ParseStream.html" title="type syn::parse::ParseStream">ParseStream</a><'_>) -> <a class="type" href="type.Result.html" title="type syn::parse::Result">Result</a><T>, </span></h3></section></summary><div class="impl-items"><section id="associatedtype.Output-1" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Output-1" class="anchor"></a><h4 class="code-header">type <a href="#associatedtype.Output" class="associatedtype">Output</a> = T</h4></section></div></details></div><script type="text/javascript" src="../../implementors/syn/parse/trait.Parser.js" data-ignore-extern-crates="" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="syn" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
5
doc/syn/parse/trait.Peek.html
Normal file
5
doc/syn/parse/trait.Peek.html
Normal file
@ -0,0 +1,5 @@
|
||||
<!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="Types that can be parsed by looking at just one token."><meta name="keywords" content="rust, rustlang, rust-lang, Peek"><title>Peek in syn::parse - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc trait"><!--[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="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Peek</a></h2><div class="sidebar-elems"><section><div class="block"><h3 class="sidebar-title"><a href="#implementors">Implementors</a></h3></div></section><h2 class="location"><a href="index.html">In syn::parse</a></h2></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../syn/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Trait <a href="../index.html">syn</a>::<wbr><a href="index.html">parse</a>::<wbr><a class="trait" href="#">Peek</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../../src/syn/lookahead.rs.html#147-151">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="docblock item-decl"><pre class="rust trait"><code>pub trait Peek: Sealed { }</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Types that can be parsed by looking at just one token.</p>
|
||||
<p>Use <a href="struct.ParseBuffer.html#method.peek"><code>ParseStream::peek</code></a> to peek one of these types in a parse stream
|
||||
without consuming it from the stream.</p>
|
||||
<p>This trait is sealed and cannot be implemented for types outside of Syn.</p>
|
||||
</div></details><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div class="item-list" id="implementors-list"><section id="impl-Peek-for-F" class="impl has-srclink"><span class="rightside"><a class="srclink" href="../../src/syn/lookahead.rs.html#153-155">source</a></span><a href="#impl-Peek-for-F" class="anchor"></a><h3 class="code-header in-band">impl<F: <a class="trait" href="https://doc.rust-lang.org/1.64.0/core/marker/trait.Copy.html" title="trait core::marker::Copy">Copy</a> + <a class="trait" href="https://doc.rust-lang.org/1.64.0/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(TokenMarker) -> T, T: <a class="trait" href="../token/trait.Token.html" title="trait syn::token::Token">Token</a>> <a class="trait" href="trait.Peek.html" title="trait syn::parse::Peek">Peek</a> for F</h3></section></div><script type="text/javascript" src="../../implementors/syn/lookahead/trait.Peek.js" data-ignore-extern-crates="" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="syn" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
4
doc/syn/parse/type.ParseStream.html
Normal file
4
doc/syn/parse/type.ParseStream.html
Normal file
@ -0,0 +1,4 @@
|
||||
<!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="Input to a Syn parser function."><meta name="keywords" content="rust, rustlang, rust-lang, ParseStream"><title>ParseStream in syn::parse - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc type"><!--[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="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">ParseStream</a></h2><div class="sidebar-elems"><h2 class="location"><a href="index.html">In syn::parse</a></h2></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../syn/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Type Definition <a href="../index.html">syn</a>::<wbr><a href="index.html">parse</a>::<wbr><a class="type" href="#">ParseStream</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../../src/syn/parse.rs.html#230">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="docblock item-decl"><pre class="rust typedef"><code>pub type ParseStream<'a> = &'a <a class="struct" href="struct.ParseBuffer.html" title="struct syn::parse::ParseBuffer">ParseBuffer</a><'a>;</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Input to a Syn parser function.</p>
|
||||
<p>See the methods of this type under the documentation of <a href="struct.ParseBuffer.html" title="ParseBuffer"><code>ParseBuffer</code></a>. For
|
||||
an overview of parsing in Syn, refer to the <a href="index.html">module documentation</a>.</p>
|
||||
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="syn" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
2
doc/syn/parse/type.Result.html
Normal file
2
doc/syn/parse/type.Result.html
Normal file
@ -0,0 +1,2 @@
|
||||
<!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="The result of a Syn parser."><meta name="keywords" content="rust, rustlang, rust-lang, Result"><title>Result in syn::parse - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><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="../../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../../dark.css" disabled><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc type"><!--[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="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../syn/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Result</a></h2><div class="sidebar-elems"><h2 class="location"><a href="index.html">In syn::parse</a></h2></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../../syn/index.html"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Type Definition <a href="../index.html">syn</a>::<wbr><a href="index.html">parse</a>::<wbr><a class="type" href="#">Result</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../../src/syn/error.rs.html#15">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="docblock item-decl"><pre class="rust typedef"><code>pub type Result<T> = <a class="enum" href="https://doc.rust-lang.org/1.64.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="struct.Error.html" title="struct syn::parse::Error">Error</a>>;</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>The result of a Syn parser.</p>
|
||||
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="syn" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html>
|
Reference in New Issue
Block a user