blob: 17a9d3c752ee043e47729a7d4a9baab8cd676302 [file] [log] [blame]
David Tolnay80a914f2018-08-30 23:49:53 -07001// 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 Tolnay18c754c2018-08-21 23:26:58 -04009//! Parsing interface for parsing a token stream into a syntax tree node.
David Tolnay80a914f2018-08-30 23:49:53 -070010//!
David Tolnaye0c51762018-08-31 11:05:22 -070011//! 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 Tolnay80a914f2018-08-30 23:49:53 -070016//!
David Tolnaye0c51762018-08-31 11:05:22 -070017//! [`ParseStream`]: type.ParseStream.html
18//! [`Result<T>`]: type.Result.html
David Tolnay80a914f2018-08-30 23:49:53 -070019//! [`Cursor`]: ../buffer/index.html
David Tolnay80a914f2018-08-30 23:49:53 -070020//!
David Tolnay43984452018-09-01 17:43:56 -070021//! # Example
David Tolnay80a914f2018-08-30 23:49:53 -070022//!
David Tolnay43984452018-09-01 17:43:56 -070023//! Here is a snippet of parsing code to get a feel for the style of the
24//! library. We define data structures for a subset of Rust syntax including
25//! enums (not shown) and structs, then provide implementations of the [`Parse`]
26//! trait to parse these syntax tree data structures from a token stream.
27//!
David Tolnay88d9f622018-09-01 17:52:33 -070028//! Once `Parse` impls have been defined, they can be called conveniently from a
29//! procedural macro as shown at the bottom of the snippet. If the caller
30//! provides syntactically invalid input to the procedural macro, they will
31//! receive a helpful compiler error message pointing out the exact token that
32//! triggered the failure to parse.
33//!
David Tolnay43984452018-09-01 17:43:56 -070034//! ```
David Tolnay88d9f622018-09-01 17:52:33 -070035//! # extern crate proc_macro;
David Tolnay43984452018-09-01 17:43:56 -070036//! # extern crate syn;
37//! #
David Tolnay88d9f622018-09-01 17:52:33 -070038//! use proc_macro::TokenStream;
39//! use syn::{braced, parse_macro_input, token, Field, Ident, Token};
David Tolnay43984452018-09-01 17:43:56 -070040//! use syn::parse::{Parse, ParseStream, Result};
41//! use syn::punctuated::Punctuated;
42//!
43//! enum Item {
44//! Struct(ItemStruct),
45//! Enum(ItemEnum),
46//! }
47//!
48//! struct ItemStruct {
49//! struct_token: Token![struct],
50//! ident: Ident,
51//! brace_token: token::Brace,
52//! fields: Punctuated<Field, Token![,]>,
53//! }
54//! #
55//! # enum ItemEnum {}
56//!
57//! impl Parse for Item {
58//! fn parse(input: ParseStream) -> Result<Self> {
59//! let lookahead = input.lookahead1();
60//! if lookahead.peek(Token![struct]) {
61//! input.parse().map(Item::Struct)
62//! } else if lookahead.peek(Token![enum]) {
63//! input.parse().map(Item::Enum)
64//! } else {
65//! Err(lookahead.error())
66//! }
67//! }
68//! }
69//!
70//! impl Parse for ItemStruct {
71//! fn parse(input: ParseStream) -> Result<Self> {
72//! let content;
73//! Ok(ItemStruct {
74//! struct_token: input.parse()?,
75//! ident: input.parse()?,
76//! brace_token: braced!(content in input),
77//! fields: content.parse_terminated(Field::parse_named)?,
78//! })
79//! }
80//! }
81//! #
82//! # impl Parse for ItemEnum {
83//! # fn parse(input: ParseStream) -> Result<Self> {
84//! # unimplemented!()
85//! # }
86//! # }
David Tolnay88d9f622018-09-01 17:52:33 -070087//!
88//! # const IGNORE: &str = stringify! {
89//! #[proc_macro]
90//! # };
91//! pub fn my_macro(tokens: TokenStream) -> TokenStream {
92//! let input = parse_macro_input!(tokens as Item);
93//!
94//! /* ... */
95//! # "".parse().unwrap()
96//! }
97//! #
98//! # fn main() {}
David Tolnay43984452018-09-01 17:43:56 -070099//! ```
100//!
101//! # The `syn::parse*` functions
David Tolnay80a914f2018-08-30 23:49:53 -0700102//!
103//! The [`syn::parse`], [`syn::parse2`], and [`syn::parse_str`] functions serve
104//! as an entry point for parsing syntax tree nodes that can be parsed in an
105//! obvious default way. These functions can return any syntax tree node that
David Tolnay8aacee12018-08-31 09:15:15 -0700106//! implements the [`Parse`] trait, which includes most types in Syn.
David Tolnay80a914f2018-08-30 23:49:53 -0700107//!
108//! [`syn::parse`]: ../fn.parse.html
109//! [`syn::parse2`]: ../fn.parse2.html
110//! [`syn::parse_str`]: ../fn.parse_str.html
David Tolnay8aacee12018-08-31 09:15:15 -0700111//! [`Parse`]: trait.Parse.html
David Tolnay80a914f2018-08-30 23:49:53 -0700112//!
113//! ```
114//! use syn::Type;
115//!
David Tolnay8aacee12018-08-31 09:15:15 -0700116//! # fn run_parser() -> Result<(), syn::parse::Error> {
David Tolnay80a914f2018-08-30 23:49:53 -0700117//! let t: Type = syn::parse_str("std::collections::HashMap<String, Value>")?;
118//! # Ok(())
119//! # }
120//! #
121//! # fn main() {
122//! # run_parser().unwrap();
123//! # }
124//! ```
125//!
126//! The [`parse_quote!`] macro also uses this approach.
127//!
128//! [`parse_quote!`]: ../macro.parse_quote.html
129//!
David Tolnay43984452018-09-01 17:43:56 -0700130//! # The `Parser` trait
David Tolnay80a914f2018-08-30 23:49:53 -0700131//!
132//! Some types can be parsed in several ways depending on context. For example
133//! an [`Attribute`] can be either "outer" like `#[...]` or "inner" like
134//! `#![...]` and parsing the wrong one would be a bug. Similarly [`Punctuated`]
135//! may or may not allow trailing punctuation, and parsing it the wrong way
136//! would either reject valid input or accept invalid input.
137//!
138//! [`Attribute`]: ../struct.Attribute.html
139//! [`Punctuated`]: ../punctuated/index.html
140//!
David Tolnaye0c51762018-08-31 11:05:22 -0700141//! The `Parse` trait is not implemented in these cases because there is no good
David Tolnay80a914f2018-08-30 23:49:53 -0700142//! behavior to consider the default.
143//!
144//! ```ignore
145//! // Can't parse `Punctuated` without knowing whether trailing punctuation
146//! // should be allowed in this context.
147//! let path: Punctuated<PathSegment, Token![::]> = syn::parse(tokens)?;
148//! ```
149//!
150//! In these cases the types provide a choice of parser functions rather than a
David Tolnaye0c51762018-08-31 11:05:22 -0700151//! single `Parse` implementation, and those parser functions can be invoked
David Tolnay80a914f2018-08-30 23:49:53 -0700152//! through the [`Parser`] trait.
153//!
154//! [`Parser`]: trait.Parser.html
155//!
156//! ```
David Tolnay80a914f2018-08-30 23:49:53 -0700157//! # extern crate syn;
158//! #
159//! # extern crate proc_macro2;
160//! # use proc_macro2::TokenStream;
161//! #
David Tolnay3e3f7752018-08-31 09:33:59 -0700162//! use syn::parse::Parser;
David Tolnay80a914f2018-08-30 23:49:53 -0700163//! use syn::punctuated::Punctuated;
David Tolnay9b00f652018-09-01 10:31:02 -0700164//! use syn::{Attribute, Expr, PathSegment, Token};
David Tolnay80a914f2018-08-30 23:49:53 -0700165//!
David Tolnay3e3f7752018-08-31 09:33:59 -0700166//! # fn run_parsers() -> Result<(), syn::parse::Error> {
David Tolnay80a914f2018-08-30 23:49:53 -0700167//! # let tokens = TokenStream::new().into();
168//! // Parse a nonempty sequence of path segments separated by `::` punctuation
169//! // with no trailing punctuation.
170//! let parser = Punctuated::<PathSegment, Token![::]>::parse_separated_nonempty;
171//! let path = parser.parse(tokens)?;
172//!
173//! # let tokens = TokenStream::new().into();
174//! // Parse a possibly empty sequence of expressions terminated by commas with
175//! // an optional trailing punctuation.
176//! let parser = Punctuated::<Expr, Token![,]>::parse_terminated;
177//! let args = parser.parse(tokens)?;
178//!
179//! # let tokens = TokenStream::new().into();
180//! // Parse zero or more outer attributes but not inner attributes.
David Tolnay3e3f7752018-08-31 09:33:59 -0700181//! let parser = Attribute::parse_outer;
182//! let attrs = parser.parse(tokens)?;
David Tolnay80a914f2018-08-30 23:49:53 -0700183//! #
184//! # Ok(())
185//! # }
186//! #
187//! # fn main() {}
188//! ```
189//!
David Tolnaye0c51762018-08-31 11:05:22 -0700190//! ---
David Tolnay80a914f2018-08-30 23:49:53 -0700191//!
192//! *This module is available if Syn is built with the `"parsing"` feature.*
David Tolnay18c754c2018-08-21 23:26:58 -0400193
194use std::cell::Cell;
195use std::fmt::Display;
196use std::marker::PhantomData;
197use std::mem;
198use std::ops::Deref;
David Tolnayeafc8052018-08-25 16:33:53 -0400199use std::rc::Rc;
David Tolnay80a914f2018-08-30 23:49:53 -0700200use std::str::FromStr;
David Tolnayeafc8052018-08-25 16:33:53 -0400201
David Tolnay80a914f2018-08-30 23:49:53 -0700202#[cfg(all(
203 not(all(target_arch = "wasm32", target_os = "unknown")),
204 feature = "proc-macro"
205))]
206use proc_macro;
David Tolnayf07b3342018-09-01 11:58:11 -0700207use proc_macro2::{self, Delimiter, Group, Literal, Punct, Span, TokenStream, TokenTree};
David Tolnay18c754c2018-08-21 23:26:58 -0400208
David Tolnay80a914f2018-08-30 23:49:53 -0700209use buffer::{Cursor, TokenBuffer};
David Tolnayb6254182018-08-25 08:44:54 -0400210use error;
David Tolnay94f06632018-08-31 10:17:17 -0700211use lookahead;
212use private;
David Tolnay577d0332018-08-25 21:45:24 -0400213use punctuated::Punctuated;
David Tolnay4fb71232018-08-25 23:14:50 -0400214use token::Token;
David Tolnay18c754c2018-08-21 23:26:58 -0400215
David Tolnayb6254182018-08-25 08:44:54 -0400216pub use error::{Error, Result};
217pub use lookahead::{Lookahead1, Peek};
David Tolnay18c754c2018-08-21 23:26:58 -0400218
219/// Parsing interface implemented by all types that can be parsed in a default
220/// way from a token stream.
221pub trait Parse: Sized {
222 fn parse(input: ParseStream) -> Result<Self>;
223}
224
225/// Input to a Syn parser function.
David Tolnaya0daa482018-09-01 02:09:40 -0700226///
227/// See the methods of this type under the documentation of [`ParseBuffer`]. For
228/// an overview of parsing in Syn, refer to the [module documentation].
229///
230/// [module documentation]: index.html
David Tolnay18c754c2018-08-21 23:26:58 -0400231pub type ParseStream<'a> = &'a ParseBuffer<'a>;
232
233/// Cursor position within a buffered token stream.
David Tolnay20d29a12018-09-01 15:15:33 -0700234///
235/// This type is more commonly used through the type alias [`ParseStream`] which
236/// is an alias for `&ParseBuffer`.
237///
238/// `ParseStream` is the input type for all parser functions in Syn. They have
239/// the signature `fn(ParseStream) -> Result<T>`.
David Tolnay18c754c2018-08-21 23:26:58 -0400240pub struct ParseBuffer<'a> {
241 scope: Span,
David Tolnay5d7f2252018-09-02 08:21:40 -0700242 // Instead of Cell<Cursor<'a>> so that ParseBuffer<'a> is covariant in 'a.
243 // The rest of the code in this module needs to be careful that only a
244 // cursor derived from this `cell` is ever assigned to this `cell`.
245 //
246 // Cell<Cursor<'a>> cannot be covariant in 'a because then we could take a
247 // ParseBuffer<'a>, upcast to ParseBuffer<'short> for some lifetime shorter
248 // than 'a, and then assign a Cursor<'short> into the Cell.
249 //
250 // By extension, it would not be safe to expose an API that accepts a
251 // Cursor<'a> and trusts that it lives as long as the cursor currently in
252 // the cell.
David Tolnay18c754c2018-08-21 23:26:58 -0400253 cell: Cell<Cursor<'static>>,
254 marker: PhantomData<Cursor<'a>>,
David Tolnayeafc8052018-08-25 16:33:53 -0400255 unexpected: Rc<Cell<Option<Span>>>,
256}
257
258impl<'a> Drop for ParseBuffer<'a> {
259 fn drop(&mut self) {
260 if !self.is_empty() && self.unexpected.get().is_none() {
261 self.unexpected.set(Some(self.cursor().span()));
262 }
263 }
David Tolnay18c754c2018-08-21 23:26:58 -0400264}
265
David Tolnay642832f2018-09-01 13:08:10 -0700266/// Cursor state associated with speculative parsing.
267///
268/// This type is the input of the closure provided to [`ParseStream::step`].
269///
270/// [`ParseStream::step`]: struct.ParseBuffer.html#method.step
David Tolnay9bd34392018-09-01 13:19:53 -0700271///
272/// # Example
273///
274/// ```
275/// # extern crate proc_macro2;
276/// # extern crate syn;
277/// #
278/// use proc_macro2::TokenTree;
279/// use syn::parse::{ParseStream, Result};
280///
281/// // This function advances the stream past the next occurrence of `@`. If
282/// // no `@` is present in the stream, the stream position is unchanged and
283/// // an error is returned.
284/// fn skip_past_next_at(input: ParseStream) -> Result<()> {
285/// input.step(|cursor| {
286/// let mut rest = *cursor;
287/// while let Some((tt, next)) = cursor.token_tree() {
288/// match tt {
289/// TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
290/// return Ok(((), next));
291/// }
292/// _ => rest = next,
293/// }
294/// }
295/// Err(cursor.error("no `@` was found after this point"))
296/// })
297/// }
298/// #
299/// # fn main() {}
300/// ```
David Tolnay18c754c2018-08-21 23:26:58 -0400301#[derive(Copy, Clone)]
302pub struct StepCursor<'c, 'a> {
303 scope: Span,
David Tolnay56924f42018-09-02 08:24:58 -0700304 // This field is covariant in 'c.
David Tolnay18c754c2018-08-21 23:26:58 -0400305 cursor: Cursor<'c>,
David Tolnay56924f42018-09-02 08:24:58 -0700306 // This field is contravariant in 'c. Together these make StepCursor
307 // invariant in 'c. Also covariant in 'a. The user cannot cast 'c to a
308 // different lifetime but can upcast into a StepCursor with a shorter
309 // lifetime 'a.
310 //
311 // As long as we only ever construct a StepCursor for which 'c outlives 'a,
312 // this means if ever a StepCursor<'c, 'a> exists we are guaranteed that 'c
313 // outlives 'a.
David Tolnay18c754c2018-08-21 23:26:58 -0400314 marker: PhantomData<fn(Cursor<'c>) -> Cursor<'a>>,
315}
316
317impl<'c, 'a> Deref for StepCursor<'c, 'a> {
318 type Target = Cursor<'c>;
319
320 fn deref(&self) -> &Self::Target {
321 &self.cursor
322 }
323}
324
325impl<'c, 'a> StepCursor<'c, 'a> {
David Tolnay642832f2018-09-01 13:08:10 -0700326 /// Triggers an error at the current position of the parse stream.
327 ///
328 /// The `ParseStream::step` invocation will return this same error without
329 /// advancing the stream state.
David Tolnay18c754c2018-08-21 23:26:58 -0400330 pub fn error<T: Display>(self, message: T) -> Error {
331 error::new_at(self.scope, self.cursor, message)
332 }
333}
334
David Tolnay6ea3fdc2018-09-01 13:30:53 -0700335impl private {
336 pub fn advance_step_cursor<'c, 'a>(proof: StepCursor<'c, 'a>, to: Cursor<'c>) -> Cursor<'a> {
David Tolnay56924f42018-09-02 08:24:58 -0700337 // Refer to the comments within the StepCursor definition. We use the
338 // fact that a StepCursor<'c, 'a> exists as proof that 'c outlives 'a.
339 // Cursor is covariant in its lifetime parameter so we can cast a
340 // Cursor<'c> to one with the shorter lifetime Cursor<'a>.
David Tolnay6ea3fdc2018-09-01 13:30:53 -0700341 let _ = proof;
342 unsafe { mem::transmute::<Cursor<'c>, Cursor<'a>>(to) }
343 }
344}
345
David Tolnay66cb0c42018-08-31 09:01:30 -0700346fn skip(input: ParseStream) -> bool {
David Tolnay4ac232d2018-08-31 10:18:03 -0700347 input
348 .step(|cursor| {
349 if let Some((_lifetime, rest)) = cursor.lifetime() {
350 Ok((true, rest))
351 } else if let Some((_token, rest)) = cursor.token_tree() {
352 Ok((true, rest))
353 } else {
354 Ok((false, *cursor))
355 }
356 }).unwrap()
David Tolnay66cb0c42018-08-31 09:01:30 -0700357}
358
David Tolnay10951d52018-08-31 10:27:39 -0700359impl private {
David Tolnay70f30e92018-09-01 02:04:17 -0700360 pub fn new_parse_buffer(
361 scope: Span,
362 cursor: Cursor,
363 unexpected: Rc<Cell<Option<Span>>>,
364 ) -> ParseBuffer {
David Tolnay18c754c2018-08-21 23:26:58 -0400365 ParseBuffer {
366 scope: scope,
David Tolnay5d7f2252018-09-02 08:21:40 -0700367 // See comment on `cell` in the struct definition.
368 cell: Cell::new(unsafe { mem::transmute::<Cursor, Cursor<'static>>(cursor) }),
David Tolnay18c754c2018-08-21 23:26:58 -0400369 marker: PhantomData,
David Tolnayeafc8052018-08-25 16:33:53 -0400370 unexpected: unexpected,
David Tolnay18c754c2018-08-21 23:26:58 -0400371 }
372 }
373
David Tolnay94f06632018-08-31 10:17:17 -0700374 pub fn get_unexpected(buffer: &ParseBuffer) -> Rc<Cell<Option<Span>>> {
375 buffer.unexpected.clone()
376 }
377}
378
379impl<'a> ParseBuffer<'a> {
David Tolnay725e1c62018-09-01 12:07:25 -0700380 /// Parses a syntax tree node of type `T`, advancing the position of our
381 /// parse stream past it.
David Tolnay18c754c2018-08-21 23:26:58 -0400382 pub fn parse<T: Parse>(&self) -> Result<T> {
383 T::parse(self)
384 }
385
David Tolnay725e1c62018-09-01 12:07:25 -0700386 /// Calls the given parser function to parse a syntax tree node of type `T`
387 /// from this stream.
David Tolnay21ce84c2018-09-01 15:37:51 -0700388 ///
389 /// # Example
390 ///
391 /// The parser below invokes [`Attribute::parse_outer`] to parse a vector of
392 /// zero or more outer attributes.
393 ///
394 /// [`Attribute::parse_outer`]: ../struct.Attribute.html#method.parse_outer
395 ///
396 /// ```
397 /// # extern crate syn;
398 /// #
399 /// use syn::{Attribute, Ident, Token};
400 /// use syn::parse::{Parse, ParseStream, Result};
401 ///
402 /// // Parses a unit struct with attributes.
403 /// //
404 /// // #[path = "s.tmpl"]
405 /// // struct S;
406 /// struct UnitStruct {
407 /// attrs: Vec<Attribute>,
408 /// struct_token: Token![struct],
409 /// name: Ident,
410 /// semi_token: Token![;],
411 /// }
412 ///
413 /// impl Parse for UnitStruct {
414 /// fn parse(input: ParseStream) -> Result<Self> {
415 /// Ok(UnitStruct {
416 /// attrs: input.call(Attribute::parse_outer)?,
417 /// struct_token: input.parse()?,
418 /// name: input.parse()?,
419 /// semi_token: input.parse()?,
420 /// })
421 /// }
422 /// }
423 /// #
424 /// # fn main() {}
425 /// ```
David Tolnay3a515a02018-08-25 21:08:27 -0400426 pub fn call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T> {
427 function(self)
428 }
429
David Tolnay725e1c62018-09-01 12:07:25 -0700430 /// Looks at the next token in the parse stream to determine whether it
431 /// matches the requested type of token.
432 ///
433 /// Does not advance the position of the parse stream.
David Tolnayddebc3e2018-09-01 16:29:20 -0700434 ///
David Tolnay7d229e82018-09-01 16:42:34 -0700435 /// # Syntax
436 ///
437 /// Note that this method does not use turbofish syntax. Pass the peek type
438 /// inside of parentheses.
439 ///
440 /// - `input.peek(Token![struct])`
441 /// - `input.peek(Token![==])`
442 /// - `input.peek(Ident)`
443 /// - `input.peek(Lifetime)`
444 /// - `input.peek(token::Brace)`
445 ///
David Tolnayddebc3e2018-09-01 16:29:20 -0700446 /// # Example
447 ///
448 /// In this example we finish parsing the list of supertraits when the next
449 /// token in the input is either `where` or an opening curly brace.
450 ///
451 /// ```
452 /// # extern crate syn;
453 /// #
454 /// use syn::{braced, token, Generics, Ident, Token, TypeParamBound};
455 /// use syn::parse::{Parse, ParseStream, Result};
456 /// use syn::punctuated::Punctuated;
457 ///
458 /// // Parses a trait definition containing no associated items.
459 /// //
460 /// // trait Marker<'de, T>: A + B<'de> where Box<T>: Clone {}
461 /// struct MarkerTrait {
462 /// trait_token: Token![trait],
463 /// ident: Ident,
464 /// generics: Generics,
465 /// colon_token: Option<Token![:]>,
466 /// supertraits: Punctuated<TypeParamBound, Token![+]>,
467 /// brace_token: token::Brace,
468 /// }
469 ///
470 /// impl Parse for MarkerTrait {
471 /// fn parse(input: ParseStream) -> Result<Self> {
472 /// let trait_token: Token![trait] = input.parse()?;
473 /// let ident: Ident = input.parse()?;
474 /// let mut generics: Generics = input.parse()?;
475 /// let colon_token: Option<Token![:]> = input.parse()?;
476 ///
477 /// let mut supertraits = Punctuated::new();
478 /// if colon_token.is_some() {
479 /// loop {
480 /// supertraits.push_value(input.parse()?);
481 /// if input.peek(Token![where]) || input.peek(token::Brace) {
482 /// break;
483 /// }
484 /// supertraits.push_punct(input.parse()?);
485 /// }
486 /// }
487 ///
488 /// generics.where_clause = input.parse()?;
489 /// let content;
490 /// let empty_brace_token = braced!(content in input);
491 ///
492 /// Ok(MarkerTrait {
493 /// trait_token: trait_token,
494 /// ident: ident,
495 /// generics: generics,
496 /// colon_token: colon_token,
497 /// supertraits: supertraits,
498 /// brace_token: empty_brace_token,
499 /// })
500 /// }
501 /// }
502 /// #
503 /// # fn main() {}
504 /// ```
David Tolnayb77c8b62018-08-25 16:39:41 -0400505 pub fn peek<T: Peek>(&self, token: T) -> bool {
David Tolnay576779a2018-09-01 11:54:12 -0700506 let _ = token;
507 T::Token::peek(self.cursor())
David Tolnayb77c8b62018-08-25 16:39:41 -0400508 }
509
David Tolnay725e1c62018-09-01 12:07:25 -0700510 /// Looks at the second-next token in the parse stream.
David Tolnaye334b872018-09-01 16:38:10 -0700511 ///
512 /// This is commonly useful as a way to implement contextual keywords.
513 ///
514 /// # Example
515 ///
516 /// This example needs to use `peek2` because the symbol `union` is not a
517 /// keyword in Rust. We can't use just `peek` and decide to parse a union if
518 /// the very next token is `union`, because someone is free to write a `mod
519 /// union` and a macro invocation that looks like `union::some_macro! { ...
520 /// }`. In other words `union` is a contextual keyword.
521 ///
522 /// ```
523 /// # extern crate syn;
524 /// #
525 /// use syn::{Ident, ItemUnion, Macro, Token};
526 /// use syn::parse::{Parse, ParseStream, Result};
527 ///
528 /// // Parses either a union or a macro invocation.
529 /// enum UnionOrMacro {
530 /// // union MaybeUninit<T> { uninit: (), value: T }
531 /// Union(ItemUnion),
532 /// // lazy_static! { ... }
533 /// Macro(Macro),
534 /// }
535 ///
536 /// impl Parse for UnionOrMacro {
537 /// fn parse(input: ParseStream) -> Result<Self> {
538 /// if input.peek(Token![union]) && input.peek2(Ident) {
539 /// input.parse().map(UnionOrMacro::Union)
540 /// } else {
541 /// input.parse().map(UnionOrMacro::Macro)
542 /// }
543 /// }
544 /// }
545 /// #
546 /// # fn main() {}
547 /// ```
David Tolnay4fb71232018-08-25 23:14:50 -0400548 pub fn peek2<T: Peek>(&self, token: T) -> bool {
David Tolnay4fb71232018-08-25 23:14:50 -0400549 let ahead = self.fork();
David Tolnay66cb0c42018-08-31 09:01:30 -0700550 skip(&ahead) && ahead.peek(token)
David Tolnay4fb71232018-08-25 23:14:50 -0400551 }
552
David Tolnay725e1c62018-09-01 12:07:25 -0700553 /// Looks at the third-next token in the parse stream.
David Tolnay4fb71232018-08-25 23:14:50 -0400554 pub fn peek3<T: Peek>(&self, token: T) -> bool {
David Tolnay4fb71232018-08-25 23:14:50 -0400555 let ahead = self.fork();
David Tolnay66cb0c42018-08-31 09:01:30 -0700556 skip(&ahead) && skip(&ahead) && ahead.peek(token)
David Tolnay4fb71232018-08-25 23:14:50 -0400557 }
558
David Tolnay725e1c62018-09-01 12:07:25 -0700559 /// Parses zero or more occurrences of `T` separated by punctuation of type
560 /// `P`, with optional trailing punctuation.
561 ///
562 /// Parsing continues until the end of this parse stream. The entire content
563 /// of this parse stream must consist of `T` and `P`.
David Tolnay0abe65b2018-09-01 14:31:43 -0700564 ///
565 /// # Example
566 ///
567 /// ```rust
568 /// # extern crate quote;
569 /// # extern crate syn;
570 /// #
571 /// # use quote::quote;
572 /// #
573 /// use syn::{parenthesized, token, Ident, Token, Type};
574 /// use syn::parse::{Parse, ParseStream, Result};
575 /// use syn::punctuated::Punctuated;
576 ///
577 /// // Parse a simplified tuple struct syntax like:
578 /// //
579 /// // struct S(A, B);
580 /// struct TupleStruct {
581 /// struct_token: Token![struct],
582 /// ident: Ident,
583 /// paren_token: token::Paren,
584 /// fields: Punctuated<Type, Token![,]>,
585 /// semi_token: Token![;],
586 /// }
587 ///
588 /// impl Parse for TupleStruct {
589 /// fn parse(input: ParseStream) -> Result<Self> {
590 /// let content;
591 /// Ok(TupleStruct {
592 /// struct_token: input.parse()?,
593 /// ident: input.parse()?,
594 /// paren_token: parenthesized!(content in input),
595 /// fields: content.parse_terminated(Type::parse)?,
596 /// semi_token: input.parse()?,
597 /// })
598 /// }
599 /// }
600 /// #
601 /// # fn main() {
602 /// # let input = quote! {
603 /// # struct S(A, B);
604 /// # };
605 /// # syn::parse2::<TupleStruct>(input).unwrap();
606 /// # }
607 /// ```
David Tolnay577d0332018-08-25 21:45:24 -0400608 pub fn parse_terminated<T, P: Parse>(
609 &self,
610 parser: fn(ParseStream) -> Result<T>,
611 ) -> Result<Punctuated<T, P>> {
David Tolnayd0f80212018-08-30 18:32:14 -0700612 Punctuated::parse_terminated_with(self, parser)
David Tolnay577d0332018-08-25 21:45:24 -0400613 }
614
David Tolnay725e1c62018-09-01 12:07:25 -0700615 /// Returns whether there are tokens remaining in this stream.
616 ///
617 /// This method returns true at the end of the content of a set of
618 /// delimiters, as well as at the very end of the complete macro input.
David Tolnaycce6b5f2018-09-01 14:24:46 -0700619 ///
620 /// # Example
621 ///
622 /// ```rust
623 /// # extern crate syn;
624 /// #
625 /// use syn::{braced, token, Ident, Item, Token};
626 /// use syn::parse::{Parse, ParseStream, Result};
627 ///
628 /// // Parses a Rust `mod m { ... }` containing zero or more items.
629 /// struct Mod {
630 /// mod_token: Token![mod],
631 /// name: Ident,
632 /// brace_token: token::Brace,
633 /// items: Vec<Item>,
634 /// }
635 ///
636 /// impl Parse for Mod {
637 /// fn parse(input: ParseStream) -> Result<Self> {
638 /// let content;
639 /// Ok(Mod {
640 /// mod_token: input.parse()?,
641 /// name: input.parse()?,
642 /// brace_token: braced!(content in input),
643 /// items: {
644 /// let mut items = Vec::new();
645 /// while !content.is_empty() {
646 /// items.push(content.parse()?);
647 /// }
648 /// items
649 /// },
650 /// })
651 /// }
652 /// }
653 /// #
654 /// # fn main() {}
David Tolnayf5d30452018-09-01 02:29:04 -0700655 pub fn is_empty(&self) -> bool {
656 self.cursor().eof()
657 }
658
David Tolnay725e1c62018-09-01 12:07:25 -0700659 /// Constructs a helper for peeking at the next token in this stream and
660 /// building an error message if it is not one of a set of expected tokens.
David Tolnay2c77e772018-09-01 14:18:46 -0700661 ///
662 /// # Example
663 ///
664 /// ```
665 /// # extern crate syn;
666 /// #
667 /// use syn::{ConstParam, Ident, Lifetime, LifetimeDef, Token, TypeParam};
668 /// use syn::parse::{Parse, ParseStream, Result};
669 ///
670 /// // A generic parameter, a single one of the comma-separated elements inside
671 /// // angle brackets in:
672 /// //
673 /// // fn f<T: Clone, 'a, 'b: 'a, const N: usize>() { ... }
674 /// //
675 /// // On invalid input, lookahead gives us a reasonable error message.
676 /// //
677 /// // error: expected one of: identifier, lifetime, `const`
678 /// // |
679 /// // 5 | fn f<!Sized>() {}
680 /// // | ^
681 /// enum GenericParam {
682 /// Type(TypeParam),
683 /// Lifetime(LifetimeDef),
684 /// Const(ConstParam),
685 /// }
686 ///
687 /// impl Parse for GenericParam {
688 /// fn parse(input: ParseStream) -> Result<Self> {
689 /// let lookahead = input.lookahead1();
690 /// if lookahead.peek(Ident) {
691 /// input.parse().map(GenericParam::Type)
692 /// } else if lookahead.peek(Lifetime) {
693 /// input.parse().map(GenericParam::Lifetime)
694 /// } else if lookahead.peek(Token![const]) {
695 /// input.parse().map(GenericParam::Const)
696 /// } else {
697 /// Err(lookahead.error())
698 /// }
699 /// }
700 /// }
701 /// #
702 /// # fn main() {}
703 /// ```
David Tolnayf5d30452018-09-01 02:29:04 -0700704 pub fn lookahead1(&self) -> Lookahead1<'a> {
705 lookahead::new(self.scope, self.cursor())
706 }
707
David Tolnay725e1c62018-09-01 12:07:25 -0700708 /// Forks a parse stream so that parsing tokens out of either the original
709 /// or the fork does not advance the position of the other.
710 ///
711 /// # Performance
712 ///
713 /// Forking a parse stream is a cheap fixed amount of work and does not
714 /// involve copying token buffers. Where you might hit performance problems
715 /// is if your macro ends up parsing a large amount of content more than
716 /// once.
717 ///
718 /// ```
719 /// # use syn::Expr;
720 /// # use syn::parse::{ParseStream, Result};
721 /// #
722 /// # fn bad(input: ParseStream) -> Result<Expr> {
723 /// // Do not do this.
724 /// if input.fork().parse::<Expr>().is_ok() {
725 /// return input.parse::<Expr>();
726 /// }
727 /// # unimplemented!()
728 /// # }
729 /// ```
730 ///
731 /// As a rule, avoid parsing an unbounded amount of tokens out of a forked
732 /// parse stream. Only use a fork when the amount of work performed against
733 /// the fork is small and bounded.
734 ///
David Tolnayec149b02018-09-01 14:17:28 -0700735 /// For a lower level but occasionally more performant way to perform
David Tolnay725e1c62018-09-01 12:07:25 -0700736 /// speculative parsing, consider using [`ParseStream::step`] instead.
737 ///
738 /// [`ParseStream::step`]: #method.step
David Tolnayec149b02018-09-01 14:17:28 -0700739 ///
740 /// # Example
741 ///
742 /// The parse implementation shown here parses possibly restricted `pub`
743 /// visibilities.
744 ///
745 /// - `pub`
746 /// - `pub(crate)`
747 /// - `pub(self)`
748 /// - `pub(super)`
749 /// - `pub(in some::path)`
750 ///
751 /// To handle the case of visibilities inside of tuple structs, the parser
752 /// needs to distinguish parentheses that specify visibility restrictions
753 /// from parentheses that form part of a tuple type.
754 ///
755 /// ```
756 /// # struct A;
757 /// # struct B;
758 /// # struct C;
759 /// #
760 /// struct S(pub(crate) A, pub (B, C));
761 /// ```
762 ///
763 /// In this example input the first tuple struct element of `S` has
764 /// `pub(crate)` visibility while the second tuple struct element has `pub`
765 /// visibility; the parentheses around `(B, C)` are part of the type rather
766 /// than part of a visibility restriction.
767 ///
768 /// The parser uses a forked parse stream to check the first token inside of
769 /// parentheses after the `pub` keyword. This is a small bounded amount of
770 /// work performed against the forked parse stream.
771 ///
772 /// ```
773 /// # extern crate syn;
774 /// #
775 /// use syn::{parenthesized, token, Ident, Path, Token};
776 /// use syn::ext::IdentExt;
777 /// use syn::parse::{Parse, ParseStream, Result};
778 ///
779 /// struct PubVisibility {
780 /// pub_token: Token![pub],
781 /// restricted: Option<Restricted>,
782 /// }
783 ///
784 /// struct Restricted {
785 /// paren_token: token::Paren,
786 /// in_token: Option<Token![in]>,
787 /// path: Path,
788 /// }
789 ///
790 /// impl Parse for PubVisibility {
791 /// fn parse(input: ParseStream) -> Result<Self> {
792 /// let pub_token: Token![pub] = input.parse()?;
793 ///
794 /// if input.peek(token::Paren) {
795 /// let ahead = input.fork();
796 /// let mut content;
797 /// parenthesized!(content in ahead);
798 ///
799 /// if content.peek(Token![crate])
800 /// || content.peek(Token![self])
801 /// || content.peek(Token![super])
802 /// {
803 /// return Ok(PubVisibility {
804 /// pub_token: pub_token,
805 /// restricted: Some(Restricted {
806 /// paren_token: parenthesized!(content in input),
807 /// in_token: None,
808 /// path: Path::from(content.call(Ident::parse_any)?),
809 /// }),
810 /// });
811 /// } else if content.peek(Token![in]) {
812 /// return Ok(PubVisibility {
813 /// pub_token: pub_token,
814 /// restricted: Some(Restricted {
815 /// paren_token: parenthesized!(content in input),
816 /// in_token: Some(content.parse()?),
817 /// path: content.call(Path::parse_mod_style)?,
818 /// }),
819 /// });
820 /// }
821 /// }
822 ///
823 /// Ok(PubVisibility {
824 /// pub_token: pub_token,
825 /// restricted: None,
826 /// })
827 /// }
828 /// }
829 /// #
830 /// # fn main() {}
831 /// ```
David Tolnayb77c8b62018-08-25 16:39:41 -0400832 pub fn fork(&self) -> Self {
David Tolnay6456a9d2018-08-26 08:11:18 -0400833 ParseBuffer {
834 scope: self.scope,
835 cell: self.cell.clone(),
836 marker: PhantomData,
837 // Not the parent's unexpected. Nothing cares whether the clone
838 // parses all the way.
839 unexpected: Rc::new(Cell::new(None)),
840 }
David Tolnayb77c8b62018-08-25 16:39:41 -0400841 }
842
David Tolnay725e1c62018-09-01 12:07:25 -0700843 /// Triggers an error at the current position of the parse stream.
David Tolnay23fce0b2018-09-01 13:50:31 -0700844 ///
845 /// # Example
846 ///
847 /// ```
848 /// # extern crate syn;
849 /// #
850 /// use syn::{Expr, Token};
851 /// use syn::parse::{Parse, ParseStream, Result};
852 ///
853 /// // Some kind of loop: `while` or `for` or `loop`.
854 /// struct Loop {
855 /// expr: Expr,
856 /// }
857 ///
858 /// impl Parse for Loop {
859 /// fn parse(input: ParseStream) -> Result<Self> {
860 /// if input.peek(Token![while])
861 /// || input.peek(Token![for])
862 /// || input.peek(Token![loop])
863 /// {
864 /// Ok(Loop {
865 /// expr: input.parse()?,
866 /// })
867 /// } else {
868 /// Err(input.error("expected some kind of loop"))
869 /// }
870 /// }
871 /// }
872 /// ```
David Tolnay4fb71232018-08-25 23:14:50 -0400873 pub fn error<T: Display>(&self, message: T) -> Error {
874 error::new_at(self.scope, self.cursor(), message)
875 }
876
David Tolnay725e1c62018-09-01 12:07:25 -0700877 /// Speculatively parses tokens from this parse stream, advancing the
878 /// position of this stream only if parsing succeeds.
David Tolnay9bd34392018-09-01 13:19:53 -0700879 ///
David Tolnayad1d1d22018-09-01 13:34:43 -0700880 /// This is a powerful low-level API used for defining the `Parse` impls of
881 /// the basic built-in token types. It is not something that will be used
882 /// widely outside of the Syn codebase.
883 ///
David Tolnay9bd34392018-09-01 13:19:53 -0700884 /// # Example
885 ///
886 /// ```
887 /// # extern crate proc_macro2;
888 /// # extern crate syn;
889 /// #
890 /// use proc_macro2::TokenTree;
891 /// use syn::parse::{ParseStream, Result};
892 ///
893 /// // This function advances the stream past the next occurrence of `@`. If
894 /// // no `@` is present in the stream, the stream position is unchanged and
895 /// // an error is returned.
896 /// fn skip_past_next_at(input: ParseStream) -> Result<()> {
897 /// input.step(|cursor| {
898 /// let mut rest = *cursor;
899 /// while let Some((tt, next)) = cursor.token_tree() {
900 /// match tt {
901 /// TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
902 /// return Ok(((), next));
903 /// }
904 /// _ => rest = next,
905 /// }
906 /// }
907 /// Err(cursor.error("no `@` was found after this point"))
908 /// })
909 /// }
910 /// #
911 /// # fn main() {}
912 /// ```
David Tolnayb50c65a2018-08-30 21:14:57 -0700913 pub fn step<F, R>(&self, function: F) -> Result<R>
David Tolnay18c754c2018-08-21 23:26:58 -0400914 where
915 F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,
916 {
David Tolnay6b65f852018-09-01 11:56:25 -0700917 let (node, rest) = function(StepCursor {
David Tolnay18c754c2018-08-21 23:26:58 -0400918 scope: self.scope,
919 cursor: self.cell.get(),
920 marker: PhantomData,
David Tolnay6b65f852018-09-01 11:56:25 -0700921 })?;
922 self.cell.set(rest);
923 Ok(node)
David Tolnay18c754c2018-08-21 23:26:58 -0400924 }
David Tolnayeafc8052018-08-25 16:33:53 -0400925
David Tolnay725e1c62018-09-01 12:07:25 -0700926 /// Provides low-level access to the token representation underlying this
927 /// parse stream.
928 ///
929 /// Cursors are immutable so no operations you perform against the cursor
930 /// will affect the state of this parse stream.
David Tolnayf5d30452018-09-01 02:29:04 -0700931 pub fn cursor(&self) -> Cursor<'a> {
932 self.cell.get()
933 }
934
David Tolnay94f06632018-08-31 10:17:17 -0700935 fn check_unexpected(&self) -> Result<()> {
David Tolnayeafc8052018-08-25 16:33:53 -0400936 match self.unexpected.get() {
937 Some(span) => Err(Error::new(span, "unexpected token")),
938 None => Ok(()),
939 }
940 }
David Tolnay18c754c2018-08-21 23:26:58 -0400941}
942
David Tolnaya7d69fc2018-08-26 13:30:24 -0400943impl<T: Parse> Parse for Box<T> {
944 fn parse(input: ParseStream) -> Result<Self> {
945 input.parse().map(Box::new)
946 }
947}
948
David Tolnay4fb71232018-08-25 23:14:50 -0400949impl<T: Parse + Token> Parse for Option<T> {
David Tolnay18c754c2018-08-21 23:26:58 -0400950 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay00f81fd2018-09-01 10:50:12 -0700951 if T::peek(input.cursor()) {
David Tolnay4fb71232018-08-25 23:14:50 -0400952 Ok(Some(input.parse()?))
953 } else {
954 Ok(None)
David Tolnay18c754c2018-08-21 23:26:58 -0400955 }
David Tolnay18c754c2018-08-21 23:26:58 -0400956 }
957}
David Tolnay4ac232d2018-08-31 10:18:03 -0700958
David Tolnay80a914f2018-08-30 23:49:53 -0700959impl Parse for TokenStream {
960 fn parse(input: ParseStream) -> Result<Self> {
961 input.step(|cursor| Ok((cursor.token_stream(), Cursor::empty())))
962 }
963}
964
965impl Parse for TokenTree {
966 fn parse(input: ParseStream) -> Result<Self> {
967 input.step(|cursor| match cursor.token_tree() {
968 Some((tt, rest)) => Ok((tt, rest)),
969 None => Err(cursor.error("expected token tree")),
970 })
971 }
972}
973
974impl Parse for Group {
975 fn parse(input: ParseStream) -> Result<Self> {
976 input.step(|cursor| {
977 for delim in &[Delimiter::Parenthesis, Delimiter::Brace, Delimiter::Bracket] {
978 if let Some((inside, span, rest)) = cursor.group(*delim) {
979 let mut group = Group::new(*delim, inside.token_stream());
980 group.set_span(span);
981 return Ok((group, rest));
982 }
983 }
984 Err(cursor.error("expected group token"))
985 })
986 }
987}
988
989impl Parse for Punct {
990 fn parse(input: ParseStream) -> Result<Self> {
991 input.step(|cursor| match cursor.punct() {
992 Some((punct, rest)) => Ok((punct, rest)),
993 None => Err(cursor.error("expected punctuation token")),
994 })
995 }
996}
997
998impl Parse for Literal {
999 fn parse(input: ParseStream) -> Result<Self> {
1000 input.step(|cursor| match cursor.literal() {
1001 Some((literal, rest)) => Ok((literal, rest)),
1002 None => Err(cursor.error("expected literal token")),
1003 })
1004 }
1005}
1006
1007/// Parser that can parse Rust tokens into a particular syntax tree node.
1008///
1009/// Refer to the [module documentation] for details about parsing in Syn.
1010///
1011/// [module documentation]: index.html
1012///
1013/// *This trait is available if Syn is built with the `"parsing"` feature.*
1014pub trait Parser: Sized {
1015 type Output;
1016
1017 /// Parse a proc-macro2 token stream into the chosen syntax tree node.
1018 fn parse2(self, tokens: TokenStream) -> Result<Self::Output>;
1019
1020 /// Parse tokens of source code into the chosen syntax tree node.
1021 ///
1022 /// *This method is available if Syn is built with both the `"parsing"` and
1023 /// `"proc-macro"` features.*
1024 #[cfg(all(
1025 not(all(target_arch = "wasm32", target_os = "unknown")),
1026 feature = "proc-macro"
1027 ))]
1028 fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output> {
1029 self.parse2(proc_macro2::TokenStream::from(tokens))
1030 }
1031
1032 /// Parse a string of Rust code into the chosen syntax tree node.
1033 ///
1034 /// # Hygiene
1035 ///
1036 /// Every span in the resulting syntax tree will be set to resolve at the
1037 /// macro call site.
1038 fn parse_str(self, s: &str) -> Result<Self::Output> {
1039 self.parse2(proc_macro2::TokenStream::from_str(s)?)
1040 }
1041}
1042
David Tolnay7b07aa12018-09-01 11:41:12 -07001043fn tokens_to_parse_buffer(tokens: &TokenBuffer) -> ParseBuffer {
1044 let scope = Span::call_site();
1045 let cursor = tokens.begin();
1046 let unexpected = Rc::new(Cell::new(None));
1047 private::new_parse_buffer(scope, cursor, unexpected)
1048}
1049
David Tolnay80a914f2018-08-30 23:49:53 -07001050impl<F, T> Parser for F
1051where
1052 F: FnOnce(ParseStream) -> Result<T>,
1053{
1054 type Output = T;
1055
1056 fn parse2(self, tokens: TokenStream) -> Result<T> {
1057 let buf = TokenBuffer::new2(tokens);
David Tolnay7b07aa12018-09-01 11:41:12 -07001058 let state = tokens_to_parse_buffer(&buf);
David Tolnay80a914f2018-08-30 23:49:53 -07001059 let node = self(&state)?;
1060 state.check_unexpected()?;
1061 if state.is_empty() {
1062 Ok(node)
1063 } else {
1064 Err(state.error("unexpected token"))
1065 }
1066 }
1067}