rtic/stable/api/syn/meta/fn.parser.html

93 lines
9 KiB
HTML
Raw Normal View History

<!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="Make a parser that is usable with `parse_macro_input!` in a `#[proc_macro_attribute]` macro."><title>parser in syn::meta - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-492a78a4a87dcc01.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="syn" data-themes="" data-resource-suffix="" data-rustdoc-version="1.82.0 (f6e511eec 2024-10-15)" data-channel="1.82.0" data-search-js="search-a99f1315e7cc5121.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-118b08c4c78b968e.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-921df33f47b8780c.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-3b12f09e550e0385.css"></noscript><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 fn"><!--[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" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../syn/index.html">syn</a><span class="version">2.0.85</span></h2></div><div class="sidebar-elems"><h2><a href="index.html">In syn::<wbr>meta</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Function <a href="../index.html">syn</a>::<wbr><a href="index.html">meta</a>::<wbr><a class="fn" href="#">parser</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../../src/syn/meta.rs.html#132-140">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><pre class="rust item-decl"><code>pub fn parser(
logic: impl <a class="trait" href="https://doc.rust-lang.org/1.82.0/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(<a class="struct" href="struct.ParseNestedMeta.html" title="struct syn::meta::ParseNestedMeta">ParseNestedMeta</a>&lt;'_&gt;) -&gt; <a class="type" href="../parse/type.Result.html" title="type syn::parse::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.82.0/std/primitive.unit.html">()</a>&gt;,
) -&gt; impl <a class="trait" href="../parse/trait.Parser.html" title="trait syn::parse::Parser">Parser</a>&lt;Output = <a class="primitive" href="https://doc.rust-lang.org/1.82.0/std/primitive.unit.html">()</a>&gt;</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Make a parser that is usable with <code>parse_macro_input!</code> in a
<code>#[proc_macro_attribute]</code> macro.</p>
<p><em>Warning:</em> When parsing attribute args <strong>other than</strong> the
<code>proc_macro::TokenStream</code> input of a <code>proc_macro_attribute</code>, you do <strong>not</strong>
need this function. In several cases your callers will get worse error
messages if you use this function, because the surrounding delimiters span
is concealed from attribute macros by rustc. Use
<a href="../struct.Attribute.html#method.parse_nested_meta" title="method syn::Attribute::parse_nested_meta"><code>Attribute::parse_nested_meta</code></a> instead.</p>
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
<p>This example implements an attribute macro whose invocations look like this:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[tea(kind = <span class="string">"EarlGrey"</span>, hot)]
</span><span class="kw">struct </span>Picard {...}</code></pre></div>
<p>The “parameters” supported by the attribute are:</p>
<ul>
<li><code>kind = "..."</code></li>
<li><code>hot</code></li>
<li><code>with(sugar, milk, ...)</code>, a comma-separated list of ingredients</li>
</ul>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>proc_macro::TokenStream;
<span class="kw">use </span>syn::{parse_macro_input, LitStr, Path};
<span class="attr">#[proc_macro_attribute]
</span><span class="kw">pub fn </span>tea(args: TokenStream, input: TokenStream) -&gt; TokenStream {
<span class="kw">let </span><span class="kw-2">mut </span>kind: <span class="prelude-ty">Option</span>&lt;LitStr&gt; = <span class="prelude-val">None</span>;
<span class="kw">let </span><span class="kw-2">mut </span>hot: bool = <span class="bool-val">false</span>;
<span class="kw">let </span><span class="kw-2">mut </span>with: Vec&lt;Path&gt; = Vec::new();
<span class="kw">let </span>tea_parser = syn::meta::parser(|meta| {
<span class="kw">if </span>meta.path.is_ident(<span class="string">"kind"</span>) {
kind = <span class="prelude-val">Some</span>(meta.value()<span class="question-mark">?</span>.parse()<span class="question-mark">?</span>);
<span class="prelude-val">Ok</span>(())
} <span class="kw">else if </span>meta.path.is_ident(<span class="string">"hot"</span>) {
hot = <span class="bool-val">true</span>;
<span class="prelude-val">Ok</span>(())
} <span class="kw">else if </span>meta.path.is_ident(<span class="string">"with"</span>) {
meta.parse_nested_meta(|meta| {
with.push(meta.path);
<span class="prelude-val">Ok</span>(())
})
} <span class="kw">else </span>{
<span class="prelude-val">Err</span>(meta.error(<span class="string">"unsupported tea property"</span>))
}
});
<span class="macro">parse_macro_input!</span>(args with tea_parser);
<span class="macro">eprintln!</span>(<span class="string">"kind={kind:?} hot={hot} with={with:?}"</span>);
<span class="comment">/* ... */
</span>}</code></pre></div>
<p>The <code>syn::meta</code> library will take care of dealing with the commas including
trailing commas, and producing sensible error messages on unexpected input.</p>
<div class="example-wrap"><pre class="language-console"><code>error: expected `,`
--&gt; src/main.rs:3:37
|
3 | #[tea(kind = &quot;EarlGrey&quot;, with(sugar = &quot;lol&quot;, milk))]
| ^
</code></pre></div><h2 id="example-1"><a class="doc-anchor" href="#example-1">§</a>Example</h2>
<p>Same as above but we factor out most of the logic into a separate function.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>proc_macro::TokenStream;
<span class="kw">use </span>syn::meta::ParseNestedMeta;
<span class="kw">use </span>syn::parse::{Parser, <span class="prelude-ty">Result</span>};
<span class="kw">use </span>syn::{parse_macro_input, LitStr, Path};
<span class="attr">#[proc_macro_attribute]
</span><span class="kw">pub fn </span>tea(args: TokenStream, input: TokenStream) -&gt; TokenStream {
<span class="kw">let </span><span class="kw-2">mut </span>attrs = TeaAttributes::default();
<span class="kw">let </span>tea_parser = syn::meta::parser(|meta| attrs.parse(meta));
<span class="macro">parse_macro_input!</span>(args with tea_parser);
<span class="comment">/* ... */
</span>}
<span class="attr">#[derive(Default)]
</span><span class="kw">struct </span>TeaAttributes {
kind: <span class="prelude-ty">Option</span>&lt;LitStr&gt;,
hot: bool,
with: Vec&lt;Path&gt;,
}
<span class="kw">impl </span>TeaAttributes {
<span class="kw">fn </span>parse(<span class="kw-2">&amp;mut </span><span class="self">self</span>, meta: ParseNestedMeta) -&gt; <span class="prelude-ty">Result</span>&lt;()&gt; {
<span class="kw">if </span>meta.path.is_ident(<span class="string">"kind"</span>) {
<span class="self">self</span>.kind = <span class="prelude-val">Some</span>(meta.value()<span class="question-mark">?</span>.parse()<span class="question-mark">?</span>);
<span class="prelude-val">Ok</span>(())
} <span class="kw">else </span><span class="comment">/* just like in last example */
</span>}
}</code></pre></div>
</div></details></section></div></main></body></html>