blob: a106de502a8edfea09a7b990a784f8d3b2295f0a [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
David Tolnay8e6096a2018-09-06 02:14:47 -070029//! procedural macro through [`parse_macro_input!`] as shown at the bottom of
30//! the snippet. If the caller provides syntactically invalid input to the
31//! procedural macro, they will receive a helpful compiler error message
32//! pointing out the exact token that triggered the failure to parse.
33//!
34//! [`parse_macro_input!`]: ../macro.parse_macro_input.html
David Tolnay88d9f622018-09-01 17:52:33 -070035//!
David Tolnay43984452018-09-01 17:43:56 -070036//! ```
David Tolnay88d9f622018-09-01 17:52:33 -070037//! # extern crate proc_macro;
David Tolnay43984452018-09-01 17:43:56 -070038//! # extern crate syn;
39//! #
David Tolnay88d9f622018-09-01 17:52:33 -070040//! use proc_macro::TokenStream;
41//! use syn::{braced, parse_macro_input, token, Field, Ident, Token};
David Tolnay43984452018-09-01 17:43:56 -070042//! use syn::parse::{Parse, ParseStream, Result};
43//! use syn::punctuated::Punctuated;
44//!
45//! enum Item {
46//! Struct(ItemStruct),
47//! Enum(ItemEnum),
48//! }
49//!
50//! struct ItemStruct {
51//! struct_token: Token![struct],
52//! ident: Ident,
53//! brace_token: token::Brace,
54//! fields: Punctuated<Field, Token![,]>,
55//! }
56//! #
57//! # enum ItemEnum {}
58//!
59//! impl Parse for Item {
60//! fn parse(input: ParseStream) -> Result<Self> {
61//! let lookahead = input.lookahead1();
62//! if lookahead.peek(Token![struct]) {
63//! input.parse().map(Item::Struct)
64//! } else if lookahead.peek(Token![enum]) {
65//! input.parse().map(Item::Enum)
66//! } else {
67//! Err(lookahead.error())
68//! }
69//! }
70//! }
71//!
72//! impl Parse for ItemStruct {
73//! fn parse(input: ParseStream) -> Result<Self> {
74//! let content;
75//! Ok(ItemStruct {
76//! struct_token: input.parse()?,
77//! ident: input.parse()?,
78//! brace_token: braced!(content in input),
79//! fields: content.parse_terminated(Field::parse_named)?,
80//! })
81//! }
82//! }
83//! #
84//! # impl Parse for ItemEnum {
85//! # fn parse(input: ParseStream) -> Result<Self> {
86//! # unimplemented!()
87//! # }
88//! # }
David Tolnay88d9f622018-09-01 17:52:33 -070089//!
90//! # const IGNORE: &str = stringify! {
91//! #[proc_macro]
92//! # };
93//! pub fn my_macro(tokens: TokenStream) -> TokenStream {
94//! let input = parse_macro_input!(tokens as Item);
95//!
96//! /* ... */
97//! # "".parse().unwrap()
98//! }
99//! #
100//! # fn main() {}
David Tolnay43984452018-09-01 17:43:56 -0700101//! ```
102//!
103//! # The `syn::parse*` functions
David Tolnay80a914f2018-08-30 23:49:53 -0700104//!
105//! The [`syn::parse`], [`syn::parse2`], and [`syn::parse_str`] functions serve
106//! as an entry point for parsing syntax tree nodes that can be parsed in an
107//! obvious default way. These functions can return any syntax tree node that
David Tolnay8aacee12018-08-31 09:15:15 -0700108//! implements the [`Parse`] trait, which includes most types in Syn.
David Tolnay80a914f2018-08-30 23:49:53 -0700109//!
110//! [`syn::parse`]: ../fn.parse.html
111//! [`syn::parse2`]: ../fn.parse2.html
112//! [`syn::parse_str`]: ../fn.parse_str.html
David Tolnay8aacee12018-08-31 09:15:15 -0700113//! [`Parse`]: trait.Parse.html
David Tolnay80a914f2018-08-30 23:49:53 -0700114//!
115//! ```
116//! use syn::Type;
117//!
David Tolnay8aacee12018-08-31 09:15:15 -0700118//! # fn run_parser() -> Result<(), syn::parse::Error> {
David Tolnay80a914f2018-08-30 23:49:53 -0700119//! let t: Type = syn::parse_str("std::collections::HashMap<String, Value>")?;
120//! # Ok(())
121//! # }
122//! #
123//! # fn main() {
124//! # run_parser().unwrap();
125//! # }
126//! ```
127//!
128//! The [`parse_quote!`] macro also uses this approach.
129//!
130//! [`parse_quote!`]: ../macro.parse_quote.html
131//!
David Tolnay43984452018-09-01 17:43:56 -0700132//! # The `Parser` trait
David Tolnay80a914f2018-08-30 23:49:53 -0700133//!
134//! Some types can be parsed in several ways depending on context. For example
135//! an [`Attribute`] can be either "outer" like `#[...]` or "inner" like
136//! `#![...]` and parsing the wrong one would be a bug. Similarly [`Punctuated`]
137//! may or may not allow trailing punctuation, and parsing it the wrong way
138//! would either reject valid input or accept invalid input.
139//!
140//! [`Attribute`]: ../struct.Attribute.html
141//! [`Punctuated`]: ../punctuated/index.html
142//!
David Tolnaye0c51762018-08-31 11:05:22 -0700143//! The `Parse` trait is not implemented in these cases because there is no good
David Tolnay80a914f2018-08-30 23:49:53 -0700144//! behavior to consider the default.
145//!
146//! ```ignore
147//! // Can't parse `Punctuated` without knowing whether trailing punctuation
148//! // should be allowed in this context.
149//! let path: Punctuated<PathSegment, Token![::]> = syn::parse(tokens)?;
150//! ```
151//!
152//! In these cases the types provide a choice of parser functions rather than a
David Tolnaye0c51762018-08-31 11:05:22 -0700153//! single `Parse` implementation, and those parser functions can be invoked
David Tolnay80a914f2018-08-30 23:49:53 -0700154//! through the [`Parser`] trait.
155//!
156//! [`Parser`]: trait.Parser.html
157//!
158//! ```
David Tolnay80a914f2018-08-30 23:49:53 -0700159//! # extern crate syn;
160//! #
161//! # extern crate proc_macro2;
162//! # use proc_macro2::TokenStream;
163//! #
David Tolnay3e3f7752018-08-31 09:33:59 -0700164//! use syn::parse::Parser;
David Tolnay80a914f2018-08-30 23:49:53 -0700165//! use syn::punctuated::Punctuated;
David Tolnay9b00f652018-09-01 10:31:02 -0700166//! use syn::{Attribute, Expr, PathSegment, Token};
David Tolnay80a914f2018-08-30 23:49:53 -0700167//!
David Tolnay3e3f7752018-08-31 09:33:59 -0700168//! # fn run_parsers() -> Result<(), syn::parse::Error> {
David Tolnay80a914f2018-08-30 23:49:53 -0700169//! # let tokens = TokenStream::new().into();
170//! // Parse a nonempty sequence of path segments separated by `::` punctuation
171//! // with no trailing punctuation.
172//! let parser = Punctuated::<PathSegment, Token![::]>::parse_separated_nonempty;
173//! let path = parser.parse(tokens)?;
174//!
175//! # let tokens = TokenStream::new().into();
176//! // Parse a possibly empty sequence of expressions terminated by commas with
177//! // an optional trailing punctuation.
178//! let parser = Punctuated::<Expr, Token![,]>::parse_terminated;
179//! let args = parser.parse(tokens)?;
180//!
181//! # let tokens = TokenStream::new().into();
182//! // Parse zero or more outer attributes but not inner attributes.
David Tolnay3e3f7752018-08-31 09:33:59 -0700183//! let parser = Attribute::parse_outer;
184//! let attrs = parser.parse(tokens)?;
David Tolnay80a914f2018-08-30 23:49:53 -0700185//! #
186//! # Ok(())
187//! # }
188//! #
189//! # fn main() {}
190//! ```
191//!
David Tolnaye0c51762018-08-31 11:05:22 -0700192//! ---
David Tolnay80a914f2018-08-30 23:49:53 -0700193//!
194//! *This module is available if Syn is built with the `"parsing"` feature.*
David Tolnay18c754c2018-08-21 23:26:58 -0400195
196use std::cell::Cell;
197use std::fmt::Display;
198use std::marker::PhantomData;
199use std::mem;
200use std::ops::Deref;
David Tolnayeafc8052018-08-25 16:33:53 -0400201use std::rc::Rc;
David Tolnay80a914f2018-08-30 23:49:53 -0700202use std::str::FromStr;
David Tolnayeafc8052018-08-25 16:33:53 -0400203
David Tolnay80a914f2018-08-30 23:49:53 -0700204#[cfg(all(
205 not(all(target_arch = "wasm32", target_os = "unknown")),
206 feature = "proc-macro"
207))]
208use proc_macro;
David Tolnayf07b3342018-09-01 11:58:11 -0700209use proc_macro2::{self, Delimiter, Group, Literal, Punct, Span, TokenStream, TokenTree};
David Tolnay18c754c2018-08-21 23:26:58 -0400210
David Tolnay80a914f2018-08-30 23:49:53 -0700211use buffer::{Cursor, TokenBuffer};
David Tolnayb6254182018-08-25 08:44:54 -0400212use error;
David Tolnay94f06632018-08-31 10:17:17 -0700213use lookahead;
214use private;
David Tolnay577d0332018-08-25 21:45:24 -0400215use punctuated::Punctuated;
David Tolnay4fb71232018-08-25 23:14:50 -0400216use token::Token;
David Tolnay18c754c2018-08-21 23:26:58 -0400217
David Tolnayb6254182018-08-25 08:44:54 -0400218pub use error::{Error, Result};
219pub use lookahead::{Lookahead1, Peek};
David Tolnay18c754c2018-08-21 23:26:58 -0400220
221/// Parsing interface implemented by all types that can be parsed in a default
222/// way from a token stream.
223pub trait Parse: Sized {
224 fn parse(input: ParseStream) -> Result<Self>;
225}
226
227/// Input to a Syn parser function.
David Tolnaya0daa482018-09-01 02:09:40 -0700228///
229/// See the methods of this type under the documentation of [`ParseBuffer`]. For
230/// an overview of parsing in Syn, refer to the [module documentation].
231///
232/// [module documentation]: index.html
David Tolnay18c754c2018-08-21 23:26:58 -0400233pub type ParseStream<'a> = &'a ParseBuffer<'a>;
234
235/// Cursor position within a buffered token stream.
David Tolnay20d29a12018-09-01 15:15:33 -0700236///
237/// This type is more commonly used through the type alias [`ParseStream`] which
238/// is an alias for `&ParseBuffer`.
239///
240/// `ParseStream` is the input type for all parser functions in Syn. They have
241/// the signature `fn(ParseStream) -> Result<T>`.
David Tolnay18c754c2018-08-21 23:26:58 -0400242pub struct ParseBuffer<'a> {
243 scope: Span,
David Tolnay5d7f2252018-09-02 08:21:40 -0700244 // Instead of Cell<Cursor<'a>> so that ParseBuffer<'a> is covariant in 'a.
245 // The rest of the code in this module needs to be careful that only a
246 // cursor derived from this `cell` is ever assigned to this `cell`.
247 //
248 // Cell<Cursor<'a>> cannot be covariant in 'a because then we could take a
249 // ParseBuffer<'a>, upcast to ParseBuffer<'short> for some lifetime shorter
250 // than 'a, and then assign a Cursor<'short> into the Cell.
251 //
252 // By extension, it would not be safe to expose an API that accepts a
253 // Cursor<'a> and trusts that it lives as long as the cursor currently in
254 // the cell.
David Tolnay18c754c2018-08-21 23:26:58 -0400255 cell: Cell<Cursor<'static>>,
256 marker: PhantomData<Cursor<'a>>,
David Tolnayeafc8052018-08-25 16:33:53 -0400257 unexpected: Rc<Cell<Option<Span>>>,
258}
259
260impl<'a> Drop for ParseBuffer<'a> {
261 fn drop(&mut self) {
262 if !self.is_empty() && self.unexpected.get().is_none() {
263 self.unexpected.set(Some(self.cursor().span()));
264 }
265 }
David Tolnay18c754c2018-08-21 23:26:58 -0400266}
267
David Tolnay642832f2018-09-01 13:08:10 -0700268/// Cursor state associated with speculative parsing.
269///
270/// This type is the input of the closure provided to [`ParseStream::step`].
271///
272/// [`ParseStream::step`]: struct.ParseBuffer.html#method.step
David Tolnay9bd34392018-09-01 13:19:53 -0700273///
274/// # Example
275///
276/// ```
277/// # extern crate proc_macro2;
278/// # extern crate syn;
279/// #
280/// use proc_macro2::TokenTree;
281/// use syn::parse::{ParseStream, Result};
282///
283/// // This function advances the stream past the next occurrence of `@`. If
284/// // no `@` is present in the stream, the stream position is unchanged and
285/// // an error is returned.
286/// fn skip_past_next_at(input: ParseStream) -> Result<()> {
287/// input.step(|cursor| {
288/// let mut rest = *cursor;
289/// while let Some((tt, next)) = cursor.token_tree() {
290/// match tt {
291/// TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
292/// return Ok(((), next));
293/// }
294/// _ => rest = next,
295/// }
296/// }
297/// Err(cursor.error("no `@` was found after this point"))
298/// })
299/// }
300/// #
301/// # fn main() {}
302/// ```
David Tolnay18c754c2018-08-21 23:26:58 -0400303#[derive(Copy, Clone)]
304pub struct StepCursor<'c, 'a> {
305 scope: Span,
David Tolnay56924f42018-09-02 08:24:58 -0700306 // This field is covariant in 'c.
David Tolnay18c754c2018-08-21 23:26:58 -0400307 cursor: Cursor<'c>,
David Tolnay56924f42018-09-02 08:24:58 -0700308 // This field is contravariant in 'c. Together these make StepCursor
309 // invariant in 'c. Also covariant in 'a. The user cannot cast 'c to a
310 // different lifetime but can upcast into a StepCursor with a shorter
311 // lifetime 'a.
312 //
313 // As long as we only ever construct a StepCursor for which 'c outlives 'a,
314 // this means if ever a StepCursor<'c, 'a> exists we are guaranteed that 'c
315 // outlives 'a.
David Tolnay18c754c2018-08-21 23:26:58 -0400316 marker: PhantomData<fn(Cursor<'c>) -> Cursor<'a>>,
317}
318
319impl<'c, 'a> Deref for StepCursor<'c, 'a> {
320 type Target = Cursor<'c>;
321
322 fn deref(&self) -> &Self::Target {
323 &self.cursor
324 }
325}
326
327impl<'c, 'a> StepCursor<'c, 'a> {
David Tolnay642832f2018-09-01 13:08:10 -0700328 /// Triggers an error at the current position of the parse stream.
329 ///
330 /// The `ParseStream::step` invocation will return this same error without
331 /// advancing the stream state.
David Tolnay18c754c2018-08-21 23:26:58 -0400332 pub fn error<T: Display>(self, message: T) -> Error {
333 error::new_at(self.scope, self.cursor, message)
334 }
335}
336
David Tolnay6ea3fdc2018-09-01 13:30:53 -0700337impl private {
338 pub fn advance_step_cursor<'c, 'a>(proof: StepCursor<'c, 'a>, to: Cursor<'c>) -> Cursor<'a> {
David Tolnay56924f42018-09-02 08:24:58 -0700339 // Refer to the comments within the StepCursor definition. We use the
340 // fact that a StepCursor<'c, 'a> exists as proof that 'c outlives 'a.
341 // Cursor is covariant in its lifetime parameter so we can cast a
342 // Cursor<'c> to one with the shorter lifetime Cursor<'a>.
David Tolnay6ea3fdc2018-09-01 13:30:53 -0700343 let _ = proof;
344 unsafe { mem::transmute::<Cursor<'c>, Cursor<'a>>(to) }
345 }
346}
347
David Tolnay66cb0c42018-08-31 09:01:30 -0700348fn skip(input: ParseStream) -> bool {
David Tolnay4ac232d2018-08-31 10:18:03 -0700349 input
350 .step(|cursor| {
351 if let Some((_lifetime, rest)) = cursor.lifetime() {
352 Ok((true, rest))
353 } else if let Some((_token, rest)) = cursor.token_tree() {
354 Ok((true, rest))
355 } else {
356 Ok((false, *cursor))
357 }
358 }).unwrap()
David Tolnay66cb0c42018-08-31 09:01:30 -0700359}
360
David Tolnay10951d52018-08-31 10:27:39 -0700361impl private {
David Tolnay70f30e92018-09-01 02:04:17 -0700362 pub fn new_parse_buffer(
363 scope: Span,
364 cursor: Cursor,
365 unexpected: Rc<Cell<Option<Span>>>,
366 ) -> ParseBuffer {
David Tolnay18c754c2018-08-21 23:26:58 -0400367 ParseBuffer {
368 scope: scope,
David Tolnay5d7f2252018-09-02 08:21:40 -0700369 // See comment on `cell` in the struct definition.
370 cell: Cell::new(unsafe { mem::transmute::<Cursor, Cursor<'static>>(cursor) }),
David Tolnay18c754c2018-08-21 23:26:58 -0400371 marker: PhantomData,
David Tolnayeafc8052018-08-25 16:33:53 -0400372 unexpected: unexpected,
David Tolnay18c754c2018-08-21 23:26:58 -0400373 }
374 }
375
David Tolnay94f06632018-08-31 10:17:17 -0700376 pub fn get_unexpected(buffer: &ParseBuffer) -> Rc<Cell<Option<Span>>> {
377 buffer.unexpected.clone()
378 }
379}
380
381impl<'a> ParseBuffer<'a> {
David Tolnay725e1c62018-09-01 12:07:25 -0700382 /// Parses a syntax tree node of type `T`, advancing the position of our
383 /// parse stream past it.
David Tolnay18c754c2018-08-21 23:26:58 -0400384 pub fn parse<T: Parse>(&self) -> Result<T> {
385 T::parse(self)
386 }
387
David Tolnay725e1c62018-09-01 12:07:25 -0700388 /// Calls the given parser function to parse a syntax tree node of type `T`
389 /// from this stream.
David Tolnay21ce84c2018-09-01 15:37:51 -0700390 ///
391 /// # Example
392 ///
393 /// The parser below invokes [`Attribute::parse_outer`] to parse a vector of
394 /// zero or more outer attributes.
395 ///
396 /// [`Attribute::parse_outer`]: ../struct.Attribute.html#method.parse_outer
397 ///
398 /// ```
399 /// # extern crate syn;
400 /// #
401 /// use syn::{Attribute, Ident, Token};
402 /// use syn::parse::{Parse, ParseStream, Result};
403 ///
404 /// // Parses a unit struct with attributes.
405 /// //
406 /// // #[path = "s.tmpl"]
407 /// // struct S;
408 /// struct UnitStruct {
409 /// attrs: Vec<Attribute>,
410 /// struct_token: Token![struct],
411 /// name: Ident,
412 /// semi_token: Token![;],
413 /// }
414 ///
415 /// impl Parse for UnitStruct {
416 /// fn parse(input: ParseStream) -> Result<Self> {
417 /// Ok(UnitStruct {
418 /// attrs: input.call(Attribute::parse_outer)?,
419 /// struct_token: input.parse()?,
420 /// name: input.parse()?,
421 /// semi_token: input.parse()?,
422 /// })
423 /// }
424 /// }
425 /// #
426 /// # fn main() {}
427 /// ```
David Tolnay3a515a02018-08-25 21:08:27 -0400428 pub fn call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T> {
429 function(self)
430 }
431
David Tolnay725e1c62018-09-01 12:07:25 -0700432 /// Looks at the next token in the parse stream to determine whether it
433 /// matches the requested type of token.
434 ///
435 /// Does not advance the position of the parse stream.
David Tolnayddebc3e2018-09-01 16:29:20 -0700436 ///
David Tolnay7d229e82018-09-01 16:42:34 -0700437 /// # Syntax
438 ///
439 /// Note that this method does not use turbofish syntax. Pass the peek type
440 /// inside of parentheses.
441 ///
442 /// - `input.peek(Token![struct])`
443 /// - `input.peek(Token![==])`
444 /// - `input.peek(Ident)`
445 /// - `input.peek(Lifetime)`
446 /// - `input.peek(token::Brace)`
447 ///
David Tolnayddebc3e2018-09-01 16:29:20 -0700448 /// # Example
449 ///
450 /// In this example we finish parsing the list of supertraits when the next
451 /// token in the input is either `where` or an opening curly brace.
452 ///
453 /// ```
454 /// # extern crate syn;
455 /// #
456 /// use syn::{braced, token, Generics, Ident, Token, TypeParamBound};
457 /// use syn::parse::{Parse, ParseStream, Result};
458 /// use syn::punctuated::Punctuated;
459 ///
460 /// // Parses a trait definition containing no associated items.
461 /// //
462 /// // trait Marker<'de, T>: A + B<'de> where Box<T>: Clone {}
463 /// struct MarkerTrait {
464 /// trait_token: Token![trait],
465 /// ident: Ident,
466 /// generics: Generics,
467 /// colon_token: Option<Token![:]>,
468 /// supertraits: Punctuated<TypeParamBound, Token![+]>,
469 /// brace_token: token::Brace,
470 /// }
471 ///
472 /// impl Parse for MarkerTrait {
473 /// fn parse(input: ParseStream) -> Result<Self> {
474 /// let trait_token: Token![trait] = input.parse()?;
475 /// let ident: Ident = input.parse()?;
476 /// let mut generics: Generics = input.parse()?;
477 /// let colon_token: Option<Token![:]> = input.parse()?;
478 ///
479 /// let mut supertraits = Punctuated::new();
480 /// if colon_token.is_some() {
481 /// loop {
482 /// supertraits.push_value(input.parse()?);
483 /// if input.peek(Token![where]) || input.peek(token::Brace) {
484 /// break;
485 /// }
486 /// supertraits.push_punct(input.parse()?);
487 /// }
488 /// }
489 ///
490 /// generics.where_clause = input.parse()?;
491 /// let content;
492 /// let empty_brace_token = braced!(content in input);
493 ///
494 /// Ok(MarkerTrait {
495 /// trait_token: trait_token,
496 /// ident: ident,
497 /// generics: generics,
498 /// colon_token: colon_token,
499 /// supertraits: supertraits,
500 /// brace_token: empty_brace_token,
501 /// })
502 /// }
503 /// }
504 /// #
505 /// # fn main() {}
506 /// ```
David Tolnayb77c8b62018-08-25 16:39:41 -0400507 pub fn peek<T: Peek>(&self, token: T) -> bool {
David Tolnay576779a2018-09-01 11:54:12 -0700508 let _ = token;
509 T::Token::peek(self.cursor())
David Tolnayb77c8b62018-08-25 16:39:41 -0400510 }
511
David Tolnay725e1c62018-09-01 12:07:25 -0700512 /// Looks at the second-next token in the parse stream.
David Tolnaye334b872018-09-01 16:38:10 -0700513 ///
514 /// This is commonly useful as a way to implement contextual keywords.
515 ///
516 /// # Example
517 ///
518 /// This example needs to use `peek2` because the symbol `union` is not a
519 /// keyword in Rust. We can't use just `peek` and decide to parse a union if
520 /// the very next token is `union`, because someone is free to write a `mod
521 /// union` and a macro invocation that looks like `union::some_macro! { ...
522 /// }`. In other words `union` is a contextual keyword.
523 ///
524 /// ```
525 /// # extern crate syn;
526 /// #
527 /// use syn::{Ident, ItemUnion, Macro, Token};
528 /// use syn::parse::{Parse, ParseStream, Result};
529 ///
530 /// // Parses either a union or a macro invocation.
531 /// enum UnionOrMacro {
532 /// // union MaybeUninit<T> { uninit: (), value: T }
533 /// Union(ItemUnion),
534 /// // lazy_static! { ... }
535 /// Macro(Macro),
536 /// }
537 ///
538 /// impl Parse for UnionOrMacro {
539 /// fn parse(input: ParseStream) -> Result<Self> {
540 /// if input.peek(Token![union]) && input.peek2(Ident) {
541 /// input.parse().map(UnionOrMacro::Union)
542 /// } else {
543 /// input.parse().map(UnionOrMacro::Macro)
544 /// }
545 /// }
546 /// }
547 /// #
548 /// # fn main() {}
549 /// ```
David Tolnay4fb71232018-08-25 23:14:50 -0400550 pub fn peek2<T: Peek>(&self, token: T) -> bool {
David Tolnay4fb71232018-08-25 23:14:50 -0400551 let ahead = self.fork();
David Tolnay66cb0c42018-08-31 09:01:30 -0700552 skip(&ahead) && ahead.peek(token)
David Tolnay4fb71232018-08-25 23:14:50 -0400553 }
554
David Tolnay725e1c62018-09-01 12:07:25 -0700555 /// Looks at the third-next token in the parse stream.
David Tolnay4fb71232018-08-25 23:14:50 -0400556 pub fn peek3<T: Peek>(&self, token: T) -> bool {
David Tolnay4fb71232018-08-25 23:14:50 -0400557 let ahead = self.fork();
David Tolnay66cb0c42018-08-31 09:01:30 -0700558 skip(&ahead) && skip(&ahead) && ahead.peek(token)
David Tolnay4fb71232018-08-25 23:14:50 -0400559 }
560
David Tolnay725e1c62018-09-01 12:07:25 -0700561 /// Parses zero or more occurrences of `T` separated by punctuation of type
562 /// `P`, with optional trailing punctuation.
563 ///
564 /// Parsing continues until the end of this parse stream. The entire content
565 /// of this parse stream must consist of `T` and `P`.
David Tolnay0abe65b2018-09-01 14:31:43 -0700566 ///
567 /// # Example
568 ///
569 /// ```rust
570 /// # extern crate quote;
571 /// # extern crate syn;
572 /// #
573 /// # use quote::quote;
574 /// #
575 /// use syn::{parenthesized, token, Ident, Token, Type};
576 /// use syn::parse::{Parse, ParseStream, Result};
577 /// use syn::punctuated::Punctuated;
578 ///
579 /// // Parse a simplified tuple struct syntax like:
580 /// //
581 /// // struct S(A, B);
582 /// struct TupleStruct {
583 /// struct_token: Token![struct],
584 /// ident: Ident,
585 /// paren_token: token::Paren,
586 /// fields: Punctuated<Type, Token![,]>,
587 /// semi_token: Token![;],
588 /// }
589 ///
590 /// impl Parse for TupleStruct {
591 /// fn parse(input: ParseStream) -> Result<Self> {
592 /// let content;
593 /// Ok(TupleStruct {
594 /// struct_token: input.parse()?,
595 /// ident: input.parse()?,
596 /// paren_token: parenthesized!(content in input),
597 /// fields: content.parse_terminated(Type::parse)?,
598 /// semi_token: input.parse()?,
599 /// })
600 /// }
601 /// }
602 /// #
603 /// # fn main() {
604 /// # let input = quote! {
605 /// # struct S(A, B);
606 /// # };
607 /// # syn::parse2::<TupleStruct>(input).unwrap();
608 /// # }
609 /// ```
David Tolnay577d0332018-08-25 21:45:24 -0400610 pub fn parse_terminated<T, P: Parse>(
611 &self,
612 parser: fn(ParseStream) -> Result<T>,
613 ) -> Result<Punctuated<T, P>> {
David Tolnayd0f80212018-08-30 18:32:14 -0700614 Punctuated::parse_terminated_with(self, parser)
David Tolnay577d0332018-08-25 21:45:24 -0400615 }
616
David Tolnay725e1c62018-09-01 12:07:25 -0700617 /// Returns whether there are tokens remaining in this stream.
618 ///
619 /// This method returns true at the end of the content of a set of
620 /// delimiters, as well as at the very end of the complete macro input.
David Tolnaycce6b5f2018-09-01 14:24:46 -0700621 ///
622 /// # Example
623 ///
624 /// ```rust
625 /// # extern crate syn;
626 /// #
627 /// use syn::{braced, token, Ident, Item, Token};
628 /// use syn::parse::{Parse, ParseStream, Result};
629 ///
630 /// // Parses a Rust `mod m { ... }` containing zero or more items.
631 /// struct Mod {
632 /// mod_token: Token![mod],
633 /// name: Ident,
634 /// brace_token: token::Brace,
635 /// items: Vec<Item>,
636 /// }
637 ///
638 /// impl Parse for Mod {
639 /// fn parse(input: ParseStream) -> Result<Self> {
640 /// let content;
641 /// Ok(Mod {
642 /// mod_token: input.parse()?,
643 /// name: input.parse()?,
644 /// brace_token: braced!(content in input),
645 /// items: {
646 /// let mut items = Vec::new();
647 /// while !content.is_empty() {
648 /// items.push(content.parse()?);
649 /// }
650 /// items
651 /// },
652 /// })
653 /// }
654 /// }
655 /// #
656 /// # fn main() {}
David Tolnayf5d30452018-09-01 02:29:04 -0700657 pub fn is_empty(&self) -> bool {
658 self.cursor().eof()
659 }
660
David Tolnay725e1c62018-09-01 12:07:25 -0700661 /// Constructs a helper for peeking at the next token in this stream and
662 /// building an error message if it is not one of a set of expected tokens.
David Tolnay2c77e772018-09-01 14:18:46 -0700663 ///
664 /// # Example
665 ///
666 /// ```
667 /// # extern crate syn;
668 /// #
669 /// use syn::{ConstParam, Ident, Lifetime, LifetimeDef, Token, TypeParam};
670 /// use syn::parse::{Parse, ParseStream, Result};
671 ///
672 /// // A generic parameter, a single one of the comma-separated elements inside
673 /// // angle brackets in:
674 /// //
675 /// // fn f<T: Clone, 'a, 'b: 'a, const N: usize>() { ... }
676 /// //
677 /// // On invalid input, lookahead gives us a reasonable error message.
678 /// //
679 /// // error: expected one of: identifier, lifetime, `const`
680 /// // |
681 /// // 5 | fn f<!Sized>() {}
682 /// // | ^
683 /// enum GenericParam {
684 /// Type(TypeParam),
685 /// Lifetime(LifetimeDef),
686 /// Const(ConstParam),
687 /// }
688 ///
689 /// impl Parse for GenericParam {
690 /// fn parse(input: ParseStream) -> Result<Self> {
691 /// let lookahead = input.lookahead1();
692 /// if lookahead.peek(Ident) {
693 /// input.parse().map(GenericParam::Type)
694 /// } else if lookahead.peek(Lifetime) {
695 /// input.parse().map(GenericParam::Lifetime)
696 /// } else if lookahead.peek(Token![const]) {
697 /// input.parse().map(GenericParam::Const)
698 /// } else {
699 /// Err(lookahead.error())
700 /// }
701 /// }
702 /// }
703 /// #
704 /// # fn main() {}
705 /// ```
David Tolnayf5d30452018-09-01 02:29:04 -0700706 pub fn lookahead1(&self) -> Lookahead1<'a> {
707 lookahead::new(self.scope, self.cursor())
708 }
709
David Tolnay725e1c62018-09-01 12:07:25 -0700710 /// Forks a parse stream so that parsing tokens out of either the original
711 /// or the fork does not advance the position of the other.
712 ///
713 /// # Performance
714 ///
715 /// Forking a parse stream is a cheap fixed amount of work and does not
716 /// involve copying token buffers. Where you might hit performance problems
717 /// is if your macro ends up parsing a large amount of content more than
718 /// once.
719 ///
720 /// ```
721 /// # use syn::Expr;
722 /// # use syn::parse::{ParseStream, Result};
723 /// #
724 /// # fn bad(input: ParseStream) -> Result<Expr> {
725 /// // Do not do this.
726 /// if input.fork().parse::<Expr>().is_ok() {
727 /// return input.parse::<Expr>();
728 /// }
729 /// # unimplemented!()
730 /// # }
731 /// ```
732 ///
733 /// As a rule, avoid parsing an unbounded amount of tokens out of a forked
734 /// parse stream. Only use a fork when the amount of work performed against
735 /// the fork is small and bounded.
736 ///
David Tolnayec149b02018-09-01 14:17:28 -0700737 /// For a lower level but occasionally more performant way to perform
David Tolnay725e1c62018-09-01 12:07:25 -0700738 /// speculative parsing, consider using [`ParseStream::step`] instead.
739 ///
740 /// [`ParseStream::step`]: #method.step
David Tolnayec149b02018-09-01 14:17:28 -0700741 ///
742 /// # Example
743 ///
744 /// The parse implementation shown here parses possibly restricted `pub`
745 /// visibilities.
746 ///
747 /// - `pub`
748 /// - `pub(crate)`
749 /// - `pub(self)`
750 /// - `pub(super)`
751 /// - `pub(in some::path)`
752 ///
753 /// To handle the case of visibilities inside of tuple structs, the parser
754 /// needs to distinguish parentheses that specify visibility restrictions
755 /// from parentheses that form part of a tuple type.
756 ///
757 /// ```
758 /// # struct A;
759 /// # struct B;
760 /// # struct C;
761 /// #
762 /// struct S(pub(crate) A, pub (B, C));
763 /// ```
764 ///
765 /// In this example input the first tuple struct element of `S` has
766 /// `pub(crate)` visibility while the second tuple struct element has `pub`
767 /// visibility; the parentheses around `(B, C)` are part of the type rather
768 /// than part of a visibility restriction.
769 ///
770 /// The parser uses a forked parse stream to check the first token inside of
771 /// parentheses after the `pub` keyword. This is a small bounded amount of
772 /// work performed against the forked parse stream.
773 ///
774 /// ```
775 /// # extern crate syn;
776 /// #
777 /// use syn::{parenthesized, token, Ident, Path, Token};
778 /// use syn::ext::IdentExt;
779 /// use syn::parse::{Parse, ParseStream, Result};
780 ///
781 /// struct PubVisibility {
782 /// pub_token: Token![pub],
783 /// restricted: Option<Restricted>,
784 /// }
785 ///
786 /// struct Restricted {
787 /// paren_token: token::Paren,
788 /// in_token: Option<Token![in]>,
789 /// path: Path,
790 /// }
791 ///
792 /// impl Parse for PubVisibility {
793 /// fn parse(input: ParseStream) -> Result<Self> {
794 /// let pub_token: Token![pub] = input.parse()?;
795 ///
796 /// if input.peek(token::Paren) {
797 /// let ahead = input.fork();
798 /// let mut content;
799 /// parenthesized!(content in ahead);
800 ///
801 /// if content.peek(Token![crate])
802 /// || content.peek(Token![self])
803 /// || content.peek(Token![super])
804 /// {
805 /// return Ok(PubVisibility {
806 /// pub_token: pub_token,
807 /// restricted: Some(Restricted {
808 /// paren_token: parenthesized!(content in input),
809 /// in_token: None,
810 /// path: Path::from(content.call(Ident::parse_any)?),
811 /// }),
812 /// });
813 /// } else if content.peek(Token![in]) {
814 /// return Ok(PubVisibility {
815 /// pub_token: pub_token,
816 /// restricted: Some(Restricted {
817 /// paren_token: parenthesized!(content in input),
818 /// in_token: Some(content.parse()?),
819 /// path: content.call(Path::parse_mod_style)?,
820 /// }),
821 /// });
822 /// }
823 /// }
824 ///
825 /// Ok(PubVisibility {
826 /// pub_token: pub_token,
827 /// restricted: None,
828 /// })
829 /// }
830 /// }
831 /// #
832 /// # fn main() {}
833 /// ```
David Tolnayb77c8b62018-08-25 16:39:41 -0400834 pub fn fork(&self) -> Self {
David Tolnay6456a9d2018-08-26 08:11:18 -0400835 ParseBuffer {
836 scope: self.scope,
837 cell: self.cell.clone(),
838 marker: PhantomData,
839 // Not the parent's unexpected. Nothing cares whether the clone
840 // parses all the way.
841 unexpected: Rc::new(Cell::new(None)),
842 }
David Tolnayb77c8b62018-08-25 16:39:41 -0400843 }
844
David Tolnay725e1c62018-09-01 12:07:25 -0700845 /// Triggers an error at the current position of the parse stream.
David Tolnay23fce0b2018-09-01 13:50:31 -0700846 ///
847 /// # Example
848 ///
849 /// ```
850 /// # extern crate syn;
851 /// #
852 /// use syn::{Expr, Token};
853 /// use syn::parse::{Parse, ParseStream, Result};
854 ///
855 /// // Some kind of loop: `while` or `for` or `loop`.
856 /// struct Loop {
857 /// expr: Expr,
858 /// }
859 ///
860 /// impl Parse for Loop {
861 /// fn parse(input: ParseStream) -> Result<Self> {
862 /// if input.peek(Token![while])
863 /// || input.peek(Token![for])
864 /// || input.peek(Token![loop])
865 /// {
866 /// Ok(Loop {
867 /// expr: input.parse()?,
868 /// })
869 /// } else {
870 /// Err(input.error("expected some kind of loop"))
871 /// }
872 /// }
873 /// }
874 /// ```
David Tolnay4fb71232018-08-25 23:14:50 -0400875 pub fn error<T: Display>(&self, message: T) -> Error {
876 error::new_at(self.scope, self.cursor(), message)
877 }
878
David Tolnay725e1c62018-09-01 12:07:25 -0700879 /// Speculatively parses tokens from this parse stream, advancing the
880 /// position of this stream only if parsing succeeds.
David Tolnay9bd34392018-09-01 13:19:53 -0700881 ///
David Tolnayad1d1d22018-09-01 13:34:43 -0700882 /// This is a powerful low-level API used for defining the `Parse` impls of
883 /// the basic built-in token types. It is not something that will be used
884 /// widely outside of the Syn codebase.
885 ///
David Tolnay9bd34392018-09-01 13:19:53 -0700886 /// # Example
887 ///
888 /// ```
889 /// # extern crate proc_macro2;
890 /// # extern crate syn;
891 /// #
892 /// use proc_macro2::TokenTree;
893 /// use syn::parse::{ParseStream, Result};
894 ///
895 /// // This function advances the stream past the next occurrence of `@`. If
896 /// // no `@` is present in the stream, the stream position is unchanged and
897 /// // an error is returned.
898 /// fn skip_past_next_at(input: ParseStream) -> Result<()> {
899 /// input.step(|cursor| {
900 /// let mut rest = *cursor;
901 /// while let Some((tt, next)) = cursor.token_tree() {
902 /// match tt {
903 /// TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
904 /// return Ok(((), next));
905 /// }
906 /// _ => rest = next,
907 /// }
908 /// }
909 /// Err(cursor.error("no `@` was found after this point"))
910 /// })
911 /// }
912 /// #
913 /// # fn main() {}
914 /// ```
David Tolnayb50c65a2018-08-30 21:14:57 -0700915 pub fn step<F, R>(&self, function: F) -> Result<R>
David Tolnay18c754c2018-08-21 23:26:58 -0400916 where
917 F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,
918 {
David Tolnayc142b092018-09-02 08:52:52 -0700919 // Since the user's function is required to work for any 'c, we know
920 // that the Cursor<'c> they return is either derived from the input
921 // StepCursor<'c, 'a> or from a Cursor<'static>.
922 //
923 // It would not be legal to write this function without the invariant
924 // lifetime 'c in StepCursor<'c, 'a>. If this function were written only
925 // in terms of 'a, the user could take our ParseBuffer<'a>, upcast it to
926 // a ParseBuffer<'short> which some shorter lifetime than 'a, invoke
927 // `step` on their ParseBuffer<'short> with a closure that returns
928 // Cursor<'short>, and we would wrongly write that Cursor<'short> into
929 // the Cell intended to hold Cursor<'a>.
930 //
931 // In some cases it may be necessary for R to contain a Cursor<'a>.
932 // Within Syn we solve this using `private::advance_step_cursor` which
933 // uses the existence of a StepCursor<'c, 'a> as proof that it is safe
934 // to cast from Cursor<'c> to Cursor<'a>. If needed outside of Syn, it
935 // would be safe to expose that API as a method on StepCursor.
David Tolnay6b65f852018-09-01 11:56:25 -0700936 let (node, rest) = function(StepCursor {
David Tolnay18c754c2018-08-21 23:26:58 -0400937 scope: self.scope,
938 cursor: self.cell.get(),
939 marker: PhantomData,
David Tolnay6b65f852018-09-01 11:56:25 -0700940 })?;
941 self.cell.set(rest);
942 Ok(node)
David Tolnay18c754c2018-08-21 23:26:58 -0400943 }
David Tolnayeafc8052018-08-25 16:33:53 -0400944
David Tolnay725e1c62018-09-01 12:07:25 -0700945 /// Provides low-level access to the token representation underlying this
946 /// parse stream.
947 ///
948 /// Cursors are immutable so no operations you perform against the cursor
949 /// will affect the state of this parse stream.
David Tolnayf5d30452018-09-01 02:29:04 -0700950 pub fn cursor(&self) -> Cursor<'a> {
951 self.cell.get()
952 }
953
David Tolnay94f06632018-08-31 10:17:17 -0700954 fn check_unexpected(&self) -> Result<()> {
David Tolnayeafc8052018-08-25 16:33:53 -0400955 match self.unexpected.get() {
956 Some(span) => Err(Error::new(span, "unexpected token")),
957 None => Ok(()),
958 }
959 }
David Tolnay18c754c2018-08-21 23:26:58 -0400960}
961
David Tolnaya7d69fc2018-08-26 13:30:24 -0400962impl<T: Parse> Parse for Box<T> {
963 fn parse(input: ParseStream) -> Result<Self> {
964 input.parse().map(Box::new)
965 }
966}
967
David Tolnay4fb71232018-08-25 23:14:50 -0400968impl<T: Parse + Token> Parse for Option<T> {
David Tolnay18c754c2018-08-21 23:26:58 -0400969 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay00f81fd2018-09-01 10:50:12 -0700970 if T::peek(input.cursor()) {
David Tolnay4fb71232018-08-25 23:14:50 -0400971 Ok(Some(input.parse()?))
972 } else {
973 Ok(None)
David Tolnay18c754c2018-08-21 23:26:58 -0400974 }
David Tolnay18c754c2018-08-21 23:26:58 -0400975 }
976}
David Tolnay4ac232d2018-08-31 10:18:03 -0700977
David Tolnay80a914f2018-08-30 23:49:53 -0700978impl Parse for TokenStream {
979 fn parse(input: ParseStream) -> Result<Self> {
980 input.step(|cursor| Ok((cursor.token_stream(), Cursor::empty())))
981 }
982}
983
984impl Parse for TokenTree {
985 fn parse(input: ParseStream) -> Result<Self> {
986 input.step(|cursor| match cursor.token_tree() {
987 Some((tt, rest)) => Ok((tt, rest)),
988 None => Err(cursor.error("expected token tree")),
989 })
990 }
991}
992
993impl Parse for Group {
994 fn parse(input: ParseStream) -> Result<Self> {
995 input.step(|cursor| {
996 for delim in &[Delimiter::Parenthesis, Delimiter::Brace, Delimiter::Bracket] {
997 if let Some((inside, span, rest)) = cursor.group(*delim) {
998 let mut group = Group::new(*delim, inside.token_stream());
999 group.set_span(span);
1000 return Ok((group, rest));
1001 }
1002 }
1003 Err(cursor.error("expected group token"))
1004 })
1005 }
1006}
1007
1008impl Parse for Punct {
1009 fn parse(input: ParseStream) -> Result<Self> {
1010 input.step(|cursor| match cursor.punct() {
1011 Some((punct, rest)) => Ok((punct, rest)),
1012 None => Err(cursor.error("expected punctuation token")),
1013 })
1014 }
1015}
1016
1017impl Parse for Literal {
1018 fn parse(input: ParseStream) -> Result<Self> {
1019 input.step(|cursor| match cursor.literal() {
1020 Some((literal, rest)) => Ok((literal, rest)),
1021 None => Err(cursor.error("expected literal token")),
1022 })
1023 }
1024}
1025
1026/// Parser that can parse Rust tokens into a particular syntax tree node.
1027///
1028/// Refer to the [module documentation] for details about parsing in Syn.
1029///
1030/// [module documentation]: index.html
1031///
1032/// *This trait is available if Syn is built with the `"parsing"` feature.*
1033pub trait Parser: Sized {
1034 type Output;
1035
1036 /// Parse a proc-macro2 token stream into the chosen syntax tree node.
1037 fn parse2(self, tokens: TokenStream) -> Result<Self::Output>;
1038
1039 /// Parse tokens of source code into the chosen syntax tree node.
1040 ///
1041 /// *This method is available if Syn is built with both the `"parsing"` and
1042 /// `"proc-macro"` features.*
1043 #[cfg(all(
1044 not(all(target_arch = "wasm32", target_os = "unknown")),
1045 feature = "proc-macro"
1046 ))]
1047 fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output> {
1048 self.parse2(proc_macro2::TokenStream::from(tokens))
1049 }
1050
1051 /// Parse a string of Rust code into the chosen syntax tree node.
1052 ///
1053 /// # Hygiene
1054 ///
1055 /// Every span in the resulting syntax tree will be set to resolve at the
1056 /// macro call site.
1057 fn parse_str(self, s: &str) -> Result<Self::Output> {
1058 self.parse2(proc_macro2::TokenStream::from_str(s)?)
1059 }
1060}
1061
David Tolnay7b07aa12018-09-01 11:41:12 -07001062fn tokens_to_parse_buffer(tokens: &TokenBuffer) -> ParseBuffer {
1063 let scope = Span::call_site();
1064 let cursor = tokens.begin();
1065 let unexpected = Rc::new(Cell::new(None));
1066 private::new_parse_buffer(scope, cursor, unexpected)
1067}
1068
David Tolnay80a914f2018-08-30 23:49:53 -07001069impl<F, T> Parser for F
1070where
1071 F: FnOnce(ParseStream) -> Result<T>,
1072{
1073 type Output = T;
1074
1075 fn parse2(self, tokens: TokenStream) -> Result<T> {
1076 let buf = TokenBuffer::new2(tokens);
David Tolnay7b07aa12018-09-01 11:41:12 -07001077 let state = tokens_to_parse_buffer(&buf);
David Tolnay80a914f2018-08-30 23:49:53 -07001078 let node = self(&state)?;
1079 state.check_unexpected()?;
1080 if state.is_empty() {
1081 Ok(node)
1082 } else {
1083 Err(state.error("unexpected token"))
1084 }
1085 }
1086}