edlang/syn/token/index.html

65 lines
27 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="Tokens representing Rust punctuation, keywords, and delimiters."><title>syn::token - Rust</title><script> if (window.location.protocol !== "file:") document.write(`<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">`)</script><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-e935ef01ae1c1829.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.78.0 (9b00956e5 2024-04-29)" data-channel="1.78.0" data-search-js="search-42d8da7a6b9792c2.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-4c98445ec4002617.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-12cf3b4f4f9dc36d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-04d5337699b92874.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod"><!--[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.63</span></h2></div><h2 class="location"><a href="#">Module token</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></section><h2><a href="../index.html">In crate syn</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>Module <a href="../index.html">syn</a>::<wbr><a class="mod" href="#">token</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/token.rs.html#1-1138">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Tokens representing Rust punctuation, keywords, and delimiters.</p>
<p>The type names in this module can be difficult to keep straight, so we
prefer to use the <a href="../macro.Token.html" title="macro syn::Token"><code>Token!</code></a> macro instead. This is a type-macro that
expands to the token type of the given token.</p>
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
<p>The <a href="../struct.ItemStatic.html" title="struct syn::ItemStatic"><code>ItemStatic</code></a> syntax tree node is defined like this.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>ItemStatic {
<span class="kw">pub </span>attrs: Vec&lt;Attribute&gt;,
<span class="kw">pub </span>vis: Visibility,
<span class="kw">pub </span>static_token: <span class="macro">Token!</span>[<span class="kw">static</span>],
<span class="kw">pub </span>mutability: <span class="prelude-ty">Option</span>&lt;<span class="macro">Token!</span>[<span class="kw-2">mut</span>]&gt;,
<span class="kw">pub </span>ident: Ident,
<span class="kw">pub </span>colon_token: <span class="macro">Token!</span>[:],
<span class="kw">pub </span>ty: Box&lt;Type&gt;,
<span class="kw">pub </span>eq_token: <span class="macro">Token!</span>[=],
<span class="kw">pub </span>expr: Box&lt;Expr&gt;,
<span class="kw">pub </span>semi_token: <span class="macro">Token!</span>[;],
}</code></pre></div>
<h2 id="parsing"><a class="doc-anchor" href="#parsing">§</a>Parsing</h2>
<p>Keywords and punctuation can be parsed through the <a href="../parse/struct.ParseBuffer.html#method.parse" title="method syn::parse::ParseBuffer::parse"><code>ParseStream::parse</code></a>
method. Delimiter tokens are parsed using the <a href="../macro.parenthesized.html" title="macro syn::parenthesized"><code>parenthesized!</code></a>,
<a href="../macro.bracketed.html" title="macro syn::bracketed"><code>bracketed!</code></a> and <a href="../macro.braced.html" title="macro syn::braced"><code>braced!</code></a> macros.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>syn::{Attribute, <span class="prelude-ty">Result</span>};
<span class="kw">use </span>syn::parse::{Parse, ParseStream};
<span class="comment">// Parse the ItemStatic struct shown above.
</span><span class="kw">impl </span>Parse <span class="kw">for </span>ItemStatic {
<span class="kw">fn </span>parse(input: ParseStream) -&gt; <span class="prelude-ty">Result</span>&lt;<span class="self">Self</span>&gt; {
<span class="prelude-val">Ok</span>(ItemStatic {
attrs: input.call(Attribute::parse_outer)<span class="question-mark">?</span>,
vis: input.parse()<span class="question-mark">?</span>,
static_token: input.parse()<span class="question-mark">?</span>,
mutability: input.parse()<span class="question-mark">?</span>,
ident: input.parse()<span class="question-mark">?</span>,
colon_token: input.parse()<span class="question-mark">?</span>,
ty: input.parse()<span class="question-mark">?</span>,
eq_token: input.parse()<span class="question-mark">?</span>,
expr: input.parse()<span class="question-mark">?</span>,
semi_token: input.parse()<span class="question-mark">?</span>,
})
}
}</code></pre></div>
<h2 id="other-operations"><a class="doc-anchor" href="#other-operations">§</a>Other operations</h2>
<p>Every keyword and punctuation token supports the following operations.</p>
<ul>
<li>
<p><a href="../parse/struct.ParseBuffer.html#method.peek" title="method syn::parse::ParseBuffer::peek">Peeking</a><code>input.peek(Token![...])</code></p>
</li>
<li>
<p><a href="../parse/struct.ParseBuffer.html#method.parse" title="method syn::parse::ParseBuffer::parse">Parsing</a><code>input.parse::&lt;Token![...]&gt;()?</code></p>
</li>
<li>
<p><a href="https://docs.rs/quote/1.0/quote/trait.ToTokens.html">Printing</a><code>quote!( ... #the_token ... )</code></p>
</li>
<li>
<p>Construction from a <a href="https://docs.rs/proc-macro2/1.0/proc_macro2/struct.Span.html"><code>Span</code></a><code>let the_token = Token![...](sp)</code></p>
</li>
<li>
<p>Field access to its span — <code>let sp = the_token.span</code></p>
</li>
</ul>
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Abstract.html" title="struct syn::token::Abstract">Abstract</a></div><div class="desc docblock-short"><code>abstract</code></div></li><li><div class="item-name"><a class="struct" href="struct.And.html" title="struct syn::token::And">And</a></div><div class="desc docblock-short"><code>&amp;</code></div></li><li><div class="item-name"><a class="struct" href="struct.AndAnd.html" title="struct syn::token::AndAnd">AndAnd</a></div><div class="desc docblock-short"><code>&amp;&amp;</code></div></li><li><div class="item-name"><a class="struct" href="struct.AndEq.html" title="struct syn::token::AndEq">AndEq</a></div><div class="desc docblock-short"><code>&amp;=</code></div></li><li><div class="item-name"><a class="struct" href="struct.As.html" title="struct syn::token::As">As</a></div><div class="desc docblock-short"><code>as</code></div></li><li><div class="item-name"><a class="struct" href="struct.Async.html" title="struct syn::token::Async">Async</a></div><div class="desc docblock-short"><code>async</code></div></li><li><div class="item-name"><a class="struct" href="struct.At.html" title="struct syn::token::At">At</a></div><div class="desc docblock-short"><code>@</code></div></li><li><div class="item-name"><a class="struct" href="struct.Auto.html" title="struct syn::token::Auto">Auto</a></div><div class="desc docblock-short"><code>auto</code></div></li><li><div class="item-name"><a class="struct" href="struct.Await.html" title="struct syn::token::Await">Await</a></div><div class="desc docblock-short"><code>await</code></div></li><li><div class="item-name"><a class="struct" href="struct.Become.html" title="struct syn::token::Become">Become</a></div><div class="desc docblock-short"><code>become</code></div></li><li><div class="item-name"><a class="struct" href="struct.Box.html" title="struct syn::token::Box">Box</a></div><div class="desc docblock-short"><code>box</code></div></li><li><div class="item-name"><a class="struct" href="struct.Brace.html" title="struct syn::token::Brace">Brace</a></div><div class="desc docblock-short"><code>{</code><code>}</code></div></li><li><div class="item-name"><a class="struct" href="struct.Bracket.html" title="struct syn::token::Bracket">Bracket</a></div><div class="desc docblock-short"><code>[</code><code>]</code></div></li><li><div class="item-name"><a class="struct" href="struct.Break.html" title="struct syn::token::Break">Break</a></div><div class="desc docblock-short"><code>break</code></div></li><li><div class="item-name"><a class="struct" href="struct.Caret.html" title="struct syn::token::Caret">Caret</a></div><div class="desc docblock-short"><code>^</code></div></li><li><div class="item-name"><a class="struct" href="struct.CaretEq.html" title="struct syn::token::CaretEq">CaretEq</a></div><div class="desc docblock-short"><code>^=</code></div></li><li><div class="item-name"><a class="struct" href="struct.Colon.html" title="struct syn::token::Colon">Colon</a></div><div class="desc docblock-short"><code>:</code></div></li><li><div class="item-name"><a class="struct" href="struct.Comma.html" title="struct syn::token::Comma">Comma</a></div><div class="desc docblock-short"><code>,</code></div></li><li><div class="item-name"><a class="struct" href="struct.Const.html" title="struct syn::token::Const">Const</a></div><div class="desc docblock-short"><code>const</code></div></li><li><div class="item-name"><a class="struct" href="struct.Continue.html" title="struct syn::token::Continue">Continue</a></div><div class="desc docblock-short"><code>continue</code></div></li><li><div class="item-name"><a class="struct" href="struct.Crate.html" title="struct syn::token::Crate">Crate</a></div><div class="desc docblock-short"><code>crate</code></div></li><li><div class="item-name"><a class="struct" href="struct.Default.html" title="struct syn::token::Default">Default</a></div><div class="desc docblock-short"><code>defa