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 operate with proc-macro2 tokens #37

Merged
merged 1 commit into from
May 26, 2017
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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ language: rust
rust:
- nightly
- stable
- 1.12.1
- 1.13.0
- 1.15.1
- beta

script:
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ repository = "https://github.com/dtolnay/quote"
documentation = "https://docs.rs/quote/"
keywords = ["syn"]
include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]

[dependencies]
proc-macro2 = { git = 'https://github.com/alexcrichton/proc-macro2' }
57 changes: 0 additions & 57 deletions src/ident.rs

This file was deleted.

67 changes: 45 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,29 @@
//! recursion limit by adding `#![recursion_limit = "128"]` to your crate. An even
//! higher limit may be necessary for especially large invocations.

extern crate proc_macro2;

mod tokens;
pub use tokens::Tokens;

mod to_tokens;
pub use to_tokens::{ToTokens, ByteStr, Hex};

mod ident;
pub use ident::Ident;
pub use to_tokens::{ToTokens, ByteStr};

pub mod __rt {
pub use proc_macro2::*;

pub fn parse(tokens: &mut ::Tokens, s: &str) {
let s: TokenStream = s.parse().expect("invalid token stream");
tokens.append_all(s.into_iter());
}

pub fn append_kind(tokens: &mut ::Tokens, kind: TokenKind) {
tokens.append(TokenTree {
span: Default::default(),
kind: kind,
})
}
}

/// The whole point.
#[macro_export]
Expand Down Expand Up @@ -187,8 +202,8 @@ macro_rules! quote_each_token {
($tokens:ident) => {};

($tokens:ident # ! $($rest:tt)*) => {
$tokens.append("#");
$tokens.append("!");
quote_each_token!($tokens #);
quote_each_token!($tokens !);
quote_each_token!($tokens $($rest)*);
};

Expand All @@ -204,19 +219,20 @@ macro_rules! quote_each_token {
for (_i, pounded_var_names!(nested_tuples_pat () $($inner)*))
in pounded_var_names!(multi_zip_expr () $($inner)*).into_iter().enumerate() {
if _i > 0 {
$tokens.append(stringify!($sep));
quote_each_token!($tokens $sep);
}
quote_each_token!($tokens $($inner)*);
}
quote_each_token!($tokens $($rest)*);
};

($tokens:ident # [ $($inner:tt)* ] $($rest:tt)*) => {
$tokens.append("#");
$tokens.append("[");
quote_each_token!($tokens $($inner)*);
$tokens.append("]");
quote_each_token!($tokens $($rest)*);
quote_each_token!($tokens #);
$crate::__rt::append_kind(&mut $tokens,
$crate::__rt::TokenKind::Sequence(
$crate::__rt::Delimiter::Brace,
quote! { $($rest)* }.into()
));
};

($tokens:ident # $first:ident $($rest:tt)*) => {
Expand All @@ -225,28 +241,35 @@ macro_rules! quote_each_token {
};

($tokens:ident ( $($first:tt)* ) $($rest:tt)*) => {
$tokens.append("(");
quote_each_token!($tokens $($first)*);
$tokens.append(")");
$crate::__rt::append_kind(&mut $tokens,
$crate::__rt::TokenKind::Sequence(
$crate::__rt::Delimiter::Parenthesis,
quote! { $($first)* }.into()
));
quote_each_token!($tokens $($rest)*);
};

($tokens:ident [ $($first:tt)* ] $($rest:tt)*) => {
$tokens.append("[");
quote_each_token!($tokens $($first)*);
$tokens.append("]");
$crate::__rt::append_kind(&mut $tokens,
$crate::__rt::TokenKind::Sequence(
$crate::__rt::Delimiter::Bracket,
quote! { $($first)* }.into()
));
quote_each_token!($tokens $($rest)*);
};

($tokens:ident { $($first:tt)* } $($rest:tt)*) => {
$tokens.append("{");
quote_each_token!($tokens $($first)*);
$tokens.append("}");
$crate::__rt::append_kind(&mut $tokens,
$crate::__rt::TokenKind::Sequence(
$crate::__rt::Delimiter::Brace,
quote! { $($first)* }.into()
));
quote_each_token!($tokens $($rest)*);
};

($tokens:ident $first:tt $($rest:tt)*) => {
$tokens.append(stringify!($first));
// TODO: this seems slow... special case some `:tt` arguments?
$crate::__rt::parse(&mut $tokens, stringify!($first));
quote_each_token!($tokens $($rest)*);
};
}
Loading