2018-04-27 02:19:09 -07:00
|
|
|
#![feature(proc_macro)]
|
|
|
|
extern crate proc_macro;
|
2018-04-30 00:35:42 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate quote;
|
|
|
|
extern crate syn;
|
2018-04-30 02:24:21 -07:00
|
|
|
|
2018-04-27 02:19:09 -07:00
|
|
|
use proc_macro::TokenStream;
|
2018-05-02 19:30:34 -07:00
|
|
|
use syn::{Attribute, DeriveInput};
|
2018-04-27 02:19:09 -07:00
|
|
|
|
2018-05-02 19:30:34 -07:00
|
|
|
fn extract_attribute_arg_by_name(name: &str, attrs: &Vec<Attribute>) -> Option<String> {
|
|
|
|
use syn::{Meta, Lit, MetaNameValue};
|
|
|
|
attrs.iter().map(|attr| attr.interpret_meta()).find(|meta| {
|
2018-05-02 04:50:26 -07:00
|
|
|
match meta {
|
2018-05-02 19:30:34 -07:00
|
|
|
&Some(Meta::NameValue(MetaNameValue { ident, .. })) if ident.as_ref() == name => true,
|
2018-05-02 04:50:26 -07:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}).and_then(|meta| {
|
|
|
|
match meta {
|
2018-05-02 19:30:34 -07:00
|
|
|
Some(Meta::NameValue(MetaNameValue { lit: Lit::Str(litstr), .. })) => Some(litstr.value()),
|
2018-05-02 04:50:26 -07:00
|
|
|
_ => None,
|
|
|
|
}
|
2018-05-02 19:30:34 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-02 20:43:05 -07:00
|
|
|
#[proc_macro_derive(ProgrammingLanguageInterface, attributes(LanguageName, SourceFileExtension, PipelineSteps))]
|
2018-05-02 19:30:34 -07:00
|
|
|
pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream {
|
|
|
|
let ast: DeriveInput = syn::parse(input).unwrap();
|
|
|
|
let name = &ast.ident;
|
|
|
|
let attrs = &ast.attrs;
|
2018-05-02 04:50:26 -07:00
|
|
|
|
2018-05-02 20:43:05 -07:00
|
|
|
let language_name: String = extract_attribute_arg_by_name("LanguageName", attrs).expect("LanguageName is required");
|
|
|
|
let file_ext = extract_attribute_arg_by_name("SourceFileExtension", attrs).expect("SourceFileExtension is required");
|
2018-05-02 02:14:36 -07:00
|
|
|
|
2018-04-30 02:24:21 -07:00
|
|
|
let tokens = quote! {
|
|
|
|
impl ProgrammingLanguageInterface for #name {
|
2018-05-02 02:14:36 -07:00
|
|
|
fn get_language_name(&self) -> String {
|
2018-05-02 16:06:28 -07:00
|
|
|
#language_name.to_string()
|
2018-05-02 02:14:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_source_file_suffix(&self) -> String {
|
2018-05-02 20:43:05 -07:00
|
|
|
#file_ext.to_string()
|
2018-05-02 02:14:36 -07:00
|
|
|
}
|
|
|
|
fn execute_pipeline(&mut self, input: &str, options: &EvalOptions) -> FinishedComputation {
|
|
|
|
let mut chain = pass_chain![self, options;
|
|
|
|
tokenizing_stage,
|
|
|
|
parsing_stage,
|
|
|
|
symbol_table_stage,
|
|
|
|
typechecking_stage,
|
|
|
|
eval_stage
|
|
|
|
];
|
|
|
|
chain(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_stages(&self) -> Vec<String> {
|
|
|
|
vec![
|
|
|
|
format!("tokenizing_stage"),
|
|
|
|
format!("parsing_stage"), //TODO handle both types of this
|
|
|
|
format!("symbol_table_stage"),
|
|
|
|
format!("typechecking_stage"),
|
|
|
|
format!("eval_stage")
|
|
|
|
]
|
|
|
|
}
|
2018-04-30 02:24:21 -07:00
|
|
|
}
|
|
|
|
};
|
2018-05-02 02:14:36 -07:00
|
|
|
|
2018-04-30 02:24:21 -07:00
|
|
|
tokens.into()
|
2018-04-30 00:35:42 -07:00
|
|
|
}
|
|
|
|
|
2018-04-27 02:19:09 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
|
|
|
assert_eq!(2 + 2, 4);
|
|
|
|
}
|
|
|
|
}
|