Major refactor of attribute handling
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::{parse::Parse, parse::ParseStream, spanned::Spanned};
|
||||
use syn::{parse::Parse, parse::ParseStream};
|
||||
|
||||
use crate::function::{ExportedFn, ExportedFnParams};
|
||||
use crate::function::ExportedFn;
|
||||
use crate::rhai_module::ExportedConst;
|
||||
|
||||
#[cfg(no_std)]
|
||||
@@ -15,92 +15,9 @@ use core::mem;
|
||||
use std::mem;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn inner_fn_attributes(f: &mut syn::ItemFn) -> syn::Result<ExportedFnParams> {
|
||||
// #[cfg] attributes are not allowed on objects
|
||||
if let Some(cfg_attr) = f.attrs.iter().find(|a| {
|
||||
a.path
|
||||
.get_ident()
|
||||
.map(|i| i.to_string() == "cfg")
|
||||
.unwrap_or(false)
|
||||
}) {
|
||||
return Err(syn::Error::new(cfg_attr.span(), "cfg attributes not allowed on this item"));
|
||||
}
|
||||
|
||||
// Find the #[rhai_fn] attribute which will turn be read for the function parameters.
|
||||
if let Some(rhai_fn_idx) = f.attrs.iter().position(|a| {
|
||||
a.path
|
||||
.get_ident()
|
||||
.map(|i| i.to_string() == "rhai_fn")
|
||||
.unwrap_or(false)
|
||||
}) {
|
||||
let rhai_fn_attr = f.attrs.remove(rhai_fn_idx);
|
||||
rhai_fn_attr.parse_args()
|
||||
} else if let syn::Visibility::Public(_) = f.vis {
|
||||
Ok(ExportedFnParams::default())
|
||||
} else {
|
||||
Ok(ExportedFnParams::skip())
|
||||
}
|
||||
}
|
||||
|
||||
fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::Error> {
|
||||
let mut renames = HashMap::<String, proc_macro2::Span>::new();
|
||||
let mut names = HashMap::<String, proc_macro2::Span>::new();
|
||||
for itemfn in fns.iter() {
|
||||
if let Some(ref name) = itemfn.params.name {
|
||||
let current_span = itemfn.params.span.as_ref().unwrap();
|
||||
let key = itemfn.arg_list().fold(name.clone(), |mut argstr, fnarg| {
|
||||
let type_string: String = match fnarg {
|
||||
syn::FnArg::Receiver(_) => unimplemented!("receiver rhai_fns not implemented"),
|
||||
syn::FnArg::Typed(syn::PatType { ref ty, .. }) =>
|
||||
ty.as_ref().to_token_stream().to_string(),
|
||||
};
|
||||
argstr.push('.');
|
||||
argstr.extend(type_string.chars());
|
||||
argstr
|
||||
});
|
||||
if let Some(other_span) = renames.insert(key,
|
||||
current_span.clone()) {
|
||||
let mut err = syn::Error::new(current_span.clone(),
|
||||
format!("duplicate Rhai signature for '{}'", &name));
|
||||
err.combine(syn::Error::new(other_span,
|
||||
format!("duplicated function renamed '{}'", &name)));
|
||||
return Err(err);
|
||||
}
|
||||
} else {
|
||||
let ident = itemfn.name();
|
||||
names.insert(ident.to_string(), ident.span());
|
||||
}
|
||||
}
|
||||
for (new_name, attr_span) in renames.drain() {
|
||||
let new_name = new_name.split('.').next().unwrap();
|
||||
if let Some(fn_span) = names.get(new_name) {
|
||||
let mut err = syn::Error::new(attr_span,
|
||||
format!("duplicate Rhai signature for '{}'", &new_name));
|
||||
err.combine(syn::Error::new(fn_span.clone(),
|
||||
format!("duplicated function '{}'", &new_name)));
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn inner_mod_attributes(f: &mut syn::ItemMod) -> syn::Result<ExportedModParams> {
|
||||
if let Some(rhai_mod_idx) = f.attrs.iter().position(|a| {
|
||||
a.path
|
||||
.get_ident()
|
||||
.map(|i| i.to_string() == "rhai_mod")
|
||||
.unwrap_or(false)
|
||||
}) {
|
||||
let rhai_mod_attr = f.attrs.remove(rhai_mod_idx);
|
||||
rhai_mod_attr.parse_args()
|
||||
} else if let syn::Visibility::Public(_) = f.vis {
|
||||
Ok(ExportedModParams::default())
|
||||
} else {
|
||||
Ok(ExportedModParams::skip())
|
||||
}
|
||||
}
|
||||
use crate::attrs::{AttrItem, ExportInfo, ExportedParams};
|
||||
use crate::function::{ExportedFnParams};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct ExportedModParams {
|
||||
@@ -108,81 +25,52 @@ pub(crate) struct ExportedModParams {
|
||||
pub skip: bool,
|
||||
}
|
||||
|
||||
impl ExportedModParams {
|
||||
pub fn skip() -> ExportedModParams {
|
||||
let mut skip = ExportedModParams::default();
|
||||
skip.skip = true;
|
||||
skip
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for ExportedModParams {
|
||||
fn parse(args: ParseStream) -> syn::Result<Self> {
|
||||
if args.is_empty() {
|
||||
return Ok(ExportedModParams::default());
|
||||
}
|
||||
|
||||
let arg_list = args.call(
|
||||
syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_separated_nonempty,
|
||||
)?;
|
||||
let info = crate::attrs::parse_attr_items(args)?;
|
||||
|
||||
let mut attrs: HashMap<syn::Ident, Option<syn::LitStr>> = HashMap::new();
|
||||
for arg in arg_list {
|
||||
let (left, right) = match arg {
|
||||
syn::Expr::Assign(syn::ExprAssign {
|
||||
ref left,
|
||||
ref right,
|
||||
..
|
||||
}) => {
|
||||
let attr_name: syn::Ident = match left.as_ref() {
|
||||
syn::Expr::Path(syn::ExprPath {
|
||||
path: attr_path, ..
|
||||
}) => attr_path.get_ident().cloned().ok_or_else(|| {
|
||||
syn::Error::new(attr_path.span(), "expecting attribute name")
|
||||
})?,
|
||||
x => return Err(syn::Error::new(x.span(), "expecting attribute name")),
|
||||
};
|
||||
let attr_value = match right.as_ref() {
|
||||
syn::Expr::Lit(syn::ExprLit {
|
||||
lit: syn::Lit::Str(string),
|
||||
..
|
||||
}) => string.clone(),
|
||||
x => return Err(syn::Error::new(x.span(), "expecting string literal")),
|
||||
};
|
||||
(attr_name, Some(attr_value))
|
||||
}
|
||||
syn::Expr::Path(syn::ExprPath {
|
||||
path: attr_path, ..
|
||||
}) => attr_path
|
||||
.get_ident()
|
||||
.cloned()
|
||||
.map(|a| (a, None))
|
||||
.ok_or_else(|| syn::Error::new(attr_path.span(), "expecting attribute name"))?,
|
||||
x => return Err(syn::Error::new(x.span(), "expecting identifier")),
|
||||
};
|
||||
attrs.insert(left, right);
|
||||
}
|
||||
Self::from_info(info)
|
||||
}
|
||||
}
|
||||
|
||||
impl ExportedParams for ExportedModParams {
|
||||
fn parse_stream(args: ParseStream) -> syn::Result<Self> {
|
||||
Self::parse(args)
|
||||
}
|
||||
|
||||
fn no_attrs() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn from_info(info: ExportInfo) -> syn::Result<Self> {
|
||||
let ExportInfo { items: attrs, .. } = info;
|
||||
let mut name = None;
|
||||
let mut skip = false;
|
||||
for (ident, value) in attrs.drain() {
|
||||
match (ident.to_string().as_ref(), value) {
|
||||
for attr in attrs {
|
||||
let AttrItem { key, value } = attr;
|
||||
match (key.to_string().as_ref(), value) {
|
||||
("name", Some(s)) => name = Some(s.value()),
|
||||
("name", None) => return Err(syn::Error::new(ident.span(), "requires value")),
|
||||
("name", None) => return Err(syn::Error::new(key.span(), "requires value")),
|
||||
("skip", None) => skip = true,
|
||||
("skip", Some(s)) => {
|
||||
return Err(syn::Error::new(s.span(), "extraneous value"))
|
||||
}
|
||||
("skip", Some(s)) => return Err(syn::Error::new(s.span(), "extraneous value")),
|
||||
(attr, _) => {
|
||||
return Err(syn::Error::new(
|
||||
ident.span(),
|
||||
key.span(),
|
||||
format!("unknown attribute '{}'", attr),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ExportedModParams { name, skip, ..Default::default() })
|
||||
Ok(ExportedModParams {
|
||||
name,
|
||||
skip,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,17 +97,26 @@ impl Parse for Module {
|
||||
syn::Item::Fn(f) => Some(f),
|
||||
_ => None,
|
||||
})
|
||||
.try_fold(Vec::new(), |mut vec, mut itemfn| {
|
||||
let params = match inner_fn_attributes(&mut itemfn) {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(e),
|
||||
.try_fold(Vec::new(), |mut vec, itemfn| {
|
||||
// #[cfg] attributes are not allowed on functions
|
||||
crate::attrs::deny_cfg_attr(&itemfn.attrs)?;
|
||||
|
||||
let mut params: ExportedFnParams =
|
||||
match crate::attrs::inner_item_attributes(&mut itemfn.attrs, "rhai_fn") {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
params.skip = if let syn::Visibility::Public(_) = itemfn.vis {
|
||||
params.skip
|
||||
} else {
|
||||
true
|
||||
};
|
||||
syn::parse2::<ExportedFn>(itemfn.to_token_stream())
|
||||
.map(|mut f| {
|
||||
f.params = params;
|
||||
f
|
||||
})
|
||||
.map(|f| if !f.params.skip { vec.push(f) })
|
||||
.map(|f| vec.push(f))
|
||||
.map(|_| vec)
|
||||
})?;
|
||||
// Gather and parse constants definitions.
|
||||
@@ -233,23 +130,14 @@ impl Parse for Module {
|
||||
..
|
||||
}) => {
|
||||
// #[cfg] attributes are not allowed on const declarations
|
||||
if let Some(cfg_attr) = attrs.iter().find(|a| {
|
||||
a.path
|
||||
.get_ident()
|
||||
.map(|i| i.to_string() == "cfg")
|
||||
.unwrap_or(false)
|
||||
}) {
|
||||
return Err(syn::Error::new(
|
||||
cfg_attr.span(),
|
||||
"cfg attributes not allowed on this item"));
|
||||
}
|
||||
crate::attrs::deny_cfg_attr(&attrs)?;
|
||||
if let syn::Visibility::Public(_) = vis {
|
||||
consts.push((ident.to_string(), expr.as_ref().clone()));
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
}
|
||||
// Gather and parse submodule definitions.
|
||||
//
|
||||
// They are actually removed from the module's body, because they will need
|
||||
@@ -257,23 +145,27 @@ impl Parse for Module {
|
||||
submodules.reserve(content.len() - fns.len() - consts.len());
|
||||
let mut i = 0;
|
||||
while i < content.len() {
|
||||
if let syn::Item::Mod(_) = &content[i] {
|
||||
if let syn::Item::Mod(_) = &content[i] {
|
||||
let mut itemmod = match content.remove(i) {
|
||||
syn::Item::Mod(m) => m,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let params = match inner_mod_attributes(&mut itemmod) {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(e),
|
||||
let mut params: ExportedModParams =
|
||||
match crate::attrs::inner_item_attributes(&mut itemmod.attrs, "rhai_mod") {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
params.skip = if let syn::Visibility::Public(_) = itemmod.vis {
|
||||
params.skip
|
||||
} else {
|
||||
true
|
||||
};
|
||||
let module = syn::parse2::<Module>(itemmod.to_token_stream())
|
||||
.map(|mut f| {
|
||||
let module =
|
||||
syn::parse2::<Module>(itemmod.to_token_stream()).map(|mut f| {
|
||||
f.params = params;
|
||||
f
|
||||
})?;
|
||||
if !module.params.skip {
|
||||
submodules.push(module);
|
||||
}
|
||||
submodules.push(module);
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
@@ -308,6 +200,10 @@ impl Module {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn skipped(&self) -> bool {
|
||||
self.params.skip
|
||||
}
|
||||
|
||||
pub fn generate(self) -> proc_macro2::TokenStream {
|
||||
match self.generate_inner() {
|
||||
Ok(tokens) => tokens,
|
||||
@@ -315,39 +211,55 @@ impl Module {
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_inner(mut self) -> Result<proc_macro2::TokenStream, syn::Error> {
|
||||
fn generate_inner(self) -> Result<proc_macro2::TokenStream, syn::Error> {
|
||||
// Check for collisions if the "name" attribute was used on inner functions.
|
||||
check_rename_collisions(&self.fns)?;
|
||||
crate::rhai_module::check_rename_collisions(&self.fns)?;
|
||||
|
||||
// Generate new module items.
|
||||
//
|
||||
// This is done before inner module recursive generation, because that is destructive.
|
||||
let mod_gen = crate::rhai_module::generate_body(&self.fns, &self.consts, &self.submodules);
|
||||
|
||||
// NB: submodules must have their new items for exporting generated in depth-first order to
|
||||
// avoid issues with reparsing them.
|
||||
let inner_modules: Vec<proc_macro2::TokenStream> = self.submodules.drain(..)
|
||||
.try_fold::<Vec<proc_macro2::TokenStream>, _,
|
||||
Result<Vec<proc_macro2::TokenStream>, syn::Error>>(
|
||||
Vec::new(), |mut acc, m| { acc.push(m.generate_inner()?); Ok(acc) })?;
|
||||
|
||||
// Generate new module items for exporting functions and constant.
|
||||
|
||||
// Rebuild the structure of the module, with the new content added.
|
||||
let Module { mod_all, .. } = self;
|
||||
// Extract the current structure of the module.
|
||||
let Module {
|
||||
mod_all,
|
||||
fns,
|
||||
consts,
|
||||
mut submodules,
|
||||
params,
|
||||
..
|
||||
} = self;
|
||||
let mut mod_all = mod_all.unwrap();
|
||||
let mod_name = mod_all.ident.clone();
|
||||
let (_, orig_content) = mod_all.content.take().unwrap();
|
||||
let mod_attrs = mem::replace(&mut mod_all.attrs, Vec::with_capacity(0));
|
||||
|
||||
Ok(quote! {
|
||||
#(#mod_attrs)*
|
||||
pub mod #mod_name {
|
||||
#(#orig_content)*
|
||||
#(#inner_modules)*
|
||||
#mod_gen
|
||||
}
|
||||
})
|
||||
if !params.skip {
|
||||
// Generate new module items.
|
||||
//
|
||||
// This is done before inner module recursive generation, because that is destructive.
|
||||
let mod_gen = crate::rhai_module::generate_body(&fns, &consts, &submodules);
|
||||
|
||||
// NB: submodules must have their new items for exporting generated in depth-first order
|
||||
// to avoid issues caused by re-parsing them
|
||||
let inner_modules: Vec<proc_macro2::TokenStream> = submodules.drain(..)
|
||||
.try_fold::<Vec<proc_macro2::TokenStream>, _,
|
||||
Result<Vec<proc_macro2::TokenStream>, syn::Error>>(
|
||||
Vec::new(), |mut acc, m| { acc.push(m.generate_inner()?); Ok(acc) })?;
|
||||
|
||||
// Regenerate the module with the new content added.
|
||||
Ok(quote! {
|
||||
#(#mod_attrs)*
|
||||
pub mod #mod_name {
|
||||
#(#orig_content)*
|
||||
#(#inner_modules)*
|
||||
#mod_gen
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// Regenerate the original module as-is.
|
||||
Ok(quote! {
|
||||
#(#mod_attrs)*
|
||||
pub mod #mod_name {
|
||||
#(#orig_content)*
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<&syn::Ident> {
|
||||
@@ -356,7 +268,10 @@ impl Module {
|
||||
|
||||
pub fn content(&self) -> Option<&Vec<syn::Item>> {
|
||||
match self.mod_all {
|
||||
Some(syn::ItemMod { content: Some((_, ref vec)), .. }) => Some(vec),
|
||||
Some(syn::ItemMod {
|
||||
content: Some((_, ref vec)),
|
||||
..
|
||||
}) => Some(vec),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -495,7 +410,8 @@ mod module_tests {
|
||||
assert!(item_mod.fns.is_empty());
|
||||
assert!(item_mod.consts.is_empty());
|
||||
assert_eq!(item_mod.submodules.len(), 1);
|
||||
assert!(item_mod.submodules[0].fns.is_empty());
|
||||
assert_eq!(item_mod.submodules[0].fns.len(), 1);
|
||||
assert!(item_mod.submodules[0].fns[0].params.skip);
|
||||
assert!(item_mod.submodules[0].consts.is_empty());
|
||||
assert!(item_mod.submodules[0].submodules.is_empty());
|
||||
}
|
||||
@@ -516,7 +432,8 @@ mod module_tests {
|
||||
let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
|
||||
assert!(item_mod.fns.is_empty());
|
||||
assert!(item_mod.consts.is_empty());
|
||||
assert!(item_mod.submodules.is_empty());
|
||||
assert_eq!(item_mod.submodules.len(), 1);
|
||||
assert!(item_mod.submodules[0].params.skip);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -548,7 +465,8 @@ mod module_tests {
|
||||
};
|
||||
|
||||
let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
|
||||
assert!(item_mod.fns.is_empty());
|
||||
assert_eq!(item_mod.fns.len(), 1);
|
||||
assert!(item_mod.fns[0].params.skip);
|
||||
assert!(item_mod.consts.is_empty());
|
||||
}
|
||||
|
||||
@@ -564,7 +482,8 @@ mod module_tests {
|
||||
};
|
||||
|
||||
let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
|
||||
assert!(item_mod.fns.is_empty());
|
||||
assert_eq!(item_mod.fns.len(), 1);
|
||||
assert!(item_mod.fns[0].params.skip);
|
||||
assert!(item_mod.consts.is_empty());
|
||||
}
|
||||
|
||||
@@ -599,7 +518,7 @@ mod generate_tests {
|
||||
.zip(expected.chars())
|
||||
.inspect(|_| counter += 1)
|
||||
.skip_while(|(a, e)| *a == *e);
|
||||
let (actual_diff, expected_diff) = {
|
||||
let (_actual_diff, _expected_diff) = {
|
||||
let mut actual_diff = String::new();
|
||||
let mut expected_diff = String::new();
|
||||
for (a, e) in iter.take(50) {
|
||||
|
Reference in New Issue
Block a user