]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/macro_crate_test.rs
e17e21ae7f41b84424b8e8f82feb932b61de3584
[rust.git] / src / test / auxiliary / macro_crate_test.rs
1 // Copyright 2013-2014 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.
4 //
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.
10
11 // force-host
12
13 #![feature(globs, plugin_registrar, macro_rules, quote, managed_boxes)]
14
15 extern crate syntax;
16 extern crate rustc;
17
18 use syntax::ast::{TokenTree, Item, MetaItem};
19 use syntax::codemap::Span;
20 use syntax::ext::base::*;
21 use syntax::parse::token;
22 use rustc::plugin::Registry;
23
24 use std::gc::{Gc, GC};
25
26 #[macro_export]
27 macro_rules! exported_macro (() => (2))
28
29 macro_rules! unexported_macro (() => (3))
30
31 #[plugin_registrar]
32 pub fn plugin_registrar(reg: &mut Registry) {
33     reg.register_macro("make_a_1", expand_make_a_1);
34     reg.register_syntax_extension(
35         token::intern("into_foo"),
36         ItemModifier(expand_into_foo));
37 }
38
39 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
40                    -> Box<MacResult> {
41     if !tts.is_empty() {
42         cx.span_fatal(sp, "make_a_1 takes no arguments");
43     }
44     MacExpr::new(quote_expr!(cx, 1i))
45 }
46
47 fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: Gc<MetaItem>, it: Gc<Item>)
48                    -> Gc<Item> {
49     box(GC) Item {
50         attrs: it.attrs.clone(),
51         ..(*quote_item!(cx, enum Foo { Bar, Baz }).unwrap()).clone()
52     }
53 }
54
55 pub fn foo() {}