]>
git.proxmox.com Git - rustc.git/blob - src/test/run-pass-fulldeps/auxiliary/cond_plugin.rs
730e112c70016f9fd2bdfb78108ca49571c65125
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(unused_parens)]
13 #![feature(plugin_registrar)]
14 #![feature(rustc_private)]
15 #![plugin(proc_macro_plugin)]
17 extern crate rustc_plugin
;
20 use rustc_plugin
::Registry
;
22 use syntax
::ext
::base
::SyntaxExtension
;
23 use syntax
::parse
::token
::Token
;
24 use syntax
::symbol
::Symbol
;
25 use syntax
::tokenstream
::{TokenTree, TokenStream}
;
28 pub fn plugin_registrar(reg
: &mut Registry
) {
29 reg
.register_syntax_extension(Symbol
::intern("cond"),
30 SyntaxExtension
::ProcMacro(Box
::new(cond
)));
33 fn cond(input
: TokenStream
) -> TokenStream
{
34 let mut conds
= Vec
::new();
35 let mut input
= input
.trees();
36 while let Some(tree
) = input
.next() {
37 let cond
: TokenStream
= match *tree
{
38 TokenTree
::Delimited(_
, ref delimited
) => delimited
.tts
.iter().cloned().collect(),
39 _
=> panic
!("Invalid input"),
41 let mut trees
= cond
.trees().cloned();
42 let test
= trees
.next();
43 let rhs
= trees
.collect
::<TokenStream
>();
45 panic
!("Invalid macro usage in cond: {}", cond
);
47 let is_else
= match test
{
48 Some(TokenTree
::Token(_
, Token
::Ident(ident
))) if ident
.name
== "else" => true,
51 conds
.push(if is_else
|| input
.peek().is_none() {
52 qquote
!({ unquote rhs }
)
54 qquote
!(if unquote(test
.unwrap()) { unquote rhs }
else)
58 conds
.into_iter().collect()