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 | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 12 | //! The [`Synom`] trait is implemented for syntax tree types that can be parsed |
| 13 | //! from one of these token cursors. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 14 | //! |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 15 | //! [`Synom`]: ../synom/trait.Synom.html |
| 16 | //! |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 17 | //! *This module is available if Syn is built with the `"parsing"` feature.* |
| 18 | //! |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 19 | //! # Example |
| 20 | //! |
| 21 | //! This example shows a basic token parser for parsing a token stream without |
| 22 | //! using Syn's parser combinator macros. |
| 23 | //! |
| 24 | //! ``` |
| 25 | //! #![feature(proc_macro)] |
| 26 | //! |
| 27 | //! extern crate syn; |
| 28 | //! extern crate proc_macro; |
| 29 | //! |
| 30 | //! #[macro_use] |
| 31 | //! extern crate quote; |
| 32 | //! |
| 33 | //! use syn::{token, ExprTuple}; |
| 34 | //! use syn::buffer::{Cursor, TokenBuffer}; |
| 35 | //! use syn::spanned::Spanned; |
| 36 | //! use syn::synom::Synom; |
| 37 | //! use proc_macro::{Diagnostic, Span, TokenStream}; |
| 38 | //! |
| 39 | //! /// A basic token parser for parsing a token stream without using Syn's |
| 40 | //! /// parser combinator macros. |
| 41 | //! pub struct Parser<'a> { |
| 42 | //! cursor: Cursor<'a>, |
| 43 | //! } |
| 44 | //! |
| 45 | //! impl<'a> Parser<'a> { |
| 46 | //! pub fn new(cursor: Cursor<'a>) -> Self { |
| 47 | //! Parser { cursor } |
| 48 | //! } |
| 49 | //! |
| 50 | //! pub fn current_span(&self) -> Span { |
| 51 | //! self.cursor.span().unstable() |
| 52 | //! } |
| 53 | //! |
| 54 | //! pub fn parse<T: Synom>(&mut self) -> Result<T, Diagnostic> { |
| 55 | //! let (val, rest) = T::parse(self.cursor) |
| 56 | //! .map_err(|e| match T::description() { |
| 57 | //! Some(desc) => { |
| 58 | //! self.current_span().error(format!("{}: expected {}", e, desc)) |
| 59 | //! } |
| 60 | //! None => { |
| 61 | //! self.current_span().error(e.to_string()) |
| 62 | //! } |
| 63 | //! })?; |
| 64 | //! |
| 65 | //! self.cursor = rest; |
| 66 | //! Ok(val) |
| 67 | //! } |
| 68 | //! |
| 69 | //! pub fn expect_eof(&mut self) -> Result<(), Diagnostic> { |
| 70 | //! if !self.cursor.eof() { |
| 71 | //! return Err(self.current_span().error("trailing characters; expected eof")); |
| 72 | //! } |
| 73 | //! |
| 74 | //! Ok(()) |
| 75 | //! } |
| 76 | //! } |
| 77 | //! |
| 78 | //! fn eval(input: TokenStream) -> Result<TokenStream, Diagnostic> { |
| 79 | //! let buffer = TokenBuffer::new(input); |
| 80 | //! let mut parser = Parser::new(buffer.begin()); |
| 81 | //! |
| 82 | //! // Parse some syntax tree types out of the input tokens. In this case we |
| 83 | //! // expect something like: |
| 84 | //! // |
| 85 | //! // (a, b, c) = (1, 2, 3) |
| 86 | //! let a = parser.parse::<ExprTuple>()?; |
| 87 | //! parser.parse::<token::Eq>()?; |
| 88 | //! let b = parser.parse::<ExprTuple>()?; |
| 89 | //! parser.expect_eof()?; |
| 90 | //! |
| 91 | //! // Perform some validation and report errors. |
| 92 | //! let (a_len, b_len) = (a.elems.len(), b.elems.len()); |
| 93 | //! if a_len != b_len { |
| 94 | //! let diag = b.span().unstable() |
| 95 | //! .error(format!("expected {} element(s), got {}", a_len, b_len)) |
| 96 | //! .span_note(a.span().unstable(), "because of this"); |
| 97 | //! |
| 98 | //! return Err(diag); |
| 99 | //! } |
| 100 | //! |
| 101 | //! // Build the output tokens. |
| 102 | //! let out = quote! { |
| 103 | //! println!("All good! Received two tuples of size {}", #a_len); |
| 104 | //! }; |
| 105 | //! |
| 106 | //! Ok(out.into()) |
| 107 | //! } |
| 108 | //! # |
| 109 | //! # extern crate proc_macro2; |
| 110 | //! # |
| 111 | //! # // This method exists on proc_macro2::Span but is behind the "nightly" |
| 112 | //! # // feature. |
| 113 | //! # trait ToUnstableSpan { |
| 114 | //! # fn unstable(&self) -> Span; |
| 115 | //! # } |
| 116 | //! # |
| 117 | //! # impl ToUnstableSpan for proc_macro2::Span { |
| 118 | //! # fn unstable(&self) -> Span { |
| 119 | //! # unimplemented!() |
| 120 | //! # } |
| 121 | //! # } |
| 122 | //! # |
| 123 | //! # fn main() {} |
| 124 | //! ``` |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 125 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 126 | // This module is heavily commented as it contains the only unsafe code in Syn, |
| 127 | // and caution should be used when editing it. The public-facing interface is |
| 128 | // 100% safe but the implementation is fragile internally. |
| 129 | |
Kartikaya Gupta | 6434beb | 2018-02-22 17:07:00 -0500 | [diff] [blame] | 130 | #[cfg(feature = "proc-macro")] |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 131 | use proc_macro as pm; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 132 | use proc_macro2::{Delimiter, Literal, Span, Ident, TokenStream}; |
| 133 | use proc_macro2::{Group, Punct, TokenTree}; |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 134 | |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 135 | use std::marker::PhantomData; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 136 | use std::ptr; |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 137 | |
David Tolnay | d1ec6ec | 2018-01-03 00:23:45 -0800 | [diff] [blame] | 138 | #[cfg(synom_verbose_trace)] |
David Tolnay | f9e1de1 | 2017-12-31 00:47:01 -0500 | [diff] [blame] | 139 | use std::fmt::{self, Debug}; |
| 140 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 141 | /// Internal type which is used instead of `TokenTree` to represent a token tree |
| 142 | /// within a `TokenBuffer`. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 143 | enum Entry { |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 144 | // Mimicking types from proc-macro. |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 145 | Group(Span, Delimiter, TokenBuffer), |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 146 | Ident(Ident), |
| 147 | Punct(Punct), |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 148 | Literal(Literal), |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 149 | // End entries contain a raw pointer to the entry from the containing |
| 150 | // token tree, or null if this is the outermost level. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 151 | End(*const Entry), |
| 152 | } |
| 153 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 154 | /// A buffer that can be efficiently traversed multiple times, unlike |
| 155 | /// `TokenStream` which requires a deep copy in order to traverse more than |
| 156 | /// once. |
| 157 | /// |
| 158 | /// See the [module documentation] for an example of `TokenBuffer` in action. |
| 159 | /// |
| 160 | /// [module documentation]: index.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 161 | /// |
| 162 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 163 | pub struct TokenBuffer { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 164 | // 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] | 165 | // 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] | 166 | // backing slices won't be moved. |
| 167 | data: Box<[Entry]>, |
| 168 | } |
| 169 | |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 170 | impl TokenBuffer { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 171 | // NOTE: DO NOT MUTATE THE `Vec` RETURNED FROM THIS FUNCTION ONCE IT |
| 172 | // RETURNS, THE ADDRESS OF ITS BACKING MEMORY MUST REMAIN STABLE. |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 173 | fn inner_new(stream: TokenStream, up: *const Entry) -> TokenBuffer { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 174 | // Build up the entries list, recording the locations of any Groups |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 175 | // in the list to be processed later. |
| 176 | let mut entries = Vec::new(); |
| 177 | let mut seqs = Vec::new(); |
David Tolnay | 50fa468 | 2017-12-26 23:17:22 -0500 | [diff] [blame] | 178 | for tt in stream { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 179 | match tt { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 180 | TokenTree::Ident(sym) => { |
| 181 | entries.push(Entry::Ident(sym)); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 182 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 183 | TokenTree::Punct(op) => { |
| 184 | entries.push(Entry::Punct(op)); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 185 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 186 | TokenTree::Literal(l) => { |
| 187 | entries.push(Entry::Literal(l)); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 188 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 189 | TokenTree::Group(g) => { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 190 | // Record the index of the interesting entry, and store an |
| 191 | // `End(null)` there temporarially. |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 192 | seqs.push((entries.len(), g.span(), g.delimiter(), g.stream().clone())); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 193 | entries.push(Entry::End(ptr::null())); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | // Add an `End` entry to the end with a reference to the enclosing token |
| 198 | // stream which was passed in. |
| 199 | entries.push(Entry::End(up)); |
| 200 | |
| 201 | // NOTE: This is done to ensure that we don't accidentally modify the |
| 202 | // length of the backing buffer. The backing buffer must remain at a |
| 203 | // constant address after this point, as we are going to store a raw |
| 204 | // pointer into it. |
| 205 | let mut entries = entries.into_boxed_slice(); |
| 206 | for (idx, span, delim, seq_stream) in seqs { |
| 207 | // We know that this index refers to one of the temporary |
| 208 | // `End(null)` entries, and we know that the last entry is |
| 209 | // `End(up)`, so the next index is also valid. |
| 210 | let seq_up = &entries[idx + 1] as *const Entry; |
| 211 | |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 212 | // The end entry stored at the end of this Entry::Group should |
| 213 | // point to the Entry which follows the Group in the list. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 214 | let inner = Self::inner_new(seq_stream, seq_up); |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 215 | entries[idx] = Entry::Group(span, delim, inner); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 216 | } |
| 217 | |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 218 | TokenBuffer { data: entries } |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 219 | } |
| 220 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 221 | /// Creates a `TokenBuffer` containing all the tokens from the input |
| 222 | /// `TokenStream`. |
hcpl | 4b72a38 | 2018-04-04 14:50:24 +0300 | [diff] [blame] | 223 | /// |
| 224 | /// *This method is available if Syn is built with both the `"parsing"` and |
| 225 | /// `"proc-macro"` features.* |
Kartikaya Gupta | 6434beb | 2018-02-22 17:07:00 -0500 | [diff] [blame] | 226 | #[cfg(feature = "proc-macro")] |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 227 | pub fn new(stream: pm::TokenStream) -> TokenBuffer { |
| 228 | Self::new2(stream.into()) |
| 229 | } |
| 230 | |
| 231 | /// Creates a `TokenBuffer` containing all the tokens from the input |
| 232 | /// `TokenStream`. |
| 233 | pub fn new2(stream: TokenStream) -> TokenBuffer { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 234 | Self::inner_new(stream, ptr::null()) |
| 235 | } |
| 236 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 237 | /// Creates a cursor referencing the first token in the buffer and able to |
| 238 | /// traverse until the end of the buffer. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 239 | pub fn begin(&self) -> Cursor { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 240 | unsafe { Cursor::create(&self.data[0], &self.data[self.data.len() - 1]) } |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 244 | /// A cheaply copyable cursor into a `TokenBuffer`. |
| 245 | /// |
| 246 | /// This cursor holds a shared reference into the immutable data which is used |
| 247 | /// internally to represent a `TokenStream`, and can be efficiently manipulated |
| 248 | /// and copied around. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 249 | /// |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 250 | /// 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] | 251 | /// object and get a cursor to its first token with `begin()`. |
| 252 | /// |
| 253 | /// Two cursors are equal if they have the same location in the same input |
| 254 | /// stream, and have the same scope. |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 255 | /// |
| 256 | /// See the [module documentation] for an example of a `Cursor` in action. |
| 257 | /// |
| 258 | /// [module documentation]: index.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 259 | /// |
| 260 | /// *This type is available if Syn is built with the `"parsing"` feature.* |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 261 | #[derive(Copy, Clone, Eq, PartialEq)] |
| 262 | pub struct Cursor<'a> { |
| 263 | /// The current entry which the `Cursor` is pointing at. |
| 264 | ptr: *const Entry, |
| 265 | /// This is the only `Entry::End(..)` object which this cursor is allowed to |
| 266 | /// point at. All other `End` objects are skipped over in `Cursor::create`. |
| 267 | scope: *const Entry, |
| 268 | /// This uses the &'a reference which guarantees that these pointers are |
| 269 | /// still valid. |
| 270 | marker: PhantomData<&'a Entry>, |
| 271 | } |
| 272 | |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 273 | impl<'a> Cursor<'a> { |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 274 | /// Creates a cursor referencing a static empty TokenStream. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 275 | pub fn empty() -> Self { |
Michael Layzell | 69cf908 | 2017-06-03 12:15:58 -0400 | [diff] [blame] | 276 | // It's safe in this situation for us to put an `Entry` object in global |
| 277 | // storage, despite it not actually being safe to send across threads |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 278 | // (`Ident` is a reference into a thread-local table). This is because |
| 279 | // this entry never includes a `Ident` object. |
Michael Layzell | 69cf908 | 2017-06-03 12:15:58 -0400 | [diff] [blame] | 280 | // |
| 281 | // This wrapper struct allows us to break the rules and put a `Sync` |
| 282 | // object in global storage. |
| 283 | struct UnsafeSyncEntry(Entry); |
| 284 | unsafe impl Sync for UnsafeSyncEntry {} |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 285 | static EMPTY_ENTRY: UnsafeSyncEntry = UnsafeSyncEntry(Entry::End(0 as *const Entry)); |
Michael Layzell | 69cf908 | 2017-06-03 12:15:58 -0400 | [diff] [blame] | 286 | |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 287 | Cursor { |
Michael Layzell | 69cf908 | 2017-06-03 12:15:58 -0400 | [diff] [blame] | 288 | ptr: &EMPTY_ENTRY.0, |
| 289 | scope: &EMPTY_ENTRY.0, |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 290 | marker: PhantomData, |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /// This create method intelligently exits non-explicitly-entered |
| 295 | /// `None`-delimited scopes when the cursor reaches the end of them, |
| 296 | /// allowing for them to be treated transparently. |
| 297 | unsafe fn create(mut ptr: *const Entry, scope: *const Entry) -> Self { |
| 298 | // NOTE: If we're looking at a `End(..)`, we want to advance the cursor |
| 299 | // past it, unless `ptr == scope`, which means that we're at the edge of |
| 300 | // 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] | 301 | // from None-delimited groups entered with `ignore_none`. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 302 | while let Entry::End(exit) = *ptr { |
| 303 | if ptr == scope { |
| 304 | break; |
| 305 | } |
| 306 | ptr = exit; |
| 307 | } |
| 308 | |
| 309 | Cursor { |
| 310 | ptr: ptr, |
| 311 | scope: scope, |
| 312 | marker: PhantomData, |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /// Get the current entry. |
| 317 | fn entry(self) -> &'a Entry { |
| 318 | unsafe { &*self.ptr } |
| 319 | } |
| 320 | |
| 321 | /// Bump the cursor to point at the next token after the current one. This |
| 322 | /// is undefined behavior if the cursor is currently looking at an |
| 323 | /// `Entry::End`. |
| 324 | unsafe fn bump(self) -> Cursor<'a> { |
| 325 | Cursor::create(self.ptr.offset(1), self.scope) |
| 326 | } |
| 327 | |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 328 | /// If the cursor is looking at a `None`-delimited group, move it to look at |
| 329 | /// the first token inside instead. If the group is empty, this will move |
| 330 | /// the cursor past the `None`-delimited group. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 331 | /// |
| 332 | /// WARNING: This mutates its argument. |
| 333 | fn ignore_none(&mut self) { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 334 | if let Entry::Group(_, Delimiter::None, ref buf) = *self.entry() { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 335 | // NOTE: We call `Cursor::create` here to make sure that situations |
| 336 | // where we should immediately exit the span after entering it are |
| 337 | // handled correctly. |
| 338 | unsafe { |
| 339 | *self = Cursor::create(&buf.data[0], self.scope); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 344 | /// Checks whether the cursor is currently pointing at the end of its valid |
| 345 | /// scope. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 346 | #[inline] |
| 347 | pub fn eof(self) -> bool { |
| 348 | // We're at eof if we're at the end of our scope. |
| 349 | self.ptr == self.scope |
| 350 | } |
| 351 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 352 | /// If the cursor is pointing at a `Group` with the given delimiter, returns |
| 353 | /// a cursor into that group and one pointing to the next `TokenTree`. |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 354 | 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] | 355 | // 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] | 356 | // ignore them. We have to make sure to _not_ ignore them when we want |
| 357 | // to enter them, of course. For obvious reasons. |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 358 | if delim != Delimiter::None { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 359 | self.ignore_none(); |
| 360 | } |
| 361 | |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 362 | if let Entry::Group(span, group_delim, ref buf) = *self.entry() { |
| 363 | if group_delim == delim { |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 364 | return Some((buf.begin(), span, unsafe { self.bump() })); |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 365 | } |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 366 | } |
David Tolnay | c10676a | 2017-12-27 23:42:36 -0500 | [diff] [blame] | 367 | |
| 368 | None |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 371 | /// 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] | 372 | /// pointing at the next `TokenTree`. |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 373 | pub fn term(mut self) -> Option<(Ident, Cursor<'a>)> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 374 | self.ignore_none(); |
| 375 | match *self.entry() { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 376 | Entry::Ident(ref term) => Some((term.clone(), unsafe { self.bump() })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 377 | _ => None, |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 381 | /// 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] | 382 | /// pointing at the next `TokenTree`. |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 383 | pub fn op(mut self) -> Option<(Punct, Cursor<'a>)> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 384 | self.ignore_none(); |
| 385 | match *self.entry() { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 386 | Entry::Punct(ref op) => Some((op.clone(), unsafe { self.bump() })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 387 | _ => None, |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 391 | /// If the cursor is pointing at a `Literal`, return it along with a cursor |
| 392 | /// pointing at the next `TokenTree`. |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 393 | pub fn literal(mut self) -> Option<(Literal, Cursor<'a>)> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 394 | self.ignore_none(); |
| 395 | match *self.entry() { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 396 | Entry::Literal(ref lit) => Some((lit.clone(), unsafe { self.bump() })), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 397 | _ => None, |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 401 | /// Copies all remaining tokens visible from this cursor into a |
| 402 | /// `TokenStream`. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 403 | pub fn token_stream(self) -> TokenStream { |
| 404 | let mut tts = Vec::new(); |
| 405 | let mut cursor = self; |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 406 | while let Some((tt, rest)) = cursor.token_tree() { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 407 | tts.push(tt); |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 408 | cursor = rest; |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 409 | } |
| 410 | tts.into_iter().collect() |
| 411 | } |
| 412 | |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 413 | /// If the cursor is pointing at a `TokenTree`, returns it along with a |
| 414 | /// cursor pointing at the next `TokenTree`. |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 415 | /// |
David Tolnay | 7c3e77d | 2018-01-06 17:42:53 -0800 | [diff] [blame] | 416 | /// Returns `None` if the cursor has reached the end of its stream. |
| 417 | /// |
| 418 | /// This method does not treat `None`-delimited groups as transparent, and |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 419 | /// will return a `Group(None, ..)` if the cursor is looking at one. |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 420 | pub fn token_tree(self) -> Option<(TokenTree, Cursor<'a>)> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 421 | let tree = match *self.entry() { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 422 | Entry::Group(span, delim, ref buf) => { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 423 | let stream = buf.begin().token_stream(); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 424 | let mut g = Group::new(delim, stream); |
| 425 | g.set_span(span); |
| 426 | TokenTree::from(g) |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 427 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 428 | Entry::Literal(ref lit) => lit.clone().into(), |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 429 | Entry::Ident(ref term) => term.clone().into(), |
| 430 | Entry::Punct(ref op) => op.clone().into(), |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 431 | Entry::End(..) => { |
| 432 | return None; |
| 433 | } |
| 434 | }; |
| 435 | |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 436 | Some((tree, unsafe { self.bump() })) |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 437 | } |
David Tolnay | 225efa2 | 2017-12-31 16:51:29 -0500 | [diff] [blame] | 438 | |
| 439 | /// Returns the `Span` of the current token, or `Span::call_site()` if this |
| 440 | /// cursor points to eof. |
| 441 | pub fn span(self) -> Span { |
| 442 | match *self.entry() { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 443 | Entry::Group(span, ..) => span, |
| 444 | Entry::Literal(ref l) => l.span(), |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame^] | 445 | Entry::Ident(ref t) => t.span(), |
| 446 | Entry::Punct(ref o) => o.span(), |
David Tolnay | 225efa2 | 2017-12-31 16:51:29 -0500 | [diff] [blame] | 447 | Entry::End(..) => Span::call_site(), |
| 448 | } |
| 449 | } |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // We do a custom implementation for `Debug` as the default implementation is |
| 453 | // pretty useless. |
David Tolnay | d1ec6ec | 2018-01-03 00:23:45 -0800 | [diff] [blame] | 454 | #[cfg(synom_verbose_trace)] |
David Tolnay | f9e1de1 | 2017-12-31 00:47:01 -0500 | [diff] [blame] | 455 | impl<'a> Debug for Cursor<'a> { |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 456 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Nika Layzell | ae81b37 | 2017-12-05 14:12:33 -0500 | [diff] [blame] | 457 | // Print what the cursor is currently looking at. |
| 458 | // This will look like Cursor("some remaining tokens here") |
| 459 | f.debug_tuple("Cursor") |
| 460 | .field(&self.token_stream().to_string()) |
Michael Layzell | 2a60e25 | 2017-05-31 21:36:47 -0400 | [diff] [blame] | 461 | .finish() |
| 462 | } |
| 463 | } |