2014-05-24 18:16:10 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-05-26 16:48:54 -05:00
|
|
|
//! Used by plugin crates to tell `rustc` about the plugins they provide.
|
|
|
|
|
2014-07-20 22:27:59 -05:00
|
|
|
use lint::{LintPassObject, LintId, Lint};
|
2014-06-18 19:26:14 -05:00
|
|
|
|
2014-05-24 18:16:10 -05:00
|
|
|
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
|
2014-09-10 22:59:26 -05:00
|
|
|
use syntax::ext::base::{IdentTT, LetSyntaxTT, ItemDecorator, ItemModifier};
|
2014-05-24 18:16:10 -05:00
|
|
|
use syntax::ext::base::{MacroExpanderFn};
|
|
|
|
use syntax::codemap::Span;
|
|
|
|
use syntax::parse::token;
|
|
|
|
use syntax::ast;
|
|
|
|
|
2014-07-20 22:27:59 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2014-05-26 16:48:54 -05:00
|
|
|
/// Structure used to register plugins.
|
|
|
|
///
|
|
|
|
/// A plugin registrar function takes an `&mut Registry` and should call
|
|
|
|
/// methods to register its plugins.
|
|
|
|
///
|
|
|
|
/// This struct has public fields and other methods for use by `rustc`
|
|
|
|
/// itself. They are not documented here, and plugin authors should
|
|
|
|
/// not use them.
|
2014-05-24 18:16:10 -05:00
|
|
|
pub struct Registry {
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub krate_span: Span,
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub syntax_exts: Vec<NamedSyntaxExtension>,
|
2014-06-18 19:26:14 -05:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub lint_passes: Vec<LintPassObject>,
|
2014-07-20 22:27:59 -05:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub lint_groups: HashMap<&'static str, Vec<LintId>>,
|
2014-05-24 18:16:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Registry {
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn new(krate: &ast::Crate) -> Registry {
|
|
|
|
Registry {
|
|
|
|
krate_span: krate.span,
|
|
|
|
syntax_exts: vec!(),
|
2014-06-18 19:26:14 -05:00
|
|
|
lint_passes: vec!(),
|
2014-07-20 22:27:59 -05:00
|
|
|
lint_groups: HashMap::new(),
|
2014-05-24 18:16:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 16:48:54 -05:00
|
|
|
/// Register a syntax extension of any kind.
|
|
|
|
///
|
|
|
|
/// This is the most general hook into `libsyntax`'s expansion behavior.
|
2014-05-24 18:16:10 -05:00
|
|
|
pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
|
|
|
|
self.syntax_exts.push((name, match extension {
|
|
|
|
NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
|
|
|
|
IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)),
|
|
|
|
ItemDecorator(ext) => ItemDecorator(ext),
|
|
|
|
ItemModifier(ext) => ItemModifier(ext),
|
2014-07-07 11:54:08 -05:00
|
|
|
// there's probably a nicer way to signal this:
|
|
|
|
LetSyntaxTT(_, _) => fail!("can't register a new LetSyntax!"),
|
2014-05-24 18:16:10 -05:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2014-05-26 16:48:54 -05:00
|
|
|
/// Register a macro of the usual kind.
|
|
|
|
///
|
|
|
|
/// This is a convenience wrapper for `register_syntax_extension`.
|
2014-09-10 22:59:26 -05:00
|
|
|
/// It builds for you a `NormalTT` that calls `expander`,
|
2014-05-26 16:48:54 -05:00
|
|
|
/// and also takes care of interning the macro's name.
|
2014-05-24 18:16:10 -05:00
|
|
|
pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
|
2014-09-10 22:59:26 -05:00
|
|
|
self.register_syntax_extension(token::intern(name), NormalTT(box expander, None));
|
2014-05-24 18:16:10 -05:00
|
|
|
}
|
2014-06-18 19:26:14 -05:00
|
|
|
|
|
|
|
/// Register a compiler lint pass.
|
|
|
|
pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
|
|
|
|
self.lint_passes.push(lint_pass);
|
|
|
|
}
|
2014-07-20 22:27:59 -05:00
|
|
|
|
|
|
|
/// Register a lint group.
|
|
|
|
pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
|
2014-09-14 22:27:36 -05:00
|
|
|
self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
|
2014-07-20 22:27:59 -05:00
|
|
|
}
|
2014-05-24 18:16:10 -05:00
|
|
|
}
|