Skip to content

Commit

Permalink
Remove dependency on proc-macro2
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Aug 25, 2020
1 parent 6d05cb8 commit 85520ea
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ proc-macro = true
version_check = "0.9.2"

[dependencies]
proc-macro2 = "1.0"
17 changes: 12 additions & 5 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proc_macro2::{Delimiter, Literal, Span, TokenStream, TokenTree};
use proc_macro::{Delimiter, Literal, Span, TokenStream, TokenTree};
use std::iter::Peekable;

use crate::{
Expand All @@ -23,17 +23,22 @@ pub(crate) fn parse_input(input: TokenStream) -> Result<Func> {
parse_as_empty(input)?;

if body.is_none()
|| !sig.iter().any(|tt| if let TokenTree::Ident(i) = tt { i == "fn" } else { false })
|| !sig
.iter()
.any(|tt| if let TokenTree::Ident(i) = tt { i.to_string() == "fn" } else { false })
{
return Err(error!(
Span::call_site(),
"#[const_fn] attribute may only be used on functions"
));
}
if !sig.iter().any(|tt| if let TokenTree::Ident(i) = tt { i == "const" } else { false }) {
if !sig
.iter()
.any(|tt| if let TokenTree::Ident(i) = tt { i.to_string() == "const" } else { false })
{
let span = sig
.iter()
.position(|tt| if let TokenTree::Ident(i) = tt { i == "fn" } else { false })
.position(|tt| if let TokenTree::Ident(i) = tt { i.to_string() == "fn" } else { false })
.map(|i| sig[i].span())
.unwrap();
return Err(error!(span, "#[const_fn] attribute may only be used on const functions"));
Expand All @@ -50,7 +55,9 @@ impl ToTokens for Func {
} else {
self.sig
.iter()
.filter(|tt| if let TokenTree::Ident(i) = tt { i != "const" } else { true })
.filter(
|tt| if let TokenTree::Ident(i) = tt { i.to_string() != "const" } else { true },
)
.for_each(|tt| tt.to_tokens(tokens));
}
self.body.to_tokens(tokens);
Expand Down
21 changes: 10 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ mod ast;
mod error;
mod to_tokens;

use proc_macro::TokenStream;
use proc_macro2::{Delimiter, TokenStream as TokenStream2, TokenTree};
use proc_macro::{Delimiter, TokenStream, TokenTree};
use std::str::FromStr;

use crate::{
Expand All @@ -70,11 +69,11 @@ pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
/// See crate level documentation for details.
#[proc_macro_attribute]
pub fn const_fn(args: TokenStream, input: TokenStream) -> TokenStream {
let arg = match parse_arg(args.into()) {
let arg = match parse_arg(args) {
Ok(a) => a,
Err(e) => return e.to_compile_error(),
};
let mut func = match ast::parse_input(input.into()) {
let mut func = match ast::parse_input(input) {
Ok(i) => i,
Err(e) => return e.to_compile_error(),
};
Expand All @@ -86,27 +85,27 @@ pub fn const_fn(args: TokenStream, input: TokenStream) -> TokenStream {
tokens.extend(cfg_not);
func.print_const = false;
tokens.extend(func.to_token_stream());
tokens.into()
tokens
}
Arg::Feature(f) => {
let (mut tokens, cfg_not) = cfg_attrs(f);
tokens.extend(func.to_token_stream());
tokens.extend(cfg_not);
func.print_const = false;
tokens.extend(func.to_token_stream());
tokens.into()
tokens
}
Arg::Version(req) => {
if req.major > 1 || req.minor > VERSION.minor {
func.print_const = false;
}
func.to_token_stream().into()
func.to_token_stream()
}
Arg::Nightly => {
if !VERSION.nightly {
func.print_const = false;
}
func.to_token_stream().into()
func.to_token_stream()
}
}
}
Expand All @@ -117,12 +116,12 @@ enum Arg {
// `const_fn(nightly)`
Nightly,
// `const_fn(cfg(...))`
Cfg(TokenStream2),
Cfg(TokenStream),
// `const_fn(feature = "...")`
Feature(TokenStream2),
Feature(TokenStream),
}

fn parse_arg(tokens: TokenStream2) -> Result<Arg> {
fn parse_arg(tokens: TokenStream) -> Result<Arg> {
let tokens2 = tokens.clone();
let mut iter = tokens.into_iter();

Expand Down
16 changes: 1 addition & 15 deletions src/to_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proc_macro2::*;
use proc_macro::*;
use std::iter;

pub(crate) trait ToTokens {
Expand Down Expand Up @@ -34,17 +34,3 @@ impl ToTokens for TokenStream {
tokens.extend(self.clone());
}
}

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

impl<T: ToTokens> ToTokens for &T {
fn to_tokens(&self, tokens: &mut TokenStream) {
T::to_tokens(*self, tokens);
}
}
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use proc_macro2::*;
use proc_macro::*;
use std::iter::FromIterator;

use crate::Result;

macro_rules! error {
($span:expr, $msg:expr) => {{
crate::error::Error::new($span.unwrap(), $msg)
crate::error::Error::new($span, $msg)
}};
($span:expr, $($tt:tt)*) => {
error!($span, format!($($tt)*))
Expand Down

0 comments on commit 85520ea

Please sign in to comment.