Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to proc-macro2 0.4 #73

Merged
merged 7 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,4 @@ 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 }

[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"]
proc-macro2 = { version = "0.4", default-features = false }
113 changes: 113 additions & 0 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use super::ToTokens;

use proc_macro2::{TokenStream, TokenTree};

/// TokenStream extension trait with methods for appending tokens.
///
/// This trait is sealed and cannot be implemented outside of the `quote` crate.
pub trait TokenStreamExt: private::Sealed {
fn append<U>(&mut self, token: U)
where
U: Into<TokenTree>;

fn append_all<T, I>(&mut self, iter: I)
where
T: ToTokens,
I: IntoIterator<Item = T>;

fn append_separated<T, I, U>(&mut self, iter: I, op: U)
where
T: ToTokens,
I: IntoIterator<Item = T>,
U: ToTokens;

fn append_terminated<T, I, U>(&mut self, iter: I, term: U)
where
T: ToTokens,
I: IntoIterator<Item = T>,
U: ToTokens;
}

impl TokenStreamExt for TokenStream {
/// For use by `ToTokens` implementations.
///
/// Appends the token specified to this list of tokens.
fn append<U>(&mut self, token: U)
where
U: Into<TokenTree>,
{
self.extend(Some(token.into()));
}

/// For use by `ToTokens` implementations.
///
/// ```
/// # #[macro_use] extern crate quote;
/// # extern crate proc_macro2;
/// # use quote::{TokenStreamExt, ToTokens};
/// # use proc_macro2::TokenStream;
/// # fn main() {
/// struct X;
///
/// impl ToTokens for X {
/// fn to_tokens(&self, tokens: &mut TokenStream) {
/// tokens.append_all(&[true, false]);
/// }
/// }
///
/// let tokens = quote!(#X);
/// assert_eq!(tokens.to_string(), "true false");
/// # }
/// ```
fn append_all<T, I>(&mut self, iter: I)
where
T: ToTokens,
I: IntoIterator<Item = T>,
{
for token in iter {
token.to_tokens(self);
}
}

/// For use by `ToTokens` implementations.
///
/// Appends all of the items in the iterator `I`, separated by the tokens
/// `U`.
fn append_separated<T, I, U>(&mut self, iter: I, op: U)
where
T: ToTokens,
I: IntoIterator<Item = T>,
U: ToTokens,
{
for (i, token) in iter.into_iter().enumerate() {
if i > 0 {
op.to_tokens(self);
}
token.to_tokens(self);
}
}

/// For use by `ToTokens` implementations.
///
/// Appends all tokens in the iterator `I`, appending `U` after each
/// element, including after the last element of the iterator.
fn append_terminated<T, I, U>(&mut self, iter: I, term: U)
where
T: ToTokens,
I: IntoIterator<Item = T>,
U: ToTokens,
{
for token in iter {
token.to_tokens(self);
term.to_tokens(self);
}
}
}

mod private {
use proc_macro2::TokenStream;

pub trait Sealed {}

impl Sealed for TokenStream {}
}
37 changes: 17 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,10 @@
// Quote types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/quote/0.5.2")]

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

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

mod to_tokens;
pub use to_tokens::ToTokens;
Expand All @@ -111,9 +109,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 @@ -163,9 +161,8 @@ pub mod __rt {
/// # Example
///
/// ```
/// # #[cfg(feature = "proc-macro")]
/// # #[cfg(any())]
/// extern crate proc_macro;
/// # #[cfg(not(feature = "proc-macro"))]
/// # extern crate proc_macro2 as proc_macro;
///
/// #[macro_use]
Expand Down Expand Up @@ -266,8 +263,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 +275,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 +311,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 +449,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 +466,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
Loading