aboutsummaryrefslogtreecommitdiffstats
path: root/components/plugins/lib.rs
blob: 69f823c2d9181ecc80d1e0f141bed0be7a02305a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Servo's compiler plugin/macro crate
//!
//! Attributes this crate provides:
//!
//!  - `#[privatize]` : Forces all fields in a struct/enum to be private
//!  - `#[jstraceable]` : Auto-derives an implementation of `JSTraceable` for a struct in the script crate
//!  - `#[must_root]` : Prevents data of the marked type from being used on the stack. See the lints module for more details
//!  - `#[dom_struct]` : Implies `#[privatize]`,`#[jstraceable]`, and `#[must_root]`.
//!     Use this for structs that correspond to a DOM type

#![feature(macro_rules, plugin_registrar, quote, phase, if_let)]

#![deny(unused_imports)]
#![deny(unused_variables)]

#[phase(plugin,link)]
extern crate syntax;
#[phase(plugin, link)]
extern crate rustc;
#[cfg(test)]
extern crate sync;

use rustc::lint::LintPassObject;
use rustc::plugin::Registry;
use syntax::ext::base::{Decorator, Modifier};

use syntax::parse::token::intern;

// Public for documentation to show up
/// Handles the auto-deriving for `#[jstraceable]`
pub mod jstraceable;
/// Autogenerates implementations of Reflectable on DOM structs
pub mod reflector;
pub mod lints;
/// Utilities for writing plugins
pub mod utils;

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
    reg.register_syntax_extension(intern("dom_struct"), Modifier(box jstraceable::expand_dom_struct));
    reg.register_syntax_extension(intern("jstraceable"), Decorator(box jstraceable::expand_jstraceable));
    reg.register_syntax_extension(intern("_generate_reflector"), Decorator(box reflector::expand_reflector));
    reg.register_lint_pass(box lints::TransmutePass as LintPassObject);
    reg.register_lint_pass(box lints::UnrootedPass as LintPassObject);
    reg.register_lint_pass(box lints::PrivatizePass as LintPassObject);
    reg.register_lint_pass(box lints::InheritancePass as LintPassObject);
}


#[macro_export]
macro_rules! define_css_keyword_enum {
    ($name: ident: $( $css: expr => $variant: ident ),+,) => {
        define_css_keyword_enum!($name: $( $css => $variant ),+)
    };
    ($name: ident: $( $css: expr => $variant: ident ),+) => {
        #[allow(non_camel_case_types)]
        #[deriving(Clone, Eq, PartialEq, FromPrimitive)]
        pub enum $name {
            $( $variant ),+
        }

        impl $name {
            pub fn parse(component_value: &::cssparser::ast::ComponentValue) -> Result<$name, ()> {
                match component_value {
                    &::cssparser::ast::Ident(ref value) => {
                        match_ignore_ascii_case! { value:
                            $( $css => Ok($name::$variant) ),+
                            _ => Err(())
                        }
                    }
                    _ => Err(())
                }
            }
        }

        impl ::std::fmt::Show for $name {
            #[inline]
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                use cssparser::ToCss;
                self.fmt_to_css(f)
            }
        }

        impl ::cssparser::ToCss for $name {
            fn to_css<W>(&self, dest: &mut W) -> ::text_writer::Result
            where W: ::text_writer::TextWriter {
                match self {
                    $( &$name::$variant => dest.write_str($css) ),+
                }
            }
        }
    }
}


#[macro_export]
macro_rules! match_ignore_ascii_case {
    ( $value: expr: $( $string: expr => $result: expr ),+ _ => $fallback: expr, ) => {
        match_ignore_ascii_case! { $value:
            $( $string => $result ),+
            _ => $fallback
        }
    };
    ( $value: expr: $( $string: expr => $result: expr ),+ _ => $fallback: expr ) => {
        {
            use std::ascii::AsciiExt;
            match $value.as_slice() {
                $( s if s.eq_ignore_ascii_case($string) => $result, )+
                _ => $fallback
            }
        }
    };
}