rust/src/test/run-pass-fulldeps/auxiliary/cond_plugin.rs

60 lines
2.0 KiB
Rust
Raw Normal View History

2016-08-04 14:20:01 -05:00
// Copyright 2016 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.
#![allow(unused_parens)]
#![feature(plugin)]
#![feature(plugin_registrar)]
#![feature(rustc_private)]
#![plugin(proc_macro_plugin)]
2016-08-04 14:20:01 -05:00
extern crate rustc_plugin;
extern crate syntax;
use rustc_plugin::Registry;
2017-01-17 21:27:09 -06:00
use syntax::ext::base::SyntaxExtension;
use syntax::parse::token::Token;
use syntax::symbol::Symbol;
2016-08-04 14:20:01 -05:00
use syntax::tokenstream::{TokenTree, TokenStream};
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
2017-01-17 21:27:09 -06:00
reg.register_syntax_extension(Symbol::intern("cond"),
SyntaxExtension::ProcMacro(Box::new(cond)));
2016-08-04 14:20:01 -05:00
}
2017-01-17 21:27:09 -06:00
fn cond(input: TokenStream) -> TokenStream {
let mut conds = Vec::new();
let mut input = input.trees();
while let Some(tree) = input.next() {
let cond: TokenStream = match *tree {
TokenTree::Delimited(_, ref delimited) => delimited.tts.iter().cloned().collect(),
_ => panic!("Invalid input"),
};
let mut trees = cond.trees().cloned();
let test = trees.next();
let rhs = trees.collect::<TokenStream>();
if rhs.is_empty() {
panic!("Invalid macro usage in cond: {}", cond);
}
let is_else = match test {
Some(TokenTree::Token(_, Token::Ident(ident))) if ident.name == "else" => true,
_ => false,
};
conds.push(if is_else || input.peek().is_none() {
qquote!({ unquote rhs })
} else {
qquote!(if unquote(test.unwrap()) { unquote rhs } else)
});
}
conds.into_iter().collect()
2016-08-04 14:20:01 -05:00
}