rtic/macros/src/lib.rs

48 lines
1.1 KiB
Rust
Raw Normal View History

2017-07-21 06:14:41 +02:00
//! Procedural macros for the RTFM framework
#![deny(warnings)]
2017-07-12 06:44:54 +02:00
#![feature(proc_macro)]
2017-07-04 18:26:11 +02:00
#![recursion_limit = "128"]
#[macro_use]
extern crate error_chain;
2017-07-04 18:26:11 +02:00
extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate rtfm_syntax as syntax;
2017-07-04 18:26:11 +02:00
extern crate syn;
use proc_macro::TokenStream;
use syntax::App;
use syntax::error::*;
mod analyze;
2017-07-04 18:26:11 +02:00
mod check;
mod trans;
2017-07-12 06:44:54 +02:00
2017-07-21 06:14:41 +02:00
/// The `app!` macro, a macro used to specify the tasks and resources of a
/// RTFM application.
2017-07-12 06:44:54 +02:00
#[proc_macro]
pub fn app(ts: TokenStream) -> TokenStream {
match run(ts) {
Err(e) => panic!("{}", error_chain::ChainedError::display(&e)),
Ok(ts) => ts,
}
}
fn run(ts: TokenStream) -> Result<TokenStream> {
2017-07-04 18:26:11 +02:00
let input = format!("{}", ts);
let app = App::parse(&input).chain_err(|| "parsing")?;
let app = syntax::check::app(app).chain_err(|| "checking the AST")?;
let app = check::app(app)?;
let ownerships = analyze::app(&app);
let tokens = trans::app(&app, &ownerships);
2017-07-04 18:26:11 +02:00
Ok(format!("{}", tokens)
.parse()
.map_err(|_| "BUG: error parsing the generated code")?)
2017-07-04 18:26:11 +02:00
}