mirror of
https://github.com/edg-l/edlang.git
synced 2024-11-23 08:28:24 +00:00
94 lines
10 KiB
HTML
94 lines
10 KiB
HTML
|
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Make a parser that is usable with `parse_macro_input!` in a `#[proc_macro_attribute]` macro."><title>parser in syn::meta - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-ac92e1bbe349e143.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.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc 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">☰</button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../syn/index.html">syn</a><span class="version">2.0.48</span></h2></div><div class="sidebar-elems"><h2><a href="index.html">In syn::meta</a></h2></div></nav><div class="sidebar-resizer"></div>
|
|||
|
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../../syn/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>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"><img src="../../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../../src/syn/meta.rs.html#131-139">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</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.76.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><'_>) -> <a class="type" href="../parse/type.Result.html" title="type syn::parse::Result">Result</a><<a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.unit.html">()</a>>
|
|||
|
) -> impl <a class="trait" href="../parse/trait.Parser.html" title="trait syn::parse::Parser">Parser</a><Output = <a class="primitive" href="https://doc.rust-lang.org/1.76.0/std/primitive.unit.html">()</a>></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 delimiter’s 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 href="#example">Example</a></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) -> TokenStream {
|
|||
|
<span class="kw">let </span><span class="kw-2">mut </span>kind: <span class="prelude-ty">Option</span><LitStr> = <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<Path> = 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 `,`
|
|||
|
--> src/main.rs:3:37
|
|||
|
|
|
|||
|
3 | #[tea(kind = "EarlGrey", with(sugar = "lol", milk))]
|
|||
|
| ^
|
|||
|
</code></pre></div><h2 id="example-1"><a href="#example-1">Example</a></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) -> 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><LitStr>,
|
|||
|
hot: bool,
|
|||
|
with: Vec<Path>,
|
|||
|
}
|
|||
|
|
|||
|
<span class="kw">impl </span>TeaAttributes {
|
|||
|
<span class="kw">fn </span>parse(<span class="kw-2">&mut </span><span class="self">self</span>, meta: ParseNestedMeta) -> <span class="prelude-ty">Result</span><()> {
|
|||
|
<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>
|