blob: 4dd735deed27cdcc8f7a1c417706f680e6dad248 [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};
Louis Kureuil Personc0beaf32018-09-05 00:12:43 +0200217pub use lookahead::{Lookahead1, Peek, TokenMarker};
218pub use keyword::Keyword;
David Tolnay18c754c2018-08-21 23:26:58 -0400219
220/// Parsing interface implemented by all types that can be parsed in a default
221/// way from a token stream.
222pub trait Parse: Sized {
223 fn parse(input: ParseStream) -> Result<Self>;
224}
225
226/// Input to a Syn parser function.
David Tolnaya0daa482018-09-01 02:09:40 -0700227///
228/// See the methods of this type under the documentation of [`ParseBuffer`]. For
229/// an overview of parsing in Syn, refer to the [module documentation].
230///
231/// [module documentation]: index.html
David Tolnay18c754c2018-08-21 23:26:58 -0400232pub type ParseStream<'a> = &'a ParseBuffer<'a>;
233
234/// Cursor position within a buffered token stream.
David Tolnay20d29a12018-09-01 15:15:33 -0700235///
236/// This type is more commonly used through the type alias [`ParseStream`] which
237/// is an alias for `&ParseBuffer`.
238///
239/// `ParseStream` is the input type for all parser functions in Syn. They have
240/// the signature `fn(ParseStream) -> Result<T>`.
David Tolnay18c754c2018-08-21 23:26:58 -0400241pub struct ParseBuffer<'a> {
242 scope: Span,
David Tolnay5d7f2252018-09-02 08:21:40 -0700243 // Instead of Cell<Cursor<'a>> so that ParseBuffer<'a> is covariant in 'a.
244 // The rest of the code in this module needs to be careful that only a
245 // cursor derived from this `cell` is ever assigned to this `cell`.
246 //
247 // Cell<Cursor<'a>> cannot be covariant in 'a because then we could take a
248 // ParseBuffer<'a>, upcast to ParseBuffer<'short> for some lifetime shorter
249 // than 'a, and then assign a Cursor<'short> into the Cell.
250 //
251 // By extension, it would not be safe to expose an API that accepts a
252 // Cursor<'a> and trusts that it lives as long as the cursor currently in
253 // the cell.
David Tolnay18c754c2018-08-21 23:26:58 -0400254 cell: Cell<Cursor<'static>>,
255 marker: PhantomData<Cursor<'a>>,
David Tolnayeafc8052018-08-25 16:33:53 -0400256 unexpected: Rc<Cell<Option<Span>>>,
257}
258
259impl<'a> Drop for ParseBuffer<'a> {
260 fn drop(&mut self) {
261 if !self.is_empty() && self.unexpected.get().is_none() {
262 self.unexpected.set(Some(self.cursor().span()));
263 }
264 }
David Tolnay18c754c2018-08-21 23:26:58 -0400265}
266
David Tolnay642832f2018-09-01 13:08:10 -0700267/// Cursor state associated with speculative parsing.
268///
269/// This type is the input of the closure provided to [`ParseStream::step`].
270///
271/// [`ParseStream::step`]: struct.ParseBuffer.html#method.step
David Tolnay9bd34392018-09-01 13:19:53 -0700272///
273/// # Example
274///
275/// ```
276/// # extern crate proc_macro2;
277/// # extern crate syn;
278/// #
279/// use proc_macro2::TokenTree;
280/// use syn::parse::{ParseStream, Result};
281///
282/// // This function advances the stream past the next occurrence of `@`. If
283/// // no `@` is present in the stream, the stream position is unchanged and
284/// // an error is returned.
285/// fn skip_past_next_at(input: ParseStream) -> Result<()> {
286/// input.step(|cursor| {
287/// let mut rest = *cursor;
288/// while let Some((tt, next)) = cursor.token_tree() {
289/// match tt {
290/// TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
291/// return Ok(((), next));
292/// }
293/// _ => rest = next,
294/// }
295/// }
296/// Err(cursor.error("no `@` was found after this point"))
297/// })
298/// }
299/// #
300/// # fn main() {}
301/// ```
David Tolnay18c754c2018-08-21 23:26:58 -0400302#[derive(Copy, Clone)]
303pub struct StepCursor<'c, 'a> {
304 scope: Span,
David Tolnay56924f42018-09-02 08:24:58 -0700305 // This field is covariant in 'c.
David Tolnay18c754c2018-08-21 23:26:58 -0400306 cursor: Cursor<'c>,
David Tolnay56924f42018-09-02 08:24:58 -0700307 // This field is contravariant in 'c. Together these make StepCursor
308 // invariant in 'c. Also covariant in 'a. The user cannot cast 'c to a
309 // different lifetime but can upcast into a StepCursor with a shorter
310 // lifetime 'a.
311 //
312 // As long as we only ever construct a StepCursor for which 'c outlives 'a,
313 // this means if ever a StepCursor<'c, 'a> exists we are guaranteed that 'c
314 // outlives 'a.
David Tolnay18c754c2018-08-21 23:26:58 -0400315 marker: PhantomData<fn(Cursor<'c>) -> Cursor<'a>>,
316}
317
318impl<'c, 'a> Deref for StepCursor<'c, 'a> {
319 type Target = Cursor<'c>;
320
321 fn deref(&self) -> &Self::Target {
322 &self.cursor
323 }
324}
325
326impl<'c, 'a> StepCursor<'c, 'a> {
David Tolnay642832f2018-09-01 13:08:10 -0700327 /// Triggers an error at the current position of the parse stream.
328 ///
329 /// The `ParseStream::step` invocation will return this same error without
330 /// advancing the stream state.
David Tolnay18c754c2018-08-21 23:26:58 -0400331 pub fn error<T: Display>(self, message: T) -> Error {
332 error::new_at(self.scope, self.cursor, message)
333 }
334}
335
David Tolnay6ea3fdc2018-09-01 13:30:53 -0700336impl private {
337 pub fn advance_step_cursor<'c, 'a>(proof: StepCursor<'c, 'a>, to: Cursor<'c>) -> Cursor<'a> {
David Tolnay56924f42018-09-02 08:24:58 -0700338 // Refer to the comments within the StepCursor definition. We use the
339 // fact that a StepCursor<'c, 'a> exists as proof that 'c outlives 'a.
340 // Cursor is covariant in its lifetime parameter so we can cast a
341 // Cursor<'c> to one with the shorter lifetime Cursor<'a>.
David Tolnay6ea3fdc2018-09-01 13:30:53 -0700342 let _ = proof;
343 unsafe { mem::transmute::<Cursor<'c>, Cursor<'a>>(to) }
344 }
345}
346
David Tolnay66cb0c42018-08-31 09:01:30 -0700347fn skip(input: ParseStream) -> bool {
David Tolnay4ac232d2018-08-31 10:18:03 -0700348 input
349 .step(|cursor| {
350 if let Some((_lifetime, rest)) = cursor.lifetime() {
351 Ok((true, rest))
352 } else if let Some((_token, rest)) = cursor.token_tree() {
353 Ok((true, rest))
354 } else {
355 Ok((false, *cursor))
356 }
357 }).unwrap()
David Tolnay66cb0c42018-08-31 09:01:30 -0700358}
359
David Tolnay10951d52018-08-31 10:27:39 -0700360impl private {
David Tolnay70f30e92018-09-01 02:04:17 -0700361 pub fn new_parse_buffer(
362 scope: Span,
363 cursor: Cursor,
364 unexpected: Rc<Cell<Option<Span>>>,
365 ) -> ParseBuffer {
David Tolnay18c754c2018-08-21 23:26:58 -0400366 ParseBuffer {
367 scope: scope,
David Tolnay5d7f2252018-09-02 08:21:40 -0700368 // See comment on `cell` in the struct definition.
369 cell: Cell::new(unsafe { mem::transmute::<Cursor, Cursor<'static>>(cursor) }),
David Tolnay18c754c2018-08-21 23:26:58 -0400370 marker: PhantomData,
David Tolnayeafc8052018-08-25 16:33:53 -0400371 unexpected: unexpected,
David Tolnay18c754c2018-08-21 23:26:58 -0400372 }
373 }
374
David Tolnay94f06632018-08-31 10:17:17 -0700375 pub fn get_unexpected(buffer: &ParseBuffer) -> Rc<Cell<Option<Span>>> {
376 buffer.unexpected.clone()
377 }
378}
379
380impl<'a> ParseBuffer<'a> {
David Tolnay725e1c62018-09-01 12:07:25 -0700381 /// Parses a syntax tree node of type `T`, advancing the position of our
382 /// parse stream past it.
David Tolnay18c754c2018-08-21 23:26:58 -0400383 pub fn parse<T: Parse>(&self) -> Result<T> {
384 T::parse(self)
385 }
386
David Tolnay725e1c62018-09-01 12:07:25 -0700387 /// Calls the given parser function to parse a syntax tree node of type `T`
388 /// from this stream.
David Tolnay21ce84c2018-09-01 15:37:51 -0700389 ///
390 /// # Example
391 ///
392 /// The parser below invokes [`Attribute::parse_outer`] to parse a vector of
393 /// zero or more outer attributes.
394 ///
395 /// [`Attribute::parse_outer`]: ../struct.Attribute.html#method.parse_outer
396 ///
397 /// ```
398 /// # extern crate syn;
399 /// #
400 /// use syn::{Attribute, Ident, Token};
401 /// use syn::parse::{Parse, ParseStream, Result};
402 ///
403 /// // Parses a unit struct with attributes.
404 /// //
405 /// // #[path = "s.tmpl"]
406 /// // struct S;
407 /// struct UnitStruct {
408 /// attrs: Vec<Attribute>,
409 /// struct_token: Token![struct],
410 /// name: Ident,
411 /// semi_token: Token![;],
412 /// }
413 ///
414 /// impl Parse for UnitStruct {
415 /// fn parse(input: ParseStream) -> Result<Self> {
416 /// Ok(UnitStruct {
417 /// attrs: input.call(Attribute::parse_outer)?,
418 /// struct_token: input.parse()?,
419 /// name: input.parse()?,
420 /// semi_token: input.parse()?,
421 /// })
422 /// }
423 /// }
424 /// #
425 /// # fn main() {}
426 /// ```
David Tolnay3a515a02018-08-25 21:08:27 -0400427 pub fn call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T> {
428 function(self)
429 }
430
David Tolnay725e1c62018-09-01 12:07:25 -0700431 /// Looks at the next token in the parse stream to determine whether it
432 /// matches the requested type of token.
433 ///
434 /// Does not advance the position of the parse stream.
David Tolnayddebc3e2018-09-01 16:29:20 -0700435 ///
David Tolnay7d229e82018-09-01 16:42:34 -0700436 /// # Syntax
437 ///
438 /// Note that this method does not use turbofish syntax. Pass the peek type
439 /// inside of parentheses.
440 ///
441 /// - `input.peek(Token![struct])`
442 /// - `input.peek(Token![==])`
443 /// - `input.peek(Ident)`
444 /// - `input.peek(Lifetime)`
445 /// - `input.peek(token::Brace)`
446 ///
David Tolnayddebc3e2018-09-01 16:29:20 -0700447 /// # Example
448 ///
449 /// In this example we finish parsing the list of supertraits when the next
450 /// token in the input is either `where` or an opening curly brace.
451 ///
452 /// ```
453 /// # extern crate syn;
454 /// #
455 /// use syn::{braced, token, Generics, Ident, Token, TypeParamBound};
456 /// use syn::parse::{Parse, ParseStream, Result};
457 /// use syn::punctuated::Punctuated;
458 ///
459 /// // Parses a trait definition containing no associated items.
460 /// //
461 /// // trait Marker<'de, T>: A + B<'de> where Box<T>: Clone {}
462 /// struct MarkerTrait {
463 /// trait_token: Token![trait],
464 /// ident: Ident,
465 /// generics: Generics,
466 /// colon_token: Option<Token![:]>,
467 /// supertraits: Punctuated<TypeParamBound, Token![+]>,
468 /// brace_token: token::Brace,
469 /// }
470 ///
471 /// impl Parse for MarkerTrait {
472 /// fn parse(input: ParseStream) -> Result<Self> {
473 /// let trait_token: Token![trait] = input.parse()?;
474 /// let ident: Ident = input.parse()?;
475 /// let mut generics: Generics = input.parse()?;
476 /// let colon_token: Option<Token![:]> = input.parse()?;
477 ///
478 /// let mut supertraits = Punctuated::new();
479 /// if colon_token.is_some() {
480 /// loop {
481 /// supertraits.push_value(input.parse()?);
482 /// if input.peek(Token![where]) || input.peek(token::Brace) {
483 /// break;
484 /// }
485 /// supertraits.push_punct(input.parse()?);
486 /// }
487 /// }
488 ///
489 /// generics.where_clause = input.parse()?;
490 /// let content;
491 /// let empty_brace_token = braced!(content in input);
492 ///
493 /// Ok(MarkerTrait {
494 /// trait_token: trait_token,
495 /// ident: ident,
496 /// generics: generics,
497 /// colon_token: colon_token,
498 /// supertraits: supertraits,
499 /// brace_token: empty_brace_token,
500 /// })
501 /// }
502 /// }
503 /// #
504 /// # fn main() {}
505 /// ```
David Tolnayb77c8b62018-08-25 16:39:41 -0400506 pub fn peek<T: Peek>(&self, token: T) -> bool {
David Tolnay576779a2018-09-01 11:54:12 -0700507 let _ = token;
508 T::Token::peek(self.cursor())
David Tolnayb77c8b62018-08-25 16:39:41 -0400509 }
510
David Tolnay725e1c62018-09-01 12:07:25 -0700511 /// Looks at the second-next token in the parse stream.
David Tolnaye334b872018-09-01 16:38:10 -0700512 ///
513 /// This is commonly useful as a way to implement contextual keywords.
514 ///
515 /// # Example
516 ///
517 /// This example needs to use `peek2` because the symbol `union` is not a
518 /// keyword in Rust. We can't use just `peek` and decide to parse a union if
519 /// the very next token is `union`, because someone is free to write a `mod
520 /// union` and a macro invocation that looks like `union::some_macro! { ...
521 /// }`. In other words `union` is a contextual keyword.
522 ///
523 /// ```
524 /// # extern crate syn;
525 /// #
526 /// use syn::{Ident, ItemUnion, Macro, Token};
527 /// use syn::parse::{Parse, ParseStream, Result};
528 ///
529 /// // Parses either a union or a macro invocation.
530 /// enum UnionOrMacro {
531 /// // union MaybeUninit<T> { uninit: (), value: T }
532 /// Union(ItemUnion),
533 /// // lazy_static! { ... }
534 /// Macro(Macro),
535 /// }
536 ///
537 /// impl Parse for UnionOrMacro {
538 /// fn parse(input: ParseStream) -> Result<Self> {
539 /// if input.peek(Token![union]) && input.peek2(Ident) {
540 /// input.parse().map(UnionOrMacro::Union)
541 /// } else {
542 /// input.parse().map(UnionOrMacro::Macro)
543 /// }
544 /// }
545 /// }
546 /// #
547 /// # fn main() {}
548 /// ```
David Tolnay4fb71232018-08-25 23:14:50 -0400549 pub fn peek2<T: Peek>(&self, token: T) -> bool {
David Tolnay4fb71232018-08-25 23:14:50 -0400550 let ahead = self.fork();
David Tolnay66cb0c42018-08-31 09:01:30 -0700551 skip(&ahead) && ahead.peek(token)
David Tolnay4fb71232018-08-25 23:14:50 -0400552 }
553
David Tolnay725e1c62018-09-01 12:07:25 -0700554 /// Looks at the third-next token in the parse stream.
David Tolnay4fb71232018-08-25 23:14:50 -0400555 pub fn peek3<T: Peek>(&self, token: T) -> bool {
David Tolnay4fb71232018-08-25 23:14:50 -0400556 let ahead = self.fork();
David Tolnay66cb0c42018-08-31 09:01:30 -0700557 skip(&ahead) && skip(&ahead) && ahead.peek(token)
David Tolnay4fb71232018-08-25 23:14:50 -0400558 }
559
David Tolnay725e1c62018-09-01 12:07:25 -0700560 /// Parses zero or more occurrences of `T` separated by punctuation of type
561 /// `P`, with optional trailing punctuation.
562 ///
563 /// Parsing continues until the end of this parse stream. The entire content
564 /// of this parse stream must consist of `T` and `P`.
David Tolnay0abe65b2018-09-01 14:31:43 -0700565 ///
566 /// # Example
567 ///
568 /// ```rust
569 /// # extern crate quote;
570 /// # extern crate syn;
571 /// #
572 /// # use quote::quote;
573 /// #
574 /// use syn::{parenthesized, token, Ident, Token, Type};
575 /// use syn::parse::{Parse, ParseStream, Result};
576 /// use syn::punctuated::Punctuated;
577 ///
578 /// // Parse a simplified tuple struct syntax like:
579 /// //
580 /// // struct S(A, B);
581 /// struct TupleStruct {
582 /// struct_token: Token![struct],
583 /// ident: Ident,
584 /// paren_token: token::Paren,
585 /// fields: Punctuated<Type, Token![,]>,
586 /// semi_token: Token![;],
587 /// }
588 ///
589 /// impl Parse for TupleStruct {
590 /// fn parse(input: ParseStream) -> Result<Self> {
591 /// let content;
592 /// Ok(TupleStruct {
593 /// struct_token: input.parse()?,
594 /// ident: input.parse()?,
595 /// paren_token: parenthesized!(content in input),
596 /// fields: content.parse_terminated(Type::parse)?,
597 /// semi_token: input.parse()?,
598 /// })
599 /// }
600 /// }
601 /// #
602 /// # fn main() {
603 /// # let input = quote! {
604 /// # struct S(A, B);
605 /// # };
606 /// # syn::parse2::<TupleStruct>(input).unwrap();
607 /// # }
608 /// ```
David Tolnay577d0332018-08-25 21:45:24 -0400609 pub fn parse_terminated<T, P: Parse>(
610 &self,
611 parser: fn(ParseStream) -> Result<T>,
612 ) -> Result<Punctuated<T, P>> {
David Tolnayd0f80212018-08-30 18:32:14 -0700613 Punctuated::parse_terminated_with(self, parser)
David Tolnay577d0332018-08-25 21:45:24 -0400614 }
615
David Tolnay725e1c62018-09-01 12:07:25 -0700616 /// Returns whether there are tokens remaining in this stream.
617 ///
618 /// This method returns true at the end of the content of a set of
619 /// delimiters, as well as at the very end of the complete macro input.
David Tolnaycce6b5f2018-09-01 14:24:46 -0700620 ///
621 /// # Example
622 ///
623 /// ```rust
624 /// # extern crate syn;
625 /// #
626 /// use syn::{braced, token, Ident, Item, Token};
627 /// use syn::parse::{Parse, ParseStream, Result};
628 ///
629 /// // Parses a Rust `mod m { ... }` containing zero or more items.
630 /// struct Mod {
631 /// mod_token: Token![mod],
632 /// name: Ident,
633 /// brace_token: token::Brace,
634 /// items: Vec<Item>,
635 /// }
636 ///
637 /// impl Parse for Mod {
638 /// fn parse(input: ParseStream) -> Result<Self> {
639 /// let content;
640 /// Ok(Mod {
641 /// mod_token: input.parse()?,
642 /// name: input.parse()?,
643 /// brace_token: braced!(content in input),
644 /// items: {
645 /// let mut items = Vec::new();
646 /// while !content.is_empty() {
647 /// items.push(content.parse()?);
648 /// }
649 /// items
650 /// },
651 /// })
652 /// }
653 /// }
654 /// #
655 /// # fn main() {}
David Tolnayf5d30452018-09-01 02:29:04 -0700656 pub fn is_empty(&self) -> bool {
657 self.cursor().eof()
658 }
659
David Tolnay725e1c62018-09-01 12:07:25 -0700660 /// Constructs a helper for peeking at the next token in this stream and
661 /// building an error message if it is not one of a set of expected tokens.
David Tolnay2c77e772018-09-01 14:18:46 -0700662 ///
663 /// # Example
664 ///
665 /// ```
666 /// # extern crate syn;
667 /// #
668 /// use syn::{ConstParam, Ident, Lifetime, LifetimeDef, Token, TypeParam};
669 /// use syn::parse::{Parse, ParseStream, Result};
670 ///
671 /// // A generic parameter, a single one of the comma-separated elements inside
672 /// // angle brackets in:
673 /// //
674 /// // fn f<T: Clone, 'a, 'b: 'a, const N: usize>() { ... }
675 /// //
676 /// // On invalid input, lookahead gives us a reasonable error message.
677 /// //
678 /// // error: expected one of: identifier, lifetime, `const`
679 /// // |
680 /// // 5 | fn f<!Sized>() {}
681 /// // | ^
682 /// enum GenericParam {
683 /// Type(TypeParam),
684 /// Lifetime(LifetimeDef),
685 /// Const(ConstParam),
686 /// }
687 ///
688 /// impl Parse for GenericParam {
689 /// fn parse(input: ParseStream) -> Result<Self> {
690 /// let lookahead = input.lookahead1();
691 /// if lookahead.peek(Ident) {
692 /// input.parse().map(GenericParam::Type)
693 /// } else if lookahead.peek(Lifetime) {
694 /// input.parse().map(GenericParam::Lifetime)
695 /// } else if lookahead.peek(Token![const]) {
696 /// input.parse().map(GenericParam::Const)
697 /// } else {
698 /// Err(lookahead.error())
699 /// }
700 /// }
701 /// }
702 /// #
703 /// # fn main() {}
704 /// ```
David Tolnayf5d30452018-09-01 02:29:04 -0700705 pub fn lookahead1(&self) -> Lookahead1<'a> {
706 lookahead::new(self.scope, self.cursor())
707 }
708
David Tolnay725e1c62018-09-01 12:07:25 -0700709 /// Forks a parse stream so that parsing tokens out of either the original
710 /// or the fork does not advance the position of the other.
711 ///
712 /// # Performance
713 ///
714 /// Forking a parse stream is a cheap fixed amount of work and does not
715 /// involve copying token buffers. Where you might hit performance problems
716 /// is if your macro ends up parsing a large amount of content more than
717 /// once.
718 ///
719 /// ```
720 /// # use syn::Expr;
721 /// # use syn::parse::{ParseStream, Result};
722 /// #
723 /// # fn bad(input: ParseStream) -> Result<Expr> {
724 /// // Do not do this.
725 /// if input.fork().parse::<Expr>().is_ok() {
726 /// return input.parse::<Expr>();
727 /// }
728 /// # unimplemented!()
729 /// # }
730 /// ```
731 ///
732 /// As a rule, avoid parsing an unbounded amount of tokens out of a forked
733 /// parse stream. Only use a fork when the amount of work performed against
734 /// the fork is small and bounded.
735 ///
David Tolnayec149b02018-09-01 14:17:28 -0700736 /// For a lower level but occasionally more performant way to perform
David Tolnay725e1c62018-09-01 12:07:25 -0700737 /// speculative parsing, consider using [`ParseStream::step`] instead.
738 ///
739 /// [`ParseStream::step`]: #method.step
David Tolnayec149b02018-09-01 14:17:28 -0700740 ///
741 /// # Example
742 ///
743 /// The parse implementation shown here parses possibly restricted `pub`
744 /// visibilities.
745 ///
746 /// - `pub`
747 /// - `pub(crate)`
748 /// - `pub(self)`
749 /// - `pub(super)`
750 /// - `pub(in some::path)`
751 ///
752 /// To handle the case of visibilities inside of tuple structs, the parser
753 /// needs to distinguish parentheses that specify visibility restrictions
754 /// from parentheses that form part of a tuple type.
755 ///
756 /// ```
757 /// # struct A;
758 /// # struct B;
759 /// # struct C;
760 /// #
761 /// struct S(pub(crate) A, pub (B, C));
762 /// ```
763 ///
764 /// In this example input the first tuple struct element of `S` has
765 /// `pub(crate)` visibility while the second tuple struct element has `pub`
766 /// visibility; the parentheses around `(B, C)` are part of the type rather
767 /// than part of a visibility restriction.
768 ///
769 /// The parser uses a forked parse stream to check the first token inside of
770 /// parentheses after the `pub` keyword. This is a small bounded amount of
771 /// work performed against the forked parse stream.
772 ///
773 /// ```
774 /// # extern crate syn;
775 /// #
776 /// use syn::{parenthesized, token, Ident, Path, Token};
777 /// use syn::ext::IdentExt;
778 /// use syn::parse::{Parse, ParseStream, Result};
779 ///
780 /// struct PubVisibility {
781 /// pub_token: Token![pub],
782 /// restricted: Option<Restricted>,
783 /// }
784 ///
785 /// struct Restricted {
786 /// paren_token: token::Paren,
787 /// in_token: Option<Token![in]>,
788 /// path: Path,
789 /// }
790 ///
791 /// impl Parse for PubVisibility {
792 /// fn parse(input: ParseStream) -> Result<Self> {
793 /// let pub_token: Token![pub] = input.parse()?;
794 ///
795 /// if input.peek(token::Paren) {
796 /// let ahead = input.fork();
797 /// let mut content;
798 /// parenthesized!(content in ahead);
799 ///
800 /// if content.peek(Token![crate])
801 /// || content.peek(Token![self])
802 /// || content.peek(Token![super])
803 /// {
804 /// return Ok(PubVisibility {
805 /// pub_token: pub_token,
806 /// restricted: Some(Restricted {
807 /// paren_token: parenthesized!(content in input),
808 /// in_token: None,
809 /// path: Path::from(content.call(Ident::parse_any)?),
810 /// }),
811 /// });
812 /// } else if content.peek(Token![in]) {
813 /// return Ok(PubVisibility {
814 /// pub_token: pub_token,
815 /// restricted: Some(Restricted {
816 /// paren_token: parenthesized!(content in input),
817 /// in_token: Some(content.parse()?),
818 /// path: content.call(Path::parse_mod_style)?,
819 /// }),
820 /// });
821 /// }
822 /// }
823 ///
824 /// Ok(PubVisibility {
825 /// pub_token: pub_token,
826 /// restricted: None,
827 /// })
828 /// }
829 /// }
830 /// #
831 /// # fn main() {}
832 /// ```
David Tolnayb77c8b62018-08-25 16:39:41 -0400833 pub fn fork(&self) -> Self {
David Tolnay6456a9d2018-08-26 08:11:18 -0400834 ParseBuffer {
835 scope: self.scope,
836 cell: self.cell.clone(),
837 marker: PhantomData,
838 // Not the parent's unexpected. Nothing cares whether the clone
839 // parses all the way.
840 unexpected: Rc::new(Cell::new(None)),
841 }
David Tolnayb77c8b62018-08-25 16:39:41 -0400842 }
843
David Tolnay725e1c62018-09-01 12:07:25 -0700844 /// Triggers an error at the current position of the parse stream.
David Tolnay23fce0b2018-09-01 13:50:31 -0700845 ///
846 /// # Example
847 ///
848 /// ```
849 /// # extern crate syn;
850 /// #
851 /// use syn::{Expr, Token};
852 /// use syn::parse::{Parse, ParseStream, Result};
853 ///
854 /// // Some kind of loop: `while` or `for` or `loop`.
855 /// struct Loop {
856 /// expr: Expr,
857 /// }
858 ///
859 /// impl Parse for Loop {
860 /// fn parse(input: ParseStream) -> Result<Self> {
861 /// if input.peek(Token![while])
862 /// || input.peek(Token![for])
863 /// || input.peek(Token![loop])
864 /// {
865 /// Ok(Loop {
866 /// expr: input.parse()?,
867 /// })
868 /// } else {
869 /// Err(input.error("expected some kind of loop"))
870 /// }
871 /// }
872 /// }
873 /// ```
David Tolnay4fb71232018-08-25 23:14:50 -0400874 pub fn error<T: Display>(&self, message: T) -> Error {
875 error::new_at(self.scope, self.cursor(), message)
876 }
877
David Tolnay725e1c62018-09-01 12:07:25 -0700878 /// Speculatively parses tokens from this parse stream, advancing the
879 /// position of this stream only if parsing succeeds.
David Tolnay9bd34392018-09-01 13:19:53 -0700880 ///
David Tolnayad1d1d22018-09-01 13:34:43 -0700881 /// This is a powerful low-level API used for defining the `Parse` impls of
882 /// the basic built-in token types. It is not something that will be used
883 /// widely outside of the Syn codebase.
884 ///
David Tolnay9bd34392018-09-01 13:19:53 -0700885 /// # Example
886 ///
887 /// ```
888 /// # extern crate proc_macro2;
889 /// # extern crate syn;
890 /// #
891 /// use proc_macro2::TokenTree;
892 /// use syn::parse::{ParseStream, Result};
893 ///
894 /// // This function advances the stream past the next occurrence of `@`. If
895 /// // no `@` is present in the stream, the stream position is unchanged and
896 /// // an error is returned.
897 /// fn skip_past_next_at(input: ParseStream) -> Result<()> {
898 /// input.step(|cursor| {
899 /// let mut rest = *cursor;
900 /// while let Some((tt, next)) = cursor.token_tree() {
901 /// match tt {
902 /// TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
903 /// return Ok(((), next));
904 /// }
905 /// _ => rest = next,
906 /// }
907 /// }
908 /// Err(cursor.error("no `@` was found after this point"))
909 /// })
910 /// }
911 /// #
912 /// # fn main() {}
913 /// ```
David Tolnayb50c65a2018-08-30 21:14:57 -0700914 pub fn step<F, R>(&self, function: F) -> Result<R>
David Tolnay18c754c2018-08-21 23:26:58 -0400915 where
916 F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,
917 {
David Tolnayc142b092018-09-02 08:52:52 -0700918 // Since the user's function is required to work for any 'c, we know
919 // that the Cursor<'c> they return is either derived from the input
920 // StepCursor<'c, 'a> or from a Cursor<'static>.
921 //
922 // It would not be legal to write this function without the invariant
923 // lifetime 'c in StepCursor<'c, 'a>. If this function were written only
924 // in terms of 'a, the user could take our ParseBuffer<'a>, upcast it to
925 // a ParseBuffer<'short> which some shorter lifetime than 'a, invoke
926 // `step` on their ParseBuffer<'short> with a closure that returns
927 // Cursor<'short>, and we would wrongly write that Cursor<'short> into
928 // the Cell intended to hold Cursor<'a>.
929 //
930 // In some cases it may be necessary for R to contain a Cursor<'a>.
931 // Within Syn we solve this using `private::advance_step_cursor` which
932 // uses the existence of a StepCursor<'c, 'a> as proof that it is safe
933 // to cast from Cursor<'c> to Cursor<'a>. If needed outside of Syn, it
934 // would be safe to expose that API as a method on StepCursor.
David Tolnay6b65f852018-09-01 11:56:25 -0700935 let (node, rest) = function(StepCursor {
David Tolnay18c754c2018-08-21 23:26:58 -0400936 scope: self.scope,
937 cursor: self.cell.get(),
938 marker: PhantomData,
David Tolnay6b65f852018-09-01 11:56:25 -0700939 })?;
940 self.cell.set(rest);
941 Ok(node)
David Tolnay18c754c2018-08-21 23:26:58 -0400942 }
David Tolnayeafc8052018-08-25 16:33:53 -0400943
David Tolnay725e1c62018-09-01 12:07:25 -0700944 /// Provides low-level access to the token representation underlying this
945 /// parse stream.
946 ///
947 /// Cursors are immutable so no operations you perform against the cursor
948 /// will affect the state of this parse stream.
David Tolnayf5d30452018-09-01 02:29:04 -0700949 pub fn cursor(&self) -> Cursor<'a> {
950 self.cell.get()
951 }
952
David Tolnay94f06632018-08-31 10:17:17 -0700953 fn check_unexpected(&self) -> Result<()> {
David Tolnayeafc8052018-08-25 16:33:53 -0400954 match self.unexpected.get() {
955 Some(span) => Err(Error::new(span, "unexpected token")),
956 None => Ok(()),
957 }
958 }
David Tolnay18c754c2018-08-21 23:26:58 -0400959}
960
David Tolnaya7d69fc2018-08-26 13:30:24 -0400961impl<T: Parse> Parse for Box<T> {
962 fn parse(input: ParseStream) -> Result<Self> {
963 input.parse().map(Box::new)
964 }
965}
966
David Tolnay4fb71232018-08-25 23:14:50 -0400967impl<T: Parse + Token> Parse for Option<T> {
David Tolnay18c754c2018-08-21 23:26:58 -0400968 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay00f81fd2018-09-01 10:50:12 -0700969 if T::peek(input.cursor()) {
David Tolnay4fb71232018-08-25 23:14:50 -0400970 Ok(Some(input.parse()?))
971 } else {
972 Ok(None)
David Tolnay18c754c2018-08-21 23:26:58 -0400973 }
David Tolnay18c754c2018-08-21 23:26:58 -0400974 }
975}
David Tolnay4ac232d2018-08-31 10:18:03 -0700976
David Tolnay80a914f2018-08-30 23:49:53 -0700977impl Parse for TokenStream {
978 fn parse(input: ParseStream) -> Result<Self> {
979 input.step(|cursor| Ok((cursor.token_stream(), Cursor::empty())))
980 }
981}
982
983impl Parse for TokenTree {
984 fn parse(input: ParseStream) -> Result<Self> {
985 input.step(|cursor| match cursor.token_tree() {
986 Some((tt, rest)) => Ok((tt, rest)),
987 None => Err(cursor.error("expected token tree")),
988 })
989 }
990}
991
992impl Parse for Group {
993 fn parse(input: ParseStream) -> Result<Self> {
994 input.step(|cursor| {
995 for delim in &[Delimiter::Parenthesis, Delimiter::Brace, Delimiter::Bracket] {
996 if let Some((inside, span, rest)) = cursor.group(*delim) {
997 let mut group = Group::new(*delim, inside.token_stream());
998 group.set_span(span);
999 return Ok((group, rest));
1000 }
1001 }
1002 Err(cursor.error("expected group token"))
1003 })
1004 }
1005}
1006
1007impl Parse for Punct {
1008 fn parse(input: ParseStream) -> Result<Self> {
1009 input.step(|cursor| match cursor.punct() {
1010 Some((punct, rest)) => Ok((punct, rest)),
1011 None => Err(cursor.error("expected punctuation token")),
1012 })
1013 }
1014}
1015
1016impl Parse for Literal {
1017 fn parse(input: ParseStream) -> Result<Self> {
1018 input.step(|cursor| match cursor.literal() {
1019 Some((literal, rest)) => Ok((literal, rest)),
1020 None => Err(cursor.error("expected literal token")),
1021 })
1022 }
1023}
1024
1025/// Parser that can parse Rust tokens into a particular syntax tree node.
1026///
1027/// Refer to the [module documentation] for details about parsing in Syn.
1028///
1029/// [module documentation]: index.html
1030///
1031/// *This trait is available if Syn is built with the `"parsing"` feature.*
1032pub trait Parser: Sized {
1033 type Output;
1034
1035 /// Parse a proc-macro2 token stream into the chosen syntax tree node.
1036 fn parse2(self, tokens: TokenStream) -> Result<Self::Output>;
1037
1038 /// Parse tokens of source code into the chosen syntax tree node.
1039 ///
1040 /// *This method is available if Syn is built with both the `"parsing"` and
1041 /// `"proc-macro"` features.*
1042 #[cfg(all(
1043 not(all(target_arch = "wasm32", target_os = "unknown")),
1044 feature = "proc-macro"
1045 ))]
1046 fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output> {
1047 self.parse2(proc_macro2::TokenStream::from(tokens))
1048 }
1049
1050 /// Parse a string of Rust code into the chosen syntax tree node.
1051 ///
1052 /// # Hygiene
1053 ///
1054 /// Every span in the resulting syntax tree will be set to resolve at the
1055 /// macro call site.
1056 fn parse_str(self, s: &str) -> Result<Self::Output> {
1057 self.parse2(proc_macro2::TokenStream::from_str(s)?)
1058 }
1059}
1060
David Tolnay7b07aa12018-09-01 11:41:12 -07001061fn tokens_to_parse_buffer(tokens: &TokenBuffer) -> ParseBuffer {
1062 let scope = Span::call_site();
1063 let cursor = tokens.begin();
1064 let unexpected = Rc::new(Cell::new(None));
1065 private::new_parse_buffer(scope, cursor, unexpected)
1066}
1067
David Tolnay80a914f2018-08-30 23:49:53 -07001068impl<F, T> Parser for F
1069where
1070 F: FnOnce(ParseStream) -> Result<T>,
1071{
1072 type Output = T;
1073
1074 fn parse2(self, tokens: TokenStream) -> Result<T> {
1075 let buf = TokenBuffer::new2(tokens);
David Tolnay7b07aa12018-09-01 11:41:12 -07001076 let state = tokens_to_parse_buffer(&buf);
David Tolnay80a914f2018-08-30 23:49:53 -07001077 let node = self(&state)?;
1078 state.check_unexpected()?;
1079 if state.is_empty() {
1080 Ok(node)
1081 } else {
1082 Err(state.error("unexpected token"))
1083 }
1084 }
1085}