]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/macro_crate_test.rs
rollup merge of #21151: brson/beta
[rust.git] / src / test / auxiliary / macro_crate_test.rs
1 // Copyright 2013-2015 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(plugin_registrar, quote)]
14 #![feature(box_syntax)]
15
16 extern crate syntax;
17 extern crate rustc;
18
19 use syntax::ast::{TokenTree, Item, MetaItem, ImplItem, TraitItem, Method};
20 use syntax::codemap::Span;
21 use syntax::ext::base::*;
22 use syntax::parse::token;
23 use syntax::parse;
24 use syntax::ptr::P;
25 use rustc::plugin::Registry;
26
27 #[macro_export]
28 macro_rules! exported_macro { () => (2i) }
29
30 macro_rules! unexported_macro { () => (3i) }
31
32 #[plugin_registrar]
33 pub fn plugin_registrar(reg: &mut Registry) {
34     reg.register_macro("make_a_1", expand_make_a_1);
35     reg.register_macro("forged_ident", expand_forged_ident);
36     reg.register_macro("identity", expand_identity);
37     reg.register_syntax_extension(
38         token::intern("into_foo"),
39         Modifier(box expand_into_foo));
40     reg.register_syntax_extension(
41         token::intern("into_multi_foo"),
42         MultiModifier(box expand_into_foo_multi));
43 }
44
45 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
46                    -> Box<MacResult+'static> {
47     if !tts.is_empty() {
48         cx.span_fatal(sp, "make_a_1 takes no arguments");
49     }
50     MacExpr::new(quote_expr!(cx, 1i))
51 }
52
53 // See Issue #15750
54 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
55                    -> Box<MacResult+'static> {
56     // Parse an expression and emit it unchanged.
57     let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
58         cx.cfg(), tts.to_vec());
59     let expr = parser.parse_expr();
60     MacExpr::new(quote_expr!(&mut *cx, $expr))
61 }
62
63 fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: &MetaItem, it: P<Item>)
64                    -> P<Item> {
65     P(Item {
66         attrs: it.attrs.clone(),
67         ..(*quote_item!(cx, enum Foo { Bar, Baz }).unwrap()).clone()
68     })
69 }
70
71 fn expand_into_foo_multi(cx: &mut ExtCtxt,
72                          sp: Span,
73                          attr: &MetaItem,
74                          it: Annotatable) -> Annotatable {
75     match it {
76         Annotatable::Item(it) => {
77             Annotatable::Item(P(Item {
78                 attrs: it.attrs.clone(),
79                 ..(*quote_item!(cx, enum Foo2 { Bar2, Baz2 }).unwrap()).clone()
80             }))
81         }
82         Annotatable::ImplItem(it) => {
83             Annotatable::ImplItem(ImplItem::MethodImplItem(
84                 quote_method!(cx, fn foo(&self) -> i32 { 42 })
85             ))
86         }
87         Annotatable::TraitItem(it) => {
88             Annotatable::TraitItem(TraitItem::ProvidedMethod(
89                 quote_method!(cx, fn foo(&self) -> i32 { 0 })
90             ))
91         }
92     }
93 }
94
95 fn expand_forged_ident(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult+'static> {
96     use syntax::ext::quote::rt::*;
97
98     if !tts.is_empty() {
99         cx.span_fatal(sp, "forged_ident takes no arguments");
100     }
101
102     // Most of this is modelled after the expansion of the `quote_expr!`
103     // macro ...
104     let parse_sess = cx.parse_sess();
105     let cfg = cx.cfg();
106
107     // ... except this is where we inject a forged identifier,
108     // and deliberately do not call `cx.parse_tts_with_hygiene`
109     // (because we are testing that this will be *rejected*
110     //  by the default parser).
111
112     let expr = {
113         let tt = cx.parse_tts("\x00name_2,ctxt_0\x00".to_string());
114         let mut parser = new_parser_from_tts(parse_sess, cfg, tt);
115         parser.parse_expr()
116     };
117     MacExpr::new(expr)
118 }
119
120 pub fn foo() {}