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}
;
16 use syntax
::ext
::base
::{SyntaxExtension, NamedSyntaxExtension, NormalTT}
;
17 use syntax
::ext
::base
::{IdentTT, Decorator, Modifier, MultiModifier, MacroRulesTT}
;
18 use syntax
::ext
::base
::MacroExpanderFn
;
19 use syntax
::codemap
::Span
;
20 use syntax
::parse
::token
;
24 use std
::collections
::HashMap
;
26 /// Structure used to register plugins.
28 /// A plugin registrar function takes an `&mut Registry` and should call
29 /// methods to register its plugins.
31 /// This struct has public fields and other methods for use by `rustc`
32 /// itself. They are not documented here, and plugin authors should
34 pub struct Registry
<'a
> {
35 /// Compiler session. Useful if you want to emit diagnostic messages
36 /// from the plugin registrar.
37 pub sess
: &'a Session
,
40 pub args_hidden
: Option
<Vec
<P
<ast
::MetaItem
>>>,
46 pub syntax_exts
: Vec
<NamedSyntaxExtension
>,
49 pub lint_passes
: Vec
<LintPassObject
>,
52 pub lint_groups
: HashMap
<&'
static str, Vec
<LintId
>>,
55 impl<'a
> Registry
<'a
> {
57 pub fn new(sess
: &'a Session
, krate
: &ast
::Crate
) -> Registry
<'a
> {
61 krate_span
: krate
.span
,
64 lint_groups
: HashMap
::new(),
68 /// Get the plugin's arguments, if any.
70 /// These are specified inside the `plugin` crate attribute as
73 /// #![plugin(my_plugin_name(... args ...))]
75 pub fn args
<'b
>(&'b
self) -> &'b Vec
<P
<ast
::MetaItem
>> {
76 self.args_hidden
.as_ref().expect("args not set")
79 /// Register a syntax extension of any kind.
81 /// This is the most general hook into `libsyntax`'s expansion behavior.
82 pub fn register_syntax_extension(&mut self, name
: ast
::Name
, extension
: SyntaxExtension
) {
83 self.syntax_exts
.push((name
, match extension
{
84 NormalTT(ext
, _
, allow_internal_unstable
) => {
85 NormalTT(ext
, Some(self.krate_span
), allow_internal_unstable
)
87 IdentTT(ext
, _
, allow_internal_unstable
) => {
88 IdentTT(ext
, Some(self.krate_span
), allow_internal_unstable
)
90 Decorator(ext
) => Decorator(ext
),
91 Modifier(ext
) => Modifier(ext
),
92 MultiModifier(ext
) => MultiModifier(ext
),
94 self.sess
.err("plugin tried to register a new MacroRulesTT");
100 /// Register a macro of the usual kind.
102 /// This is a convenience wrapper for `register_syntax_extension`.
103 /// It builds for you a `NormalTT` that calls `expander`,
104 /// and also takes care of interning the macro's name.
105 pub fn register_macro(&mut self, name
: &str, expander
: MacroExpanderFn
) {
106 self.register_syntax_extension(token
::intern(name
),
107 NormalTT(Box
::new(expander
), None
, false));
110 /// Register a compiler lint pass.
111 pub fn register_lint_pass(&mut self, lint_pass
: LintPassObject
) {
112 self.lint_passes
.push(lint_pass
);
115 /// Register a lint group.
116 pub fn register_lint_group(&mut self, name
: &'
static str, to
: Vec
<&'
static Lint
>) {
117 self.lint_groups
.insert(name
, to
.into_iter().map(|x
| LintId
::of(x
)).collect());