Skip to content

Commit 6f6b004

Browse files
committed
Improve docs of Token! macro
1 parent 319433e commit 6f6b004

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

src/token.rs

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -840,25 +840,70 @@ define_delimiters! {
840840
/// A type-macro that expands to the name of the Rust type representation of a
841841
/// given token.
842842
///
843-
/// It can expand to the token type and be used to construct a token from a [`Span`]:
843+
/// As a type, `Token!` is commonly used in the type of struct fields, the type
844+
/// of a `let` statement, or in turbofish for a `parse` function.
845+
///
844846
/// ```
845-
/// # use syn::{Token};
846-
/// use proc_macro2::Span;
847+
/// use syn::{Ident, Token};
848+
/// use syn::parse::{Parse, ParseStream, Result};
849+
///
850+
/// // `struct Foo;`
851+
/// pub struct UnitStruct {
852+
/// struct_token: Token![struct],
853+
/// ident: Ident,
854+
/// semi_token: Token![;],
855+
/// }
847856
///
848-
/// let fn_token: Token![fn] = Token![fn](Span::call_site());
857+
/// impl Parse for UnitStruct {
858+
/// fn parse(input: ParseStream) -> Result<Self> {
859+
/// let struct_token: Token![struct] = input.parse()?;
860+
/// let ident: Ident = input.parse()?;
861+
/// let semi_token = input.parse::<Token![;]>()?;
862+
/// Ok(UnitStruct { struct_token, ident, semi_token })
863+
/// }
864+
/// }
849865
/// ```
850-
/// It can be used with [`ParseBuffer::peek`]:
866+
///
867+
/// As an expression, `Token!` is used for peeking tokens or instantiating
868+
/// tokens from a span.
869+
///
851870
/// ```
852-
/// # use syn::{Token, parse::ParseStream};
853-
/// # fn parser(input: ParseStream) {
854-
/// input.peek(Token![<<]);
871+
/// # use syn::{Ident, Token};
872+
/// # use syn::parse::{Parse, ParseStream, Result};
873+
/// #
874+
/// # struct UnitStruct {
875+
/// # struct_token: Token![struct],
876+
/// # ident: Ident,
877+
/// # semi_token: Token![;],
878+
/// # }
879+
/// #
880+
/// # impl Parse for UnitStruct {
881+
/// # fn parse(input: ParseStream) -> Result<Self> {
882+
/// # unimplemented!()
883+
/// # }
884+
/// # }
885+
/// #
886+
/// fn make_unit_struct(name: Ident) -> UnitStruct {
887+
/// let span = name.span();
888+
/// UnitStruct {
889+
/// struct_token: Token![struct](span),
890+
/// ident: name,
891+
/// semi_token: Token![;](span),
892+
/// }
893+
/// }
894+
///
895+
/// # fn parse(input: ParseStream) -> Result<()> {
896+
/// if input.peek(Token![struct]) {
897+
/// let unit_struct: UnitStruct = input.parse()?;
898+
/// /* ... */
899+
/// }
900+
/// # Ok(())
855901
/// # }
856902
/// ```
857903
///
858-
/// See the [token module] documentation for details and more examples.
904+
/// See the [token module] documentation for details and examples.
859905
///
860906
/// [token module]: crate::token
861-
/// [`ParseBuffer::peek`]: crate::parse::ParseBuffer::peek
862907
#[macro_export]
863908
macro_rules! Token {
864909
[abstract] => { $crate::token::Abstract };

0 commit comments

Comments
 (0)