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
#![crate_type = "dylib"]
#![feature(plugin_registrar, quote)]
#![feature(slice_patterns)]
#![feature(rustc_private)]
#![recursion_limit = "1000"]  // if_chain

#[macro_use]
extern crate if_chain;
#[macro_use]
extern crate rustc;
extern crate rustc_plugin;
extern crate syntax;
extern crate maud;

use rustc_plugin::Registry;
use syntax::codemap::Span;
use syntax::errors::FatalError;
use syntax::ext::base::{DummyResult, ExtCtxt, MacEager, MacResult};
use syntax::print::pprust;
use syntax::tokenstream::TokenTree;

mod lints;
mod parse;
mod render;

type PResult<T> = Result<T, FatalError>;

fn expand_html<'cx>(cx: &'cx mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'cx> {
    match parse::parse(cx, sp, args) {
        Ok(expr) => MacEager::expr(expr),
        Err(..) => DummyResult::expr(sp),
    }
}

fn expand_html_debug<'cx>(cx: &'cx mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'cx> {
    match parse::parse(cx, sp, args) {
        Ok(expr) => {
            cx.span_warn(sp, &format!("expansion:\n{}",
                                      pprust::expr_to_string(&expr)));
            MacEager::expr(expr)
        },
        Err(..) => DummyResult::expr(sp),
    }
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
    reg.register_macro("html", expand_html);
    reg.register_macro("html_debug", expand_html_debug);
    lints::register_lints(reg);
}