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