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.
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.
13 #![feature(plugin_registrar, quote)]
14 #![feature(box_syntax)]
19 use syntax::ast::{TokenTree, Item, MetaItem};
20 use syntax::codemap::Span;
21 use syntax::ext::base::*;
22 use syntax::parse::token;
25 use rustc::plugin::Registry;
28 macro_rules! exported_macro { () => (2i) }
30 macro_rules! unexported_macro { () => (3i) }
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));
42 fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
43 -> Box<MacResult+'static> {
45 cx.span_fatal(sp, "make_a_1 takes no arguments");
47 MacExpr::new(quote_expr!(cx, 1i))
51 fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
52 -> Box<MacResult+'static> {
53 // Parse an expression and emit it unchanged.
54 let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
55 cx.cfg(), tts.to_vec());
56 let expr = parser.parse_expr();
57 MacExpr::new(quote_expr!(&mut *cx, $expr))
60 fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: &MetaItem, it: P<Item>)
63 attrs: it.attrs.clone(),
64 ..(*quote_item!(cx, enum Foo { Bar, Baz }).unwrap()).clone()
68 fn expand_forged_ident(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult+'static> {
69 use syntax::ext::quote::rt::*;
72 cx.span_fatal(sp, "forged_ident takes no arguments");
75 // Most of this is modelled after the expansion of the `quote_expr!`
77 let parse_sess = cx.parse_sess();
80 // ... except this is where we inject a forged identifier,
81 // and deliberately do not call `cx.parse_tts_with_hygiene`
82 // (because we are testing that this will be *rejected*
83 // by the default parser).
86 let tt = cx.parse_tts("\x00name_2,ctxt_0\x00".to_string());
87 let mut parser = new_parser_from_tts(parse_sess, cfg, tt);