From 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:20:39 +0200 Subject: Merging upstream version 1.70.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/syn/src/parse.rs | 102 ++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 38 deletions(-) (limited to 'vendor/syn/src/parse.rs') diff --git a/vendor/syn/src/parse.rs b/vendor/syn/src/parse.rs index bac4ca05a..61a10d2bc 100644 --- a/vendor/syn/src/parse.rs +++ b/vendor/syn/src/parse.rs @@ -66,7 +66,7 @@ //! struct_token: input.parse()?, //! ident: input.parse()?, //! brace_token: braced!(content in input), -//! fields: content.parse_terminated(Field::parse_named)?, +//! fields: content.parse_terminated(Field::parse_named, Token![,])?, //! }) //! } //! } @@ -84,7 +84,7 @@ //! let input = parse_macro_input!(tokens as Item); //! //! /* ... */ -//! # "".parse().unwrap() +//! # TokenStream::new() //! } //! ``` //! @@ -178,10 +178,6 @@ //! Ok(()) //! } //! ``` -//! -//! --- -//! -//! *This module is available only if Syn is built with the `"parsing"` feature.* #[path = "discouraged.rs"] pub mod discouraged; @@ -685,7 +681,7 @@ impl<'a> ParseBuffer<'a> { /// struct_token: input.parse()?, /// ident: input.parse()?, /// paren_token: parenthesized!(content in input), - /// fields: content.parse_terminated(Type::parse)?, + /// fields: content.parse_terminated(Type::parse, Token![,])?, /// semi_token: input.parse()?, /// }) /// } @@ -696,10 +692,63 @@ impl<'a> ParseBuffer<'a> { /// # }; /// # syn::parse2::(input).unwrap(); /// ``` - pub fn parse_terminated( + /// + /// # See also + /// + /// If your separator is anything more complicated than an invocation of the + /// `Token!` macro, this method won't be applicable and you can instead + /// directly use `Punctuated`'s parser functions: [`parse_terminated`], + /// [`parse_separated_nonempty`] etc. + /// + /// [`parse_terminated`]: Punctuated::parse_terminated + /// [`parse_separated_nonempty`]: Punctuated::parse_separated_nonempty + /// + /// ``` + /// use syn::{custom_keyword, Expr, Result, Token}; + /// use syn::parse::{Parse, ParseStream}; + /// use syn::punctuated::Punctuated; + /// + /// mod kw { + /// syn::custom_keyword!(fin); + /// } + /// + /// struct Fin(kw::fin, Token![;]); + /// + /// impl Parse for Fin { + /// fn parse(input: ParseStream) -> Result { + /// Ok(Self(input.parse()?, input.parse()?)) + /// } + /// } + /// + /// struct Thing { + /// steps: Punctuated, + /// } + /// + /// impl Parse for Thing { + /// fn parse(input: ParseStream) -> Result { + /// # if true { + /// Ok(Thing { + /// steps: Punctuated::parse_terminated(input)?, + /// }) + /// # } else { + /// // or equivalently, this means the same thing: + /// # Ok(Thing { + /// steps: input.call(Punctuated::parse_terminated)?, + /// # }) + /// # } + /// } + /// } + /// ``` + pub fn parse_terminated( &self, parser: fn(ParseStream) -> Result, - ) -> Result> { + separator: P, + ) -> Result> + where + P: Peek, + P::Token: Parse, + { + let _ = separator; Punctuated::parse_terminated_with(self, parser) } @@ -750,7 +799,7 @@ impl<'a> ParseBuffer<'a> { /// # Example /// /// ``` - /// use syn::{ConstParam, Ident, Lifetime, LifetimeDef, Result, Token, TypeParam}; + /// use syn::{ConstParam, Ident, Lifetime, LifetimeParam, Result, Token, TypeParam}; /// use syn::parse::{Parse, ParseStream}; /// /// // A generic parameter, a single one of the comma-separated elements inside @@ -766,7 +815,7 @@ impl<'a> ParseBuffer<'a> { /// // | ^ /// enum GenericParam { /// Type(TypeParam), - /// Lifetime(LifetimeDef), + /// Lifetime(LifetimeParam), /// Const(ConstParam), /// } /// @@ -1101,10 +1150,8 @@ impl Parse for TokenTree { impl Parse for Group { fn parse(input: ParseStream) -> Result { input.step(|cursor| { - for delim in &[Delimiter::Parenthesis, Delimiter::Brace, Delimiter::Bracket] { - if let Some((inside, span, rest)) = cursor.group(*delim) { - let mut group = Group::new(*delim, inside.token_stream()); - group.set_span(span); + if let Some((group, rest)) = cursor.any_group_token() { + if group.delimiter() != Delimiter::None { return Ok((group, rest)); } } @@ -1138,8 +1185,6 @@ impl Parse for Literal { /// Refer to the [module documentation] for details about parsing in Syn. /// /// [module documentation]: self -/// -/// *This trait is available only if Syn is built with the `"parsing"` feature.* pub trait Parser: Sized { type Output; @@ -1153,13 +1198,11 @@ pub trait Parser: Sized { /// /// This function will check that the input is fully parsed. If there are /// any unparsed tokens at the end of the stream, an error is returned. - /// - /// *This method is available only if Syn is built with both the `"parsing"` and - /// `"proc-macro"` features.* #[cfg(all( not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "wasi"))), feature = "proc-macro" ))] + #[cfg_attr(doc_cfg, doc(cfg(feature = "proc-macro")))] fn parse(self, tokens: proc_macro::TokenStream) -> Result { self.parse2(proc_macro2::TokenStream::from(tokens)) } @@ -1184,13 +1227,6 @@ pub trait Parser: Sized { let _ = scope; self.parse2(tokens) } - - // Not public API. - #[doc(hidden)] - #[cfg(any(feature = "full", feature = "derive"))] - fn __parse_stream(self, input: ParseStream) -> Result { - input.parse().and_then(|tokens| self.parse2(tokens)) - } } fn tokens_to_parse_buffer(tokens: &TokenBuffer) -> ParseBuffer { @@ -1232,11 +1268,6 @@ where Ok(node) } } - - #[cfg(any(feature = "full", feature = "derive"))] - fn __parse_stream(self, input: ParseStream) -> Result { - self(input) - } } #[cfg(any(feature = "full", feature = "derive"))] @@ -1244,11 +1275,6 @@ pub(crate) fn parse_scoped(f: F, scope: Span, tokens: TokenStream) -> f.__parse_scoped(scope, tokens) } -#[cfg(any(feature = "full", feature = "derive"))] -pub(crate) fn parse_stream(f: F, input: ParseStream) -> Result { - f.__parse_stream(input) -} - /// An empty syntax tree node that consumes no tokens when parsed. /// /// This is useful for attribute macros that want to ensure they are not @@ -1268,7 +1294,7 @@ pub(crate) fn parse_stream(f: F, input: ParseStream) -> Result