af1d0f8ad9
--HG-- rename : third_party/rust/proc-macro2/.cargo-checksum.json => third_party/rust/proc-macro2-0.2.2/.cargo-checksum.json rename : third_party/rust/proc-macro2/.travis.yml => third_party/rust/proc-macro2-0.2.2/.travis.yml rename : third_party/rust/proc-macro2/Cargo.toml => third_party/rust/proc-macro2-0.2.2/Cargo.toml rename : third_party/rust/syn-0.11.11/LICENSE-APACHE => third_party/rust/proc-macro2-0.2.2/LICENSE-APACHE rename : third_party/rust/proc-macro2/README.md => third_party/rust/proc-macro2-0.2.2/README.md rename : third_party/rust/proc-macro2/src/lib.rs => third_party/rust/proc-macro2-0.2.2/src/lib.rs rename : third_party/rust/proc-macro2/src/macros.rs => third_party/rust/proc-macro2-0.2.2/src/macros.rs rename : third_party/rust/proc-macro2/src/stable.rs => third_party/rust/proc-macro2-0.2.2/src/stable.rs rename : third_party/rust/proc-macro2/src/strnom.rs => third_party/rust/proc-macro2-0.2.2/src/strnom.rs rename : third_party/rust/proc-macro2/src/unstable.rs => third_party/rust/proc-macro2-0.2.2/src/unstable.rs rename : third_party/rust/proc-macro2/tests/test.rs => third_party/rust/proc-macro2-0.2.2/tests/test.rs rename : third_party/rust/quote/.cargo-checksum.json => third_party/rust/quote-0.4.2/.cargo-checksum.json rename : third_party/rust/quote/Cargo.toml => third_party/rust/quote-0.4.2/Cargo.toml rename : third_party/rust/synom/LICENSE-APACHE => third_party/rust/quote-0.4.2/LICENSE-APACHE rename : third_party/rust/syn-0.11.11/LICENSE-MIT => third_party/rust/quote-0.4.2/LICENSE-MIT rename : third_party/rust/quote/README.md => third_party/rust/quote-0.4.2/README.md rename : third_party/rust/quote/src/lib.rs => third_party/rust/quote-0.4.2/src/lib.rs rename : third_party/rust/quote/src/to_tokens.rs => third_party/rust/quote-0.4.2/src/to_tokens.rs rename : third_party/rust/quote/src/tokens.rs => third_party/rust/quote-0.4.2/src/tokens.rs rename : third_party/rust/quote/tests/test.rs => third_party/rust/quote-0.4.2/tests/test.rs rename : third_party/rust/syn/.cargo-checksum.json => third_party/rust/syn-0.12.12/.cargo-checksum.json rename : third_party/rust/syn/Cargo.toml => third_party/rust/syn-0.12.12/Cargo.toml rename : third_party/rust/unicode-xid-0.0.4/LICENSE-APACHE => third_party/rust/syn-0.12.12/LICENSE-APACHE rename : third_party/rust/syn/README.md => third_party/rust/syn-0.12.12/README.md rename : third_party/rust/syn/src/attr.rs => third_party/rust/syn-0.12.12/src/attr.rs rename : third_party/rust/syn/src/buffer.rs => third_party/rust/syn-0.12.12/src/buffer.rs rename : third_party/rust/syn/src/data.rs => third_party/rust/syn-0.12.12/src/data.rs rename : third_party/rust/syn/src/expr.rs => third_party/rust/syn-0.12.12/src/expr.rs rename : third_party/rust/syn/src/gen/fold.rs => third_party/rust/syn-0.12.12/src/gen/fold.rs rename : third_party/rust/syn/src/gen/visit.rs => third_party/rust/syn-0.12.12/src/gen/visit.rs rename : third_party/rust/syn/src/gen/visit_mut.rs => third_party/rust/syn-0.12.12/src/gen/visit_mut.rs rename : third_party/rust/syn/src/generics.rs => third_party/rust/syn-0.12.12/src/generics.rs rename : third_party/rust/syn/src/ident.rs => third_party/rust/syn-0.12.12/src/ident.rs rename : third_party/rust/syn/src/item.rs => third_party/rust/syn-0.12.12/src/item.rs rename : third_party/rust/syn/src/lib.rs => third_party/rust/syn-0.12.12/src/lib.rs rename : third_party/rust/syn/src/lifetime.rs => third_party/rust/syn-0.12.12/src/lifetime.rs rename : third_party/rust/syn/src/lit.rs => third_party/rust/syn-0.12.12/src/lit.rs rename : third_party/rust/syn/src/punctuated.rs => third_party/rust/syn-0.12.12/src/punctuated.rs rename : third_party/rust/syn/src/spanned.rs => third_party/rust/syn-0.12.12/src/spanned.rs rename : third_party/rust/syn/src/synom.rs => third_party/rust/syn-0.12.12/src/synom.rs rename : third_party/rust/syn/src/token.rs => third_party/rust/syn-0.12.12/src/token.rs rename : third_party/rust/syn/src/tt.rs => third_party/rust/syn-0.12.12/src/tt.rs rename : third_party/rust/syn/src/ty.rs => third_party/rust/syn-0.12.12/src/ty.rs extra : rebase_source : 07c6aa03703317fd127f1627f22e207c225abddb |
||
---|---|---|
.. | ||
src | ||
tests | ||
.cargo-checksum.json | ||
Cargo.toml | ||
LICENSE-APACHE | ||
LICENSE-MIT | ||
README.md |
README.md
Rust Quasi-Quoting
This crate provides the quote!
macro for turning Rust syntax tree data
structures into tokens of source code.
Procedural macros in Rust receive a stream of tokens as input, execute arbitrary Rust code to determine how to manipulate those tokens, and produce a stream of tokens to hand back to the compiler to compile into the caller's crate. Quasi-quoting is a solution to one piece of that -- producing tokens to return to the compiler.
The idea of quasi-quoting is that we write code that we treat as data.
Within the quote!
macro, we can write what looks like code to our text editor
or IDE. We get all the benefits of the editor's brace matching, syntax
highlighting, indentation, and maybe autocompletion. But rather than compiling
that as code into the current crate, we can treat it as data, pass it around,
mutate it, and eventually hand it back to the compiler as tokens to compile into
the macro caller's crate.
This crate is motivated by the procedural macro use case, but is a general-purpose Rust quasi-quoting library and is not specific to procedural macros.
Version requirement: Quote supports any compiler version back to Rust's very first support for procedural macros in Rust 1.15.0.
[dependencies]
quote = "0.4"
#[macro_use]
extern crate quote;
Syntax
The quote crate provides a quote!
macro within which you can write Rust code
that gets packaged into a quote::Tokens
and can be treated as data. You
should think of Tokens
as representing a fragment of Rust source code. Call
to_string()
on a Tokens
to get back the fragment of source code as a string,
or call into()
to stream them as a TokenStream
back to the compiler in a
procedural macro.
Within the quote!
macro, interpolation is done with #var
. Any type
implementing the quote::ToTokens
trait can be interpolated. This includes
most Rust primitive types as well as most of the syntax tree types from syn
.
let tokens = quote! {
struct SerializeWith #generics #where_clause {
value: &'a #field_ty,
phantom: ::std::marker::PhantomData<#item_ty>,
}
impl #generics serde::Serialize for SerializeWith #generics #where_clause {
fn serialize<S>(&self, s: &mut S) -> Result<(), S::Error>
where S: serde::Serializer
{
#path(self.value, s)
}
}
SerializeWith {
value: #value,
phantom: ::std::marker::PhantomData::<#item_ty>,
}
};
Repetition
Repetition is done using #(...)*
or #(...),*
similar to macro_rules!
. This
iterates through the elements of any variable interpolated within the repetition
and inserts a copy of the repetition body for each one. The variables in an
interpolation may be anything that implements IntoIterator
, including Vec
or
a pre-existing iterator.
#(#var)*
— no separators#(#var),*
— the character before the asterisk is used as a separator#( struct #var; )*
— the repetition can contain other things#( #k => println!("{}", #v), )*
— even multiple interpolations
Note that there is a difference between #(#var ,)*
and #(#var),*
—the latter
does not produce a trailing comma. This matches the behavior of delimiters in
macro_rules!
.
Hygiene
Any interpolated tokens preserve the Span
information provided by their
ToTokens
implementation. Tokens that originate within a quote!
invocation
are spanned with Span::def_site()
.
A different span can be provided explicitly through the quote_spanned!
macro.
Recursion limit
The quote!
macro relies on deep recursion so some large invocations may fail
with "recursion limit reached" when you compile. If it fails, bump up the
recursion limit by adding #![recursion_limit = "128"]
to your crate. An even
higher limit may be necessary for especially large invocations. You don't need
this unless the compiler tells you that you need it.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.