]> git.lizzy.rs Git - rust.git/blob - src/librustc/plugin/registry.rs
auto merge of #14554 : kmcallister/rust/plugin_registrar, r=cmr
[rust.git] / src / librustc / plugin / registry.rs
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.
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 //! Used by plugin crates to tell `rustc` about the plugins they provide.
12
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;
18 use syntax::ast;
19
20 /// Structure used to register plugins.
21 ///
22 /// A plugin registrar function takes an `&mut Registry` and should call
23 /// methods to register its plugins.
24 ///
25 /// This struct has public fields and other methods for use by `rustc`
26 /// itself. They are not documented here, and plugin authors should
27 /// not use them.
28 pub struct Registry {
29     #[doc(hidden)]
30     pub krate_span: Span,
31
32     #[doc(hidden)]
33     pub syntax_exts: Vec<NamedSyntaxExtension>,
34 }
35
36 impl Registry {
37     #[doc(hidden)]
38     pub fn new(krate: &ast::Crate) -> Registry {
39         Registry {
40             krate_span: krate.span,
41             syntax_exts: vec!(),
42         }
43     }
44
45     /// Register a syntax extension of any kind.
46     ///
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),
54         }));
55     }
56
57     /// Register a macro of the usual kind.
58     ///
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(
64             token::intern(name),
65             NormalTT(box BasicMacroExpander {
66                 expander: expander,
67                 span: None,
68             }, None));
69     }
70 }