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 lint::LintPassObject;
15 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
16 use syntax::ext::base::{IdentTT, ItemDecorator, ItemModifier, BasicMacroExpander};
17 use syntax::ext::base::{MacroExpanderFn};
18 use syntax::codemap::Span;
19 use syntax::parse::token;
22 /// Structure used to register plugins.
24 /// A plugin registrar function takes an `&mut Registry` and should call
25 /// methods to register its plugins.
27 /// This struct has public fields and other methods for use by `rustc`
28 /// itself. They are not documented here, and plugin authors should
35 pub syntax_exts: Vec<NamedSyntaxExtension>,
38 pub lint_passes: Vec<LintPassObject>,
43 pub fn new(krate: &ast::Crate) -> Registry {
45 krate_span: krate.span,
51 /// Register a syntax extension of any kind.
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),
63 /// Register a macro of the usual kind.
65 /// This is a convenience wrapper for `register_syntax_extension`.
66 /// It builds for you a `NormalTT` with a `BasicMacroExpander`,
67 /// and also takes care of interning the macro's name.
68 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
69 self.register_syntax_extension(
71 NormalTT(box BasicMacroExpander {
77 /// Register a compiler lint pass.
78 pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
79 self.lint_passes.push(lint_pass);