gecko-dev/third_party/rust/quote
Emilio Cobos Álvarez 199da14cd0 No bug - Revendor rust dependencies. r=me on a CLOSED TREE
MozReview-Commit-ID: 7LSWUCyCxkT


--HG--
rename : third_party/rust/quote/.cargo-checksum.json => third_party/rust/quote-0.3.15/.cargo-checksum.json
rename : third_party/rust/quote/Cargo.toml => third_party/rust/quote-0.3.15/Cargo.toml
rename : third_party/rust/syn/LICENSE-MIT => third_party/rust/quote-0.3.15/LICENSE-MIT
rename : third_party/rust/quote/README.md => third_party/rust/quote-0.3.15/README.md
rename : third_party/rust/quote/src/ident.rs => third_party/rust/quote-0.3.15/src/ident.rs
rename : third_party/rust/quote/src/lib.rs => third_party/rust/quote-0.3.15/src/lib.rs
rename : third_party/rust/quote/src/to_tokens.rs => third_party/rust/quote-0.3.15/src/to_tokens.rs
rename : third_party/rust/quote/src/tokens.rs => third_party/rust/quote-0.3.15/src/tokens.rs
rename : third_party/rust/quote/tests/test.rs => third_party/rust/quote-0.3.15/tests/test.rs
rename : third_party/rust/syn/.cargo-checksum.json => third_party/rust/syn-0.11.11/.cargo-checksum.json
rename : third_party/rust/syn/Cargo.toml => third_party/rust/syn-0.11.11/Cargo.toml
rename : third_party/rust/syn/LICENSE-MIT => third_party/rust/syn-0.11.11/LICENSE-MIT
rename : third_party/rust/syn/README.md => third_party/rust/syn-0.11.11/README.md
rename : third_party/rust/syn/src/aster/generics.rs => third_party/rust/syn-0.11.11/src/aster/generics.rs
rename : third_party/rust/syn/src/aster/ident.rs => third_party/rust/syn-0.11.11/src/aster/ident.rs
rename : third_party/rust/syn/src/aster/invoke.rs => third_party/rust/syn-0.11.11/src/aster/invoke.rs
rename : third_party/rust/syn/src/aster/lifetime.rs => third_party/rust/syn-0.11.11/src/aster/lifetime.rs
rename : third_party/rust/syn/src/aster/mod.rs => third_party/rust/syn-0.11.11/src/aster/mod.rs
rename : third_party/rust/syn/src/aster/path.rs => third_party/rust/syn-0.11.11/src/aster/path.rs
rename : third_party/rust/syn/src/aster/qpath.rs => third_party/rust/syn-0.11.11/src/aster/qpath.rs
rename : third_party/rust/syn/src/aster/ty.rs => third_party/rust/syn-0.11.11/src/aster/ty.rs
rename : third_party/rust/syn/src/aster/ty_param.rs => third_party/rust/syn-0.11.11/src/aster/ty_param.rs
rename : third_party/rust/syn/src/aster/where_predicate.rs => third_party/rust/syn-0.11.11/src/aster/where_predicate.rs
rename : third_party/rust/syn/src/attr.rs => third_party/rust/syn-0.11.11/src/attr.rs
rename : third_party/rust/syn/src/constant.rs => third_party/rust/syn-0.11.11/src/constant.rs
rename : third_party/rust/syn/src/data.rs => third_party/rust/syn-0.11.11/src/data.rs
rename : third_party/rust/syn/src/derive.rs => third_party/rust/syn-0.11.11/src/derive.rs
rename : third_party/rust/syn/src/escape.rs => third_party/rust/syn-0.11.11/src/escape.rs
rename : third_party/rust/syn/src/expr.rs => third_party/rust/syn-0.11.11/src/expr.rs
rename : third_party/rust/syn/src/fold.rs => third_party/rust/syn-0.11.11/src/fold.rs
rename : third_party/rust/syn/src/generics.rs => third_party/rust/syn-0.11.11/src/generics.rs
rename : third_party/rust/syn/src/ident.rs => third_party/rust/syn-0.11.11/src/ident.rs
rename : third_party/rust/syn/src/item.rs => third_party/rust/syn-0.11.11/src/item.rs
rename : third_party/rust/syn/src/krate.rs => third_party/rust/syn-0.11.11/src/krate.rs
rename : third_party/rust/syn/src/lib.rs => third_party/rust/syn-0.11.11/src/lib.rs
rename : third_party/rust/syn/src/lit.rs => third_party/rust/syn-0.11.11/src/lit.rs
rename : third_party/rust/syn/src/mac.rs => third_party/rust/syn-0.11.11/src/mac.rs
rename : third_party/rust/syn/src/op.rs => third_party/rust/syn-0.11.11/src/op.rs
rename : third_party/rust/syn/src/ty.rs => third_party/rust/syn-0.11.11/src/ty.rs
rename : third_party/rust/syn/src/visit.rs => third_party/rust/syn-0.11.11/src/visit.rs
rename : third_party/rust/unicode-xid/.cargo-checksum.json => third_party/rust/unicode-xid-0.0.4/.cargo-checksum.json
rename : third_party/rust/unicode-xid/Cargo.toml => third_party/rust/unicode-xid-0.0.4/Cargo.toml
2018-02-14 12:40:42 +01:00
..
src No bug - Revendor rust dependencies. r=me on a CLOSED TREE 2018-02-14 12:40:42 +01:00
tests No bug - Revendor rust dependencies. r=me on a CLOSED TREE 2018-02-14 12:40:42 +01:00
.cargo-checksum.json No bug - Revendor rust dependencies. r=me on a CLOSED TREE 2018-02-14 12:40:42 +01:00
Cargo.toml No bug - Revendor rust dependencies. r=me on a CLOSED TREE 2018-02-14 12:40:42 +01:00
LICENSE-APACHE
LICENSE-MIT
README.md No bug - Revendor rust dependencies. r=me on a CLOSED TREE 2018-02-14 12:40:42 +01:00

README.md

Rust Quasi-Quoting

Build Status Latest Version Rust Documentation

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

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.