Refactor codegen some

This commit is contained in:
greg 2018-10-15 20:12:00 -07:00
parent abe2db25b2
commit e8dfc2be34

View File

@ -52,19 +52,8 @@ fn extract_attribute_list(name: &str, attrs: &Vec<Attribute>) -> Option<Vec<(Ide
}) })
} }
#[proc_macro_derive(ProgrammingLanguageInterface, attributes(LanguageName, SourceFileExtension, PipelineSteps, DocMethod))] fn get_attribute_identifier(attr_name: &str, attrs: &Vec<Attribute>) -> Option<proc_macro2::Ident> {
pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream { find_attr_by_name(attr_name, attrs).and_then(|attr| {
let ast: DeriveInput = syn::parse(input).unwrap();
let name = &ast.ident;
let attrs = &ast.attrs;
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");
let passes = extract_attribute_list("PipelineSteps", attrs).expect("PipelineSteps are required");
let pass_idents = passes.iter().map(|x| x.0.clone());
let doc_method: Option<proc_macro2::Ident>= find_attr_by_name("DocMethod", attrs).and_then(|attr| {
let tts = attr.tts.clone().into_iter().collect::<Vec<_>>(); let tts = attr.tts.clone().into_iter().collect::<Vec<_>>();
if tts.len() == 2 { if tts.len() == 2 {
@ -76,9 +65,22 @@ pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream
} else { } else {
None None
} }
}); })
}
let doc_method_body = match doc_method { #[proc_macro_derive(ProgrammingLanguageInterface,
attributes(LanguageName, SourceFileExtension, PipelineSteps, DocMethod))]
pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(input).unwrap();
let name = &ast.ident;
let attrs = &ast.attrs;
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");
let passes = extract_attribute_list("PipelineSteps", attrs).expect("PipelineSteps are required");
let pass_idents = passes.iter().map(|x| x.0.clone());
let get_doc_impl = match get_attribute_identifier("DocMethod", attrs) {
None => quote! { }, None => quote! { },
Some(method_name) => quote! { Some(method_name) => quote! {
fn get_doc(&self, commands: &Vec<&str>) -> Option<String> { fn get_doc(&self, commands: &Vec<&str>) -> Option<String> {
@ -118,7 +120,7 @@ pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream
fn get_passes(&self) -> Vec<PassDescriptor> { fn get_passes(&self) -> Vec<PassDescriptor> {
vec![ #(#pass_descriptors),* ] vec![ #(#pass_descriptors),* ]
} }
#doc_method_body #get_doc_impl
} }
}; };