]> git.lizzy.rs Git - rust.git/blob - src/librustc/plugin/registry.rs
auto merge of #15531 : steveklabnik/rust/guide_looping, r=brson
[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 lint::LintPassObject;
14
15 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
16 use syntax::ext::base::{IdentTT, LetSyntaxTT, ItemDecorator, ItemModifier, BasicMacroExpander};
17 use syntax::ext::base::{MacroExpanderFn};
18 use syntax::codemap::Span;
19 use syntax::parse::token;
20 use syntax::ast;
21
22 /// Structure used to register plugins.
23 ///
24 /// A plugin registrar function takes an `&mut Registry` and should call
25 /// methods to register its plugins.
26 ///
27 /// This struct has public fields and other methods for use by `rustc`
28 /// itself. They are not documented here, and plugin authors should
29 /// not use them.
30 pub struct Registry {
31     #[doc(hidden)]
32     pub krate_span: Span,
33
34     #[doc(hidden)]
35     pub syntax_exts: Vec<NamedSyntaxExtension>,
36
37     #[doc(hidden)]
38     pub lint_passes: Vec<LintPassObject>,
39 }
40
41 impl Registry {
42     #[doc(hidden)]
43     pub fn new(krate: &ast::Crate) -> Registry {
44         Registry {
45             krate_span: krate.span,
46             syntax_exts: vec!(),
47             lint_passes: vec!(),
48         }
49     }
50
51     /// Register a syntax extension of any kind.
52     ///
53     /// This is the most general hook into `libsyntax`'s expansion behavior.
54     pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
55         self.syntax_exts.push((name, match extension {
56             NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
57             IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)),
58             ItemDecorator(ext) => ItemDecorator(ext),
59             ItemModifier(ext) => ItemModifier(ext),
60             // there's probably a nicer way to signal this:
61             LetSyntaxTT(_, _) => fail!("can't register a new LetSyntax!"),
62         }));
63     }
64
65     /// Register a macro of the usual kind.
66     ///
67     /// This is a convenience wrapper for `register_syntax_extension`.
68     /// It builds for you a `NormalTT` with a `BasicMacroExpander`,
69     /// and also takes care of interning the macro's name.
70     pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
71         self.register_syntax_extension(
72             token::intern(name),
73             NormalTT(box BasicMacroExpander {
74                 expander: expander,
75                 span: None,
76             }, None));
77     }
78
79     /// Register a compiler lint pass.
80     pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
81         self.lint_passes.push(lint_pass);
82     }
83 }