schala/schala-codegen/src/lib.rs

96 lines
2.0 KiB
Rust
Raw Normal View History

#![feature(proc_macro)]
extern crate proc_macro;
2018-04-28 03:31:53 -07:00
#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
2018-04-28 03:31:53 -07:00
use syn::{Expr, Lit, ExprLit};
use syn::punctuated::Punctuated;
use syn::synom::Synom;
2018-04-28 15:35:04 -07:00
fn get_string_args(input: Expr) -> Vec<String> {
2018-04-28 03:31:53 -07:00
let mut contained_strings = Vec::new();
match input {
Expr::Array(array) => {
for item in array.elems {
if let Expr::Lit(ExprLit { lit: Lit::Str(s), ..}) = item {
contained_strings.push(s.value());
} else {
2018-04-28 15:35:04 -07:00
panic!("Non-string-literal input to compiler_pass_sequence");
2018-04-28 03:31:53 -07:00
}
}
},
2018-04-28 15:35:04 -07:00
_ => panic!("Non-array input to compiler_pass_sequence"),
2018-04-28 00:08:16 -07:00
}
2018-04-28 15:35:04 -07:00
contained_strings
}
2018-04-28 03:31:53 -07:00
2018-04-28 15:35:04 -07:00
#[proc_macro]
pub fn compiler_pass_sequence(input: TokenStream) -> TokenStream {
/*
for token_tree in input {
//println!("ITEM: {:?}", token_tree.kind);
match token_tree.kind {
TokenNode::Literal(l) => println!("{:?}", l),
_ => ()
2018-04-28 03:31:53 -07:00
}
}
2018-04-28 15:35:04 -07:00
*/
2018-04-28 03:31:53 -07:00
2018-04-28 15:35:04 -07:00
let input: Expr = syn::parse(input).unwrap();
let stages = get_string_args(input);
let from_macro = format!("{:?}", stages);
2018-04-28 03:31:53 -07:00
let output = quote! {
fn new_execute(&mut self, input: &str, _options: &EvalOptions) -> FinishedComputation {
let evaluation = UnfinishedComputation::default();
2018-04-28 03:31:53 -07:00
evaluation.output(Err(#from_macro.to_string()))
}
};
output.into()
2018-04-28 00:08:16 -07:00
}
/* #[compiler_pass(<name of pass>*/
#[proc_macro_attribute]
pub fn compiler_pass(metadata: TokenStream, function: TokenStream) -> TokenStream {
//println!("FROM MACRO: {}", function);
println!("Compiler pass metadata: {}", metadata);
function
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
2018-04-28 00:08:16 -07:00
/* in Rocket
*
#[get("/")]
fn hi() -> &'static str {
"hello"
}
GETS MAPPED TO:
static hi_info = RouteInfo {
name: "hi",
method: Method::Get,
path: "/",
handler: hi_route,
}
fn hi_route(req: &Request) -> Outcome {
let responder = hi();
Outcome::from(req, responder);
}
*/