2020-08-01 18:52:26 +02:00
|
|
|
use quote::{quote, ToTokens};
|
2020-08-24 00:53:30 +02:00
|
|
|
use syn::{parse::Parse, parse::ParseStream};
|
2020-08-01 18:52:26 +02:00
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
use crate::function::ExportedFn;
|
2020-08-01 18:52:26 +02:00
|
|
|
use crate::rhai_module::ExportedConst;
|
|
|
|
|
2020-08-14 06:04:24 +02:00
|
|
|
#[cfg(no_std)]
|
|
|
|
use alloc::vec as new_vec;
|
|
|
|
#[cfg(not(no_std))]
|
|
|
|
use std::vec as new_vec;
|
|
|
|
|
|
|
|
#[cfg(no_std)]
|
|
|
|
use core::mem;
|
2020-08-22 06:05:18 +02:00
|
|
|
#[cfg(not(no_std))]
|
|
|
|
use std::mem;
|
2020-08-14 06:04:24 +02:00
|
|
|
|
2020-08-21 05:20:12 +02:00
|
|
|
use std::borrow::Cow;
|
2020-08-20 06:12:39 +02:00
|
|
|
|
2020-09-02 06:15:22 +02:00
|
|
|
use crate::attrs::{AttrItem, ExportInfo, ExportScope, ExportedParams};
|
|
|
|
use crate::function::ExportedFnParams;
|
2020-08-21 05:20:12 +02:00
|
|
|
|
2020-12-24 14:28:40 +01:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
|
2021-02-21 07:11:19 +01:00
|
|
|
pub struct ExportedModParams {
|
2020-12-24 14:28:40 +01:00
|
|
|
pub name: String,
|
2020-09-02 06:15:22 +02:00
|
|
|
skip: bool,
|
|
|
|
pub scope: ExportScope,
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Parse for ExportedModParams {
|
|
|
|
fn parse(args: ParseStream) -> syn::Result<Self> {
|
|
|
|
if args.is_empty() {
|
|
|
|
return Ok(ExportedModParams::default());
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
let info = crate::attrs::parse_attr_items(args)?;
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2020-08-21 05:20:12 +02:00
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
fn from_info(info: ExportInfo) -> syn::Result<Self> {
|
|
|
|
let ExportInfo { items: attrs, .. } = info;
|
2020-12-24 14:28:40 +01:00
|
|
|
let mut name = Default::default();
|
2020-08-21 05:20:12 +02:00
|
|
|
let mut skip = false;
|
2020-12-24 14:28:40 +01:00
|
|
|
let mut scope = None;
|
2020-08-24 00:53:30 +02:00
|
|
|
for attr in attrs {
|
2020-09-09 23:24:37 +02:00
|
|
|
let AttrItem { key, value, .. } = attr;
|
2020-08-24 00:53:30 +02:00
|
|
|
match (key.to_string().as_ref(), value) {
|
2020-12-24 14:28:40 +01:00
|
|
|
("name", Some(s)) => {
|
|
|
|
let new_name = s.value();
|
|
|
|
if name == new_name {
|
|
|
|
return Err(syn::Error::new(key.span(), "conflicting name"));
|
|
|
|
}
|
|
|
|
name = new_name;
|
|
|
|
}
|
2020-08-24 00:53:30 +02:00
|
|
|
("name", None) => return Err(syn::Error::new(key.span(), "requires value")),
|
2020-12-24 14:28:40 +01:00
|
|
|
|
2020-08-21 05:20:12 +02:00
|
|
|
("skip", None) => skip = true,
|
2020-08-24 00:53:30 +02:00
|
|
|
("skip", Some(s)) => return Err(syn::Error::new(s.span(), "extraneous value")),
|
2020-12-24 14:28:40 +01:00
|
|
|
|
|
|
|
("export_prefix", Some(_)) | ("export_all", None) if scope.is_some() => {
|
|
|
|
return Err(syn::Error::new(key.span(), "duplicate export scope"));
|
|
|
|
}
|
|
|
|
("export_prefix", Some(s)) => scope = Some(ExportScope::Prefix(s.value())),
|
2020-09-02 06:15:22 +02:00
|
|
|
("export_prefix", None) => {
|
|
|
|
return Err(syn::Error::new(key.span(), "requires value"))
|
|
|
|
}
|
2020-12-24 14:28:40 +01:00
|
|
|
("export_all", None) => scope = Some(ExportScope::All),
|
2020-09-02 06:15:22 +02:00
|
|
|
("export_all", Some(s)) => {
|
|
|
|
return Err(syn::Error::new(s.span(), "extraneous value"))
|
|
|
|
}
|
2020-08-21 05:20:12 +02:00
|
|
|
(attr, _) => {
|
|
|
|
return Err(syn::Error::new(
|
2020-08-24 00:53:30 +02:00
|
|
|
key.span(),
|
2020-08-21 05:20:12 +02:00
|
|
|
format!("unknown attribute '{}'", attr),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
Ok(ExportedModParams {
|
|
|
|
name,
|
|
|
|
skip,
|
2020-12-24 14:28:40 +01:00
|
|
|
scope: scope.unwrap_or_default(),
|
2020-08-24 00:53:30 +02:00
|
|
|
..Default::default()
|
|
|
|
})
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 18:52:26 +02:00
|
|
|
#[derive(Debug)]
|
2021-02-21 07:11:19 +01:00
|
|
|
pub struct Module {
|
2020-12-24 14:28:40 +01:00
|
|
|
mod_all: syn::ItemMod,
|
2020-08-01 18:52:26 +02:00
|
|
|
fns: Vec<ExportedFn>,
|
|
|
|
consts: Vec<ExportedConst>,
|
2021-02-18 10:42:49 +01:00
|
|
|
sub_modules: Vec<Module>,
|
2020-08-21 05:20:12 +02:00
|
|
|
params: ExportedModParams,
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 06:15:22 +02:00
|
|
|
impl Module {
|
|
|
|
pub fn set_params(&mut self, params: ExportedModParams) -> syn::Result<()> {
|
|
|
|
self.params = params;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 18:52:26 +02:00
|
|
|
impl Parse for Module {
|
|
|
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
2020-08-16 04:51:14 +02:00
|
|
|
let mut mod_all: syn::ItemMod = input.parse()?;
|
2020-08-01 18:52:26 +02:00
|
|
|
let fns: Vec<_>;
|
2020-08-22 06:21:56 +02:00
|
|
|
let mut consts: Vec<_> = new_vec![];
|
2021-02-18 10:42:49 +01:00
|
|
|
let mut sub_modules: Vec<_> = Vec::new();
|
2020-08-16 04:51:14 +02:00
|
|
|
if let Some((_, ref mut content)) = mod_all.content {
|
2020-08-21 05:20:12 +02:00
|
|
|
// Gather and parse functions.
|
2020-08-02 09:39:08 +02:00
|
|
|
fns = content
|
2020-08-16 04:51:14 +02:00
|
|
|
.iter_mut()
|
2020-08-02 09:39:08 +02:00
|
|
|
.filter_map(|item| match item {
|
2020-08-16 04:51:14 +02:00
|
|
|
syn::Item::Fn(f) => Some(f),
|
2020-08-02 09:39:08 +02:00
|
|
|
_ => None,
|
2020-08-01 18:52:26 +02:00
|
|
|
})
|
2021-02-18 10:42:49 +01:00
|
|
|
.try_fold(Vec::new(), |mut vec, item_fn| {
|
2020-08-24 00:53:30 +02:00
|
|
|
// #[cfg] attributes are not allowed on functions
|
2021-02-18 10:42:49 +01:00
|
|
|
crate::attrs::deny_cfg_attr(&item_fn.attrs)?;
|
2020-08-24 00:53:30 +02:00
|
|
|
|
2020-09-02 06:15:22 +02:00
|
|
|
let params: ExportedFnParams =
|
2021-02-18 10:42:49 +01:00
|
|
|
match crate::attrs::inner_item_attributes(&mut item_fn.attrs, "rhai_fn") {
|
2020-08-24 00:53:30 +02:00
|
|
|
Ok(p) => p,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2021-02-18 10:42:49 +01:00
|
|
|
syn::parse2::<ExportedFn>(item_fn.to_token_stream())
|
2020-08-28 06:08:34 +02:00
|
|
|
.and_then(|mut f| {
|
|
|
|
f.set_params(params)?;
|
|
|
|
Ok(f)
|
2020-08-16 04:51:14 +02:00
|
|
|
})
|
2020-08-24 00:53:30 +02:00
|
|
|
.map(|f| vec.push(f))
|
2020-08-01 18:52:26 +02:00
|
|
|
.map(|_| vec)
|
|
|
|
})?;
|
2020-08-21 05:20:12 +02:00
|
|
|
// Gather and parse constants definitions.
|
2020-08-22 06:21:56 +02:00
|
|
|
for item in content.iter() {
|
|
|
|
match item {
|
2020-08-02 09:39:08 +02:00
|
|
|
syn::Item::Const(syn::ItemConst {
|
|
|
|
vis,
|
|
|
|
ref expr,
|
|
|
|
ident,
|
2020-08-22 06:21:56 +02:00
|
|
|
attrs,
|
2020-09-13 16:36:24 +02:00
|
|
|
ty,
|
2020-08-02 09:39:08 +02:00
|
|
|
..
|
|
|
|
}) => {
|
2020-08-22 06:21:56 +02:00
|
|
|
// #[cfg] attributes are not allowed on const declarations
|
2020-08-24 00:53:30 +02:00
|
|
|
crate::attrs::deny_cfg_attr(&attrs)?;
|
2020-08-02 09:39:08 +02:00
|
|
|
if let syn::Visibility::Public(_) = vis {
|
2020-09-13 16:36:24 +02:00
|
|
|
consts.push((ident.to_string(), ty.clone(), expr.as_ref().clone()));
|
2020-08-02 09:39:08 +02:00
|
|
|
}
|
2020-08-24 00:53:30 +02:00
|
|
|
}
|
|
|
|
_ => {}
|
2020-08-22 06:21:56 +02:00
|
|
|
}
|
2020-08-24 00:53:30 +02:00
|
|
|
}
|
2021-02-18 10:42:49 +01:00
|
|
|
// Gather and parse sub-module definitions.
|
2020-08-21 05:20:12 +02:00
|
|
|
//
|
|
|
|
// They are actually removed from the module's body, because they will need
|
|
|
|
// re-generating later when generated code is added.
|
2021-02-18 10:42:49 +01:00
|
|
|
sub_modules.reserve(content.len() - fns.len() - consts.len());
|
2020-08-21 05:20:12 +02:00
|
|
|
let mut i = 0;
|
|
|
|
while i < content.len() {
|
2020-08-24 00:53:30 +02:00
|
|
|
if let syn::Item::Mod(_) = &content[i] {
|
2021-02-18 10:42:49 +01:00
|
|
|
let mut item_mod = match content.remove(i) {
|
2020-08-21 05:20:12 +02:00
|
|
|
syn::Item::Mod(m) => m,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2021-02-18 10:42:49 +01:00
|
|
|
let params: ExportedModParams = match crate::attrs::inner_item_attributes(
|
|
|
|
&mut item_mod.attrs,
|
|
|
|
"rhai_mod",
|
|
|
|
) {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2020-08-24 00:53:30 +02:00
|
|
|
let module =
|
2021-02-18 10:42:49 +01:00
|
|
|
syn::parse2::<Module>(item_mod.to_token_stream()).and_then(|mut m| {
|
2020-09-02 06:15:22 +02:00
|
|
|
m.set_params(params)?;
|
|
|
|
Ok(m)
|
2020-08-21 05:20:12 +02:00
|
|
|
})?;
|
2021-02-18 10:42:49 +01:00
|
|
|
sub_modules.push(module);
|
2020-08-21 05:20:12 +02:00
|
|
|
} else {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 18:52:26 +02:00
|
|
|
} else {
|
2020-08-14 06:04:24 +02:00
|
|
|
fns = new_vec![];
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
Ok(Module {
|
2020-12-24 14:28:40 +01:00
|
|
|
mod_all,
|
2020-08-01 18:52:26 +02:00
|
|
|
fns,
|
|
|
|
consts,
|
2021-02-18 10:42:49 +01:00
|
|
|
sub_modules,
|
2020-08-21 05:20:12 +02:00
|
|
|
params: ExportedModParams::default(),
|
2020-08-01 18:52:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 16:12:11 +02:00
|
|
|
#[allow(dead_code)]
|
2020-08-01 18:52:26 +02:00
|
|
|
impl Module {
|
2020-12-24 14:28:40 +01:00
|
|
|
pub fn attrs(&self) -> &Vec<syn::Attribute> {
|
|
|
|
&self.mod_all.attrs
|
2020-08-22 06:05:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-24 14:28:40 +01:00
|
|
|
pub fn module_name(&self) -> &syn::Ident {
|
|
|
|
&self.mod_all.ident
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
|
2020-12-24 14:28:40 +01:00
|
|
|
pub fn exported_name(&self) -> Cow<str> {
|
|
|
|
if !self.params.name.is_empty() {
|
2021-02-26 13:00:25 +01:00
|
|
|
(&self.params.name).into()
|
2020-08-21 05:20:12 +02:00
|
|
|
} else {
|
2020-12-24 14:28:40 +01:00
|
|
|
self.module_name().to_string().into()
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 06:15:22 +02:00
|
|
|
pub fn update_scope(&mut self, parent_scope: &ExportScope) {
|
|
|
|
let keep = match (self.params.skip, parent_scope) {
|
|
|
|
(true, _) => false,
|
2020-12-24 14:28:40 +01:00
|
|
|
(_, ExportScope::PubOnly) => matches!(self.mod_all.vis, syn::Visibility::Public(_)),
|
|
|
|
(_, ExportScope::Prefix(s)) => self.mod_all.ident.to_string().starts_with(s),
|
2020-09-02 06:15:22 +02:00
|
|
|
(_, ExportScope::All) => true,
|
|
|
|
};
|
|
|
|
self.params.skip = !keep;
|
|
|
|
}
|
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
pub fn skipped(&self) -> bool {
|
|
|
|
self.params.skip
|
|
|
|
}
|
|
|
|
|
2020-08-01 18:52:26 +02:00
|
|
|
pub fn generate(self) -> proc_macro2::TokenStream {
|
2020-08-21 05:20:12 +02:00
|
|
|
match self.generate_inner() {
|
|
|
|
Ok(tokens) => tokens,
|
|
|
|
Err(e) => e.to_compile_error(),
|
2020-08-20 06:12:39 +02:00
|
|
|
}
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
fn generate_inner(self) -> Result<proc_macro2::TokenStream, syn::Error> {
|
2020-08-21 05:20:12 +02:00
|
|
|
// Check for collisions if the "name" attribute was used on inner functions.
|
2020-08-24 00:53:30 +02:00
|
|
|
crate::rhai_module::check_rename_collisions(&self.fns)?;
|
2020-08-21 05:20:12 +02:00
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
// Extract the current structure of the module.
|
|
|
|
let Module {
|
2020-12-24 14:28:40 +01:00
|
|
|
mut mod_all,
|
2020-09-02 06:15:22 +02:00
|
|
|
mut fns,
|
2020-08-24 00:53:30 +02:00
|
|
|
consts,
|
2021-02-18 10:42:49 +01:00
|
|
|
mut sub_modules,
|
2020-08-24 00:53:30 +02:00
|
|
|
params,
|
|
|
|
..
|
|
|
|
} = self;
|
2021-02-21 06:51:24 +01:00
|
|
|
let mod_vis = mod_all.vis;
|
2020-08-01 18:52:26 +02:00
|
|
|
let mod_name = mod_all.ident.clone();
|
|
|
|
let (_, orig_content) = mod_all.content.take().unwrap();
|
2020-11-15 06:49:54 +01:00
|
|
|
let mod_attrs = mem::take(&mut mod_all.attrs);
|
2020-08-01 18:52:26 +02:00
|
|
|
|
2020-08-24 00:53:30 +02:00
|
|
|
if !params.skip {
|
|
|
|
// Generate new module items.
|
|
|
|
//
|
|
|
|
// This is done before inner module recursive generation, because that is destructive.
|
2020-09-02 06:15:22 +02:00
|
|
|
let mod_gen = crate::rhai_module::generate_body(
|
|
|
|
&mut fns,
|
|
|
|
&consts,
|
2021-02-18 10:42:49 +01:00
|
|
|
&mut sub_modules,
|
2020-09-02 06:15:22 +02:00
|
|
|
¶ms.scope,
|
|
|
|
);
|
2020-08-24 00:53:30 +02:00
|
|
|
|
2021-02-18 10:42:49 +01:00
|
|
|
// NB: sub-modules must have their new items for exporting generated in depth-first order
|
2020-08-24 00:53:30 +02:00
|
|
|
// to avoid issues caused by re-parsing them
|
2021-02-18 10:42:49 +01:00
|
|
|
let inner_modules: Vec<proc_macro2::TokenStream> = sub_modules.drain(..)
|
2020-08-24 00:53:30 +02:00
|
|
|
.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)*
|
2021-02-21 06:51:24 +01:00
|
|
|
#mod_vis mod #mod_name {
|
2020-08-24 00:53:30 +02:00
|
|
|
#(#orig_content)*
|
|
|
|
#(#inner_modules)*
|
|
|
|
#mod_gen
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Regenerate the original module as-is.
|
|
|
|
Ok(quote! {
|
|
|
|
#(#mod_attrs)*
|
2021-02-21 06:51:24 +01:00
|
|
|
#mod_vis mod #mod_name {
|
2020-08-24 00:53:30 +02:00
|
|
|
#(#orig_content)*
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
|
2020-12-24 14:28:40 +01:00
|
|
|
pub fn name(&self) -> &syn::Ident {
|
|
|
|
&self.mod_all.ident
|
2020-08-21 05:20:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 21:10:46 +02:00
|
|
|
pub fn consts(&self) -> &[ExportedConst] {
|
|
|
|
&self.consts
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fns(&self) -> &[ExportedFn] {
|
|
|
|
&self.fns
|
|
|
|
}
|
|
|
|
|
2021-02-18 10:42:49 +01:00
|
|
|
pub fn sub_modules(&self) -> &[Module] {
|
|
|
|
&self.sub_modules
|
2020-09-05 21:10:46 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 05:20:12 +02:00
|
|
|
pub fn content(&self) -> Option<&Vec<syn::Item>> {
|
|
|
|
match self.mod_all {
|
2020-12-24 14:28:40 +01:00
|
|
|
syn::ItemMod {
|
2020-08-24 00:53:30 +02:00
|
|
|
content: Some((_, ref vec)),
|
|
|
|
..
|
2020-12-24 14:28:40 +01:00
|
|
|
} => Some(vec),
|
2020-08-21 05:20:12 +02:00
|
|
|
_ => None,
|
2020-08-01 18:52:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|