mentat/syn/buffer/index.html
2018-08-22 17:04:13 +00:00

239 lines
No EOL
16 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="rustdoc">
<meta name="description" content="API documentation for the Rust `buffer` mod in crate `syn`.">
<meta name="keywords" content="rust, rustlang, rust-lang, buffer">
<title>syn::buffer - Rust</title>
<link rel="stylesheet" type="text/css" href="../../normalize.css">
<link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle">
<link rel="stylesheet" type="text/css" href="../../dark.css">
<link rel="stylesheet" type="text/css" href="../../main.css" id="themeStyle">
<script src="../../storage.js"></script>
</head>
<body class="rustdoc mod">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<nav class="sidebar">
<div class="sidebar-menu">&#9776;</div>
<p class='location'>Module buffer</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li></ul></div><p class='location'><a href='../index.html'>syn</a></p><script>window.sidebarCurrent = {name: 'buffer', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div>
</nav>
<div class="theme-picker">
<button id="theme-picker" aria-label="Pick another theme!">
<img src="../../brush.svg" width="18" alt="Pick another theme!">
</button>
<div id="theme-choices"></div>
</div>
<script src="../../theme.js"></script>
<nav class="sub">
<form class="search-form js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>syn</a>::<wbr><a class="mod" href=''>buffer</a></span><span class='out-of-band'><span id='render-detail'>
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
[<span class='inner'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../../src/syn/buffer.rs.html#9-469' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>A stably addressed token buffer supporting efficient traversal based on a
cheaply copyable cursor.</p>
<p>The <a href="../synom/trait.Synom.html"><code>Synom</code></a> trait is implemented for syntax tree types that can be parsed
from one of these token cursors.</p>
<p><em>This module is available if Syn is built with the <code>&quot;parsing&quot;</code> feature.</em></p>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<p>This example shows a basic token parser for parsing a token stream without
using Syn's parser combinator macros.</p>
<pre class="rust rust-example-rendered">
<span class="attribute">#![<span class="ident">feature</span>(<span class="ident">proc_macro_diagnostic</span>)]</span>
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">syn</span>;
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">proc_macro</span>;
<span class="attribute">#[<span class="ident">macro_use</span>]</span>
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">quote</span>;
<span class="kw">use</span> <span class="ident">syn</span>::{<span class="ident">token</span>, <span class="ident">ExprTuple</span>};
<span class="kw">use</span> <span class="ident">syn</span>::<span class="ident">buffer</span>::{<span class="ident">Cursor</span>, <span class="ident">TokenBuffer</span>};
<span class="kw">use</span> <span class="ident">syn</span>::<span class="ident">spanned</span>::<span class="ident">Spanned</span>;
<span class="kw">use</span> <span class="ident">syn</span>::<span class="ident">synom</span>::<span class="ident">Synom</span>;
<span class="kw">use</span> <span class="ident">proc_macro</span>::{<span class="ident">Diagnostic</span>, <span class="ident">Span</span>, <span class="ident">TokenStream</span>};
<span class="doccomment">/// A basic token parser for parsing a token stream without using Syn&#39;s</span>
<span class="doccomment">/// parser combinator macros.</span>
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Parser</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> {
<span class="ident">cursor</span>: <span class="ident">Cursor</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span>,
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> <span class="ident">Parser</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> {
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">new</span>(<span class="ident">cursor</span>: <span class="ident">Cursor</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span>) <span class="op">-&gt;</span> <span class="self">Self</span> {
<span class="ident">Parser</span> { <span class="ident">cursor</span> }
}
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">current_span</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">Span</span> {
<span class="self">self</span>.<span class="ident">cursor</span>.<span class="ident">span</span>().<span class="ident">unstable</span>()
}
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">parse</span><span class="op">&lt;</span><span class="ident">T</span>: <span class="ident">Synom</span><span class="op">&gt;</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">T</span>, <span class="ident">Diagnostic</span><span class="op">&gt;</span> {
<span class="kw">let</span> (<span class="ident">val</span>, <span class="ident">rest</span>) <span class="op">=</span> <span class="ident">T</span>::<span class="ident">parse</span>(<span class="self">self</span>.<span class="ident">cursor</span>)
.<span class="ident">map_err</span>(<span class="op">|</span><span class="ident">e</span><span class="op">|</span> <span class="kw">match</span> <span class="ident">T</span>::<span class="ident">description</span>() {
<span class="prelude-val">Some</span>(<span class="ident">desc</span>) <span class="op">=&gt;</span> {
<span class="self">self</span>.<span class="ident">current_span</span>().<span class="ident">error</span>(<span class="macro">format</span><span class="macro">!</span>(<span class="string">&quot;{}: expected {}&quot;</span>, <span class="ident">e</span>, <span class="ident">desc</span>))
}
<span class="prelude-val">None</span> <span class="op">=&gt;</span> {
<span class="self">self</span>.<span class="ident">current_span</span>().<span class="ident">error</span>(<span class="ident">e</span>.<span class="ident">to_string</span>())
}
})<span class="question-mark">?</span>;
<span class="self">self</span>.<span class="ident">cursor</span> <span class="op">=</span> <span class="ident">rest</span>;
<span class="prelude-val">Ok</span>(<span class="ident">val</span>)
}
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">expect_eof</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span>(), <span class="ident">Diagnostic</span><span class="op">&gt;</span> {
<span class="kw">if</span> <span class="op">!</span><span class="self">self</span>.<span class="ident">cursor</span>.<span class="ident">eof</span>() {
<span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="self">self</span>.<span class="ident">current_span</span>().<span class="ident">error</span>(<span class="string">&quot;trailing characters; expected eof&quot;</span>));
}
<span class="prelude-val">Ok</span>(())
}
}
<span class="kw">fn</span> <span class="ident">eval</span>(<span class="ident">input</span>: <span class="ident">TokenStream</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span><span class="ident">TokenStream</span>, <span class="ident">Diagnostic</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">TokenBuffer</span>::<span class="ident">new</span>(<span class="ident">input</span>);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">parser</span> <span class="op">=</span> <span class="ident">Parser</span>::<span class="ident">new</span>(<span class="ident">buffer</span>.<span class="ident">begin</span>());
<span class="comment">// Parse some syntax tree types out of the input tokens. In this case we</span>
<span class="comment">// expect something like:</span>
<span class="comment">//</span>
<span class="comment">// (a, b, c) = (1, 2, 3)</span>
<span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">parser</span>.<span class="ident">parse</span>::<span class="op">&lt;</span><span class="ident">ExprTuple</span><span class="op">&gt;</span>()<span class="question-mark">?</span>;
<span class="ident">parser</span>.<span class="ident">parse</span>::<span class="op">&lt;</span><span class="ident">token</span>::<span class="ident">Eq</span><span class="op">&gt;</span>()<span class="question-mark">?</span>;
<span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="ident">parser</span>.<span class="ident">parse</span>::<span class="op">&lt;</span><span class="ident">ExprTuple</span><span class="op">&gt;</span>()<span class="question-mark">?</span>;
<span class="ident">parser</span>.<span class="ident">expect_eof</span>()<span class="question-mark">?</span>;
<span class="comment">// Perform some validation and report errors.</span>
<span class="kw">let</span> (<span class="ident">a_len</span>, <span class="ident">b_len</span>) <span class="op">=</span> (<span class="ident">a</span>.<span class="ident">elems</span>.<span class="ident">len</span>(), <span class="ident">b</span>.<span class="ident">elems</span>.<span class="ident">len</span>());
<span class="kw">if</span> <span class="ident">a_len</span> <span class="op">!=</span> <span class="ident">b_len</span> {
<span class="kw">let</span> <span class="ident">diag</span> <span class="op">=</span> <span class="ident">b</span>.<span class="ident">span</span>().<span class="ident">unstable</span>()
.<span class="ident">error</span>(<span class="macro">format</span><span class="macro">!</span>(<span class="string">&quot;expected {} element(s), got {}&quot;</span>, <span class="ident">a_len</span>, <span class="ident">b_len</span>))
.<span class="ident">span_note</span>(<span class="ident">a</span>.<span class="ident">span</span>().<span class="ident">unstable</span>(), <span class="string">&quot;because of this&quot;</span>);
<span class="kw">return</span> <span class="prelude-val">Err</span>(<span class="ident">diag</span>);
}
<span class="comment">// Build the output tokens.</span>
<span class="kw">let</span> <span class="ident">out</span> <span class="op">=</span> <span class="macro">quote</span><span class="macro">!</span> {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;All good! Received two tuples of size {}&quot;</span>, #<span class="ident">a_len</span>);
};
<span class="prelude-val">Ok</span>(<span class="ident">out</span>.<span class="ident">into</span>())
}</pre>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.Cursor.html"
title='struct syn::buffer::Cursor'>Cursor</a></td>
<td class='docblock-short'>
<p>A cheaply copyable cursor into a <code>TokenBuffer</code>.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.TokenBuffer.html"
title='struct syn::buffer::TokenBuffer'>TokenBuffer</a></td>
<td class='docblock-short'>
<p>A buffer that can be efficiently traversed multiple times, unlike
<code>TokenStream</code> which requires a deep copy in order to traverse more than
once.</p>
</td>
</tr></table></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<aside id="help" class="hidden">
<div>
<h1 class="hidden">Help</h1>
<div class="shortcuts">
<h2>Keyboard Shortcuts</h2>
<dl>
<dt><kbd>?</kbd></dt>
<dd>Show this help dialog</dd>
<dt><kbd>S</kbd></dt>
<dd>Focus the search field</dd>
<dt><kbd></kbd></dt>
<dd>Move up in search results</dd>
<dt><kbd></kbd></dt>
<dd>Move down in search results</dd>
<dt><kbd></kbd></dt>
<dd>Switch tab</dd>
<dt><kbd>&#9166;</kbd></dt>
<dd>Go to active search result</dd>
<dt><kbd>+</kbd></dt>
<dd>Expand all sections</dd>
<dt><kbd>-</kbd></dt>
<dd>Collapse all sections</dd>
</dl>
</div>
<div class="infos">
<h2>Search Tricks</h2>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>type</code>, <code>macro</code>,
and <code>const</code>.
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code> or <code>* -> vec</code>)
</p>
</div>
</div>
</aside>
<script>
window.rootPath = "../../";
window.currentCrate = "syn";
</script>
<script src="../../main.js"></script>
<script defer src="../../search-index.js"></script>
</body>
</html>