-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
236 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,5 +29,3 @@ version_check = "0.9.2" | |
[dependencies] | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
syn = "1.0" | ||
syn-mid = "0.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use proc_macro2::{Delimiter, Literal, Span, TokenStream, TokenTree}; | ||
use quote::ToTokens; | ||
use std::iter::Peekable; | ||
|
||
use crate::utils::{parse_as_empty, tt_span}; | ||
|
||
pub(crate) struct Func { | ||
pub(crate) attrs: Vec<Attribute>, | ||
pub(crate) sig: Vec<TokenTree>, | ||
pub(crate) body: TokenTree, | ||
pub(crate) print_const: bool, | ||
} | ||
|
||
pub(crate) fn parse_input(input: TokenStream) -> Result<Func, TokenStream> { | ||
let mut input = input.into_iter().peekable(); | ||
|
||
let attrs = parse_attrs(&mut input)?; | ||
let sig = parse_signature(&mut input); | ||
let body = input.next(); | ||
parse_as_empty(input)?; | ||
|
||
if body.is_none() | ||
|| !sig.iter().any(|tt| if let TokenTree::Ident(i) = tt { i == "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 }) { | ||
return Err(error!( | ||
Span::call_site(), | ||
"#[const_fn] attribute may only be used on const functions" | ||
)); | ||
} | ||
|
||
Ok(Func { attrs, sig, body: body.unwrap(), print_const: true }) | ||
} | ||
|
||
impl ToTokens for Func { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
self.attrs.iter().for_each(|attr| attr.to_tokens(tokens)); | ||
if self.print_const { | ||
self.sig.iter().for_each(|attr| attr.to_tokens(tokens)); | ||
} else { | ||
self.sig | ||
.iter() | ||
.filter(|tt| if let TokenTree::Ident(i) = tt { i != "const" } else { true }) | ||
.for_each(|tt| tt.to_tokens(tokens)); | ||
} | ||
self.body.to_tokens(tokens); | ||
} | ||
} | ||
|
||
fn parse_signature(input: &mut Peekable<impl Iterator<Item = TokenTree>>) -> Vec<TokenTree> { | ||
let mut sig = Vec::new(); | ||
loop { | ||
match input.peek() { | ||
Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Brace => { | ||
return sig; | ||
} | ||
None => return sig, | ||
_ => sig.push(input.next().unwrap()), | ||
} | ||
} | ||
} | ||
|
||
fn parse_attrs( | ||
input: &mut Peekable<impl Iterator<Item = TokenTree>>, | ||
) -> Result<Vec<Attribute>, TokenStream> { | ||
let mut attrs = Vec::new(); | ||
|
||
loop { | ||
let pound_token = match input.peek() { | ||
Some(TokenTree::Punct(p)) if p.as_char() == '#' => input.next().unwrap(), | ||
_ => return Ok(attrs), | ||
}; | ||
let group = match input.peek() { | ||
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Bracket => { | ||
input.next().unwrap() | ||
} | ||
tt => return Err(error!(tt_span(tt), "expected `[`")), | ||
}; | ||
attrs.push(Attribute { pound_token, group }); | ||
} | ||
} | ||
|
||
pub(crate) struct Attribute { | ||
// `#` | ||
pub(crate) pound_token: TokenTree, | ||
// `[...]` | ||
pub(crate) group: TokenTree, | ||
} | ||
|
||
impl ToTokens for Attribute { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
self.pound_token.to_tokens(tokens); | ||
self.group.to_tokens(tokens); | ||
} | ||
} | ||
|
||
pub(crate) struct LitStr { | ||
token: Literal, | ||
value: String, | ||
} | ||
|
||
impl LitStr { | ||
pub(crate) fn new(token: &Literal) -> Result<Self, TokenStream> { | ||
let value = token.to_string(); | ||
// unlike `syn::LitStr`, only accepts `"..."` | ||
if value.starts_with('"') && value.ends_with('"') { | ||
Ok(Self { token: token.clone(), value }) | ||
} else { | ||
Err(error!(token.span(), "expected string literal")) | ||
} | ||
} | ||
|
||
pub(crate) fn value(&self) -> &str { | ||
&self.value[1..self.value.len() - 1] | ||
} | ||
|
||
pub(crate) fn span(&self) -> Span { | ||
self.token.span() | ||
} | ||
} | ||
|
||
impl ToTokens for LitStr { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
self.token.to_tokens(tokens); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use proc_macro2::{Span, TokenStream, TokenTree}; | ||
|
||
macro_rules! error { | ||
($span:expr, $msg:expr) => {{ | ||
let msg = $msg; | ||
quote::quote_spanned!($span=> compile_error! { #msg }) | ||
}}; | ||
($span:expr, $($tt:tt)*) => { | ||
error!($span, format!($($tt)*)) | ||
}; | ||
} | ||
|
||
pub(crate) fn tt_span(tt: Option<&TokenTree>) -> Span { | ||
tt.map_or_else(Span::call_site, TokenTree::span) | ||
} | ||
|
||
pub(crate) fn parse_as_empty( | ||
mut tokens: impl Iterator<Item = TokenTree>, | ||
) -> Result<(), TokenStream> { | ||
match tokens.next() { | ||
Some(tt) => Err(error!(tt.span(), "unexpected token: {}", tt)), | ||
None => Ok(()), | ||
} | ||
} |