2017-07-12 11:50:05 -05:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// force-host
|
|
|
|
// no-prefer-dynamic
|
|
|
|
|
|
|
|
#![feature(proc_macro)]
|
|
|
|
#![crate_type = "proc-macro"]
|
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
2018-04-02 10:19:32 -05:00
|
|
|
use proc_macro::{TokenStream, TokenTree, Delimiter, Literal, Spacing, Group};
|
2017-07-12 11:50:05 -05: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 10:19:32 -05:00
|
|
|
match &slice[0] {
|
|
|
|
TokenTree::Op(tt) => assert_eq!(tt.op(), '#'),
|
2017-07-12 11:50:05 -05:00
|
|
|
_ => panic!("expected '#' char"),
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
match &slice[1] {
|
|
|
|
TokenTree::Group(tt) => assert_eq!(tt.delimiter(), Delimiter::Bracket),
|
2017-07-12 11:50:05 -05:00
|
|
|
_ => panic!("expected brackets"),
|
|
|
|
}
|
|
|
|
*slice = &slice[2..];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_doc(slice: &mut &[TokenTree]) {
|
2018-04-02 10:19:32 -05:00
|
|
|
match &slice[0] {
|
|
|
|
TokenTree::Op(tt) => {
|
|
|
|
assert_eq!(tt.op(), '#');
|
|
|
|
assert_eq!(tt.spacing(), Spacing::Alone);
|
|
|
|
}
|
2018-03-30 04:55:54 -05:00
|
|
|
_ => panic!("expected #"),
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
let inner = match &slice[1] {
|
|
|
|
TokenTree::Group(tt) => {
|
|
|
|
assert_eq!(tt.delimiter(), Delimiter::Bracket);
|
|
|
|
tt.stream()
|
|
|
|
}
|
2018-03-30 04:55:54 -05: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 10:19:32 -05:00
|
|
|
match &tokens[0] {
|
2018-04-09 16:49:25 -05:00
|
|
|
TokenTree::Term(tt) => assert_eq!("doc", &*tt.to_string()),
|
2018-03-30 04:55:54 -05:00
|
|
|
_ => panic!("expected `doc`"),
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
match &tokens[1] {
|
|
|
|
TokenTree::Op(tt) => {
|
|
|
|
assert_eq!(tt.op(), '=');
|
|
|
|
assert_eq!(tt.spacing(), Spacing::Alone);
|
|
|
|
}
|
2018-03-30 04:55:54 -05:00
|
|
|
_ => panic!("expected equals"),
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
match tokens[2] {
|
|
|
|
TokenTree::Literal(_) => {}
|
2018-03-30 04:55:54 -05:00
|
|
|
_ => panic!("expected literal"),
|
2017-07-12 11:50:05 -05:00
|
|
|
}
|
2018-03-30 04:55:54 -05:00
|
|
|
|
|
|
|
*slice = &slice[2..];
|
2017-07-12 11:50:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_invoc(slice: &mut &[TokenTree]) {
|
2018-04-02 10:19:32 -05:00
|
|
|
match &slice[0] {
|
|
|
|
TokenTree::Op(tt) => assert_eq!(tt.op(), '#'),
|
2017-07-12 11:50:05 -05:00
|
|
|
_ => panic!("expected '#' char"),
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
match &slice[1] {
|
|
|
|
TokenTree::Group(tt) => assert_eq!(tt.delimiter(), Delimiter::Bracket),
|
2017-07-12 11:50:05 -05:00
|
|
|
_ => panic!("expected brackets"),
|
|
|
|
}
|
|
|
|
*slice = &slice[2..];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_foo(slice: &mut &[TokenTree]) {
|
2018-04-02 10:19:32 -05:00
|
|
|
match &slice[0] {
|
2018-04-09 16:49:25 -05:00
|
|
|
TokenTree::Term(tt) => assert_eq!(&*tt.to_string(), "fn"),
|
2017-07-12 11:50:05 -05:00
|
|
|
_ => panic!("expected fn"),
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
match &slice[1] {
|
2018-04-09 16:49:25 -05:00
|
|
|
TokenTree::Term(tt) => assert_eq!(&*tt.to_string(), "foo"),
|
2017-07-12 11:50:05 -05:00
|
|
|
_ => panic!("expected foo"),
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
match &slice[2] {
|
|
|
|
TokenTree::Group(tt) => {
|
|
|
|
assert_eq!(tt.delimiter(), Delimiter::Parenthesis);
|
|
|
|
assert!(tt.stream().is_empty());
|
|
|
|
}
|
2017-07-12 11:50:05 -05:00
|
|
|
_ => panic!("expected parens"),
|
|
|
|
}
|
2018-04-02 10:19:32 -05:00
|
|
|
match &slice[3] {
|
|
|
|
TokenTree::Group(tt) => assert_eq!(tt.delimiter(), Delimiter::Brace),
|
2017-07-12 11:50:05 -05: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 10:19:32 -05:00
|
|
|
TokenTree::Group(b) => {
|
|
|
|
TokenTree::Group(Group::new(b.delimiter(), fold_stream(b.stream())))
|
|
|
|
}
|
|
|
|
TokenTree::Op(b) => TokenTree::Op(b),
|
|
|
|
TokenTree::Term(a) => TokenTree::Term(a),
|
|
|
|
TokenTree::Literal(a) => {
|
2017-07-12 11:50:05 -05:00
|
|
|
if a.to_string() != "\"foo\"" {
|
2018-04-02 10:19:32 -05:00
|
|
|
TokenTree::Literal(a)
|
2017-07-12 11:50:05 -05:00
|
|
|
} else {
|
2018-04-02 10:19:32 -05:00
|
|
|
TokenTree::Literal(Literal::i32_unsuffixed(3))
|
2017-07-12 11:50:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|