David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 9 | //! A stably addressed token buffer supporting efficient traversal based on a |
| 10 | //! cheaply copyable cursor. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 11 | //! |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 12 | //! *This module is available if Syn is built with the `"parsing"` feature.* |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 13 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 14 | // This module is heavily commented as it contains the only unsafe code in Syn, |
| 15 | // and caution should be used when editing it. The public-facing interface is |
| 16 | // 100% safe but the implementation is fragile internally. |
| 17 | |
David Tolnay | 278f9e3 | 2018-08-14 22:41:11 -0700 | [diff] [blame] | 18 | #[cfg(all( |
| 19 | not(all(target_arch = "wasm32", target_os = "unknown")), |
| 20 | feature = "proc-macro" |
| 21 | ))] |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 22 | use proc_macro as pm; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 23 | use proc_macro2::{Delimiter, Ident, Literal, Span, TokenStream}; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 24 | use proc_macro2::{Group, Punct, TokenTree}; |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 25 | |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 26 | use std::marker::PhantomData; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 27 | use std::ptr; |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 28 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 29 | /// Internal type which is used instead of `TokenTree` to represent a token tree |
| 30 | /// within a `TokenBuffer`. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 31 | enum Entry { |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 32 | // Mimicking types from proc-macro. |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 33 | Group(Span, Delimiter, TokenBuffer), |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 34 | Ident(Ident), |
| 35 | Punct(Punct), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 36 | Literal(Literal), |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 37 | // End entries contain a raw pointer to the entry from the containing |
| 38 | // token tree, or null if this is the outermost level. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 39 | End(*const Entry), |
| 40 | } |
| 41 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 42 | /// A buffer that can be efficiently traversed multiple times, unlike |
| 43 | /// `TokenStream` which requires a deep copy in order to traverse more than |
| 44 | /// once. |
| 45 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 46 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 47 | pub struct TokenBuffer { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 48 | // NOTE: Do not derive clone on this - there are raw pointers inside which |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 49 | // will be messed up. Moving the `TokenBuffer` itself is safe as the actual |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 50 | // backing slices won't be moved. |
| 51 | data: Box<[Entry]>, |
| 52 | } |
| 53 | |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 54 | impl TokenBuffer { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 55 | // NOTE: DO NOT MUTATE THE `Vec` RETURNED FROM THIS FUNCTION ONCE IT |
| 56 | // RETURNS, THE ADDRESS OF ITS BACKING MEMORY MUST REMAIN STABLE. |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 57 | fn inner_new(stream: TokenStream, up: *const Entry) -> TokenBuffer { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 58 | // Build up the entries list, recording the locations of any Groups |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 59 | // in the list to be processed later. |
| 60 | let mut entries = Vec::new(); |
| 61 | let mut seqs = Vec::new(); |
David Tolnay | 50fa468 | 2017-12-26 23:17:22 -0500 | [diff] [blame] | 62 | for tt in stream { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 63 | match tt { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 64 | TokenTree::Ident(sym) => { |
| 65 | entries.push(Entry::Ident(sym)); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 66 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 67 | TokenTree::Punct(op) => { |
| 68 | entries.push(Entry::Punct(op)); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 69 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 70 | TokenTree::Literal(l) => { |
| 71 | entries.push(Entry::Literal(l)); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 72 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 73 | TokenTree::Group(g) => { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 74 | // Record the index of the interesting entry, and store an |
| 75 | // `End(null)` there temporarially. |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 76 | seqs.push((entries.len(), g.span(), g.delimiter(), g.stream().clone())); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 77 | entries.push(Entry::End(ptr::null())); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | // Add an `End` entry to the end with a reference to the enclosing token |
| 82 | // stream which was passed in. |
| 83 | entries.push(Entry::End(up)); |
| 84 | |
| 85 | // NOTE: This is done to ensure that we don't accidentally modify the |
| 86 | // length of the backing buffer. The backing buffer must remain at a |
| 87 | // constant address after this point, as we are going to store a raw |
| 88 | // pointer into it. |
| 89 | let mut entries = entries.into_boxed_slice(); |
| 90 | for (idx, span, delim, seq_stream) in seqs { |
| 91 | // We know that this index refers to one of the temporary |
| 92 | // `End(null)` entries, and we know that the last entry is |
| 93 | // `End(up)`, so the next index is also valid. |
| 94 | let seq_up = &entries[idx + 1] as *const Entry; |
| 95 | |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 96 | // The end entry stored at the end of this Entry::Group should |
| 97 | // point to the Entry which follows the Group in the list. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 98 | let inner = Self::inner_new(seq_stream, seq_up); |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 99 | entries[idx] = Entry::Group(span, delim, inner); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 100 | } |
| 101 | |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 102 | TokenBuffer { data: entries } |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 103 | } |
| 104 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 105 | /// Creates a `TokenBuffer` containing all the tokens from the input |
| 106 | /// `TokenStream`. |
hcpl | 4b72a38 | 2018-04-04 14:50:24 +0300 | [diff] [blame] | 107 | /// |
| 108 | /// *This method is available if Syn is built with both the `"parsing"` and |
| 109 | /// `"proc-macro"` features.* |
David Tolnay | 278f9e3 | 2018-08-14 22:41:11 -0700 | [diff] [blame] | 110 | #[cfg(all( |
| 111 | not(all(target_arch = "wasm32", target_os = "unknown")), |
| 112 | feature = "proc-macro" |
| 113 | ))] |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 114 | pub fn new(stream: pm::TokenStream) -> TokenBuffer { |
| 115 | Self::new2(stream.into()) |
| 116 | } |
| 117 | |
| 118 | /// Creates a `TokenBuffer` containing all the tokens from the input |
| 119 | /// `TokenStream`. |
| 120 | pub fn new2(stream: TokenStream) -> TokenBuffer { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 121 | Self::inner_new(stream, ptr::null()) |
| 122 | } |
| 123 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 124 | /// Creates a cursor referencing the first token in the buffer and able to |
| 125 | /// traverse until the end of the buffer. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 126 | pub fn begin(&self) -> Cursor { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 127 | unsafe { Cursor::create(&self.data[0], &self.data[self.data.len() - 1]) } |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 131 | /// A cheaply copyable cursor into a `TokenBuffer`. |
| 132 | /// |
| 133 | /// This cursor holds a shared reference into the immutable data which is used |
| 134 | /// internally to represent a `TokenStream`, and can be efficiently manipulated |
| 135 | /// and copied around. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 136 | /// |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 137 | /// An empty `Cursor` can be created directly, or one may create a `TokenBuffer` |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 138 | /// object and get a cursor to its first token with `begin()`. |
| 139 | /// |
| 140 | /// Two cursors are equal if they have the same location in the same input |
| 141 | /// stream, and have the same scope. |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 142 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 143 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 144 | #[derive(Copy, Clone, Eq, PartialEq)] |
| 145 | pub struct Cursor<'a> { |
| 146 | /// The current entry which the `Cursor` is pointing at. |
| 147 | ptr: *const Entry, |
| 148 | /// This is the only `Entry::End(..)` object which this cursor is allowed to |
| 149 | /// point at. All other `End` objects are skipped over in `Cursor::create`. |
| 150 | scope: *const Entry, |
| 151 | /// This uses the &'a reference which guarantees that these pointers are |
| 152 | /// still valid. |
| 153 | marker: PhantomData<&'a Entry>, |
| 154 | } |
| 155 | |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 156 | impl<'a> Cursor<'a> { |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 157 | /// Creates a cursor referencing a static empty TokenStream. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 158 | pub fn empty() -> Self { |
Michael Layzell | 69cf908 | 2017-06-03 12:15:58 -0400 | [diff] [blame] | 159 | // It's safe in this situation for us to put an `Entry` object in global |
| 160 | // storage, despite it not actually being safe to send across threads |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 161 | // (`Ident` is a reference into a thread-local table). This is because |
| 162 | // this entry never includes a `Ident` object. |
Michael Layzell | 69cf908 | 2017-06-03 12:15:58 -0400 | [diff] [blame] | 163 | // |
| 164 | // This wrapper struct allows us to break the rules and put a `Sync` |
| 165 | // object in global storage. |
| 166 | struct UnsafeSyncEntry(Entry); |
| 167 | unsafe impl Sync for UnsafeSyncEntry {} |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 168 | static EMPTY_ENTRY: UnsafeSyncEntry = UnsafeSyncEntry(Entry::End(0 as *const Entry)); |
Michael Layzell | 69cf908 | 2017-06-03 12:15:58 -0400 | [diff] [blame] | 169 | |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 170 | Cursor { |
Michael Layzell | 69cf908 | 2017-06-03 12:15:58 -0400 | [diff] [blame] | 171 | ptr: &EMPTY_ENTRY.0, |
| 172 | scope: &EMPTY_ENTRY.0, |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 173 | marker: PhantomData, |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /// This create method intelligently exits non-explicitly-entered |
| 178 | /// `None`-delimited scopes when the cursor reaches the end of them, |
| 179 | /// allowing for them to be treated transparently. |
| 180 | unsafe fn create(mut ptr: *const Entry, scope: *const Entry) -> Self { |
| 181 | // NOTE: If we're looking at a `End(..)`, we want to advance the cursor |
| 182 | // past it, unless `ptr == scope`, which means that we're at the edge of |
| 183 | // our cursor's scope. We should only have `ptr != scope` at the exit |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 184 | // from None-delimited groups entered with `ignore_none`. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 185 | while let Entry::End(exit) = *ptr { |
| 186 | if ptr == scope { |
| 187 | break; |
| 188 | } |
| 189 | ptr = exit; |
| 190 | } |
| 191 | |
| 192 | Cursor { |
| 193 | ptr: ptr, |
| 194 | scope: scope, |
| 195 | marker: PhantomData, |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /// Get the current entry. |
| 200 | fn entry(self) -> &'a Entry { |
| 201 | unsafe { &*self.ptr } |
| 202 | } |
| 203 | |
| 204 | /// Bump the cursor to point at the next token after the current one. This |
| 205 | /// is undefined behavior if the cursor is currently looking at an |
| 206 | /// `Entry::End`. |
| 207 | unsafe fn bump(self) -> Cursor<'a> { |
| 208 | Cursor::create(self.ptr.offset(1), self.scope) |
| 209 | } |
| 210 | |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 211 | /// If the cursor is looking at a `None`-delimited group, move it to look at |
| 212 | /// the first token inside instead. If the group is empty, this will move |
| 213 | /// the cursor past the `None`-delimited group. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 214 | /// |
| 215 | /// WARNING: This mutates its argument. |
| 216 | fn ignore_none(&mut self) { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 217 | if let Entry::Group(_, Delimiter::None, ref buf) = *self.entry() { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 218 | // NOTE: We call `Cursor::create` here to make sure that situations |
| 219 | // where we should immediately exit the span after entering it are |
| 220 | // handled correctly. |
| 221 | unsafe { |
| 222 | *self = Cursor::create(&buf.data[0], self.scope); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 227 | /// Checks whether the cursor is currently pointing at the end of its valid |
| 228 | /// scope. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 229 | #[inline] |
| 230 | pub fn eof(self) -> bool { |
| 231 | // We're at eof if we're at the end of our scope. |
| 232 | self.ptr == self.scope |
| 233 | } |
| 234 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 235 | /// If the cursor is pointing at a `Group` with the given delimiter, returns |
| 236 | /// a cursor into that group and one pointing to the next `TokenTree`. |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 237 | pub fn group(mut self, delim: Delimiter) -> Option<(Cursor<'a>, Span, Cursor<'a>)> { |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 238 | // If we're not trying to enter a none-delimited group, we want to |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 239 | // ignore them. We have to make sure to _not_ ignore them when we want |
| 240 | // to enter them, of course. For obvious reasons. |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 241 | if delim != Delimiter::None { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 242 | self.ignore_none(); |
| 243 | } |
| 244 | |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 245 | if let Entry::Group(span, group_delim, ref buf) = *self.entry() { |
| 246 | if group_delim == delim { |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 247 | return Some((buf.begin(), span, unsafe { self.bump() })); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 248 | } |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 249 | } |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 250 | |
| 251 | None |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 254 | /// If the cursor is pointing at a `Ident`, returns it along with a cursor |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 255 | /// pointing at the next `TokenTree`. |
David Tolnay | 55a5f3a | 2018-05-20 18:00:51 -0700 | [diff] [blame] | 256 | pub fn ident(mut self) -> Option<(Ident, Cursor<'a>)> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 257 | self.ignore_none(); |
| 258 | match *self.entry() { |
David Tolnay | a4319b7 | 2018-06-02 00:49:15 -0700 | [diff] [blame] | 259 | Entry::Ident(ref ident) => Some((ident.clone(), unsafe { self.bump() })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 260 | _ => None, |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 264 | /// If the cursor is pointing at an `Punct`, returns it along with a cursor |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 265 | /// pointing at the next `TokenTree`. |
David Tolnay | 55a5f3a | 2018-05-20 18:00:51 -0700 | [diff] [blame] | 266 | pub fn punct(mut self) -> Option<(Punct, Cursor<'a>)> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 267 | self.ignore_none(); |
| 268 | match *self.entry() { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 269 | Entry::Punct(ref op) => Some((op.clone(), unsafe { self.bump() })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 270 | _ => None, |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 274 | /// If the cursor is pointing at a `Literal`, return it along with a cursor |
| 275 | /// pointing at the next `TokenTree`. |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 276 | pub fn literal(mut self) -> Option<(Literal, Cursor<'a>)> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 277 | self.ignore_none(); |
| 278 | match *self.entry() { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 279 | Entry::Literal(ref lit) => Some((lit.clone(), unsafe { self.bump() })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 280 | _ => None, |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 284 | /// Copies all remaining tokens visible from this cursor into a |
| 285 | /// `TokenStream`. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 286 | pub fn token_stream(self) -> TokenStream { |
| 287 | let mut tts = Vec::new(); |
| 288 | let mut cursor = self; |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 289 | while let Some((tt, rest)) = cursor.token_tree() { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 290 | tts.push(tt); |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 291 | cursor = rest; |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 292 | } |
| 293 | tts.into_iter().collect() |
| 294 | } |
| 295 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 296 | /// If the cursor is pointing at a `TokenTree`, returns it along with a |
| 297 | /// cursor pointing at the next `TokenTree`. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 298 | /// |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 299 | /// Returns `None` if the cursor has reached the end of its stream. |
| 300 | /// |
| 301 | /// This method does not treat `None`-delimited groups as transparent, and |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 302 | /// will return a `Group(None, ..)` if the cursor is looking at one. |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 303 | pub fn token_tree(self) -> Option<(TokenTree, Cursor<'a>)> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 304 | let tree = match *self.entry() { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 305 | Entry::Group(span, delim, ref buf) => { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 306 | let stream = buf.begin().token_stream(); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 307 | let mut g = Group::new(delim, stream); |
| 308 | g.set_span(span); |
| 309 | TokenTree::from(g) |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 310 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 311 | Entry::Literal(ref lit) => lit.clone().into(), |
David Tolnay | a4319b7 | 2018-06-02 00:49:15 -0700 | [diff] [blame] | 312 | Entry::Ident(ref ident) => ident.clone().into(), |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 313 | Entry::Punct(ref op) => op.clone().into(), |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 314 | Entry::End(..) => { |
| 315 | return None; |
| 316 | } |
| 317 | }; |
| 318 | |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 319 | Some((tree, unsafe { self.bump() })) |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 320 | } |
David Tolnay | 225efa2 | 2017-12-31 16:51:29 -0500 | [diff] [blame] | 321 | |
| 322 | /// Returns the `Span` of the current token, or `Span::call_site()` if this |
| 323 | /// cursor points to eof. |
| 324 | pub fn span(self) -> Span { |
| 325 | match *self.entry() { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 326 | Entry::Group(span, ..) => span, |
| 327 | Entry::Literal(ref l) => l.span(), |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 328 | Entry::Ident(ref t) => t.span(), |
| 329 | Entry::Punct(ref o) => o.span(), |
David Tolnay | 225efa2 | 2017-12-31 16:51:29 -0500 | [diff] [blame] | 330 | Entry::End(..) => Span::call_site(), |
| 331 | } |
| 332 | } |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 333 | } |