Skip to content

Commit

Permalink
Prepare for the next proc-macro2 release
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed May 16, 2018
1 parent c1ff101 commit 0c78a73
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 263 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ rust:

script:
- cargo test
- cargo test --no-default-features
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "quote"
version = "0.5.2" # don't forget to update html_root_url, version in readme for breaking changes
version = "0.6.0" # don't forget to update html_root_url, version in readme for breaking changes
authors = ["David Tolnay <[email protected]>"]
license = "MIT/Apache-2.0"
description = "Quasi-quoting macro quote!(...)"
Expand All @@ -11,10 +11,7 @@ readme = "README.md"
include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]

[dependencies]
proc-macro2 = { version = "0.3", default-features = false }
proc-macro2 = { version = "0.4", default-features = false }

[features]
default = ["proc-macro"]
# Disabling the proc-macro feature removes the dynamic library dependency on
# libproc_macro in the rustc compiler.
proc-macro = ["proc-macro2/proc-macro"]
[patch.crates-io]
proc-macro2 = { git = 'https://github.com/alexcrichton/proc-macro2', branch = 'next' }
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Rust Quasi-Quoting
This crate provides the [`quote!`] macro for turning Rust syntax tree data
structures into tokens of source code.

[`quote!`]: https://docs.rs/quote/0.5/quote/macro.quote.html
[`quote!`]: https://docs.rs/quote/0.6/quote/macro.quote.html

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
Expand All @@ -33,7 +33,7 @@ first support for procedural macros in Rust 1.15.0.*

```toml
[dependencies]
quote = "0.5"
quote = "0.6"
```

```rust
Expand All @@ -50,13 +50,13 @@ should think of `Tokens` as representing a fragment of Rust source code. Call
or call `into()` to stream them as a `TokenStream` back to the compiler in a
procedural macro.

[`quote::Tokens`]: https://docs.rs/quote/0.5/quote/struct.Tokens.html
[`quote::Tokens`]: https://docs.rs/quote/0.6/quote/struct.Tokens.html

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`].

[`quote::ToTokens`]: https://docs.rs/quote/0.5/quote/trait.ToTokens.html
[`quote::ToTokens`]: https://docs.rs/quote/0.6/quote/trait.ToTokens.html
[`syn`]: https://github.com/dtolnay/syn

```rust
Expand Down Expand Up @@ -110,7 +110,7 @@ are spanned with [`Span::def_site()`].
A different span can be provided explicitly through the [`quote_spanned!`]
macro.

[`quote_spanned!`]: https://docs.rs/quote/0.5/quote/macro.quote_spanned.html
[`quote_spanned!`]: https://docs.rs/quote/0.6/quote/macro.quote_spanned.html

### Limitations

Expand Down
34 changes: 17 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//!
//! ```toml
//! [dependencies]
//! quote = "0.5"
//! quote = "0.6"
//! ```
//!
//! ```
Expand Down Expand Up @@ -92,14 +92,14 @@
//! An even higher limit may be necessary for especially large invocations.
// Quote types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/quote/0.5.2")]
#![doc(html_root_url = "https://docs.rs/quote/0.6.0")]

#[cfg(feature = "proc-macro")]
extern crate proc_macro;
extern crate proc_macro2;

mod tokens;
pub use tokens::Tokens;
pub use tokens::TokenStreamExt;

mod to_tokens;
pub use to_tokens::ToTokens;
Expand All @@ -111,9 +111,9 @@ pub mod __rt {
pub use proc_macro2::*;

// Not public API.
pub fn parse(tokens: &mut ::Tokens, span: Span, s: &str) {
pub fn parse(tokens: &mut TokenStream, span: Span, s: &str) {
let s: TokenStream = s.parse().expect("invalid token stream");
tokens.append_all(s.into_iter().map(|mut t| {
tokens.extend(s.into_iter().map(|mut t| {
t.set_span(span);
t
}));
Expand Down Expand Up @@ -266,8 +266,8 @@ macro_rules! quote {
/// # extern crate quote;
/// # extern crate proc_macro2;
/// #
/// # use quote::{Tokens, ToTokens};
/// # use proc_macro2::Span;
/// # use quote::{TokenStreamExt, ToTokens};
/// # use proc_macro2::{Span, TokenStream};
/// #
/// # struct Type;
/// #
Expand All @@ -278,7 +278,7 @@ macro_rules! quote {
/// # }
/// #
/// # impl ToTokens for Type {
/// # fn to_tokens(&self, _tokens: &mut Tokens) {}
/// # fn to_tokens(&self, _tokens: &mut TokenStream) {}
/// # }
/// #
/// # fn main() {
Expand Down Expand Up @@ -314,7 +314,7 @@ macro_rules! quote {
macro_rules! quote_spanned {
($span:expr=> $($tt:tt)*) => {
{
let mut _s = $crate::Tokens::new();
let mut _s = $crate::__rt::TokenStream::empty();
let _span = $span;
quote_each_token!(_s _span $($tt)*);
_s
Expand Down Expand Up @@ -452,13 +452,13 @@ macro_rules! quote_each_token {

($tokens:ident $span:ident # [ $($inner:tt)* ] $($rest:tt)*) => {
quote_each_token!($tokens $span #);
$tokens.append({
$tokens.extend({
let mut g = $crate::__rt::Group::new(
$crate::__rt::Delimiter::Bracket,
quote_spanned!($span=> $($inner)*).into(),
);
g.set_span($span);
g
Some($crate::__rt::TokenTree::from(g))
});
quote_each_token!($tokens $span $($rest)*);
};
Expand All @@ -469,37 +469,37 @@ macro_rules! quote_each_token {
};

($tokens:ident $span:ident ( $($first:tt)* ) $($rest:tt)*) => {
$tokens.append({
$tokens.extend({
let mut g = $crate::__rt::Group::new(
$crate::__rt::Delimiter::Parenthesis,
quote_spanned!($span=> $($first)*).into(),
);
g.set_span($span);
g
Some($crate::__rt::TokenTree::from(g))
});
quote_each_token!($tokens $span $($rest)*);
};

($tokens:ident $span:ident [ $($first:tt)* ] $($rest:tt)*) => {
$tokens.append({
$tokens.extend({
let mut g = $crate::__rt::Group::new(
$crate::__rt::Delimiter::Bracket,
quote_spanned!($span=> $($first)*).into(),
);
g.set_span($span);
g
Some($crate::__rt::TokenTree::from(g))
});
quote_each_token!($tokens $span $($rest)*);
};

($tokens:ident $span:ident { $($first:tt)* } $($rest:tt)*) => {
$tokens.append({
$tokens.extend({
let mut g = $crate::__rt::Group::new(
$crate::__rt::Delimiter::Brace,
quote_spanned!($span=> $($first)*).into(),
);
g.set_span($span);
g
Some($crate::__rt::TokenTree::from(g))
});
quote_each_token!($tokens $span $($rest)*);
};
Expand Down
58 changes: 29 additions & 29 deletions src/to_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::Tokens;
use super::TokenStreamExt;

use std::borrow::Cow;

use proc_macro2::{Group, Literal, Op, Span, Term, TokenStream, TokenTree};
use proc_macro2::{Group, Literal, Punct, Span, Ident, TokenStream, TokenTree};

/// Types that can be interpolated inside a [`quote!`] invocation.
///
Expand All @@ -15,23 +15,23 @@ pub trait ToTokens {
///
/// ```
/// extern crate quote;
/// use quote::{Tokens, ToTokens};
/// use quote::{TokenStreamExt, ToTokens};
///
/// extern crate proc_macro2;
/// use proc_macro2::{TokenTree, Spacing, Span, Op};
/// use proc_macro2::{TokenTree, Spacing, Span, Punct, TokenStream};
///
/// pub struct Path {
/// pub global: bool,
/// pub segments: Vec<PathSegment>,
/// }
///
/// impl ToTokens for Path {
/// fn to_tokens(&self, tokens: &mut Tokens) {
/// fn to_tokens(&self, tokens: &mut TokenStream) {
/// for (i, segment) in self.segments.iter().enumerate() {
/// if i > 0 || self.global {
/// // Double colon `::`
/// tokens.append(Op::new(':', Spacing::Joint));
/// tokens.append(Op::new(':', Spacing::Alone));
/// tokens.append(Punct::new(':', Spacing::Joint));
/// tokens.append(Punct::new(':', Spacing::Alone));
/// }
/// segment.to_tokens(tokens);
/// }
Expand All @@ -41,71 +41,71 @@ pub trait ToTokens {
/// # pub struct PathSegment;
/// #
/// # impl ToTokens for PathSegment {
/// # fn to_tokens(&self, tokens: &mut Tokens) {
/// # fn to_tokens(&self, tokens: &mut TokenStream) {
/// # unimplemented!()
/// # }
/// # }
/// #
/// # fn main() {}
/// ```
fn to_tokens(&self, tokens: &mut Tokens);
fn to_tokens(&self, tokens: &mut TokenStream);

/// Convert `self` directly into a `Tokens` object.
///
/// This method is implicitly implemented using `to_tokens`, and acts as a
/// convenience method for consumers of the `ToTokens` trait.
fn into_tokens(self) -> Tokens
fn into_token_stream(self) -> TokenStream
where
Self: Sized,
{
let mut tokens = Tokens::new();
let mut tokens = TokenStream::empty();
self.to_tokens(&mut tokens);
tokens
}
}

impl<'a, T: ?Sized + ToTokens> ToTokens for &'a T {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens);
}
}

impl<'a, T: ?Sized + ToOwned + ToTokens> ToTokens for Cow<'a, T> {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens);
}
}

impl<T: ?Sized + ToTokens> ToTokens for Box<T> {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens);
}
}

impl<T: ToTokens> ToTokens for Option<T> {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
if let Some(ref t) = *self {
t.to_tokens(tokens);
}
}
}

impl ToTokens for str {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(Literal::string(self));
}
}

impl ToTokens for String {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.as_str().to_tokens(tokens);
}
}

macro_rules! primitive {
($($t:ident => $name:ident)*) => ($(
impl ToTokens for $t {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(Literal::$name(*self));
}
}
Expand All @@ -130,50 +130,50 @@ primitive! {
}

impl ToTokens for char {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(Literal::character(*self));
}
}

impl ToTokens for bool {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
let word = if *self { "true" } else { "false" };
tokens.append(Term::new(word, Span::call_site()));
tokens.append(Ident::new(word, Span::call_site()));
}
}

impl ToTokens for Group {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self.clone());
}
}

impl ToTokens for Term {
fn to_tokens(&self, tokens: &mut Tokens) {
impl ToTokens for Ident {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self.clone());
}
}

impl ToTokens for Op {
fn to_tokens(&self, tokens: &mut Tokens) {
impl ToTokens for Punct {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self.clone());
}
}

impl ToTokens for Literal {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self.clone());
}
}

impl ToTokens for TokenTree {
fn to_tokens(&self, dst: &mut Tokens) {
fn to_tokens(&self, dst: &mut TokenStream) {
dst.append(self.clone());
}
}

impl ToTokens for TokenStream {
fn to_tokens(&self, dst: &mut Tokens) {
fn to_tokens(&self, dst: &mut TokenStream) {
dst.append_all(self.clone().into_iter());
}
}
Loading

0 comments on commit 0c78a73

Please sign in to comment.