Compare commits

...

1 commit

Author SHA1 Message Date
b827a5c640 wip: proc: simplify expose macro
Some checks are pending
/ build (push) Waiting to run
2025-05-06 01:12:14 +03:00
4 changed files with 201 additions and 340 deletions

View file

@ -8,6 +8,7 @@ edition = { workspace = true }
proc-macro = true proc-macro = true
[dependencies] [dependencies]
syn = { version = "2", features = ["full", "extra-traits"] } syn = { version = "2", features = ["full", "extra-traits"] }
quote = { version = "1" } quote = { version = "1" }
proc-macro2 = { version = "1", features = ["span-locations"] } proc-macro2 = { version = "1", features = ["span-locations"] }
heck = { version = "0.5" }

View file

@ -1,6 +1,6 @@
extern crate proc_macro; extern crate proc_macro;
pub(crate) use std::collections::{HashMap, BTreeMap}; pub(crate) use std::collections::{BTreeMap, BTreeSet};
pub(crate) use std::cmp::Ordering; pub(crate) use std::cmp::Ordering;
pub(crate) use proc_macro::TokenStream; pub(crate) use proc_macro::TokenStream;
pub(crate) use proc_macro2::{ pub(crate) use proc_macro2::{
@ -22,10 +22,27 @@ mod proc_expose;
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn view (meta: TokenStream, item: TokenStream) -> TokenStream { pub fn view (meta: TokenStream, item: TokenStream) -> TokenStream {
self::proc_view::view_impl(meta.into(), item.into()).into() use self::proc_view::{ViewDefinition, ViewMeta, ViewImpl};
write_macro(ViewDefinition(
parse_macro_input!(meta as ViewMeta),
parse_macro_input!(data as ViewImpl),
))
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn expose (meta: TokenStream, item: TokenStream) -> TokenStream { pub fn expose (meta: TokenStream, item: TokenStream) -> TokenStream {
self::proc_expose::expose_impl(meta.into(), item.into()).into() use self::proc_view::{ExposeDefinition, ExposeMeta, ExposeImpl};
write_macro(ExposeDefinition(
parse_macro_input!(meta as ExposeMeta),
parse_macro_input!(data as ExposeImpl),
))
}
fn write_macro <T: ToTokens> (t: T) -> TokenStream {
let mut out = TokenStream2::new();
self::proc_expose::ExposeDefinition(
parse_macro_input!(meta as ExposeMeta),
parse_macro_input!(data as ExposeImpl),
).to_tokens(&mut out);
out.into()
} }

View file

@ -1,27 +1,59 @@
use crate::*; use crate::*;
use syn::parse::discouraged::Speculative; use syn::parse::discouraged::Speculative;
pub(crate) fn expose_impl (meta: TokenStream, data: TokenStream) -> TokenStream { #[derive(Debug, Clone)]
let mut out = TokenStream2::new(); pub(crate) struct ExposeDefinition(pub(crate) ExposeMeta, pub(crate) ExposeImpl);
ExposeDefinition {
meta: parse_macro_input!(meta as ExposeMeta), #[derive(Debug, Clone)]
data: parse_macro_input!(data as ExposeImpl), pub(crate) struct ExposeMeta;
}.to_tokens(&mut out);
out.into() #[derive(Debug, Clone)]
pub(crate) struct ExposeImpl {
block: ItemImpl,
exposed: BTreeMap<ExposeType, BTreeMap<ExposeSym, ExposeArm>>,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct ExposeDefinition { struct ExposeArm {
meta: ExposeMeta, key: ExposeSym,
data: ExposeImpl, value: Expr
} }
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> { fn parse (input: ParseStream) -> Result<Self> {
Ok(Self { Ok(Self)
meta: input.parse::<ExposeMeta>()?, }
data: input.parse::<ExposeImpl>()?, }
})
impl Parse for ExposeImpl {
fn parse (input: ParseStream) -> Result<Self> {
let block = input.parse::<ItemImpl>()?;
let mut exposed: BTreeMap<ExposeType, BTreeMap<ExposeSym, ExposeArm>> = 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(&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 })
} }
} }
@ -34,62 +66,6 @@ impl ToTokens for ExposeDefinition {
} }
} }
#[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 { impl ToTokens for ExposeImpl {
fn to_tokens (&self, out: &mut TokenStream2) { fn to_tokens (&self, out: &mut TokenStream2) {
let Self { target, items, types } = self; let Self { target, items, types } = self;
@ -97,7 +73,7 @@ impl ToTokens for ExposeImpl {
out.append(token); out.append(token);
} }
for (t, variants) in types.iter() { for (t, variants) in types.iter() {
let predef = match format!("{}", quote! { #t }).as_str() { let predefined = match format!("{}", quote! { #t }).as_str() {
"bool" => vec![ "bool" => vec![
quote! { ::tengri::dsl::Value::Sym(":true") => true }, quote! { ::tengri::dsl::Value::Sym(":true") => true },
quote! { ::tengri::dsl::Value::Sym(":false") => false }, quote! { ::tengri::dsl::Value::Sym(":false") => false },
@ -113,7 +89,7 @@ impl ToTokens for ExposeImpl {
impl ::tengri::dsl::Context<#t> for #target { impl ::tengri::dsl::Context<#t> for #target {
fn get (&self, dsl: &::tengri::dsl::Value) -> Option<#t> { fn get (&self, dsl: &::tengri::dsl::Value) -> Option<#t> {
Some(match dsl { Some(match dsl {
#(#predef,)* #(#predefined,)*
#(#values,)* #(#values,)*
_ => return None _ => return None
}) })
@ -127,58 +103,6 @@ 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 { impl ToTokens for ExposeArm {
fn to_tokens (&self, out: &mut TokenStream2) { fn to_tokens (&self, out: &mut TokenStream2) {
let Self { key, value } = self; let Self { key, value } = self;
@ -207,9 +131,6 @@ impl ToTokens for ExposeArm {
} }
} }
#[derive(Debug, Clone)]
struct ExposeSym(LitStr);
impl From<LitStr> for ExposeSym { impl From<LitStr> for ExposeSym {
fn from (this: LitStr) -> Self { fn from (this: LitStr) -> Self {
Self(this) Self(this)
@ -242,9 +163,6 @@ impl PartialEq for ExposeSym {
impl Eq for ExposeSym {} impl Eq for ExposeSym {}
#[derive(Debug, Clone)]
struct ExposeType(Type);
impl From<Type> for ExposeType { impl From<Type> for ExposeType {
fn from (this: Type) -> Self { fn from (this: Type) -> Self {
Self(this) Self(this)
@ -284,109 +202,107 @@ impl ToTokens for ExposeType {
} }
#[cfg(test)] #[test] fn test_expose_definition () { #[cfg(test)] #[test] fn test_expose_definition () {
let parsed: ExposeImpl = pq! { // TODO
//#[tengri_proc::expose] //let parsed: ExposeImpl = pq! {
impl Something { ////#[tengri_proc::expose]
#[tengri::expose(bool)] { //impl Something {
":bool1" => true || false, //fn something () -> bool {}
} //}
fn something () {} //};
} //// FIXME:
}; ////assert_eq!(
// FIXME: ////format!("{}", quote! { #parsed }),
//assert_eq!( ////format!("{}", quote! {
//format!("{}", quote! { #parsed }), ////impl Something {
//format!("{}", quote! { ////fn something () {}
//impl Something { ////}
//fn something () {} ////impl ::tengri::Context<bool> for Something {
//} ////fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
//impl ::tengri::Context<bool> for Something { ////Some(match dsl {
//fn get (&self, dsl: &::tengri::Value) -> Option<bool> { ////::tengri::Value::Sym(":true") => true,
//Some(match dsl { ////::tengri::Value::Sym(":false") => false,
//::tengri::Value::Sym(":true") => true, ////::tengri::Value::Sym(":bool1") => true || false,
//::tengri::Value::Sym(":false") => false, ////_ => return None
//::tengri::Value::Sym(":bool1") => true || false, ////})
//_ => return None ////}
//}) ////}
//} ////})
//} ////);
//})
//);
let parsed: ExposeImpl = pq! { //let parsed: ExposeImpl = pq! {
//#[tengri_proc::expose] ////#[tengri_proc::expose]
impl Something { //impl Something {
#[tengri::expose(bool)] { //#[tengri::expose(bool)] {
":bool1" => true || false, //":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 () {}
//} //}
//impl ::tengri::Context<Arc<str>> for Something { //#[tengri::expose(u16)] {
//fn get (&self, dsl: &::tengri::Value) -> Option<Arc<str>> { //":u161" => 0 + 1,
//Some(match dsl {
//::tengri::Value::Sym(":arcstr1") => "foo".into(),
//_ => return None
//})
//}
//} //}
//impl ::tengri::Context<Option<Arc<str>>> for Something { //#[tengri::expose(usize)] {
//fn get (&self, dsl: &::tengri::Value) -> Option<Option<Arc<str>>> { //":usize1" => 1 + 2,
//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 { //#[tengri::expose(Arc<str>)] {
//fn get (&self, dsl: &::tengri::Value) -> Option<bool> { //":arcstr1" => "foo".into(),
//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 { //#[tengri::expose(Option<Arc<str>>)] {
//fn get (&self, dsl: &::tengri::Value) -> Option<u16> { //":optarcstr1" => Some("bar".into()),
//Some(match dsl { //":optarcstr2" => Some("baz".into()),
//::tengri::Value::Num(n) => *n as u16,
//::tengri::Value::Sym(":u161") => 0 + 1,
//_ => return None
//})
//}
//} //}
//impl ::tengri::Context<usize> for Something { //fn something () {}
//fn get (&self, dsl: &::tengri::Value) -> Option<usize> { //}
//Some(match dsl { //};
//::tengri::Value::Num(n) => *n as usize, //// FIXME:
//::tengri::Value::Sym(":usize1") => 1 + 2, ////assert_eq!(
//_ => return None ////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
////})
////}
////}
////})
////)
} }

View file

@ -1,51 +1,17 @@
use crate::*; use crate::*;
pub(crate) fn view_impl (meta: TokenStream, data: TokenStream) -> TokenStream { #[derive(Debug, Clone)]
let mut out = TokenStream2::new(); pub(crate) struct ViewDefinition(pub(crate) ViewMeta, pub(crate) ViewImpl);
ViewDefinition {
meta: parse_macro_input!(meta as ViewMeta),
data: parse_macro_input!(data as ViewImpl),
}.to_tokens(&mut out);
out.into()
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct ViewDefinition { pub(crate) struct ViewMeta {
meta: ViewMeta,
data: ViewImpl,
}
#[derive(Debug, Clone)]
struct ViewMeta {
output: Ident, output: Ident,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct ViewImpl { pub(crate) struct ViewImpl {
target: Ident, block: ItemImpl,
items: Vec<ViewItem>, exposed: BTreeSet<Ident>,
syms: Vec<ViewSym>,
}
#[derive(Debug, Clone)]
struct ViewItem {
item: ImplItem,
expose: Option<Literal>,
}
#[derive(Debug, Clone)]
struct ViewSym {
symbol: Literal,
name: Ident,
}
impl Parse for ViewDefinition {
fn parse (input: ParseStream) -> Result<Self> {
Ok(Self {
meta: input.parse::<ViewMeta>()?,
data: input.parse::<ViewImpl>()?,
})
}
} }
impl Parse for ViewMeta { impl Parse for ViewMeta {
@ -88,33 +54,32 @@ impl Parse for ViewImpl {
} }
} }
impl ToTokens for ViewDefinition {
impl Parse for ViewItem { fn to_tokens (&self, out: &mut TokenStream2) {
fn parse (input: ParseStream) -> Result<Self> { let Self(ViewMeta { output }, ViewImpl { target, syms, items }) = self;
let mut expose = None; for token in quote! {
Ok(Self { /// Augmented by [tengri_proc].
item: { impl #target {
let mut item = input.parse::<ImplItem>()?; #(#items)*
if let ImplItem::Fn(ref mut item) = item { }
item.attrs = item.attrs.iter().filter(|attr| { /// Generated by [tengri_proc].
if let Attribute { impl ::tengri::output::Content<#output> for #target {
meta: Meta::List(MetaList { path, tokens, .. }), .. fn content (&self) -> impl Render<#output> {
} = attr self.size.of(::tengri::output::View(self, self.config.view))
&& path.segments.len() == 2 }
&& nth_segment_is(&path.segments, 0, "tengri") }
&& nth_segment_is(&path.segments, 1, "view") /// Generated by [tengri_proc].
&& let Some(TokenTree::Literal(name)) = tokens.clone().into_iter().next() impl<'a> ::tengri::output::ViewContext<'a, #output> for #target {
{ fn get_content_sym (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, #output>> {
expose = Some(name); match value {
return false #(#syms)*
} _ => panic!("expected Sym(content), got: {value:?}")
true }
}).map(|x|x.clone()).collect(); }
}; }
item } {
}, out.append(token)
expose, }
})
} }
} }
@ -155,44 +120,6 @@ impl ToTokens for ViewSym {
} }
} }
impl ToTokens for ViewItem {
fn to_tokens (&self, out: &mut TokenStream2) {
self.item.to_tokens(out)
}
}
impl ToTokens for ViewDefinition {
fn to_tokens (&self, out: &mut TokenStream2) {
let Self {
meta: ViewMeta { output },
data: ViewImpl { target, syms, items },
} = self;
for token in quote! {
/// Augmented by [tengri_proc].
impl #target {
#(#items)*
}
/// Generated by [tengri_proc].
impl ::tengri::output::Content<#output> for #target {
fn content (&self) -> impl Render<#output> {
self.size.of(::tengri::output::View(self, self.config.view))
}
}
/// Generated by [tengri_proc].
impl<'a> ::tengri::output::ViewContext<'a, #output> for #target {
fn get_content_sym (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, #output>> {
match value {
#(#syms)*
_ => panic!("expected Sym(content), got: {value:?}")
}
}
}
} {
out.append(token)
}
}
}
fn nth_segment_is (segments: &Punctuated<PathSegment, PathSep>, n: usize, x: &str) -> bool { fn nth_segment_is (segments: &Punctuated<PathSegment, PathSep>, n: usize, x: &str) -> bool {
if let Some(PathSegment { arguments: PathArguments::None, ident, .. }) = segments.get(n) { if let Some(PathSegment { arguments: PathArguments::None, ident, .. }) = segments.get(n) {
if format!("{ident}") == x { if format!("{ident}") == x {