2017-07-12 16:50:05 +00:00
|
|
|
// force-host
|
|
|
|
// no-prefer-dynamic
|
|
|
|
|
|
|
|
#![crate_type = "proc-macro"]
|
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
2018-04-02 15:19:32 +00:00
|
|
|
use proc_macro::{TokenStream, TokenTree, Delimiter, Literal, Spacing, Group};
|
2017-07-12 16:50:05 +00:00
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn foo(attr: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
assert!(attr.is_empty());
|
|
|
|
let input = input.into_iter().collect::<Vec<_>>();
|
|
|
|
{
|
|
|
|
let mut cursor = &input[..];
|
|
|
|
assert_inline(&mut cursor);
|
|
|
|
assert_doc(&mut cursor);
|
|
|
|
assert_inline(&mut cursor);
|
|
|
|
assert_doc(&mut cursor);
|
|
|
|
assert_foo(&mut cursor);
|
|
|
|
assert!(cursor.is_empty());
|
|
|
|
}
|
|
|
|
fold_stream(input.into_iter().collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
|
|
|
pub fn bar(attr: TokenStream, input: TokenStream) -> TokenStream {
|
|
|
|
assert!(attr.is_empty());
|
|
|
|
let input = input.into_iter().collect::<Vec<_>>();
|
|
|
|
{
|
|
|
|
let mut cursor = &input[..];
|
|
|
|
assert_inline(&mut cursor);
|
|
|
|
assert_doc(&mut cursor);
|
|
|
|
assert_invoc(&mut cursor);
|
|
|
|
assert_inline(&mut cursor);
|
|
|
|
assert_doc(&mut cursor);
|
|
|
|
assert_foo(&mut cursor);
|
|
|
|
assert!(cursor.is_empty());
|
|
|
|
}
|
|
|
|
input.into_iter().collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_inline(slice: &mut &[TokenTree]) {
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[0] {
|
2018-05-05 18:09:41 +00:00
|
|
|
TokenTree::Punct(tt) => assert_eq!(tt.as_char(), '#'),
|
2017-07-12 16:50:05 +00:00
|
|
|
_ => panic!("expected '#' char"),
|
|
|
|
}
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[1] {
|
|
|
|
TokenTree::Group(tt) => assert_eq!(tt.delimiter(), Delimiter::Bracket),
|
2017-07-12 16:50:05 +00:00
|
|
|
_ => panic!("expected brackets"),
|
|
|
|
}
|
|
|
|
*slice = &slice[2..];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_doc(slice: &mut &[TokenTree]) {
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[0] {
|
2018-05-05 18:09:41 +00:00
|
|
|
TokenTree::Punct(tt) => {
|
|
|
|
assert_eq!(tt.as_char(), '#');
|
2018-04-02 15:19:32 +00:00
|
|
|
assert_eq!(tt.spacing(), Spacing::Alone);
|
|
|
|
}
|
2018-03-30 09:55:54 +00:00
|
|
|
_ => panic!("expected #"),
|
|
|
|
}
|
2018-04-02 15:19:32 +00:00
|
|
|
let inner = match &slice[1] {
|
|
|
|
TokenTree::Group(tt) => {
|
|
|
|
assert_eq!(tt.delimiter(), Delimiter::Bracket);
|
|
|
|
tt.stream()
|
|
|
|
}
|
2018-03-30 09:55:54 +00:00
|
|
|
_ => panic!("expected brackets"),
|
|
|
|
};
|
|
|
|
let tokens = inner.into_iter().collect::<Vec<_>>();
|
|
|
|
let tokens = &tokens[..];
|
|
|
|
|
|
|
|
if tokens.len() != 3 {
|
|
|
|
panic!("expected three tokens in doc")
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:19:32 +00:00
|
|
|
match &tokens[0] {
|
2018-05-05 18:09:41 +00:00
|
|
|
TokenTree::Ident(tt) => assert_eq!("doc", &*tt.to_string()),
|
2018-03-30 09:55:54 +00:00
|
|
|
_ => panic!("expected `doc`"),
|
|
|
|
}
|
2018-04-02 15:19:32 +00:00
|
|
|
match &tokens[1] {
|
2018-05-05 18:09:41 +00:00
|
|
|
TokenTree::Punct(tt) => {
|
|
|
|
assert_eq!(tt.as_char(), '=');
|
2018-04-02 15:19:32 +00:00
|
|
|
assert_eq!(tt.spacing(), Spacing::Alone);
|
|
|
|
}
|
2018-03-30 09:55:54 +00:00
|
|
|
_ => panic!("expected equals"),
|
|
|
|
}
|
2018-04-02 15:19:32 +00:00
|
|
|
match tokens[2] {
|
|
|
|
TokenTree::Literal(_) => {}
|
2018-03-30 09:55:54 +00:00
|
|
|
_ => panic!("expected literal"),
|
2017-07-12 16:50:05 +00:00
|
|
|
}
|
2018-03-30 09:55:54 +00:00
|
|
|
|
|
|
|
*slice = &slice[2..];
|
2017-07-12 16:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_invoc(slice: &mut &[TokenTree]) {
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[0] {
|
2018-05-05 18:09:41 +00:00
|
|
|
TokenTree::Punct(tt) => assert_eq!(tt.as_char(), '#'),
|
2017-07-12 16:50:05 +00:00
|
|
|
_ => panic!("expected '#' char"),
|
|
|
|
}
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[1] {
|
|
|
|
TokenTree::Group(tt) => assert_eq!(tt.delimiter(), Delimiter::Bracket),
|
2017-07-12 16:50:05 +00:00
|
|
|
_ => panic!("expected brackets"),
|
|
|
|
}
|
|
|
|
*slice = &slice[2..];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_foo(slice: &mut &[TokenTree]) {
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[0] {
|
2018-05-05 18:09:41 +00:00
|
|
|
TokenTree::Ident(tt) => assert_eq!(&*tt.to_string(), "fn"),
|
2017-07-12 16:50:05 +00:00
|
|
|
_ => panic!("expected fn"),
|
|
|
|
}
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[1] {
|
2018-05-05 18:09:41 +00:00
|
|
|
TokenTree::Ident(tt) => assert_eq!(&*tt.to_string(), "foo"),
|
2017-07-12 16:50:05 +00:00
|
|
|
_ => panic!("expected foo"),
|
|
|
|
}
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[2] {
|
|
|
|
TokenTree::Group(tt) => {
|
|
|
|
assert_eq!(tt.delimiter(), Delimiter::Parenthesis);
|
|
|
|
assert!(tt.stream().is_empty());
|
|
|
|
}
|
2017-07-12 16:50:05 +00:00
|
|
|
_ => panic!("expected parens"),
|
|
|
|
}
|
2018-04-02 15:19:32 +00:00
|
|
|
match &slice[3] {
|
|
|
|
TokenTree::Group(tt) => assert_eq!(tt.delimiter(), Delimiter::Brace),
|
2017-07-12 16:50:05 +00:00
|
|
|
_ => panic!("expected braces"),
|
|
|
|
}
|
|
|
|
*slice = &slice[4..];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_stream(input: TokenStream) -> TokenStream {
|
|
|
|
input.into_iter().map(fold_tree).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_tree(input: TokenTree) -> TokenTree {
|
|
|
|
match input {
|
2018-04-02 15:19:32 +00:00
|
|
|
TokenTree::Group(b) => {
|
|
|
|
TokenTree::Group(Group::new(b.delimiter(), fold_stream(b.stream())))
|
|
|
|
}
|
2018-05-05 18:09:41 +00:00
|
|
|
TokenTree::Punct(b) => TokenTree::Punct(b),
|
|
|
|
TokenTree::Ident(a) => TokenTree::Ident(a),
|
2018-04-02 15:19:32 +00:00
|
|
|
TokenTree::Literal(a) => {
|
2017-07-12 16:50:05 +00:00
|
|
|
if a.to_string() != "\"foo\"" {
|
2018-04-02 15:19:32 +00:00
|
|
|
TokenTree::Literal(a)
|
2017-07-12 16:50:05 +00:00
|
|
|
} else {
|
2018-04-02 15:19:32 +00:00
|
|
|
TokenTree::Literal(Literal::i32_unsuffixed(3))
|
2017-07-12 16:50:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|