David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [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 | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 9 | //! Parsing interface for parsing a token stream into a syntax tree node. |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 10 | //! |
David Tolnay | e0c5176 | 2018-08-31 11:05:22 -0700 | [diff] [blame] | 11 | //! Parsing in Syn is built on parser functions that take in a [`ParseStream`] |
| 12 | //! and produce a [`Result<T>`] where `T` is some syntax tree node. Underlying |
| 13 | //! these parser functions is a lower level mechanism built around the |
| 14 | //! [`Cursor`] type. `Cursor` is a cheaply copyable cursor over a range of |
| 15 | //! tokens in a token stream. |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 16 | //! |
David Tolnay | e0c5176 | 2018-08-31 11:05:22 -0700 | [diff] [blame] | 17 | //! [`ParseStream`]: type.ParseStream.html |
| 18 | //! [`Result<T>`]: type.Result.html |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 19 | //! [`Cursor`]: ../buffer/index.html |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 20 | //! |
David Tolnay | e0c5176 | 2018-08-31 11:05:22 -0700 | [diff] [blame] | 21 | //! The `ParseStream`-based interface is convenient for parser implementations, |
| 22 | //! but not necessarily when you just have some tokens that you want to parse. |
| 23 | //! For that we expose the following two entry points. |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 24 | //! |
| 25 | //! ## The `syn::parse*` functions |
| 26 | //! |
| 27 | //! The [`syn::parse`], [`syn::parse2`], and [`syn::parse_str`] functions serve |
| 28 | //! as an entry point for parsing syntax tree nodes that can be parsed in an |
| 29 | //! obvious default way. These functions can return any syntax tree node that |
David Tolnay | 8aacee1 | 2018-08-31 09:15:15 -0700 | [diff] [blame] | 30 | //! implements the [`Parse`] trait, which includes most types in Syn. |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 31 | //! |
| 32 | //! [`syn::parse`]: ../fn.parse.html |
| 33 | //! [`syn::parse2`]: ../fn.parse2.html |
| 34 | //! [`syn::parse_str`]: ../fn.parse_str.html |
David Tolnay | 8aacee1 | 2018-08-31 09:15:15 -0700 | [diff] [blame] | 35 | //! [`Parse`]: trait.Parse.html |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 36 | //! |
| 37 | //! ``` |
| 38 | //! use syn::Type; |
| 39 | //! |
David Tolnay | 8aacee1 | 2018-08-31 09:15:15 -0700 | [diff] [blame] | 40 | //! # fn run_parser() -> Result<(), syn::parse::Error> { |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 41 | //! let t: Type = syn::parse_str("std::collections::HashMap<String, Value>")?; |
| 42 | //! # Ok(()) |
| 43 | //! # } |
| 44 | //! # |
| 45 | //! # fn main() { |
| 46 | //! # run_parser().unwrap(); |
| 47 | //! # } |
| 48 | //! ``` |
| 49 | //! |
| 50 | //! The [`parse_quote!`] macro also uses this approach. |
| 51 | //! |
| 52 | //! [`parse_quote!`]: ../macro.parse_quote.html |
| 53 | //! |
| 54 | //! ## The `Parser` trait |
| 55 | //! |
| 56 | //! Some types can be parsed in several ways depending on context. For example |
| 57 | //! an [`Attribute`] can be either "outer" like `#[...]` or "inner" like |
| 58 | //! `#![...]` and parsing the wrong one would be a bug. Similarly [`Punctuated`] |
| 59 | //! may or may not allow trailing punctuation, and parsing it the wrong way |
| 60 | //! would either reject valid input or accept invalid input. |
| 61 | //! |
| 62 | //! [`Attribute`]: ../struct.Attribute.html |
| 63 | //! [`Punctuated`]: ../punctuated/index.html |
| 64 | //! |
David Tolnay | e0c5176 | 2018-08-31 11:05:22 -0700 | [diff] [blame] | 65 | //! The `Parse` trait is not implemented in these cases because there is no good |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 66 | //! behavior to consider the default. |
| 67 | //! |
| 68 | //! ```ignore |
| 69 | //! // Can't parse `Punctuated` without knowing whether trailing punctuation |
| 70 | //! // should be allowed in this context. |
| 71 | //! let path: Punctuated<PathSegment, Token![::]> = syn::parse(tokens)?; |
| 72 | //! ``` |
| 73 | //! |
| 74 | //! In these cases the types provide a choice of parser functions rather than a |
David Tolnay | e0c5176 | 2018-08-31 11:05:22 -0700 | [diff] [blame] | 75 | //! single `Parse` implementation, and those parser functions can be invoked |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 76 | //! through the [`Parser`] trait. |
| 77 | //! |
| 78 | //! [`Parser`]: trait.Parser.html |
| 79 | //! |
| 80 | //! ``` |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 81 | //! # extern crate syn; |
| 82 | //! # |
| 83 | //! # extern crate proc_macro2; |
| 84 | //! # use proc_macro2::TokenStream; |
| 85 | //! # |
David Tolnay | 3e3f775 | 2018-08-31 09:33:59 -0700 | [diff] [blame] | 86 | //! use syn::parse::Parser; |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 87 | //! use syn::punctuated::Punctuated; |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 88 | //! use syn::{Attribute, Expr, PathSegment, Token}; |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 89 | //! |
David Tolnay | 3e3f775 | 2018-08-31 09:33:59 -0700 | [diff] [blame] | 90 | //! # fn run_parsers() -> Result<(), syn::parse::Error> { |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 91 | //! # let tokens = TokenStream::new().into(); |
| 92 | //! // Parse a nonempty sequence of path segments separated by `::` punctuation |
| 93 | //! // with no trailing punctuation. |
| 94 | //! let parser = Punctuated::<PathSegment, Token![::]>::parse_separated_nonempty; |
| 95 | //! let path = parser.parse(tokens)?; |
| 96 | //! |
| 97 | //! # let tokens = TokenStream::new().into(); |
| 98 | //! // Parse a possibly empty sequence of expressions terminated by commas with |
| 99 | //! // an optional trailing punctuation. |
| 100 | //! let parser = Punctuated::<Expr, Token![,]>::parse_terminated; |
| 101 | //! let args = parser.parse(tokens)?; |
| 102 | //! |
| 103 | //! # let tokens = TokenStream::new().into(); |
| 104 | //! // Parse zero or more outer attributes but not inner attributes. |
David Tolnay | 3e3f775 | 2018-08-31 09:33:59 -0700 | [diff] [blame] | 105 | //! let parser = Attribute::parse_outer; |
| 106 | //! let attrs = parser.parse(tokens)?; |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 107 | //! # |
| 108 | //! # Ok(()) |
| 109 | //! # } |
| 110 | //! # |
| 111 | //! # fn main() {} |
| 112 | //! ``` |
| 113 | //! |
David Tolnay | e0c5176 | 2018-08-31 11:05:22 -0700 | [diff] [blame] | 114 | //! --- |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 115 | //! |
| 116 | //! *This module is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 117 | |
| 118 | use std::cell::Cell; |
| 119 | use std::fmt::Display; |
| 120 | use std::marker::PhantomData; |
| 121 | use std::mem; |
| 122 | use std::ops::Deref; |
David Tolnay | eafc805 | 2018-08-25 16:33:53 -0400 | [diff] [blame] | 123 | use std::rc::Rc; |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 124 | use std::str::FromStr; |
David Tolnay | eafc805 | 2018-08-25 16:33:53 -0400 | [diff] [blame] | 125 | |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 126 | #[cfg(all( |
| 127 | not(all(target_arch = "wasm32", target_os = "unknown")), |
| 128 | feature = "proc-macro" |
| 129 | ))] |
| 130 | use proc_macro; |
David Tolnay | f07b334 | 2018-09-01 11:58:11 -0700 | [diff] [blame] | 131 | use proc_macro2::{self, Delimiter, Group, Literal, Punct, Span, TokenStream, TokenTree}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 132 | |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 133 | use buffer::{Cursor, TokenBuffer}; |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 134 | use error; |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 135 | use lookahead; |
| 136 | use private; |
David Tolnay | 577d033 | 2018-08-25 21:45:24 -0400 | [diff] [blame] | 137 | use punctuated::Punctuated; |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 138 | use token::Token; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 139 | |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 140 | pub use error::{Error, Result}; |
| 141 | pub use lookahead::{Lookahead1, Peek}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 142 | |
| 143 | /// Parsing interface implemented by all types that can be parsed in a default |
| 144 | /// way from a token stream. |
| 145 | pub trait Parse: Sized { |
| 146 | fn parse(input: ParseStream) -> Result<Self>; |
| 147 | } |
| 148 | |
| 149 | /// Input to a Syn parser function. |
David Tolnay | a0daa48 | 2018-09-01 02:09:40 -0700 | [diff] [blame] | 150 | /// |
| 151 | /// See the methods of this type under the documentation of [`ParseBuffer`]. For |
| 152 | /// an overview of parsing in Syn, refer to the [module documentation]. |
| 153 | /// |
| 154 | /// [module documentation]: index.html |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 155 | pub type ParseStream<'a> = &'a ParseBuffer<'a>; |
| 156 | |
| 157 | /// Cursor position within a buffered token stream. |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 158 | pub struct ParseBuffer<'a> { |
| 159 | scope: Span, |
| 160 | cell: Cell<Cursor<'static>>, |
| 161 | marker: PhantomData<Cursor<'a>>, |
David Tolnay | eafc805 | 2018-08-25 16:33:53 -0400 | [diff] [blame] | 162 | unexpected: Rc<Cell<Option<Span>>>, |
| 163 | } |
| 164 | |
| 165 | impl<'a> Drop for ParseBuffer<'a> { |
| 166 | fn drop(&mut self) { |
| 167 | if !self.is_empty() && self.unexpected.get().is_none() { |
| 168 | self.unexpected.set(Some(self.cursor().span())); |
| 169 | } |
| 170 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 171 | } |
| 172 | |
David Tolnay | 642832f | 2018-09-01 13:08:10 -0700 | [diff] [blame] | 173 | /// Cursor state associated with speculative parsing. |
| 174 | /// |
| 175 | /// This type is the input of the closure provided to [`ParseStream::step`]. |
| 176 | /// |
| 177 | /// [`ParseStream::step`]: struct.ParseBuffer.html#method.step |
David Tolnay | 9bd3439 | 2018-09-01 13:19:53 -0700 | [diff] [blame] | 178 | /// |
| 179 | /// # Example |
| 180 | /// |
| 181 | /// ``` |
| 182 | /// # extern crate proc_macro2; |
| 183 | /// # extern crate syn; |
| 184 | /// # |
| 185 | /// use proc_macro2::TokenTree; |
| 186 | /// use syn::parse::{ParseStream, Result}; |
| 187 | /// |
| 188 | /// // This function advances the stream past the next occurrence of `@`. If |
| 189 | /// // no `@` is present in the stream, the stream position is unchanged and |
| 190 | /// // an error is returned. |
| 191 | /// fn skip_past_next_at(input: ParseStream) -> Result<()> { |
| 192 | /// input.step(|cursor| { |
| 193 | /// let mut rest = *cursor; |
| 194 | /// while let Some((tt, next)) = cursor.token_tree() { |
| 195 | /// match tt { |
| 196 | /// TokenTree::Punct(ref punct) if punct.as_char() == '@' => { |
| 197 | /// return Ok(((), next)); |
| 198 | /// } |
| 199 | /// _ => rest = next, |
| 200 | /// } |
| 201 | /// } |
| 202 | /// Err(cursor.error("no `@` was found after this point")) |
| 203 | /// }) |
| 204 | /// } |
| 205 | /// # |
| 206 | /// # fn main() {} |
| 207 | /// ``` |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 208 | #[derive(Copy, Clone)] |
| 209 | pub struct StepCursor<'c, 'a> { |
| 210 | scope: Span, |
| 211 | cursor: Cursor<'c>, |
| 212 | marker: PhantomData<fn(Cursor<'c>) -> Cursor<'a>>, |
| 213 | } |
| 214 | |
| 215 | impl<'c, 'a> Deref for StepCursor<'c, 'a> { |
| 216 | type Target = Cursor<'c>; |
| 217 | |
| 218 | fn deref(&self) -> &Self::Target { |
| 219 | &self.cursor |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | impl<'c, 'a> StepCursor<'c, 'a> { |
David Tolnay | 642832f | 2018-09-01 13:08:10 -0700 | [diff] [blame] | 224 | /// Triggers an error at the current position of the parse stream. |
| 225 | /// |
| 226 | /// The `ParseStream::step` invocation will return this same error without |
| 227 | /// advancing the stream state. |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 228 | pub fn error<T: Display>(self, message: T) -> Error { |
| 229 | error::new_at(self.scope, self.cursor, message) |
| 230 | } |
| 231 | } |
| 232 | |
David Tolnay | 6ea3fdc | 2018-09-01 13:30:53 -0700 | [diff] [blame] | 233 | impl private { |
| 234 | pub fn advance_step_cursor<'c, 'a>(proof: StepCursor<'c, 'a>, to: Cursor<'c>) -> Cursor<'a> { |
| 235 | let _ = proof; |
| 236 | unsafe { mem::transmute::<Cursor<'c>, Cursor<'a>>(to) } |
| 237 | } |
| 238 | } |
| 239 | |
David Tolnay | 66cb0c4 | 2018-08-31 09:01:30 -0700 | [diff] [blame] | 240 | fn skip(input: ParseStream) -> bool { |
David Tolnay | 4ac232d | 2018-08-31 10:18:03 -0700 | [diff] [blame] | 241 | input |
| 242 | .step(|cursor| { |
| 243 | if let Some((_lifetime, rest)) = cursor.lifetime() { |
| 244 | Ok((true, rest)) |
| 245 | } else if let Some((_token, rest)) = cursor.token_tree() { |
| 246 | Ok((true, rest)) |
| 247 | } else { |
| 248 | Ok((false, *cursor)) |
| 249 | } |
| 250 | }).unwrap() |
David Tolnay | 66cb0c4 | 2018-08-31 09:01:30 -0700 | [diff] [blame] | 251 | } |
| 252 | |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 253 | impl private { |
David Tolnay | 70f30e9 | 2018-09-01 02:04:17 -0700 | [diff] [blame] | 254 | pub fn new_parse_buffer( |
| 255 | scope: Span, |
| 256 | cursor: Cursor, |
| 257 | unexpected: Rc<Cell<Option<Span>>>, |
| 258 | ) -> ParseBuffer { |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 259 | let extend = unsafe { mem::transmute::<Cursor, Cursor<'static>>(cursor) }; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 260 | ParseBuffer { |
| 261 | scope: scope, |
| 262 | cell: Cell::new(extend), |
| 263 | marker: PhantomData, |
David Tolnay | eafc805 | 2018-08-25 16:33:53 -0400 | [diff] [blame] | 264 | unexpected: unexpected, |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 268 | pub fn get_unexpected(buffer: &ParseBuffer) -> Rc<Cell<Option<Span>>> { |
| 269 | buffer.unexpected.clone() |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | impl<'a> ParseBuffer<'a> { |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 274 | /// Parses a syntax tree node of type `T`, advancing the position of our |
| 275 | /// parse stream past it. |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 276 | pub fn parse<T: Parse>(&self) -> Result<T> { |
| 277 | T::parse(self) |
| 278 | } |
| 279 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 280 | /// Calls the given parser function to parse a syntax tree node of type `T` |
| 281 | /// from this stream. |
David Tolnay | 3a515a0 | 2018-08-25 21:08:27 -0400 | [diff] [blame] | 282 | pub fn call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T> { |
| 283 | function(self) |
| 284 | } |
| 285 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 286 | /// Looks at the next token in the parse stream to determine whether it |
| 287 | /// matches the requested type of token. |
| 288 | /// |
| 289 | /// Does not advance the position of the parse stream. |
David Tolnay | b77c8b6 | 2018-08-25 16:39:41 -0400 | [diff] [blame] | 290 | pub fn peek<T: Peek>(&self, token: T) -> bool { |
David Tolnay | 576779a | 2018-09-01 11:54:12 -0700 | [diff] [blame] | 291 | let _ = token; |
| 292 | T::Token::peek(self.cursor()) |
David Tolnay | b77c8b6 | 2018-08-25 16:39:41 -0400 | [diff] [blame] | 293 | } |
| 294 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 295 | /// Looks at the second-next token in the parse stream. |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 296 | pub fn peek2<T: Peek>(&self, token: T) -> bool { |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 297 | let ahead = self.fork(); |
David Tolnay | 66cb0c4 | 2018-08-31 09:01:30 -0700 | [diff] [blame] | 298 | skip(&ahead) && ahead.peek(token) |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 299 | } |
| 300 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 301 | /// Looks at the third-next token in the parse stream. |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 302 | pub fn peek3<T: Peek>(&self, token: T) -> bool { |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 303 | let ahead = self.fork(); |
David Tolnay | 66cb0c4 | 2018-08-31 09:01:30 -0700 | [diff] [blame] | 304 | skip(&ahead) && skip(&ahead) && ahead.peek(token) |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 305 | } |
| 306 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 307 | /// Parses zero or more occurrences of `T` separated by punctuation of type |
| 308 | /// `P`, with optional trailing punctuation. |
| 309 | /// |
| 310 | /// Parsing continues until the end of this parse stream. The entire content |
| 311 | /// of this parse stream must consist of `T` and `P`. |
David Tolnay | 577d033 | 2018-08-25 21:45:24 -0400 | [diff] [blame] | 312 | pub fn parse_terminated<T, P: Parse>( |
| 313 | &self, |
| 314 | parser: fn(ParseStream) -> Result<T>, |
| 315 | ) -> Result<Punctuated<T, P>> { |
David Tolnay | d0f8021 | 2018-08-30 18:32:14 -0700 | [diff] [blame] | 316 | Punctuated::parse_terminated_with(self, parser) |
David Tolnay | 577d033 | 2018-08-25 21:45:24 -0400 | [diff] [blame] | 317 | } |
| 318 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 319 | /// Returns whether there are tokens remaining in this stream. |
| 320 | /// |
| 321 | /// This method returns true at the end of the content of a set of |
| 322 | /// delimiters, as well as at the very end of the complete macro input. |
David Tolnay | f5d3045 | 2018-09-01 02:29:04 -0700 | [diff] [blame] | 323 | pub fn is_empty(&self) -> bool { |
| 324 | self.cursor().eof() |
| 325 | } |
| 326 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 327 | /// Constructs a helper for peeking at the next token in this stream and |
| 328 | /// building an error message if it is not one of a set of expected tokens. |
David Tolnay | f5d3045 | 2018-09-01 02:29:04 -0700 | [diff] [blame] | 329 | pub fn lookahead1(&self) -> Lookahead1<'a> { |
| 330 | lookahead::new(self.scope, self.cursor()) |
| 331 | } |
| 332 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 333 | /// Forks a parse stream so that parsing tokens out of either the original |
| 334 | /// or the fork does not advance the position of the other. |
| 335 | /// |
| 336 | /// # Performance |
| 337 | /// |
| 338 | /// Forking a parse stream is a cheap fixed amount of work and does not |
| 339 | /// involve copying token buffers. Where you might hit performance problems |
| 340 | /// is if your macro ends up parsing a large amount of content more than |
| 341 | /// once. |
| 342 | /// |
| 343 | /// ``` |
| 344 | /// # use syn::Expr; |
| 345 | /// # use syn::parse::{ParseStream, Result}; |
| 346 | /// # |
| 347 | /// # fn bad(input: ParseStream) -> Result<Expr> { |
| 348 | /// // Do not do this. |
| 349 | /// if input.fork().parse::<Expr>().is_ok() { |
| 350 | /// return input.parse::<Expr>(); |
| 351 | /// } |
| 352 | /// # unimplemented!() |
| 353 | /// # } |
| 354 | /// ``` |
| 355 | /// |
| 356 | /// As a rule, avoid parsing an unbounded amount of tokens out of a forked |
| 357 | /// parse stream. Only use a fork when the amount of work performed against |
| 358 | /// the fork is small and bounded. |
| 359 | /// |
| 360 | /// For a lower level but generally more performant way to perform |
| 361 | /// speculative parsing, consider using [`ParseStream::step`] instead. |
| 362 | /// |
| 363 | /// [`ParseStream::step`]: #method.step |
David Tolnay | b77c8b6 | 2018-08-25 16:39:41 -0400 | [diff] [blame] | 364 | pub fn fork(&self) -> Self { |
David Tolnay | 6456a9d | 2018-08-26 08:11:18 -0400 | [diff] [blame] | 365 | ParseBuffer { |
| 366 | scope: self.scope, |
| 367 | cell: self.cell.clone(), |
| 368 | marker: PhantomData, |
| 369 | // Not the parent's unexpected. Nothing cares whether the clone |
| 370 | // parses all the way. |
| 371 | unexpected: Rc::new(Cell::new(None)), |
| 372 | } |
David Tolnay | b77c8b6 | 2018-08-25 16:39:41 -0400 | [diff] [blame] | 373 | } |
| 374 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 375 | /// Triggers an error at the current position of the parse stream. |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 376 | pub fn error<T: Display>(&self, message: T) -> Error { |
| 377 | error::new_at(self.scope, self.cursor(), message) |
| 378 | } |
| 379 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 380 | /// Speculatively parses tokens from this parse stream, advancing the |
| 381 | /// position of this stream only if parsing succeeds. |
David Tolnay | 9bd3439 | 2018-09-01 13:19:53 -0700 | [diff] [blame] | 382 | /// |
David Tolnay | ad1d1d2 | 2018-09-01 13:34:43 -0700 | [diff] [blame] | 383 | /// This is a powerful low-level API used for defining the `Parse` impls of |
| 384 | /// the basic built-in token types. It is not something that will be used |
| 385 | /// widely outside of the Syn codebase. |
| 386 | /// |
David Tolnay | 9bd3439 | 2018-09-01 13:19:53 -0700 | [diff] [blame] | 387 | /// # Example |
| 388 | /// |
| 389 | /// ``` |
| 390 | /// # extern crate proc_macro2; |
| 391 | /// # extern crate syn; |
| 392 | /// # |
| 393 | /// use proc_macro2::TokenTree; |
| 394 | /// use syn::parse::{ParseStream, Result}; |
| 395 | /// |
| 396 | /// // This function advances the stream past the next occurrence of `@`. If |
| 397 | /// // no `@` is present in the stream, the stream position is unchanged and |
| 398 | /// // an error is returned. |
| 399 | /// fn skip_past_next_at(input: ParseStream) -> Result<()> { |
| 400 | /// input.step(|cursor| { |
| 401 | /// let mut rest = *cursor; |
| 402 | /// while let Some((tt, next)) = cursor.token_tree() { |
| 403 | /// match tt { |
| 404 | /// TokenTree::Punct(ref punct) if punct.as_char() == '@' => { |
| 405 | /// return Ok(((), next)); |
| 406 | /// } |
| 407 | /// _ => rest = next, |
| 408 | /// } |
| 409 | /// } |
| 410 | /// Err(cursor.error("no `@` was found after this point")) |
| 411 | /// }) |
| 412 | /// } |
| 413 | /// # |
| 414 | /// # fn main() {} |
| 415 | /// ``` |
David Tolnay | b50c65a | 2018-08-30 21:14:57 -0700 | [diff] [blame] | 416 | pub fn step<F, R>(&self, function: F) -> Result<R> |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 417 | where |
| 418 | F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>, |
| 419 | { |
David Tolnay | 6b65f85 | 2018-09-01 11:56:25 -0700 | [diff] [blame] | 420 | let (node, rest) = function(StepCursor { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 421 | scope: self.scope, |
| 422 | cursor: self.cell.get(), |
| 423 | marker: PhantomData, |
David Tolnay | 6b65f85 | 2018-09-01 11:56:25 -0700 | [diff] [blame] | 424 | })?; |
| 425 | self.cell.set(rest); |
| 426 | Ok(node) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 427 | } |
David Tolnay | eafc805 | 2018-08-25 16:33:53 -0400 | [diff] [blame] | 428 | |
David Tolnay | 725e1c6 | 2018-09-01 12:07:25 -0700 | [diff] [blame] | 429 | /// Provides low-level access to the token representation underlying this |
| 430 | /// parse stream. |
| 431 | /// |
| 432 | /// Cursors are immutable so no operations you perform against the cursor |
| 433 | /// will affect the state of this parse stream. |
David Tolnay | f5d3045 | 2018-09-01 02:29:04 -0700 | [diff] [blame] | 434 | pub fn cursor(&self) -> Cursor<'a> { |
| 435 | self.cell.get() |
| 436 | } |
| 437 | |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 438 | fn check_unexpected(&self) -> Result<()> { |
David Tolnay | eafc805 | 2018-08-25 16:33:53 -0400 | [diff] [blame] | 439 | match self.unexpected.get() { |
| 440 | Some(span) => Err(Error::new(span, "unexpected token")), |
| 441 | None => Ok(()), |
| 442 | } |
| 443 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 444 | } |
| 445 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 446 | impl<T: Parse> Parse for Box<T> { |
| 447 | fn parse(input: ParseStream) -> Result<Self> { |
| 448 | input.parse().map(Box::new) |
| 449 | } |
| 450 | } |
| 451 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 452 | impl<T: Parse + Token> Parse for Option<T> { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 453 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 454 | if T::peek(input.cursor()) { |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 455 | Ok(Some(input.parse()?)) |
| 456 | } else { |
| 457 | Ok(None) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 458 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 459 | } |
| 460 | } |
David Tolnay | 4ac232d | 2018-08-31 10:18:03 -0700 | [diff] [blame] | 461 | |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 462 | impl Parse for TokenStream { |
| 463 | fn parse(input: ParseStream) -> Result<Self> { |
| 464 | input.step(|cursor| Ok((cursor.token_stream(), Cursor::empty()))) |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | impl Parse for TokenTree { |
| 469 | fn parse(input: ParseStream) -> Result<Self> { |
| 470 | input.step(|cursor| match cursor.token_tree() { |
| 471 | Some((tt, rest)) => Ok((tt, rest)), |
| 472 | None => Err(cursor.error("expected token tree")), |
| 473 | }) |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | impl Parse for Group { |
| 478 | fn parse(input: ParseStream) -> Result<Self> { |
| 479 | input.step(|cursor| { |
| 480 | for delim in &[Delimiter::Parenthesis, Delimiter::Brace, Delimiter::Bracket] { |
| 481 | if let Some((inside, span, rest)) = cursor.group(*delim) { |
| 482 | let mut group = Group::new(*delim, inside.token_stream()); |
| 483 | group.set_span(span); |
| 484 | return Ok((group, rest)); |
| 485 | } |
| 486 | } |
| 487 | Err(cursor.error("expected group token")) |
| 488 | }) |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | impl Parse for Punct { |
| 493 | fn parse(input: ParseStream) -> Result<Self> { |
| 494 | input.step(|cursor| match cursor.punct() { |
| 495 | Some((punct, rest)) => Ok((punct, rest)), |
| 496 | None => Err(cursor.error("expected punctuation token")), |
| 497 | }) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | impl Parse for Literal { |
| 502 | fn parse(input: ParseStream) -> Result<Self> { |
| 503 | input.step(|cursor| match cursor.literal() { |
| 504 | Some((literal, rest)) => Ok((literal, rest)), |
| 505 | None => Err(cursor.error("expected literal token")), |
| 506 | }) |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /// Parser that can parse Rust tokens into a particular syntax tree node. |
| 511 | /// |
| 512 | /// Refer to the [module documentation] for details about parsing in Syn. |
| 513 | /// |
| 514 | /// [module documentation]: index.html |
| 515 | /// |
| 516 | /// *This trait is available if Syn is built with the `"parsing"` feature.* |
| 517 | pub trait Parser: Sized { |
| 518 | type Output; |
| 519 | |
| 520 | /// Parse a proc-macro2 token stream into the chosen syntax tree node. |
| 521 | fn parse2(self, tokens: TokenStream) -> Result<Self::Output>; |
| 522 | |
| 523 | /// Parse tokens of source code into the chosen syntax tree node. |
| 524 | /// |
| 525 | /// *This method is available if Syn is built with both the `"parsing"` and |
| 526 | /// `"proc-macro"` features.* |
| 527 | #[cfg(all( |
| 528 | not(all(target_arch = "wasm32", target_os = "unknown")), |
| 529 | feature = "proc-macro" |
| 530 | ))] |
| 531 | fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output> { |
| 532 | self.parse2(proc_macro2::TokenStream::from(tokens)) |
| 533 | } |
| 534 | |
| 535 | /// Parse a string of Rust code into the chosen syntax tree node. |
| 536 | /// |
| 537 | /// # Hygiene |
| 538 | /// |
| 539 | /// Every span in the resulting syntax tree will be set to resolve at the |
| 540 | /// macro call site. |
| 541 | fn parse_str(self, s: &str) -> Result<Self::Output> { |
| 542 | self.parse2(proc_macro2::TokenStream::from_str(s)?) |
| 543 | } |
| 544 | } |
| 545 | |
David Tolnay | 7b07aa1 | 2018-09-01 11:41:12 -0700 | [diff] [blame] | 546 | fn tokens_to_parse_buffer(tokens: &TokenBuffer) -> ParseBuffer { |
| 547 | let scope = Span::call_site(); |
| 548 | let cursor = tokens.begin(); |
| 549 | let unexpected = Rc::new(Cell::new(None)); |
| 550 | private::new_parse_buffer(scope, cursor, unexpected) |
| 551 | } |
| 552 | |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 553 | impl<F, T> Parser for F |
| 554 | where |
| 555 | F: FnOnce(ParseStream) -> Result<T>, |
| 556 | { |
| 557 | type Output = T; |
| 558 | |
| 559 | fn parse2(self, tokens: TokenStream) -> Result<T> { |
| 560 | let buf = TokenBuffer::new2(tokens); |
David Tolnay | 7b07aa1 | 2018-09-01 11:41:12 -0700 | [diff] [blame] | 561 | let state = tokens_to_parse_buffer(&buf); |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 562 | let node = self(&state)?; |
| 563 | state.check_unexpected()?; |
| 564 | if state.is_empty() { |
| 565 | Ok(node) |
| 566 | } else { |
| 567 | Err(state.error("unexpected token")) |
| 568 | } |
| 569 | } |
| 570 | } |