aboutsummaryrefslogtreecommitdiff
path: root/macros/src/lib.rs
blob: 467cbb9360a884d7b32c461098ba12901df819b1 (plain)
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
#![feature(proc_macro)]
#![recursion_limit = "128"]

#[macro_use]
extern crate error_chain;
extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate rtfm_syntax;
extern crate syn;

use proc_macro::TokenStream;
use rtfm_syntax::App;

use error::*;

mod analyze;
mod check;
mod error;
mod trans;

#[proc_macro]
pub fn rtfm(ts: TokenStream) -> TokenStream {
    match run(ts) {
        Err(e) => panic!("{}", error_chain::ChainedError::display(&e)),
        Ok(ts) => ts,
    }
}

fn run(ts: TokenStream) -> Result<TokenStream> {
    let input = format!("{}", ts);

    let app = check::app(App::parse(&input)
        .chain_err(|| "parsing the `rtfm!` macro")?).chain_err(
        || "checking the application specification",
    )?;

    let ownerships = analyze::app(&app);
    let tokens = trans::app(&app, &ownerships);

    Ok(format!("{}", tokens)
        .parse()
        .map_err(|_| "BUG: error parsing the generated code")?)
}