schala/schala-codegen/src/lib.rs

104 lines
3.6 KiB
Rust
Raw Normal View History

2018-07-16 03:40:35 -07:00
#![feature(trace_macros)]
#![feature(proc_macro)]
extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate syn;
2018-04-30 02:24:21 -07:00
2018-07-07 18:08:21 -07:00
extern crate schala_repl;
use proc_macro::TokenStream;
2018-05-03 00:20:55 -07:00
use syn::{Ident, Attribute, DeriveInput};
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 {
&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 {
Some(Meta::NameValue(MetaNameValue { lit: Lit::Str(litstr), .. })) => Some(litstr.value()),
2018-05-02 04:50:26 -07:00
_ => None,
}
})
}
2018-07-16 03:40:35 -07:00
fn extract_attribute_list(name: &str, attrs: &Vec<Attribute>) -> Option<Vec<(Ident, Option<Vec<Ident>>)>> {
2018-05-03 00:20:55 -07:00
use syn::{Meta, MetaList, NestedMeta};
attrs.iter().find(|attr| {
match attr.path.segments.iter().nth(0) {
Some(segment) if segment.ident.as_ref() == name => true,
_ => false
}
}).and_then(|attr| {
match attr.interpret_meta() {
Some(Meta::List(MetaList { nested, .. })) => {
Some(nested.iter().map(|nested_meta| match nested_meta {
2018-07-16 03:40:35 -07:00
&NestedMeta::Meta(Meta::Word(ident)) => (ident, None),
&NestedMeta::Meta(Meta::List(MetaList { ident, nested: ref nested2, .. })) => {
let own_args = nested2.iter().map(|nested_meta2| match nested_meta2 {
&NestedMeta::Meta(Meta::Word(ident)) => ident,
_ => panic!("Bad format for doubly-nested attribute list")
}).collect();
(ident, Some(own_args))
},
2018-05-03 00:20:55 -07:00
_ => panic!("Bad format for nested list")
}).collect())
},
_ => panic!("{} must be a comma-delimited list surrounded by parens", name)
}
})
}
2018-05-02 20:43:05 -07:00
#[proc_macro_derive(ProgrammingLanguageInterface, attributes(LanguageName, SourceFileExtension, PipelineSteps))]
pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream {
2018-07-16 03:40:35 -07:00
use schala_repl::PassDescriptor;
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-03 00:20:55 -07:00
let passes = extract_attribute_list("PipelineSteps", attrs).expect("PipelineSteps are required");
2018-07-16 03:40:35 -07:00
let pass_idents = passes.iter().map(|x| x.0);
//let pass_names: Vec<String> = passes.iter().map(|pass| pass.0.to_string()).collect();
let pass_descriptors = passes.iter().map(|pass| {
let name: String = pass.0.to_string();
let opts: Vec<String> = match &pass.1 {
None => vec![],
Some(opts) => opts.iter().map(|o| o.to_string()).collect(),
};
quote! {
PassDescriptor {
name: #name,
debug_options: vec![#(format!(#opts)),*]
}
}
});
2018-05-02 02:14:36 -07:00
2018-04-30 02:24:21 -07:00
let tokens = quote! {
2018-07-07 18:08:21 -07:00
use schala_repl::PassDescriptor;
2018-04-30 02:24:21 -07:00
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 {
2018-07-16 03:40:35 -07:00
let mut chain = pass_chain![self, options; #(#pass_idents),* ];
2018-05-02 02:14:36 -07:00
chain(input)
}
2018-07-06 03:17:37 -07:00
fn get_passes(&self) -> Vec<PassDescriptor> {
2018-07-16 03:40:35 -07:00
vec![ #(#pass_descriptors),* ]
//vec![ #(PassDescriptor { name: #pass_names.to_string(), debug_options: vec![] }),* ]
2018-05-02 02:14:36 -07:00
}
2018-04-30 02:24:21 -07:00
}
};
tokens.into()
}