summaryrefslogtreecommitdiffstats
path: root/vendor/syn/src/parse.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:20:39 +0000
commit1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch)
tree3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /vendor/syn/src/parse.rs
parentReleasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz
rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/syn/src/parse.rs')
-rw-r--r--vendor/syn/src/parse.rs102
1 files changed, 64 insertions, 38 deletions
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::<TupleStruct>(input).unwrap();
/// ```
- pub fn parse_terminated<T, P: Parse>(
+ ///
+ /// # 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<Self> {
+ /// Ok(Self(input.parse()?, input.parse()?))
+ /// }
+ /// }
+ ///
+ /// struct Thing {
+ /// steps: Punctuated<Expr, Fin>,
+ /// }
+ ///
+ /// impl Parse for Thing {
+ /// fn parse(input: ParseStream) -> Result<Self> {
+ /// # 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<T, P>(
&self,
parser: fn(ParseStream) -> Result<T>,
- ) -> Result<Punctuated<T, P>> {
+ separator: P,
+ ) -> Result<Punctuated<T, P::Token>>
+ 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<Self> {
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::Output> {
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<Self::Output> {
- 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::Output> {
- self(input)
- }
}
#[cfg(any(feature = "full", feature = "derive"))]
@@ -1244,11 +1275,6 @@ pub(crate) fn parse_scoped<F: Parser>(f: F, scope: Span, tokens: TokenStream) ->
f.__parse_scoped(scope, tokens)
}
-#[cfg(any(feature = "full", feature = "derive"))]
-pub(crate) fn parse_stream<F: Parser>(f: F, input: ParseStream) -> Result<F::Output> {
- 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: Parser>(f: F, input: ParseStream) -> Result<F::Out
/// parse_macro_input!(args as Nothing);
///
/// /* ... */
-/// # "".parse().unwrap()
+/// # TokenStream::new()
/// }
/// ```
///