1 //! Parsing interface for parsing a token stream into a syntax tree node.
2 //!
3 //! Parsing in Syn is built on parser functions that take in a [`ParseStream`]
4 //! and produce a [`Result<T>`] where `T` is some syntax tree node. Underlying
5 //! these parser functions is a lower level mechanism built around the
6 //! [`Cursor`] type. `Cursor` is a cheaply copyable cursor over a range of
7 //! tokens in a token stream.
8 //!
9 //! [`ParseStream`]: type.ParseStream.html
10 //! [`Result<T>`]: type.Result.html
11 //! [`Cursor`]: ../buffer/index.html
12 //!
13 //! # Example
14 //!
15 //! Here is a snippet of parsing code to get a feel for the style of the
16 //! library. We define data structures for a subset of Rust syntax including
17 //! enums (not shown) and structs, then provide implementations of the [`Parse`]
18 //! trait to parse these syntax tree data structures from a token stream.
19 //!
20 //! Once `Parse` impls have been defined, they can be called conveniently from a
21 //! procedural macro through [`parse_macro_input!`] as shown at the bottom of
22 //! the snippet. If the caller provides syntactically invalid input to the
23 //! procedural macro, they will receive a helpful compiler error message
24 //! pointing out the exact token that triggered the failure to parse.
25 //!
26 //! [`parse_macro_input!`]: ../macro.parse_macro_input.html
27 //!
28 //! ```
29 //! extern crate proc_macro;
30 //!
31 //! use proc_macro::TokenStream;
32 //! use syn::{braced, parse_macro_input, token, Field, Ident, Result, Token};
33 //! use syn::parse::{Parse, ParseStream};
34 //! use syn::punctuated::Punctuated;
35 //!
36 //! enum Item {
37 //!     Struct(ItemStruct),
38 //!     Enum(ItemEnum),
39 //! }
40 //!
41 //! struct ItemStruct {
42 //!     struct_token: Token![struct],
43 //!     ident: Ident,
44 //!     brace_token: token::Brace,
45 //!     fields: Punctuated<Field, Token![,]>,
46 //! }
47 //! #
48 //! # enum ItemEnum {}
49 //!
50 //! impl Parse for Item {
51 //!     fn parse(input: ParseStream) -> Result<Self> {
52 //!         let lookahead = input.lookahead1();
53 //!         if lookahead.peek(Token![struct]) {
54 //!             input.parse().map(Item::Struct)
55 //!         } else if lookahead.peek(Token![enum]) {
56 //!             input.parse().map(Item::Enum)
57 //!         } else {
58 //!             Err(lookahead.error())
59 //!         }
60 //!     }
61 //! }
62 //!
63 //! impl Parse for ItemStruct {
64 //!     fn parse(input: ParseStream) -> Result<Self> {
65 //!         let content;
66 //!         Ok(ItemStruct {
67 //!             struct_token: input.parse()?,
68 //!             ident: input.parse()?,
69 //!             brace_token: braced!(content in input),
70 //!             fields: content.parse_terminated(Field::parse_named)?,
71 //!         })
72 //!     }
73 //! }
74 //! #
75 //! # impl Parse for ItemEnum {
76 //! #     fn parse(input: ParseStream) -> Result<Self> {
77 //! #         unimplemented!()
78 //! #     }
79 //! # }
80 //!
81 //! # const IGNORE: &str = stringify! {
82 //! #[proc_macro]
83 //! # };
84 //! pub fn my_macro(tokens: TokenStream) -> TokenStream {
85 //!     let input = parse_macro_input!(tokens as Item);
86 //!
87 //!     /* ... */
88 //! #   "".parse().unwrap()
89 //! }
90 //! ```
91 //!
92 //! # The `syn::parse*` functions
93 //!
94 //! The [`syn::parse`], [`syn::parse2`], and [`syn::parse_str`] functions serve
95 //! as an entry point for parsing syntax tree nodes that can be parsed in an
96 //! obvious default way. These functions can return any syntax tree node that
97 //! implements the [`Parse`] trait, which includes most types in Syn.
98 //!
99 //! [`syn::parse`]: ../fn.parse.html
100 //! [`syn::parse2`]: ../fn.parse2.html
101 //! [`syn::parse_str`]: ../fn.parse_str.html
102 //! [`Parse`]: trait.Parse.html
103 //!
104 //! ```
105 //! use syn::Type;
106 //!
107 //! # fn run_parser() -> syn::Result<()> {
108 //! let t: Type = syn::parse_str("std::collections::HashMap<String, Value>")?;
109 //! #     Ok(())
110 //! # }
111 //! #
112 //! # run_parser().unwrap();
113 //! ```
114 //!
115 //! The [`parse_quote!`] macro also uses this approach.
116 //!
117 //! [`parse_quote!`]: ../macro.parse_quote.html
118 //!
119 //! # The `Parser` trait
120 //!
121 //! Some types can be parsed in several ways depending on context. For example
122 //! an [`Attribute`] can be either "outer" like `#[...]` or "inner" like
123 //! `#![...]` and parsing the wrong one would be a bug. Similarly [`Punctuated`]
124 //! may or may not allow trailing punctuation, and parsing it the wrong way
125 //! would either reject valid input or accept invalid input.
126 //!
127 //! [`Attribute`]: ../struct.Attribute.html
128 //! [`Punctuated`]: ../punctuated/index.html
129 //!
130 //! The `Parse` trait is not implemented in these cases because there is no good
131 //! behavior to consider the default.
132 //!
133 //! ```compile_fail
134 //! # extern crate proc_macro;
135 //! #
136 //! # use syn::punctuated::Punctuated;
137 //! # use syn::{PathSegment, Result, Token};
138 //! #
139 //! # fn f(tokens: proc_macro::TokenStream) -> Result<()> {
140 //! #
141 //! // Can't parse `Punctuated` without knowing whether trailing punctuation
142 //! // should be allowed in this context.
143 //! let path: Punctuated<PathSegment, Token![::]> = syn::parse(tokens)?;
144 //! #
145 //! #     Ok(())
146 //! # }
147 //! ```
148 //!
149 //! In these cases the types provide a choice of parser functions rather than a
150 //! single `Parse` implementation, and those parser functions can be invoked
151 //! through the [`Parser`] trait.
152 //!
153 //! [`Parser`]: trait.Parser.html
154 //!
155 //! ```
156 //! extern crate proc_macro;
157 //!
158 //! use proc_macro::TokenStream;
159 //! use syn::parse::Parser;
160 //! use syn::punctuated::Punctuated;
161 //! use syn::{Attribute, Expr, PathSegment, Result, Token};
162 //!
163 //! fn call_some_parser_methods(input: TokenStream) -> Result<()> {
164 //!     // Parse a nonempty sequence of path segments separated by `::` punctuation
165 //!     // with no trailing punctuation.
166 //!     let tokens = input.clone();
167 //!     let parser = Punctuated::<PathSegment, Token![::]>::parse_separated_nonempty;
168 //!     let _path = parser.parse(tokens)?;
169 //!
170 //!     // Parse a possibly empty sequence of expressions terminated by commas with
171 //!     // an optional trailing punctuation.
172 //!     let tokens = input.clone();
173 //!     let parser = Punctuated::<Expr, Token![,]>::parse_terminated;
174 //!     let _args = parser.parse(tokens)?;
175 //!
176 //!     // Parse zero or more outer attributes but not inner attributes.
177 //!     let tokens = input.clone();
178 //!     let parser = Attribute::parse_outer;
179 //!     let _attrs = parser.parse(tokens)?;
180 //!
181 //!     Ok(())
182 //! }
183 //! ```
184 //!
185 //! ---
186 //!
187 //! *This module is available if Syn is built with the `"parsing"` feature.*
188 
189 #[path = "discouraged.rs"]
190 pub mod discouraged;
191 
192 use std::cell::Cell;
193 use std::fmt::{self, Debug, Display};
194 use std::marker::PhantomData;
195 use std::mem;
196 use std::ops::Deref;
197 use std::rc::Rc;
198 use std::str::FromStr;
199 
200 #[cfg(all(
201     not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "wasi"))),
202     feature = "proc-macro"
203 ))]
204 use crate::proc_macro;
205 use proc_macro2::{self, Delimiter, Group, Literal, Punct, Span, TokenStream, TokenTree};
206 
207 use crate::buffer::{Cursor, TokenBuffer};
208 use crate::error;
209 use crate::lookahead;
210 use crate::punctuated::Punctuated;
211 use crate::token::Token;
212 
213 pub use crate::error::{Error, Result};
214 pub use crate::lookahead::{Lookahead1, Peek};
215 
216 /// Parsing interface implemented by all types that can be parsed in a default
217 /// way from a token stream.
218 pub trait Parse: Sized {
parse(input: ParseStream) -> Result<Self>219     fn parse(input: ParseStream) -> Result<Self>;
220 }
221 
222 /// Input to a Syn parser function.
223 ///
224 /// See the methods of this type under the documentation of [`ParseBuffer`]. For
225 /// an overview of parsing in Syn, refer to the [module documentation].
226 ///
227 /// [module documentation]: self
228 pub type ParseStream<'a> = &'a ParseBuffer<'a>;
229 
230 /// Cursor position within a buffered token stream.
231 ///
232 /// This type is more commonly used through the type alias [`ParseStream`] which
233 /// is an alias for `&ParseBuffer`.
234 ///
235 /// `ParseStream` is the input type for all parser functions in Syn. They have
236 /// the signature `fn(ParseStream) -> Result<T>`.
237 ///
238 /// ## Calling a parser function
239 ///
240 /// There is no public way to construct a `ParseBuffer`. Instead, if you are
241 /// looking to invoke a parser function that requires `ParseStream` as input,
242 /// you will need to go through one of the public parsing entry points.
243 ///
244 /// - The [`parse_macro_input!`] macro if parsing input of a procedural macro;
245 /// - One of [the `syn::parse*` functions][syn-parse]; or
246 /// - A method of the [`Parser`] trait.
247 ///
248 /// [syn-parse]: index.html#the-synparse-functions
249 pub struct ParseBuffer<'a> {
250     scope: Span,
251     // Instead of Cell<Cursor<'a>> so that ParseBuffer<'a> is covariant in 'a.
252     // The rest of the code in this module needs to be careful that only a
253     // cursor derived from this `cell` is ever assigned to this `cell`.
254     //
255     // Cell<Cursor<'a>> cannot be covariant in 'a because then we could take a
256     // ParseBuffer<'a>, upcast to ParseBuffer<'short> for some lifetime shorter
257     // than 'a, and then assign a Cursor<'short> into the Cell.
258     //
259     // By extension, it would not be safe to expose an API that accepts a
260     // Cursor<'a> and trusts that it lives as long as the cursor currently in
261     // the cell.
262     cell: Cell<Cursor<'static>>,
263     marker: PhantomData<Cursor<'a>>,
264     unexpected: Cell<Option<Rc<Cell<Unexpected>>>>,
265 }
266 
267 impl<'a> Drop for ParseBuffer<'a> {
drop(&mut self)268     fn drop(&mut self) {
269         if !self.is_empty() {
270             let (inner, old_span) = inner_unexpected(self);
271             if old_span.is_none() {
272                 inner.set(Unexpected::Some(self.cursor().span()));
273             }
274         }
275     }
276 }
277 
278 impl<'a> Display for ParseBuffer<'a> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result279     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
280         Display::fmt(&self.cursor().token_stream(), f)
281     }
282 }
283 
284 impl<'a> Debug for ParseBuffer<'a> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result285     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
286         Debug::fmt(&self.cursor().token_stream(), f)
287     }
288 }
289 
290 /// Cursor state associated with speculative parsing.
291 ///
292 /// This type is the input of the closure provided to [`ParseStream::step`].
293 ///
294 /// [`ParseStream::step`]: ParseBuffer::step
295 ///
296 /// # Example
297 ///
298 /// ```
299 /// use proc_macro2::TokenTree;
300 /// use syn::Result;
301 /// use syn::parse::ParseStream;
302 ///
303 /// // This function advances the stream past the next occurrence of `@`. If
304 /// // no `@` is present in the stream, the stream position is unchanged and
305 /// // an error is returned.
306 /// fn skip_past_next_at(input: ParseStream) -> Result<()> {
307 ///     input.step(|cursor| {
308 ///         let mut rest = *cursor;
309 ///         while let Some((tt, next)) = rest.token_tree() {
310 ///             match &tt {
311 ///                 TokenTree::Punct(punct) if punct.as_char() == '@' => {
312 ///                     return Ok(((), next));
313 ///                 }
314 ///                 _ => rest = next,
315 ///             }
316 ///         }
317 ///         Err(cursor.error("no `@` was found after this point"))
318 ///     })
319 /// }
320 /// #
321 /// # fn remainder_after_skipping_past_next_at(
322 /// #     input: ParseStream,
323 /// # ) -> Result<proc_macro2::TokenStream> {
324 /// #     skip_past_next_at(input)?;
325 /// #     input.parse()
326 /// # }
327 /// #
328 /// # use syn::parse::Parser;
329 /// # let remainder = remainder_after_skipping_past_next_at
330 /// #     .parse_str("a @ b c")
331 /// #     .unwrap();
332 /// # assert_eq!(remainder.to_string(), "b c");
333 /// ```
334 #[derive(Copy, Clone)]
335 pub struct StepCursor<'c, 'a> {
336     scope: Span,
337     // This field is covariant in 'c.
338     cursor: Cursor<'c>,
339     // This field is contravariant in 'c. Together these make StepCursor
340     // invariant in 'c. Also covariant in 'a. The user cannot cast 'c to a
341     // different lifetime but can upcast into a StepCursor with a shorter
342     // lifetime 'a.
343     //
344     // As long as we only ever construct a StepCursor for which 'c outlives 'a,
345     // this means if ever a StepCursor<'c, 'a> exists we are guaranteed that 'c
346     // outlives 'a.
347     marker: PhantomData<fn(Cursor<'c>) -> Cursor<'a>>,
348 }
349 
350 impl<'c, 'a> Deref for StepCursor<'c, 'a> {
351     type Target = Cursor<'c>;
352 
deref(&self) -> &Self::Target353     fn deref(&self) -> &Self::Target {
354         &self.cursor
355     }
356 }
357 
358 impl<'c, 'a> StepCursor<'c, 'a> {
359     /// Triggers an error at the current position of the parse stream.
360     ///
361     /// The `ParseStream::step` invocation will return this same error without
362     /// advancing the stream state.
error<T: Display>(self, message: T) -> Error363     pub fn error<T: Display>(self, message: T) -> Error {
364         error::new_at(self.scope, self.cursor, message)
365     }
366 }
367 
advance_step_cursor<'c, 'a>(proof: StepCursor<'c, 'a>, to: Cursor<'c>) -> Cursor<'a>368 pub(crate) fn advance_step_cursor<'c, 'a>(proof: StepCursor<'c, 'a>, to: Cursor<'c>) -> Cursor<'a> {
369     // Refer to the comments within the StepCursor definition. We use the
370     // fact that a StepCursor<'c, 'a> exists as proof that 'c outlives 'a.
371     // Cursor is covariant in its lifetime parameter so we can cast a
372     // Cursor<'c> to one with the shorter lifetime Cursor<'a>.
373     let _ = proof;
374     unsafe { mem::transmute::<Cursor<'c>, Cursor<'a>>(to) }
375 }
376 
new_parse_buffer( scope: Span, cursor: Cursor, unexpected: Rc<Cell<Unexpected>>, ) -> ParseBuffer377 pub(crate) fn new_parse_buffer(
378     scope: Span,
379     cursor: Cursor,
380     unexpected: Rc<Cell<Unexpected>>,
381 ) -> ParseBuffer {
382     ParseBuffer {
383         scope,
384         // See comment on `cell` in the struct definition.
385         cell: Cell::new(unsafe { mem::transmute::<Cursor, Cursor<'static>>(cursor) }),
386         marker: PhantomData,
387         unexpected: Cell::new(Some(unexpected)),
388     }
389 }
390 
391 #[derive(Clone)]
392 pub(crate) enum Unexpected {
393     None,
394     Some(Span),
395     Chain(Rc<Cell<Unexpected>>),
396 }
397 
398 impl Default for Unexpected {
default() -> Self399     fn default() -> Self {
400         Unexpected::None
401     }
402 }
403 
404 // We call this on Cell<Unexpected> and Cell<Option<T>> where temporarily
405 // swapping in a None is cheap.
cell_clone<T: Default + Clone>(cell: &Cell<T>) -> T406 fn cell_clone<T: Default + Clone>(cell: &Cell<T>) -> T {
407     let prev = cell.take();
408     let ret = prev.clone();
409     cell.set(prev);
410     ret
411 }
412 
inner_unexpected(buffer: &ParseBuffer) -> (Rc<Cell<Unexpected>>, Option<Span>)413 fn inner_unexpected(buffer: &ParseBuffer) -> (Rc<Cell<Unexpected>>, Option<Span>) {
414     let mut unexpected = get_unexpected(buffer);
415     loop {
416         match cell_clone(&unexpected) {
417             Unexpected::None => return (unexpected, None),
418             Unexpected::Some(span) => return (unexpected, Some(span)),
419             Unexpected::Chain(next) => unexpected = next,
420         }
421     }
422 }
423 
get_unexpected(buffer: &ParseBuffer) -> Rc<Cell<Unexpected>>424 pub(crate) fn get_unexpected(buffer: &ParseBuffer) -> Rc<Cell<Unexpected>> {
425     cell_clone(&buffer.unexpected).unwrap()
426 }
427 
428 impl<'a> ParseBuffer<'a> {
429     /// Parses a syntax tree node of type `T`, advancing the position of our
430     /// parse stream past it.
parse<T: Parse>(&self) -> Result<T>431     pub fn parse<T: Parse>(&self) -> Result<T> {
432         T::parse(self)
433     }
434 
435     /// Calls the given parser function to parse a syntax tree node of type `T`
436     /// from this stream.
437     ///
438     /// # Example
439     ///
440     /// The parser below invokes [`Attribute::parse_outer`] to parse a vector of
441     /// zero or more outer attributes.
442     ///
443     /// [`Attribute::parse_outer`]: crate::Attribute::parse_outer
444     ///
445     /// ```
446     /// use syn::{Attribute, Ident, Result, Token};
447     /// use syn::parse::{Parse, ParseStream};
448     ///
449     /// // Parses a unit struct with attributes.
450     /// //
451     /// //     #[path = "s.tmpl"]
452     /// //     struct S;
453     /// struct UnitStruct {
454     ///     attrs: Vec<Attribute>,
455     ///     struct_token: Token![struct],
456     ///     name: Ident,
457     ///     semi_token: Token![;],
458     /// }
459     ///
460     /// impl Parse for UnitStruct {
461     ///     fn parse(input: ParseStream) -> Result<Self> {
462     ///         Ok(UnitStruct {
463     ///             attrs: input.call(Attribute::parse_outer)?,
464     ///             struct_token: input.parse()?,
465     ///             name: input.parse()?,
466     ///             semi_token: input.parse()?,
467     ///         })
468     ///     }
469     /// }
470     /// ```
call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T>471     pub fn call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T> {
472         function(self)
473     }
474 
475     /// Looks at the next token in the parse stream to determine whether it
476     /// matches the requested type of token.
477     ///
478     /// Does not advance the position of the parse stream.
479     ///
480     /// # Syntax
481     ///
482     /// Note that this method does not use turbofish syntax. Pass the peek type
483     /// inside of parentheses.
484     ///
485     /// - `input.peek(Token![struct])`
486     /// - `input.peek(Token![==])`
487     /// - `input.peek(Ident)`&emsp;*(does not accept keywords)*
488     /// - `input.peek(Ident::peek_any)`
489     /// - `input.peek(Lifetime)`
490     /// - `input.peek(token::Brace)`
491     ///
492     /// # Example
493     ///
494     /// In this example we finish parsing the list of supertraits when the next
495     /// token in the input is either `where` or an opening curly brace.
496     ///
497     /// ```
498     /// use syn::{braced, token, Generics, Ident, Result, Token, TypeParamBound};
499     /// use syn::parse::{Parse, ParseStream};
500     /// use syn::punctuated::Punctuated;
501     ///
502     /// // Parses a trait definition containing no associated items.
503     /// //
504     /// //     trait Marker<'de, T>: A + B<'de> where Box<T>: Clone {}
505     /// struct MarkerTrait {
506     ///     trait_token: Token![trait],
507     ///     ident: Ident,
508     ///     generics: Generics,
509     ///     colon_token: Option<Token![:]>,
510     ///     supertraits: Punctuated<TypeParamBound, Token![+]>,
511     ///     brace_token: token::Brace,
512     /// }
513     ///
514     /// impl Parse for MarkerTrait {
515     ///     fn parse(input: ParseStream) -> Result<Self> {
516     ///         let trait_token: Token![trait] = input.parse()?;
517     ///         let ident: Ident = input.parse()?;
518     ///         let mut generics: Generics = input.parse()?;
519     ///         let colon_token: Option<Token![:]> = input.parse()?;
520     ///
521     ///         let mut supertraits = Punctuated::new();
522     ///         if colon_token.is_some() {
523     ///             loop {
524     ///                 supertraits.push_value(input.parse()?);
525     ///                 if input.peek(Token![where]) || input.peek(token::Brace) {
526     ///                     break;
527     ///                 }
528     ///                 supertraits.push_punct(input.parse()?);
529     ///             }
530     ///         }
531     ///
532     ///         generics.where_clause = input.parse()?;
533     ///         let content;
534     ///         let empty_brace_token = braced!(content in input);
535     ///
536     ///         Ok(MarkerTrait {
537     ///             trait_token,
538     ///             ident,
539     ///             generics,
540     ///             colon_token,
541     ///             supertraits,
542     ///             brace_token: empty_brace_token,
543     ///         })
544     ///     }
545     /// }
546     /// ```
peek<T: Peek>(&self, token: T) -> bool547     pub fn peek<T: Peek>(&self, token: T) -> bool {
548         let _ = token;
549         T::Token::peek(self.cursor())
550     }
551 
552     /// Looks at the second-next token in the parse stream.
553     ///
554     /// This is commonly useful as a way to implement contextual keywords.
555     ///
556     /// # Example
557     ///
558     /// This example needs to use `peek2` because the symbol `union` is not a
559     /// keyword in Rust. We can't use just `peek` and decide to parse a union if
560     /// the very next token is `union`, because someone is free to write a `mod
561     /// union` and a macro invocation that looks like `union::some_macro! { ...
562     /// }`. In other words `union` is a contextual keyword.
563     ///
564     /// ```
565     /// use syn::{Ident, ItemUnion, Macro, Result, Token};
566     /// use syn::parse::{Parse, ParseStream};
567     ///
568     /// // Parses either a union or a macro invocation.
569     /// enum UnionOrMacro {
570     ///     // union MaybeUninit<T> { uninit: (), value: T }
571     ///     Union(ItemUnion),
572     ///     // lazy_static! { ... }
573     ///     Macro(Macro),
574     /// }
575     ///
576     /// impl Parse for UnionOrMacro {
577     ///     fn parse(input: ParseStream) -> Result<Self> {
578     ///         if input.peek(Token![union]) && input.peek2(Ident) {
579     ///             input.parse().map(UnionOrMacro::Union)
580     ///         } else {
581     ///             input.parse().map(UnionOrMacro::Macro)
582     ///         }
583     ///     }
584     /// }
585     /// ```
peek2<T: Peek>(&self, token: T) -> bool586     pub fn peek2<T: Peek>(&self, token: T) -> bool {
587         let _ = token;
588         self.cursor().skip().map_or(false, T::Token::peek)
589     }
590 
591     /// Looks at the third-next token in the parse stream.
peek3<T: Peek>(&self, token: T) -> bool592     pub fn peek3<T: Peek>(&self, token: T) -> bool {
593         let _ = token;
594         self.cursor()
595             .skip()
596             .and_then(Cursor::skip)
597             .map_or(false, T::Token::peek)
598     }
599 
600     /// Parses zero or more occurrences of `T` separated by punctuation of type
601     /// `P`, with optional trailing punctuation.
602     ///
603     /// Parsing continues until the end of this parse stream. The entire content
604     /// of this parse stream must consist of `T` and `P`.
605     ///
606     /// # Example
607     ///
608     /// ```
609     /// # use quote::quote;
610     /// #
611     /// use syn::{parenthesized, token, Ident, Result, Token, Type};
612     /// use syn::parse::{Parse, ParseStream};
613     /// use syn::punctuated::Punctuated;
614     ///
615     /// // Parse a simplified tuple struct syntax like:
616     /// //
617     /// //     struct S(A, B);
618     /// struct TupleStruct {
619     ///     struct_token: Token![struct],
620     ///     ident: Ident,
621     ///     paren_token: token::Paren,
622     ///     fields: Punctuated<Type, Token![,]>,
623     ///     semi_token: Token![;],
624     /// }
625     ///
626     /// impl Parse for TupleStruct {
627     ///     fn parse(input: ParseStream) -> Result<Self> {
628     ///         let content;
629     ///         Ok(TupleStruct {
630     ///             struct_token: input.parse()?,
631     ///             ident: input.parse()?,
632     ///             paren_token: parenthesized!(content in input),
633     ///             fields: content.parse_terminated(Type::parse)?,
634     ///             semi_token: input.parse()?,
635     ///         })
636     ///     }
637     /// }
638     /// #
639     /// # let input = quote! {
640     /// #     struct S(A, B);
641     /// # };
642     /// # syn::parse2::<TupleStruct>(input).unwrap();
643     /// ```
parse_terminated<T, P: Parse>( &self, parser: fn(ParseStream) -> Result<T>, ) -> Result<Punctuated<T, P>>644     pub fn parse_terminated<T, P: Parse>(
645         &self,
646         parser: fn(ParseStream) -> Result<T>,
647     ) -> Result<Punctuated<T, P>> {
648         Punctuated::parse_terminated_with(self, parser)
649     }
650 
651     /// Returns whether there are tokens remaining in this stream.
652     ///
653     /// This method returns true at the end of the content of a set of
654     /// delimiters, as well as at the very end of the complete macro input.
655     ///
656     /// # Example
657     ///
658     /// ```
659     /// use syn::{braced, token, Ident, Item, Result, Token};
660     /// use syn::parse::{Parse, ParseStream};
661     ///
662     /// // Parses a Rust `mod m { ... }` containing zero or more items.
663     /// struct Mod {
664     ///     mod_token: Token![mod],
665     ///     name: Ident,
666     ///     brace_token: token::Brace,
667     ///     items: Vec<Item>,
668     /// }
669     ///
670     /// impl Parse for Mod {
671     ///     fn parse(input: ParseStream) -> Result<Self> {
672     ///         let content;
673     ///         Ok(Mod {
674     ///             mod_token: input.parse()?,
675     ///             name: input.parse()?,
676     ///             brace_token: braced!(content in input),
677     ///             items: {
678     ///                 let mut items = Vec::new();
679     ///                 while !content.is_empty() {
680     ///                     items.push(content.parse()?);
681     ///                 }
682     ///                 items
683     ///             },
684     ///         })
685     ///     }
686     /// }
687     /// ```
is_empty(&self) -> bool688     pub fn is_empty(&self) -> bool {
689         self.cursor().eof()
690     }
691 
692     /// Constructs a helper for peeking at the next token in this stream and
693     /// building an error message if it is not one of a set of expected tokens.
694     ///
695     /// # Example
696     ///
697     /// ```
698     /// use syn::{ConstParam, Ident, Lifetime, LifetimeDef, Result, Token, TypeParam};
699     /// use syn::parse::{Parse, ParseStream};
700     ///
701     /// // A generic parameter, a single one of the comma-separated elements inside
702     /// // angle brackets in:
703     /// //
704     /// //     fn f<T: Clone, 'a, 'b: 'a, const N: usize>() { ... }
705     /// //
706     /// // On invalid input, lookahead gives us a reasonable error message.
707     /// //
708     /// //     error: expected one of: identifier, lifetime, `const`
709     /// //       |
710     /// //     5 |     fn f<!Sized>() {}
711     /// //       |          ^
712     /// enum GenericParam {
713     ///     Type(TypeParam),
714     ///     Lifetime(LifetimeDef),
715     ///     Const(ConstParam),
716     /// }
717     ///
718     /// impl Parse for GenericParam {
719     ///     fn parse(input: ParseStream) -> Result<Self> {
720     ///         let lookahead = input.lookahead1();
721     ///         if lookahead.peek(Ident) {
722     ///             input.parse().map(GenericParam::Type)
723     ///         } else if lookahead.peek(Lifetime) {
724     ///             input.parse().map(GenericParam::Lifetime)
725     ///         } else if lookahead.peek(Token![const]) {
726     ///             input.parse().map(GenericParam::Const)
727     ///         } else {
728     ///             Err(lookahead.error())
729     ///         }
730     ///     }
731     /// }
732     /// ```
lookahead1(&self) -> Lookahead1<'a>733     pub fn lookahead1(&self) -> Lookahead1<'a> {
734         lookahead::new(self.scope, self.cursor())
735     }
736 
737     /// Forks a parse stream so that parsing tokens out of either the original
738     /// or the fork does not advance the position of the other.
739     ///
740     /// # Performance
741     ///
742     /// Forking a parse stream is a cheap fixed amount of work and does not
743     /// involve copying token buffers. Where you might hit performance problems
744     /// is if your macro ends up parsing a large amount of content more than
745     /// once.
746     ///
747     /// ```
748     /// # use syn::{Expr, Result};
749     /// # use syn::parse::ParseStream;
750     /// #
751     /// # fn bad(input: ParseStream) -> Result<Expr> {
752     /// // Do not do this.
753     /// if input.fork().parse::<Expr>().is_ok() {
754     ///     return input.parse::<Expr>();
755     /// }
756     /// # unimplemented!()
757     /// # }
758     /// ```
759     ///
760     /// As a rule, avoid parsing an unbounded amount of tokens out of a forked
761     /// parse stream. Only use a fork when the amount of work performed against
762     /// the fork is small and bounded.
763     ///
764     /// When complex speculative parsing against the forked stream is
765     /// unavoidable, use [`parse::discouraged::Speculative`] to advance the
766     /// original stream once the fork's parse is determined to have been
767     /// successful.
768     ///
769     /// For a lower level way to perform speculative parsing at the token level,
770     /// consider using [`ParseStream::step`] instead.
771     ///
772     /// [`parse::discouraged::Speculative`]: discouraged::Speculative
773     /// [`ParseStream::step`]: ParseBuffer::step
774     ///
775     /// # Example
776     ///
777     /// The parse implementation shown here parses possibly restricted `pub`
778     /// visibilities.
779     ///
780     /// - `pub`
781     /// - `pub(crate)`
782     /// - `pub(self)`
783     /// - `pub(super)`
784     /// - `pub(in some::path)`
785     ///
786     /// To handle the case of visibilities inside of tuple structs, the parser
787     /// needs to distinguish parentheses that specify visibility restrictions
788     /// from parentheses that form part of a tuple type.
789     ///
790     /// ```
791     /// # struct A;
792     /// # struct B;
793     /// # struct C;
794     /// #
795     /// struct S(pub(crate) A, pub (B, C));
796     /// ```
797     ///
798     /// In this example input the first tuple struct element of `S` has
799     /// `pub(crate)` visibility while the second tuple struct element has `pub`
800     /// visibility; the parentheses around `(B, C)` are part of the type rather
801     /// than part of a visibility restriction.
802     ///
803     /// The parser uses a forked parse stream to check the first token inside of
804     /// parentheses after the `pub` keyword. This is a small bounded amount of
805     /// work performed against the forked parse stream.
806     ///
807     /// ```
808     /// use syn::{parenthesized, token, Ident, Path, Result, Token};
809     /// use syn::ext::IdentExt;
810     /// use syn::parse::{Parse, ParseStream};
811     ///
812     /// struct PubVisibility {
813     ///     pub_token: Token![pub],
814     ///     restricted: Option<Restricted>,
815     /// }
816     ///
817     /// struct Restricted {
818     ///     paren_token: token::Paren,
819     ///     in_token: Option<Token![in]>,
820     ///     path: Path,
821     /// }
822     ///
823     /// impl Parse for PubVisibility {
824     ///     fn parse(input: ParseStream) -> Result<Self> {
825     ///         let pub_token: Token![pub] = input.parse()?;
826     ///
827     ///         if input.peek(token::Paren) {
828     ///             let ahead = input.fork();
829     ///             let mut content;
830     ///             parenthesized!(content in ahead);
831     ///
832     ///             if content.peek(Token![crate])
833     ///                 || content.peek(Token![self])
834     ///                 || content.peek(Token![super])
835     ///             {
836     ///                 return Ok(PubVisibility {
837     ///                     pub_token,
838     ///                     restricted: Some(Restricted {
839     ///                         paren_token: parenthesized!(content in input),
840     ///                         in_token: None,
841     ///                         path: Path::from(content.call(Ident::parse_any)?),
842     ///                     }),
843     ///                 });
844     ///             } else if content.peek(Token![in]) {
845     ///                 return Ok(PubVisibility {
846     ///                     pub_token,
847     ///                     restricted: Some(Restricted {
848     ///                         paren_token: parenthesized!(content in input),
849     ///                         in_token: Some(content.parse()?),
850     ///                         path: content.call(Path::parse_mod_style)?,
851     ///                     }),
852     ///                 });
853     ///             }
854     ///         }
855     ///
856     ///         Ok(PubVisibility {
857     ///             pub_token,
858     ///             restricted: None,
859     ///         })
860     ///     }
861     /// }
862     /// ```
fork(&self) -> Self863     pub fn fork(&self) -> Self {
864         ParseBuffer {
865             scope: self.scope,
866             cell: self.cell.clone(),
867             marker: PhantomData,
868             // Not the parent's unexpected. Nothing cares whether the clone
869             // parses all the way unless we `advance_to`.
870             unexpected: Cell::new(Some(Rc::new(Cell::new(Unexpected::None)))),
871         }
872     }
873 
874     /// Triggers an error at the current position of the parse stream.
875     ///
876     /// # Example
877     ///
878     /// ```
879     /// use syn::{Expr, Result, Token};
880     /// use syn::parse::{Parse, ParseStream};
881     ///
882     /// // Some kind of loop: `while` or `for` or `loop`.
883     /// struct Loop {
884     ///     expr: Expr,
885     /// }
886     ///
887     /// impl Parse for Loop {
888     ///     fn parse(input: ParseStream) -> Result<Self> {
889     ///         if input.peek(Token![while])
890     ///             || input.peek(Token![for])
891     ///             || input.peek(Token![loop])
892     ///         {
893     ///             Ok(Loop {
894     ///                 expr: input.parse()?,
895     ///             })
896     ///         } else {
897     ///             Err(input.error("expected some kind of loop"))
898     ///         }
899     ///     }
900     /// }
901     /// ```
error<T: Display>(&self, message: T) -> Error902     pub fn error<T: Display>(&self, message: T) -> Error {
903         error::new_at(self.scope, self.cursor(), message)
904     }
905 
906     /// Speculatively parses tokens from this parse stream, advancing the
907     /// position of this stream only if parsing succeeds.
908     ///
909     /// This is a powerful low-level API used for defining the `Parse` impls of
910     /// the basic built-in token types. It is not something that will be used
911     /// widely outside of the Syn codebase.
912     ///
913     /// # Example
914     ///
915     /// ```
916     /// use proc_macro2::TokenTree;
917     /// use syn::Result;
918     /// use syn::parse::ParseStream;
919     ///
920     /// // This function advances the stream past the next occurrence of `@`. If
921     /// // no `@` is present in the stream, the stream position is unchanged and
922     /// // an error is returned.
923     /// fn skip_past_next_at(input: ParseStream) -> Result<()> {
924     ///     input.step(|cursor| {
925     ///         let mut rest = *cursor;
926     ///         while let Some((tt, next)) = rest.token_tree() {
927     ///             match &tt {
928     ///                 TokenTree::Punct(punct) if punct.as_char() == '@' => {
929     ///                     return Ok(((), next));
930     ///                 }
931     ///                 _ => rest = next,
932     ///             }
933     ///         }
934     ///         Err(cursor.error("no `@` was found after this point"))
935     ///     })
936     /// }
937     /// #
938     /// # fn remainder_after_skipping_past_next_at(
939     /// #     input: ParseStream,
940     /// # ) -> Result<proc_macro2::TokenStream> {
941     /// #     skip_past_next_at(input)?;
942     /// #     input.parse()
943     /// # }
944     /// #
945     /// # use syn::parse::Parser;
946     /// # let remainder = remainder_after_skipping_past_next_at
947     /// #     .parse_str("a @ b c")
948     /// #     .unwrap();
949     /// # assert_eq!(remainder.to_string(), "b c");
950     /// ```
step<F, R>(&self, function: F) -> Result<R> where F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,951     pub fn step<F, R>(&self, function: F) -> Result<R>
952     where
953         F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,
954     {
955         // Since the user's function is required to work for any 'c, we know
956         // that the Cursor<'c> they return is either derived from the input
957         // StepCursor<'c, 'a> or from a Cursor<'static>.
958         //
959         // It would not be legal to write this function without the invariant
960         // lifetime 'c in StepCursor<'c, 'a>. If this function were written only
961         // in terms of 'a, the user could take our ParseBuffer<'a>, upcast it to
962         // a ParseBuffer<'short> which some shorter lifetime than 'a, invoke
963         // `step` on their ParseBuffer<'short> with a closure that returns
964         // Cursor<'short>, and we would wrongly write that Cursor<'short> into
965         // the Cell intended to hold Cursor<'a>.
966         //
967         // In some cases it may be necessary for R to contain a Cursor<'a>.
968         // Within Syn we solve this using `advance_step_cursor` which uses the
969         // existence of a StepCursor<'c, 'a> as proof that it is safe to cast
970         // from Cursor<'c> to Cursor<'a>. If needed outside of Syn, it would be
971         // safe to expose that API as a method on StepCursor.
972         let (node, rest) = function(StepCursor {
973             scope: self.scope,
974             cursor: self.cell.get(),
975             marker: PhantomData,
976         })?;
977         self.cell.set(rest);
978         Ok(node)
979     }
980 
981     /// Returns the `Span` of the next token in the parse stream, or
982     /// `Span::call_site()` if this parse stream has completely exhausted its
983     /// input `TokenStream`.
span(&self) -> Span984     pub fn span(&self) -> Span {
985         let cursor = self.cursor();
986         if cursor.eof() {
987             self.scope
988         } else {
989             crate::buffer::open_span_of_group(cursor)
990         }
991     }
992 
993     /// Provides low-level access to the token representation underlying this
994     /// parse stream.
995     ///
996     /// Cursors are immutable so no operations you perform against the cursor
997     /// will affect the state of this parse stream.
cursor(&self) -> Cursor<'a>998     pub fn cursor(&self) -> Cursor<'a> {
999         self.cell.get()
1000     }
1001 
check_unexpected(&self) -> Result<()>1002     fn check_unexpected(&self) -> Result<()> {
1003         match inner_unexpected(self).1 {
1004             Some(span) => Err(Error::new(span, "unexpected token")),
1005             None => Ok(()),
1006         }
1007     }
1008 }
1009 
1010 impl<T: Parse> Parse for Box<T> {
parse(input: ParseStream) -> Result<Self>1011     fn parse(input: ParseStream) -> Result<Self> {
1012         input.parse().map(Box::new)
1013     }
1014 }
1015 
1016 impl<T: Parse + Token> Parse for Option<T> {
parse(input: ParseStream) -> Result<Self>1017     fn parse(input: ParseStream) -> Result<Self> {
1018         if T::peek(input.cursor()) {
1019             Ok(Some(input.parse()?))
1020         } else {
1021             Ok(None)
1022         }
1023     }
1024 }
1025 
1026 impl Parse for TokenStream {
parse(input: ParseStream) -> Result<Self>1027     fn parse(input: ParseStream) -> Result<Self> {
1028         input.step(|cursor| Ok((cursor.token_stream(), Cursor::empty())))
1029     }
1030 }
1031 
1032 impl Parse for TokenTree {
parse(input: ParseStream) -> Result<Self>1033     fn parse(input: ParseStream) -> Result<Self> {
1034         input.step(|cursor| match cursor.token_tree() {
1035             Some((tt, rest)) => Ok((tt, rest)),
1036             None => Err(cursor.error("expected token tree")),
1037         })
1038     }
1039 }
1040 
1041 impl Parse for Group {
parse(input: ParseStream) -> Result<Self>1042     fn parse(input: ParseStream) -> Result<Self> {
1043         input.step(|cursor| {
1044             for delim in &[Delimiter::Parenthesis, Delimiter::Brace, Delimiter::Bracket] {
1045                 if let Some((inside, span, rest)) = cursor.group(*delim) {
1046                     let mut group = Group::new(*delim, inside.token_stream());
1047                     group.set_span(span);
1048                     return Ok((group, rest));
1049                 }
1050             }
1051             Err(cursor.error("expected group token"))
1052         })
1053     }
1054 }
1055 
1056 impl Parse for Punct {
parse(input: ParseStream) -> Result<Self>1057     fn parse(input: ParseStream) -> Result<Self> {
1058         input.step(|cursor| match cursor.punct() {
1059             Some((punct, rest)) => Ok((punct, rest)),
1060             None => Err(cursor.error("expected punctuation token")),
1061         })
1062     }
1063 }
1064 
1065 impl Parse for Literal {
parse(input: ParseStream) -> Result<Self>1066     fn parse(input: ParseStream) -> Result<Self> {
1067         input.step(|cursor| match cursor.literal() {
1068             Some((literal, rest)) => Ok((literal, rest)),
1069             None => Err(cursor.error("expected literal token")),
1070         })
1071     }
1072 }
1073 
1074 /// Parser that can parse Rust tokens into a particular syntax tree node.
1075 ///
1076 /// Refer to the [module documentation] for details about parsing in Syn.
1077 ///
1078 /// [module documentation]: self
1079 ///
1080 /// *This trait is available if Syn is built with the `"parsing"` feature.*
1081 pub trait Parser: Sized {
1082     type Output;
1083 
1084     /// Parse a proc-macro2 token stream into the chosen syntax tree node.
1085     ///
1086     /// This function will check that the input is fully parsed. If there are
1087     /// any unparsed tokens at the end of the stream, an error is returned.
parse2(self, tokens: TokenStream) -> Result<Self::Output>1088     fn parse2(self, tokens: TokenStream) -> Result<Self::Output>;
1089 
1090     /// Parse tokens of source code into the chosen syntax tree node.
1091     ///
1092     /// This function will check that the input is fully parsed. If there are
1093     /// any unparsed tokens at the end of the stream, an error is returned.
1094     ///
1095     /// *This method is available if Syn is built with both the `"parsing"` and
1096     /// `"proc-macro"` features.*
1097     #[cfg(all(
1098         not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "wasi"))),
1099         feature = "proc-macro"
1100     ))]
parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output>1101     fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output> {
1102         self.parse2(proc_macro2::TokenStream::from(tokens))
1103     }
1104 
1105     /// Parse a string of Rust code into the chosen syntax tree node.
1106     ///
1107     /// This function will check that the input is fully parsed. If there are
1108     /// any unparsed tokens at the end of the string, an error is returned.
1109     ///
1110     /// # Hygiene
1111     ///
1112     /// Every span in the resulting syntax tree will be set to resolve at the
1113     /// macro call site.
parse_str(self, s: &str) -> Result<Self::Output>1114     fn parse_str(self, s: &str) -> Result<Self::Output> {
1115         self.parse2(proc_macro2::TokenStream::from_str(s)?)
1116     }
1117 
1118     // Not public API.
1119     #[doc(hidden)]
__parse_scoped(self, scope: Span, tokens: TokenStream) -> Result<Self::Output>1120     fn __parse_scoped(self, scope: Span, tokens: TokenStream) -> Result<Self::Output> {
1121         let _ = scope;
1122         self.parse2(tokens)
1123     }
1124 
1125     // Not public API.
1126     #[doc(hidden)]
__parse_stream(self, input: ParseStream) -> Result<Self::Output>1127     fn __parse_stream(self, input: ParseStream) -> Result<Self::Output> {
1128         input.parse().and_then(|tokens| self.parse2(tokens))
1129     }
1130 }
1131 
tokens_to_parse_buffer(tokens: &TokenBuffer) -> ParseBuffer1132 fn tokens_to_parse_buffer(tokens: &TokenBuffer) -> ParseBuffer {
1133     let scope = Span::call_site();
1134     let cursor = tokens.begin();
1135     let unexpected = Rc::new(Cell::new(Unexpected::None));
1136     new_parse_buffer(scope, cursor, unexpected)
1137 }
1138 
1139 impl<F, T> Parser for F
1140 where
1141     F: FnOnce(ParseStream) -> Result<T>,
1142 {
1143     type Output = T;
1144 
parse2(self, tokens: TokenStream) -> Result<T>1145     fn parse2(self, tokens: TokenStream) -> Result<T> {
1146         let buf = TokenBuffer::new2(tokens);
1147         let state = tokens_to_parse_buffer(&buf);
1148         let node = self(&state)?;
1149         state.check_unexpected()?;
1150         if state.is_empty() {
1151             Ok(node)
1152         } else {
1153             Err(state.error("unexpected token"))
1154         }
1155     }
1156 
1157     #[doc(hidden)]
__parse_scoped(self, scope: Span, tokens: TokenStream) -> Result<Self::Output>1158     fn __parse_scoped(self, scope: Span, tokens: TokenStream) -> Result<Self::Output> {
1159         let buf = TokenBuffer::new2(tokens);
1160         let cursor = buf.begin();
1161         let unexpected = Rc::new(Cell::new(Unexpected::None));
1162         let state = new_parse_buffer(scope, cursor, unexpected);
1163         let node = self(&state)?;
1164         state.check_unexpected()?;
1165         if state.is_empty() {
1166             Ok(node)
1167         } else {
1168             Err(state.error("unexpected token"))
1169         }
1170     }
1171 
1172     #[doc(hidden)]
__parse_stream(self, input: ParseStream) -> Result<Self::Output>1173     fn __parse_stream(self, input: ParseStream) -> Result<Self::Output> {
1174         self(input)
1175     }
1176 }
1177 
parse_scoped<F: Parser>(f: F, scope: Span, tokens: TokenStream) -> Result<F::Output>1178 pub(crate) fn parse_scoped<F: Parser>(f: F, scope: Span, tokens: TokenStream) -> Result<F::Output> {
1179     f.__parse_scoped(scope, tokens)
1180 }
1181 
parse_stream<F: Parser>(f: F, input: ParseStream) -> Result<F::Output>1182 pub(crate) fn parse_stream<F: Parser>(f: F, input: ParseStream) -> Result<F::Output> {
1183     f.__parse_stream(input)
1184 }
1185 
1186 /// An empty syntax tree node that consumes no tokens when parsed.
1187 ///
1188 /// This is useful for attribute macros that want to ensure they are not
1189 /// provided any attribute args.
1190 ///
1191 /// ```
1192 /// extern crate proc_macro;
1193 ///
1194 /// use proc_macro::TokenStream;
1195 /// use syn::parse_macro_input;
1196 /// use syn::parse::Nothing;
1197 ///
1198 /// # const IGNORE: &str = stringify! {
1199 /// #[proc_macro_attribute]
1200 /// # };
1201 /// pub fn my_attr(args: TokenStream, input: TokenStream) -> TokenStream {
1202 ///     parse_macro_input!(args as Nothing);
1203 ///
1204 ///     /* ... */
1205 /// #   "".parse().unwrap()
1206 /// }
1207 /// ```
1208 ///
1209 /// ```text
1210 /// error: unexpected token
1211 ///  --> src/main.rs:3:19
1212 ///   |
1213 /// 3 | #[my_attr(asdf)]
1214 ///   |           ^^^^
1215 /// ```
1216 pub struct Nothing;
1217 
1218 impl Parse for Nothing {
parse(_input: ParseStream) -> Result<Self>1219     fn parse(_input: ParseStream) -> Result<Self> {
1220         Ok(Nothing)
1221     }
1222 }
1223