mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
proc: simplify expose macro
This commit is contained in:
parent
cba23a005c
commit
7570aefcc2
5 changed files with 288 additions and 405 deletions
|
|
@ -1,103 +1,76 @@
|
|||
use crate::*;
|
||||
use syn::parse::discouraged::Speculative;
|
||||
|
||||
pub(crate) fn expose_impl (meta: TokenStream, data: TokenStream) -> TokenStream {
|
||||
let mut out = TokenStream2::new();
|
||||
ExposeDefinition {
|
||||
meta: parse_macro_input!(meta as ExposeMeta),
|
||||
data: parse_macro_input!(data as ExposeImpl),
|
||||
}.to_tokens(&mut out);
|
||||
out.into()
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ExposeDef(pub(crate) ExposeMeta, pub(crate) ExposeImpl);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ExposeMeta;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ExposeImpl {
|
||||
block: ItemImpl,
|
||||
exposed: BTreeMap<ExposeType, BTreeMap<String, Ident>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeDefinition {
|
||||
meta: ExposeMeta,
|
||||
data: ExposeImpl,
|
||||
}
|
||||
struct ExposeArm(String, Ident);
|
||||
|
||||
impl Parse for ExposeDefinition {
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeSym(LitStr);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeType(Box<Type>);
|
||||
|
||||
impl Parse for ExposeMeta {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
Ok(Self {
|
||||
meta: input.parse::<ExposeMeta>()?,
|
||||
data: input.parse::<ExposeImpl>()?,
|
||||
})
|
||||
Ok(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for ExposeDefinition {
|
||||
impl Parse for ExposeImpl {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
let block = input.parse::<ItemImpl>()?;
|
||||
let mut exposed: BTreeMap<ExposeType, BTreeMap<String, Ident>> = Default::default();
|
||||
for item in block.items.iter() {
|
||||
if let ImplItem::Fn(ImplItemFn { sig: Signature { ident, output, .. }, .. }) = item {
|
||||
if let ReturnType::Type(_, return_type) = output {
|
||||
let return_type = ExposeType(return_type.clone());
|
||||
if !exposed.contains_key(&return_type) {
|
||||
exposed.insert(return_type.clone(), Default::default());
|
||||
}
|
||||
let values = exposed.get_mut(&return_type).unwrap();
|
||||
let key = format!(":{}", AsKebabCase(format!("{}", &ident)));
|
||||
if values.contains_key(&key) {
|
||||
return Err(input.error(format!("already defined: {key}")))
|
||||
}
|
||||
values.insert(key, ident.clone());
|
||||
} else {
|
||||
return Err(input.error("output type must be specified"))
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Self { block, exposed })
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for ExposeDef {
|
||||
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||
let Self { meta, data } = self;
|
||||
let Self(meta, data) = self;
|
||||
for token in quote! { #data } {
|
||||
out.append(token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeMeta {}
|
||||
|
||||
impl Parse for ExposeMeta {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
Ok(Self {})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeImpl {
|
||||
target: Ident,
|
||||
items: Vec<ImplItem>,
|
||||
types: BTreeMap<ExposeType, BTreeMap<ExposeSym, ExposeArm>>,
|
||||
}
|
||||
|
||||
impl Parse for ExposeImpl {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
let _impl = input.parse::<Token![impl]>()?;
|
||||
let target = input.parse::<Ident>()?;
|
||||
let group;
|
||||
let brace = braced!(group in input);
|
||||
let mut items = vec![];
|
||||
let mut types: BTreeMap<ExposeType, BTreeMap<ExposeSym, ExposeArm>> = Default::default();
|
||||
while !group.is_empty() {
|
||||
let fork = group.fork();
|
||||
if let Ok(block) = fork.parse::<ExposeBlock>() {
|
||||
let t = block.type_.into();
|
||||
if let Some(values) = types.get_mut(&t) {
|
||||
for (key, value) in block.values.into_iter() {
|
||||
if values.contains_key(&key) {
|
||||
return Err(input.error(format!("{key:?} ({t:?}): already exists")))
|
||||
} else {
|
||||
values.insert(key, value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
types.insert(t, block.values);
|
||||
}
|
||||
group.advance_to(&fork);
|
||||
continue
|
||||
}
|
||||
let fork = group.fork();
|
||||
if let Ok(item) = fork.parse::<ImplItem>() {
|
||||
items.push(item);
|
||||
group.advance_to(&fork);
|
||||
continue
|
||||
}
|
||||
return Err(input.error(
|
||||
"expected either item or #[tengri::expose(type)] { \":key\" => value }"
|
||||
));
|
||||
}
|
||||
Ok(Self { target, items, types })
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for ExposeImpl {
|
||||
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||
let Self { target, items, types } = self;
|
||||
for token in quote! { impl #target { #(#items)* } } {
|
||||
let Self { block, exposed } = self;
|
||||
let target = &self.block.self_ty;
|
||||
for token in quote! { #block } {
|
||||
out.append(token);
|
||||
}
|
||||
for (t, variants) in types.iter() {
|
||||
let predef = match format!("{}", quote! { #t }).as_str() {
|
||||
for (t, variants) in exposed.iter() {
|
||||
let predefined = match format!("{}", quote! { #t }).as_str() {
|
||||
"bool" => vec![
|
||||
quote! { ::tengri::dsl::Value::Sym(":true") => true },
|
||||
quote! { ::tengri::dsl::Value::Sym(":false") => false },
|
||||
|
|
@ -113,7 +86,7 @@ impl ToTokens for ExposeImpl {
|
|||
impl ::tengri::dsl::Context<#t> for #target {
|
||||
fn get (&self, dsl: &::tengri::dsl::Value) -> Option<#t> {
|
||||
Some(match dsl {
|
||||
#(#predef,)*
|
||||
#(#predefined,)*
|
||||
#(#values,)*
|
||||
_ => return None
|
||||
})
|
||||
|
|
@ -127,61 +100,9 @@ impl ToTokens for ExposeImpl {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeBlock {
|
||||
type_: Type,
|
||||
values: BTreeMap<ExposeSym, ExposeArm>,
|
||||
}
|
||||
|
||||
impl Parse for ExposeBlock {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
let _ = input.parse::<Token![#]>()?;
|
||||
|
||||
let group;
|
||||
let bracket = bracketed!(group in input);
|
||||
let path = group.parse::<Path>()?;
|
||||
let type_ = if
|
||||
path.segments.get(0).map(|x|x.ident.to_string()) == Some("tengri".to_string()) &&
|
||||
path.segments.get(1).map(|x|x.ident.to_string()) == Some("expose".to_string())
|
||||
{
|
||||
let token;
|
||||
let paren = parenthesized!(token in group);
|
||||
token.parse::<Type>()?
|
||||
} else {
|
||||
return Err(input.error("expected #[tengri::expose(type)]"))
|
||||
};
|
||||
|
||||
let group;
|
||||
let brace = braced!(group in input);
|
||||
let mut values = BTreeMap::new();
|
||||
while !group.is_empty() {
|
||||
let arm = group.parse::<ExposeArm>()?;
|
||||
values.insert(arm.key.clone(), arm);
|
||||
let _ = group.parse::<Token![,]>()?;
|
||||
}
|
||||
Ok(Self { type_, values })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeArm {
|
||||
key: ExposeSym,
|
||||
value: Expr
|
||||
}
|
||||
|
||||
impl Parse for ExposeArm {
|
||||
fn parse (input: ParseStream) -> Result<Self> {
|
||||
let key = input.parse::<LitStr>()?.into();
|
||||
let _ = input.parse::<Token![=]>()?;
|
||||
let _ = input.parse::<Token![>]>()?;
|
||||
let value = input.parse::<Expr>()?;
|
||||
Ok(Self { key, value })
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for ExposeArm {
|
||||
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||
let Self { key, value } = self;
|
||||
let Self(key, value) = self;
|
||||
out.append(Punct::new(':', Joint));
|
||||
out.append(Punct::new(':', Alone));
|
||||
out.append(Ident::new("tengri", Span::call_site()));
|
||||
|
|
@ -196,7 +117,7 @@ impl ToTokens for ExposeArm {
|
|||
out.append(Ident::new("Sym", Span::call_site()));
|
||||
out.append(Group::new(Delimiter::Parenthesis, {
|
||||
let mut out = TokenStream2::new();
|
||||
out.append(key.0.token());
|
||||
out.append(LitStr::new(&key, Span::call_site()).token());
|
||||
out
|
||||
}));
|
||||
out.append(Punct::new('=', Joint));
|
||||
|
|
@ -207,9 +128,6 @@ impl ToTokens for ExposeArm {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeSym(LitStr);
|
||||
|
||||
impl From<LitStr> for ExposeSym {
|
||||
fn from (this: LitStr) -> Self {
|
||||
Self(this)
|
||||
|
|
@ -242,12 +160,9 @@ impl PartialEq for ExposeSym {
|
|||
|
||||
impl Eq for ExposeSym {}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ExposeType(Type);
|
||||
|
||||
impl From<Type> for ExposeType {
|
||||
fn from (this: Type) -> Self {
|
||||
Self(this)
|
||||
Self(Box::new(this))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -284,109 +199,107 @@ impl ToTokens for ExposeType {
|
|||
}
|
||||
|
||||
#[cfg(test)] #[test] fn test_expose_definition () {
|
||||
let parsed: ExposeImpl = pq! {
|
||||
//#[tengri_proc::expose]
|
||||
impl Something {
|
||||
#[tengri::expose(bool)] {
|
||||
":bool1" => true || false,
|
||||
}
|
||||
fn something () {}
|
||||
}
|
||||
};
|
||||
// FIXME:
|
||||
//assert_eq!(
|
||||
//format!("{}", quote! { #parsed }),
|
||||
//format!("{}", quote! {
|
||||
//impl Something {
|
||||
//fn something () {}
|
||||
//}
|
||||
//impl ::tengri::Context<bool> for Something {
|
||||
//fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
|
||||
//Some(match dsl {
|
||||
//::tengri::Value::Sym(":true") => true,
|
||||
//::tengri::Value::Sym(":false") => false,
|
||||
//::tengri::Value::Sym(":bool1") => true || false,
|
||||
//_ => return None
|
||||
//})
|
||||
//}
|
||||
//}
|
||||
//})
|
||||
//);
|
||||
// TODO
|
||||
//let parsed: ExposeImpl = pq! {
|
||||
////#[tengri_proc::expose]
|
||||
//impl Something {
|
||||
//fn something () -> bool {}
|
||||
//}
|
||||
//};
|
||||
//// FIXME:
|
||||
////assert_eq!(
|
||||
////format!("{}", quote! { #parsed }),
|
||||
////format!("{}", quote! {
|
||||
////impl Something {
|
||||
////fn something () {}
|
||||
////}
|
||||
////impl ::tengri::Context<bool> for Something {
|
||||
////fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
|
||||
////Some(match dsl {
|
||||
////::tengri::Value::Sym(":true") => true,
|
||||
////::tengri::Value::Sym(":false") => false,
|
||||
////::tengri::Value::Sym(":bool1") => true || false,
|
||||
////_ => return None
|
||||
////})
|
||||
////}
|
||||
////}
|
||||
////})
|
||||
////);
|
||||
|
||||
let parsed: ExposeImpl = pq! {
|
||||
//#[tengri_proc::expose]
|
||||
impl Something {
|
||||
#[tengri::expose(bool)] {
|
||||
":bool1" => true || false,
|
||||
}
|
||||
#[tengri::expose(u16)] {
|
||||
":u161" => 0 + 1,
|
||||
}
|
||||
#[tengri::expose(usize)] {
|
||||
":usize1" => 1 + 2,
|
||||
}
|
||||
#[tengri::expose(Arc<str>)] {
|
||||
":arcstr1" => "foo".into(),
|
||||
}
|
||||
#[tengri::expose(Option<Arc<str>>)] {
|
||||
":optarcstr1" => Some("bar".into()),
|
||||
":optarcstr2" => Some("baz".into()),
|
||||
}
|
||||
fn something () {}
|
||||
}
|
||||
};
|
||||
// FIXME:
|
||||
//assert_eq!(
|
||||
//format!("{}", quote! { #parsed }),
|
||||
//format!("{}", quote! {
|
||||
//impl Something {
|
||||
//fn something () {}
|
||||
//let parsed: ExposeImpl = pq! {
|
||||
////#[tengri_proc::expose]
|
||||
//impl Something {
|
||||
//#[tengri::expose(bool)] {
|
||||
//":bool1" => true || false,
|
||||
//}
|
||||
//impl ::tengri::Context<Arc<str>> for Something {
|
||||
//fn get (&self, dsl: &::tengri::Value) -> Option<Arc<str>> {
|
||||
//Some(match dsl {
|
||||
//::tengri::Value::Sym(":arcstr1") => "foo".into(),
|
||||
//_ => return None
|
||||
//})
|
||||
//}
|
||||
//#[tengri::expose(u16)] {
|
||||
//":u161" => 0 + 1,
|
||||
//}
|
||||
//impl ::tengri::Context<Option<Arc<str>>> for Something {
|
||||
//fn get (&self, dsl: &::tengri::Value) -> Option<Option<Arc<str>>> {
|
||||
//Some(match dsl {
|
||||
//::tengri::Value::Sym(":optarcstr1") => Some("bar".into()),
|
||||
//::tengri::Value::Sym(":optarcstr2") => Some("baz".into()),
|
||||
//_ => return None
|
||||
//})
|
||||
//}
|
||||
//#[tengri::expose(usize)] {
|
||||
//":usize1" => 1 + 2,
|
||||
//}
|
||||
//impl ::tengri::Context<bool> for Something {
|
||||
//fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
|
||||
//Some(match dsl {
|
||||
//::tengri::Value::Sym(":true") => true,
|
||||
//::tengri::Value::Sym(":false") => false,
|
||||
//::tengri::Value::Sym(":bool1") => true || false,
|
||||
//_ => return None
|
||||
//})
|
||||
//}
|
||||
//#[tengri::expose(Arc<str>)] {
|
||||
//":arcstr1" => "foo".into(),
|
||||
//}
|
||||
//impl ::tengri::Context<u16> for Something {
|
||||
//fn get (&self, dsl: &::tengri::Value) -> Option<u16> {
|
||||
//Some(match dsl {
|
||||
//::tengri::Value::Num(n) => *n as u16,
|
||||
//::tengri::Value::Sym(":u161") => 0 + 1,
|
||||
//_ => return None
|
||||
//})
|
||||
//}
|
||||
//#[tengri::expose(Option<Arc<str>>)] {
|
||||
//":optarcstr1" => Some("bar".into()),
|
||||
//":optarcstr2" => Some("baz".into()),
|
||||
//}
|
||||
//impl ::tengri::Context<usize> for Something {
|
||||
//fn get (&self, dsl: &::tengri::Value) -> Option<usize> {
|
||||
//Some(match dsl {
|
||||
//::tengri::Value::Num(n) => *n as usize,
|
||||
//::tengri::Value::Sym(":usize1") => 1 + 2,
|
||||
//_ => return None
|
||||
//})
|
||||
//}
|
||||
//}
|
||||
//})
|
||||
//)
|
||||
//fn something () {}
|
||||
//}
|
||||
//};
|
||||
//// FIXME:
|
||||
////assert_eq!(
|
||||
////format!("{}", quote! { #parsed }),
|
||||
////format!("{}", quote! {
|
||||
////impl Something {
|
||||
////fn something () {}
|
||||
////}
|
||||
////impl ::tengri::Context<Arc<str>> for Something {
|
||||
////fn get (&self, dsl: &::tengri::Value) -> Option<Arc<str>> {
|
||||
////Some(match dsl {
|
||||
////::tengri::Value::Sym(":arcstr1") => "foo".into(),
|
||||
////_ => return None
|
||||
////})
|
||||
////}
|
||||
////}
|
||||
////impl ::tengri::Context<Option<Arc<str>>> for Something {
|
||||
////fn get (&self, dsl: &::tengri::Value) -> Option<Option<Arc<str>>> {
|
||||
////Some(match dsl {
|
||||
////::tengri::Value::Sym(":optarcstr1") => Some("bar".into()),
|
||||
////::tengri::Value::Sym(":optarcstr2") => Some("baz".into()),
|
||||
////_ => return None
|
||||
////})
|
||||
////}
|
||||
////}
|
||||
////impl ::tengri::Context<bool> for Something {
|
||||
////fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
|
||||
////Some(match dsl {
|
||||
////::tengri::Value::Sym(":true") => true,
|
||||
////::tengri::Value::Sym(":false") => false,
|
||||
////::tengri::Value::Sym(":bool1") => true || false,
|
||||
////_ => return None
|
||||
////})
|
||||
////}
|
||||
////}
|
||||
////impl ::tengri::Context<u16> for Something {
|
||||
////fn get (&self, dsl: &::tengri::Value) -> Option<u16> {
|
||||
////Some(match dsl {
|
||||
////::tengri::Value::Num(n) => *n as u16,
|
||||
////::tengri::Value::Sym(":u161") => 0 + 1,
|
||||
////_ => return None
|
||||
////})
|
||||
////}
|
||||
////}
|
||||
////impl ::tengri::Context<usize> for Something {
|
||||
////fn get (&self, dsl: &::tengri::Value) -> Option<usize> {
|
||||
////Some(match dsl {
|
||||
////::tengri::Value::Num(n) => *n as usize,
|
||||
////::tengri::Value::Sym(":usize1") => 1 + 2,
|
||||
////_ => return None
|
||||
////})
|
||||
////}
|
||||
////}
|
||||
////})
|
||||
////)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue