attr.rs |
Example |
26964 |
bigint.rs |
|
1610 |
buffer.rs |
A stably addressed token buffer supporting efficient traversal based on a
cheaply copyable cursor. |
15602 |
classify.rs |
|
12299 |
custom_keyword.rs |
|
7862 |
custom_punctuation.rs |
|
9132 |
data.rs |
|
14450 |
derive.rs |
|
8843 |
discouraged.rs |
Extensions to the parsing API with niche applicability. |
9204 |
drops.rs |
|
1430 |
error.rs |
... |
14105 |
export.rs |
|
1672 |
expr.rs |
... |
132098 |
ext.rs |
Extension traits to provide parsing methods on foreign types. |
3902 |
file.rs |
|
3905 |
fixup.rs |
|
7854 |
gen |
|
|
generics.rs |
|
43538 |
group.rs |
|
7888 |
ident.rs |
|
3143 |
item.rs |
some sane fallback |
119395 |
lib.rs |
|
33802 |
lifetime.rs |
|
3878 |
lit.rs |
... |
56206 |
lookahead.rs |
|
5220 |
mac.rs |
|
7909 |
macros.rs |
|
4281 |
meta.rs |
Facility for interpreting structured content inside of an `Attribute`. |
14137 |
op.rs |
|
8337 |
parse.rs |
... |
46791 |
parse_macro_input.rs |
... |
3496 |
parse_quote.rs |
|
6281 |
pat.rs |
some sane fallback |
31889 |
path.rs |
|
31017 |
precedence.rs |
|
4512 |
print.rs |
|
390 |
punctuated.rs |
A punctuated sequence of syntax tree nodes separated by punctuation.
Lots of things in Rust are punctuated sequences.
- The fields of a struct are `Punctuated<Field, Token![,]>`.
- The segments of a path are `Punctuated<PathSegment, Token![::]>`.
- The bounds on a generic parameter are `Punctuated<TypeParamBound,
Token![+]>`.
- The arguments to a function call are `Punctuated<Expr, Token![,]>`.
This module provides a common representation for these punctuated sequences
in the form of the [`Punctuated<T, P>`] type. We store a vector of pairs of
syntax tree node + punctuation, where every node in the sequence is followed
by punctuation except for possibly the final one.
[`Punctuated<T, P>`]: Punctuated
```text
a_function_call(arg1, arg2, arg3);
~~~~^ ~~~~^ ~~~~
``` |
30538 |
restriction.rs |
|
6037 |
sealed.rs |
|
87 |
span.rs |
|
1197 |
spanned.rs |
A trait that can provide the `Span` of the complete contents of a syntax
tree node.
<br>
# Example
Suppose in a procedural macro we have a [`Type`] that we want to assert
implements the [`Sync`] trait. Maybe this is the type of one of the fields
of a struct for which we are deriving a trait implementation, and we need to
be able to pass a reference to one of those fields across threads.
[`Type`]: crate::Type
[`Sync`]: std::marker::Sync
If the field type does *not* implement `Sync` as required, we want the
compiler to report an error pointing out exactly which type it was.
The following macro code takes a variable `ty` of type `Type` and produces a
static assertion that `Sync` is implemented for that type.
```
# extern crate proc_macro;
#
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote_spanned;
use syn::Type;
use syn::spanned::Spanned;
# const IGNORE_TOKENS: &str = stringify! {
#[proc_macro_derive(MyMacro)]
# };
pub fn my_macro(input: TokenStream) -> TokenStream {
# let ty = get_a_type();
/* ... */
let assert_sync = quote_spanned! {ty.span()=>
struct _AssertSync where #ty: Sync;
};
/* ... */
# input
}
#
# fn get_a_type() -> Type {
# unimplemented!()
# }
```
By inserting this `assert_sync` fragment into the output code generated by
our macro, the user's code will fail to compile if `ty` does not implement
`Sync`. The errors they would see look like the following.
```text
error[E0277]: the trait bound `*const i32: std::marker::Sync` is not satisfied
--> src/main.rs:10:21
|
10 | bad_field: *const i32,
| ^^^^^^^^^^ `*const i32` cannot be shared between threads safely
```
In this technique, using the `Type`'s span for the error message makes the
error appear in the correct place underlining the right type.
<br>
# Limitations
The underlying [`proc_macro::Span::join`] method is nightly-only. When
called from within a procedural macro in a nightly compiler, `Spanned` will
use `join` to produce the intended span. When not using a nightly compiler,
only the span of the *first token* of the syntax tree node is returned.
In the common case of wanting to use the joined span as the span of a
`syn::Error`, consider instead using [`syn::Error::new_spanned`] which is
able to span the error correctly under the complete syntax tree node without
needing the unstable `join`.
[`syn::Error::new_spanned`]: crate::Error::new_spanned |
3779 |
stmt.rs |
|
16676 |
thread.rs |
|
1904 |
token.rs |
Tokens representing Rust punctuation, keywords, and delimiters.
The type names in this module can be difficult to keep straight, so we
prefer to use the [`Token!`] macro instead. This is a type-macro that
expands to the token type of the given token.
[`Token!`]: crate::Token
# Example
The [`ItemStatic`] syntax tree node is defined like this.
[`ItemStatic`]: crate::ItemStatic
```
# use syn::{Attribute, Expr, Ident, Token, Type, Visibility};
#
pub struct ItemStatic {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub static_token: Token![static],
pub mutability: Option<Token![mut]>,
pub ident: Ident,
pub colon_token: Token![:],
pub ty: Box<Type>,
pub eq_token: Token![=],
pub expr: Box<Expr>,
pub semi_token: Token![;],
}
```
# Parsing
Keywords and punctuation can be parsed through the [`ParseStream::parse`]
method. Delimiter tokens are parsed using the [`parenthesized!`],
[`bracketed!`] and [`braced!`] macros.
[`ParseStream::parse`]: crate::parse::ParseBuffer::parse()
[`parenthesized!`]: crate::parenthesized!
[`bracketed!`]: crate::bracketed!
[`braced!`]: crate::braced!
```
use syn::{Attribute, Result};
use syn::parse::{Parse, ParseStream};
#
# enum ItemStatic {}
// Parse the ItemStatic struct shown above.
impl Parse for ItemStatic {
fn parse(input: ParseStream) -> Result<Self> {
# use syn::ItemStatic;
# fn parse(input: ParseStream) -> Result<ItemStatic> {
Ok(ItemStatic {
attrs: input.call(Attribute::parse_outer)?,
vis: input.parse()?,
static_token: input.parse()?,
mutability: input.parse()?,
ident: input.parse()?,
colon_token: input.parse()?,
ty: input.parse()?,
eq_token: input.parse()?,
expr: input.parse()?,
semi_token: input.parse()?,
})
# }
# unimplemented!()
}
}
```
# Other operations
Every keyword and punctuation token supports the following operations.
- [Peeking] — `input.peek(Token![...])`
- [Parsing] — `input.parse::<Token![...]>()?`
- [Printing] — `quote!( ... #the_token ... )`
- Construction from a [`Span`] — `let the_token = Token![...](sp)`
- Field access to its span — `let sp = the_token.span`
[Peeking]: crate::parse::ParseBuffer::peek()
[Parsing]: crate::parse::ParseBuffer::parse()
[Printing]: https://docs.rs/quote/1.0/quote/trait.ToTokens.html
[`Span`]: https://docs.rs/proc-macro2/1.0/proc_macro2/struct.Span.html |
36190 |
tt.rs |
|
3727 |
ty.rs |
some sane fallback |
42378 |
verbatim.rs |
|
1216 |
whitespace.rs |
|
2103 |