]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/macro_crate_test.rs
rollup merge of #17355 : gamazeps/issue17210
[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)]
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 syntax::parse;
23 use syntax::ptr::P;
24 use rustc::plugin::Registry;
25
26 #[macro_export]
27 macro_rules! exported_macro (() => (2i))
28
29 macro_rules! unexported_macro (() => (3i))
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_macro("forged_ident", expand_forged_ident);
35     reg.register_macro("identity", expand_identity);
36     reg.register_syntax_extension(
37         token::intern("into_foo"),
38         Modifier(box expand_into_foo));
39 }
40
41 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
42                    -> Box<MacResult+'static> {
43     if !tts.is_empty() {
44         cx.span_fatal(sp, "make_a_1 takes no arguments");
45     }
46     MacExpr::new(quote_expr!(cx, 1i))
47 }
48
49 // See Issue #15750
50 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
51                    -> Box<MacResult+'static> {
52     // Parse an expression and emit it unchanged.
53     let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
54         cx.cfg(), Vec::from_slice(tts));
55     let expr = parser.parse_expr();
56     MacExpr::new(quote_expr!(&mut *cx, $expr))
57 }
58
59 fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: &MetaItem, it: P<Item>)
60                    -> P<Item> {
61     P(Item {
62         attrs: it.attrs.clone(),
63         ..(*quote_item!(cx, enum Foo { Bar, Baz }).unwrap()).clone()
64     })
65 }
66
67 fn expand_forged_ident(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult+'static> {
68     use syntax::ext::quote::rt::*;
69
70     if !tts.is_empty() {
71         cx.span_fatal(sp, "forged_ident takes no arguments");
72     }
73
74     // Most of this is modelled after the expansion of the `quote_expr!`
75     // macro ...
76     let parse_sess = cx.parse_sess();
77     let cfg = cx.cfg();
78
79     // ... except this is where we inject a forged identifier,
80     // and deliberately do not call `cx.parse_tts_with_hygiene`
81     // (because we are testing that this will be *rejected*
82     //  by the default parser).
83
84     let expr = {
85         let tt = cx.parse_tts("\x00name_2,ctxt_0\x00".to_string());
86         let mut parser = new_parser_from_tts(parse_sess, cfg, tt);
87         parser.parse_expr()
88     };
89     MacExpr::new(expr)
90 }
91
92 pub fn foo() {}