// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Parsing interface for parsing a token stream into a syntax tree node.
//!
//! Parsing in Syn is built on parser functions that take in a [`ParseStream`]
//! and produce a [`Result<T>`] where `T` is some syntax tree node. Underlying
//! these parser functions is a lower level mechanism built around the
//! [`Cursor`] type. `Cursor` is a cheaply copyable cursor over a range of
//! tokens in a token stream.
//!
//! [`Result<T>`]: Result
//! [`Cursor`]: crate::buffer::Cursor
//!
//! # Example
//!
//! Here is a snippet of parsing code to get a feel for the style of the
//! library. We define data structures for a subset of Rust syntax including
//! enums (not shown) and structs, then provide implementations of the [`Parse`]
//! trait to parse these syntax tree data structures from a token stream.
//!
//! Once `Parse` impls have been defined, they can be called conveniently from a
//! procedural macro through [`parse_macro_input!`] as shown at the bottom of
//! the snippet. If the caller provides syntactically invalid input to the
//! procedural macro, they will receive a helpful compiler error message
//! pointing out the exact token that triggered the failure to parse.
//!
//! [`parse_macro_input!`]: crate::parse_macro_input!
//!
//! ```
//! # extern crate proc_macro;
//! #
//! use proc_macro::TokenStream;
//! use syn::{braced, parse_macro_input, token, Field, Ident, Result, Token};
//! use syn::parse::{Parse, ParseStream};
//! use syn::punctuated::Punctuated;
//!
//! enum Item {
//! Struct(ItemStruct),
//! Enum(ItemEnum),
//! }
//!
//! struct ItemStruct {
//! struct_token: Token![struct],
//! ident: Ident,
//! brace_token: token::Brace,
//! fields: Punctuated<Field, Token![,]>,
//! }
//! #
//! # enum ItemEnum {}
//!
//! impl Parse for Item {
//! fn parse(input: ParseStream) -> Result<Self> {
//! let lookahead = input.lookahead1();
//! if lookahead.peek(Token![struct]) {
//! input.parse().map(Item::Struct)
//! } else if lookahead.peek(Token![enum]) {
//! input.parse().map(Item::Enum)
//! } else {
//! Err(lookahead.error())
//! }
//! }
//! }
//!
//! impl Parse for ItemStruct {
//! fn parse(input: ParseStream) -> Result<Self> {
//! let content;
//! Ok(ItemStruct {
//! struct_token: input.parse()?,
//! ident: input.parse()?,
//! brace_token: braced!(content in input),
//! fields: content.parse_terminated(Field::parse_named, Token![,])?,
//! })
//! }
//! }
//! #
//! # impl Parse for ItemEnum {
//! # fn parse(input: ParseStream) -> Result<Self> {
//! # unimplemented!()
//! # }
//! # }
//!
//! # const IGNORE: &str = stringify! {
//! #[proc_macro]
//! # };
//! pub fn my_macro(tokens: TokenStream) -> TokenStream {
//! let input = parse_macro_input!(tokens as Item);
//!
//! /* ... */
//! # TokenStream::new()
//! }
//! ```
//!
//! # The `syn::parse*` functions
//!
//! The [`syn::parse`], [`syn::parse2`], and [`syn::parse_str`] functions serve
//! as an entry point for parsing syntax tree nodes that can be parsed in an
//! obvious default way. These functions can return any syntax tree node that
//! implements the [`Parse`] trait, which includes most types in Syn.
//!
//! [`syn::parse`]: crate::parse()
//! [`syn::parse2`]: crate::parse2()
//! [`syn::parse_str`]: crate::parse_str()
//!
//! ```
//! use syn::Type;
//!
//! # fn run_parser() -> syn::Result<()> {
//! let t: Type = syn::parse_str("std::collections::HashMap<String, Value>")?;
//! # Ok(())
//! # }
//! #
//! # run_parser().unwrap();
//! ```
//!
//! The [`parse_quote!`] macro also uses this approach.
//!
//! [`parse_quote!`]: crate::parse_quote!
//!
//! # The `Parser` trait
//!
//! Some types can be parsed in several ways depending on context. For example
//! an [`Attribute`] can be either &quo