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, LintId, Lint};
15 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
16 use syntax::ext::base::{IdentTT, LetSyntaxTT, Decorator, Modifier};
17 use syntax::ext::base::{MacroExpanderFn};
18 use syntax::codemap::Span;
19 use syntax::parse::token;
22 use std::collections::HashMap;
24 /// Structure used to register plugins.
26 /// A plugin registrar function takes an `&mut Registry` and should call
27 /// methods to register its plugins.
29 /// This struct has public fields and other methods for use by `rustc`
30 /// itself. They are not documented here, and plugin authors should
37 pub syntax_exts: Vec<NamedSyntaxExtension>,
40 pub lint_passes: Vec<LintPassObject>,
43 pub lint_groups: HashMap<&'static str, Vec<LintId>>,
48 pub fn new(krate: &ast::Crate) -> Registry {
50 krate_span: krate.span,
53 lint_groups: HashMap::new(),
57 /// Register a syntax extension of any kind.
59 /// This is the most general hook into `libsyntax`'s expansion behavior.
60 pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
61 self.syntax_exts.push((name, match extension {
62 NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
63 IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)),
64 Decorator(ext) => Decorator(ext),
65 Modifier(ext) => Modifier(ext),
66 // there's probably a nicer way to signal this:
67 LetSyntaxTT(_, _) => panic!("can't register a new LetSyntax!"),
71 /// Register a macro of the usual kind.
73 /// This is a convenience wrapper for `register_syntax_extension`.
74 /// It builds for you a `NormalTT` that calls `expander`,
75 /// and also takes care of interning the macro's name.
76 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
77 self.register_syntax_extension(token::intern(name), NormalTT(box expander, None));
80 /// Register a compiler lint pass.
81 pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
82 self.lint_passes.push(lint_pass);
85 /// Register a lint group.
86 pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
87 self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());