Working on improved proc_macro handling
This commit is contained in:
parent
e934d7bdc5
commit
4fccff5e27
@ -6,6 +6,7 @@ authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||
[dependencies]
|
||||
syn = { version = "0.15.6", features = ["full", "extra-traits"] }
|
||||
quote = "0.6.8"
|
||||
proc-macro2 = "0.4.19"
|
||||
schala-repl = { path = "../schala-repl" }
|
||||
|
||||
[lib]
|
||||
|
@ -1,5 +1,6 @@
|
||||
#![feature(trace_macros)]
|
||||
extern crate proc_macro;
|
||||
extern crate proc_macro2;
|
||||
#[macro_use]
|
||||
extern crate quote;
|
||||
extern crate syn;
|
||||
@ -51,8 +52,17 @@ fn extract_attribute_list(name: &str, attrs: &Vec<Attribute>) -> Option<Vec<(Ide
|
||||
})
|
||||
}
|
||||
|
||||
fn find_attr_by_name<'a>(name: &str, attrs: &'a Vec<Attribute>) -> Option<&'a Attribute> {
|
||||
attrs.iter().find(|attr| {
|
||||
let first = attr.path.segments.first();
|
||||
let seg: Option<&&syn::PathSegment> = first.as_ref().map(|x| x.value());
|
||||
seg.map(|seg| seg.ident.to_string() == name).unwrap_or(false)
|
||||
})
|
||||
}
|
||||
|
||||
#[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;
|
||||
@ -61,7 +71,29 @@ pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream
|
||||
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<String> = extract_attribute_arg_by_name("DocMethod", attrs);
|
||||
|
||||
let doc_method: Option<String>= find_attr_by_name("DocMethod", attrs).and_then(|attr| {
|
||||
let tts = attr.tts.clone().into_iter().collect::<Vec<_>>();
|
||||
|
||||
if tts.len() == 2 {
|
||||
let ref after_equals: proc_macro2::TokenTree = tts[1];
|
||||
match after_equals {
|
||||
proc_macro2::TokenTree::Ident(ident) => Some(ident.to_string()),
|
||||
_ => None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
let doc_method_body = match doc_method {
|
||||
None => quote! { },
|
||||
Some(s) => quote!{
|
||||
fn get_doc(&self, commands: &Vec<&str>) -> Option<String> {
|
||||
#s(commands)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//let pass_names: Vec<String> = passes.iter().map(|pass| pass.0.to_string()).collect();
|
||||
let pass_descriptors = passes.iter().map(|pass| {
|
||||
@ -97,7 +129,7 @@ pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream
|
||||
//vec![ #(PassDescriptor { name: #pass_names.to_string(), debug_options: vec![] }),* ]
|
||||
}
|
||||
}
|
||||
//TODO if doc_method is defined, add code here
|
||||
#doc_method_body
|
||||
};
|
||||
|
||||
let output: TokenStream = tokens.into();
|
||||
|
@ -1,4 +1,5 @@
|
||||
#![feature(trace_macros)]
|
||||
#![feature(unrestricted_attribute_tokens)]
|
||||
#![feature(slice_patterns, box_patterns, box_syntax)]
|
||||
extern crate itertools;
|
||||
#[macro_use]
|
||||
@ -35,7 +36,7 @@ mod eval;
|
||||
#[LanguageName = "Schala"]
|
||||
#[SourceFileExtension = "schala"]
|
||||
#[PipelineSteps(tokenizing, parsing(compact,expanded,trace), symbol_table, typechecking, ast_reducing, eval)]
|
||||
#[DocMethod = "get_doc"]
|
||||
#[DocMethod = get_doc]
|
||||
pub struct Schala {
|
||||
state: eval::State<'static>,
|
||||
symbol_table: Rc<RefCell<symbol_table::SymbolTable>>,
|
||||
|
Loading…
Reference in New Issue
Block a user