1 // Copyright 2012-2013 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 //! Used by plugin crates to tell `rustc` about the plugins they provide.
13 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
14 use syntax::ext::base::{IdentTT, ItemDecorator, ItemModifier, BasicMacroExpander};
15 use syntax::ext::base::{MacroExpanderFn};
16 use syntax::codemap::Span;
17 use syntax::parse::token;
20 /// Structure used to register plugins.
22 /// A plugin registrar function takes an `&mut Registry` and should call
23 /// methods to register its plugins.
25 /// This struct has public fields and other methods for use by `rustc`
26 /// itself. They are not documented here, and plugin authors should
33 pub syntax_exts: Vec<NamedSyntaxExtension>,
38 pub fn new(krate: &ast::Crate) -> Registry {
40 krate_span: krate.span,
45 /// Register a syntax extension of any kind.
47 /// This is the most general hook into `libsyntax`'s expansion behavior.
48 pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
49 self.syntax_exts.push((name, match extension {
50 NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
51 IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)),
52 ItemDecorator(ext) => ItemDecorator(ext),
53 ItemModifier(ext) => ItemModifier(ext),
57 /// Register a macro of the usual kind.
59 /// This is a convenience wrapper for `register_syntax_extension`.
60 /// It builds for you a `NormalTT` with a `BasicMacroExpander`,
61 /// and also takes care of interning the macro's name.
62 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
63 self.register_syntax_extension(
65 NormalTT(box BasicMacroExpander {