Compare commits

...

2 commits

Author SHA1 Message Date
6b7de23a3e wip: proc: command macro (pt.2)
Some checks are pending
/ build (push) Waiting to run
2025-05-07 14:36:14 +03:00
c56b08c24e proc: expose: fix output match statement 2025-05-07 12:37:38 +03:00
2 changed files with 116 additions and 50 deletions

View file

@ -4,72 +4,133 @@ use crate::*;
pub(crate) struct CommandDef(pub(crate) CommandMeta, pub(crate) CommandImpl); pub(crate) struct CommandDef(pub(crate) CommandMeta, pub(crate) CommandImpl);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct CommandMeta { pub(crate) struct CommandMeta(Ident);
target: Ident,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct CommandImpl(ItemImpl, BTreeMap<Arc<str>, CommandArm>); pub(crate) struct CommandImpl(ItemImpl, BTreeMap<Arc<str>, CommandArm>);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct CommandVariant(Ident, Vec<FnArg>); struct CommandArm(Ident, Vec<FnArg>, ReturnType);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct CommandArm(Arc<str>, Ident, Vec<FnArg>, ReturnType); struct CommandVariant(Ident, Vec<FnArg>);
impl Parse for CommandMeta { impl Parse for CommandMeta {
fn parse (input: ParseStream) -> Result<Self> { fn parse (input: ParseStream) -> Result<Self> {
Ok(Self { Ok(Self(input.parse::<Ident>()?))
target: input.parse::<Ident>()?,
})
} }
} }
impl Parse for CommandImpl { impl Parse for CommandImpl {
fn parse (input: ParseStream) -> Result<Self> { fn parse (input: ParseStream) -> Result<Self> {
let block = input.parse::<ItemImpl>()?; let block = input.parse::<ItemImpl>()?;
let exposed = Self::collect(&block.items).map_err(|e|input.error(e))?;
Ok(Self(block, exposed))
}
}
impl CommandImpl {
fn collect (items: &Vec<ImplItem>)
-> std::result::Result<BTreeMap<Arc<str>, CommandArm>, String>
{
let mut exposed: BTreeMap<Arc<str>, CommandArm> = Default::default(); let mut exposed: BTreeMap<Arc<str>, CommandArm> = Default::default();
for item in block.items.iter() { for item in items.iter() {
if let ImplItem::Fn(ImplItemFn { if let ImplItem::Fn(
sig: Signature { ident, inputs, output, .. }, .. ImplItemFn { sig: Signature { ident, inputs, output, .. }, .. }
}) = item { ) = item {
let key: Arc<str> = let key = CommandArm::ident_to_key(&ident);
format!("{}", AsKebabCase(format!("{}", &ident))).into();
let variant: Arc<str> =
format!("{}", AsUpperCamelCase(format!("{}", &ident))).into();
if exposed.contains_key(&key) { if exposed.contains_key(&key) {
return Err(input.error(format!("already defined: {ident}"))); return Err(format!("already defined: {ident}"));
} }
exposed.insert(key, CommandArm( exposed.insert(key, CommandArm(
variant,
ident.clone(), ident.clone(),
inputs.iter().map(|x|x.clone()).collect(), inputs.iter().map(|x|x.clone()).collect(),
output.clone(), output.clone(),
)); ));
} }
} }
Ok(Self(block, exposed)) Ok(exposed)
}
}
impl CommandArm {
fn to_key (&self) -> Arc<str> {
Self::ident_to_key(&self.0)
}
fn to_enum_variant (&self) -> CommandVariant {
CommandVariant(self.to_enum_variant_ident(), self.1.clone())
}
fn to_enum_variant_ident (&self) -> Ident {
Ident::new(&Self::ident_to_enum_variant(&self.0), Span::call_site())
}
fn ident_to_key (ident: &Ident) -> Arc<str> {
format!("{}", AsKebabCase(format!("{ident}"))).into()
}
fn ident_to_enum_variant (ident: &Ident) -> Arc<str> {
format!("{}", AsUpperCamelCase(format!("{ident}"))).into()
}
fn to_matcher (&self) -> TokenStream2 {
let mut out = TokenStream2::new();
let key = LitStr::new(&self.to_key(), Span::call_site());
let ident = &self.0;
let mut take_args = vec![TokenStream2::new();0];
let mut take_rest = TokenStream2::new();
for token in quote! {
Some(::tengri::dsl::Token { value: Value::Key(#key), .. }) => {
let mut iter = iter.clone();
#(#take_args)*
#(#take_rest)?
self.#ident()
},
} {
out.append(token);
}
out
//$(Some(Token { value: Value::Key($key), .. }) => {
//let mut iter = iter.clone();
//$(
//let next = iter.next();
//if next.is_none() { panic!("no argument: {}", stringify!($arg)); }
//let $arg = next.unwrap();
//$(let $arg: Option<$type> = Context::<$type>::get($state, &$arg.value);)?
//)*
//$(let $rest = iter.clone();)?
//return $command
//}),*
}
fn to_implementation (&self) -> TokenStream2 {
let mut out = TokenStream2::new();
let mut variant = TokenStream2::new();
let mut call = TokenStream2::new();
for token in quote! {
#variant => #call,
} {
out.append(token);
}
out
} }
} }
impl ToTokens for CommandDef { impl ToTokens for CommandDef {
fn to_tokens (&self, out: &mut TokenStream2) { fn to_tokens (&self, out: &mut TokenStream2) {
let Self(CommandMeta { target }, CommandImpl(block, exposed)) = self; let Self(CommandMeta(target), CommandImpl(block, exposed)) = self;
let enumeration = &block.self_ty; let enumeration = &block.self_ty;
let definitions = exposed.values().map(|x|CommandVariant( let variants = exposed.values().map(CommandArm::to_enum_variant);
x.1.clone(), let matchers = exposed.values().map(CommandArm::to_matcher);
x.2.clone(), let implementations = exposed.values().map(CommandArm::to_implementation);
));
let implementations = exposed.values().map(|x|CommandArm(
x.0.clone(),
x.1.clone(),
x.2.clone(),
x.3.clone(),
));
for token in quote! { for token in quote! {
#block #block
enum #enumeration { enum #enumeration {
#(#definitions)* #(#variants)*
}
impl<'a> TryFromDsl<'a, #target> for #enumeration {
fn try_from_expr (state: &#target, iter: TokenIter) -> Option<Self> {
let mut iter = iter.clone();
match iter.next() {
#(#matchers)*
_ => None
}
}
} }
impl Command<#target> for #enumeration { impl Command<#target> for #enumeration {
fn execute (self, state: &mut #target) -> Perhaps<Self> { fn execute (self, state: &mut #target) -> Perhaps<Self> {
@ -81,6 +142,9 @@ impl ToTokens for CommandDef {
} { } {
out.append(token) out.append(token)
} }
if exposed.len() > 0 {
panic!("{}", quote! {#out});
}
} }
} }
@ -108,34 +172,30 @@ impl ToTokens for CommandVariant {
impl ToTokens for CommandArm { impl ToTokens for CommandArm {
fn to_tokens (&self, out: &mut TokenStream2) { fn to_tokens (&self, out: &mut TokenStream2) {
let Self(symbol, ident, args, returnType) = self; let Self(ident, args, returnType) = self;
out.append(Punct::new(':', Joint)); for ident in ["tengri", "dsl", "Value", "Sym"].iter() {
out.append(Punct::new(':', Alone)); out.append(Punct::new(':', Joint));
out.append(Ident::new("tengri", Span::call_site())); out.append(Punct::new(':', Alone));
out.append(Punct::new(':', Joint)); out.append(Ident::new(ident, Span::call_site()));
out.append(Punct::new(':', Alone)); }
out.append(Ident::new("dsl", Span::call_site()));
out.append(Punct::new(':', Joint));
out.append(Punct::new(':', Alone));
out.append(Ident::new("Value", Span::call_site()));
out.append(Punct::new(':', Joint));
out.append(Punct::new(':', Alone));
out.append(Ident::new("Sym", Span::call_site()));
out.append(Group::new(Delimiter::Parenthesis, { out.append(Group::new(Delimiter::Parenthesis, {
let mut out = TokenStream2::new(); let mut out = TokenStream2::new();
out.append(self.to_enum_variant_ident());
for arg in args.iter() { for arg in args.iter() {
out.append(LitStr::new(&symbol, Span::call_site()).token());
} }
out out
})); }));
out.append(Punct::new('=', Joint)); out.append(Punct::new('=', Joint));
out.append(Punct::new('>', Alone)); out.append(Punct::new('>', Alone));
out.append(LitStr::new(&format!("{}", ident), Span::call_site()).token()); out.append(Ident::new("self", Span::call_site()));
out.append(Punct::new('.', Alone));
out.append(ident.clone());
out.append(Group::new(Delimiter::Parenthesis, { out.append(Group::new(Delimiter::Parenthesis, {
let mut out = TokenStream2::new(); let mut out = TokenStream2::new();
for arg in args.iter() { for arg in args.iter() {
// TODO // TODO
//out.append(LitStr::new(&symbol, Span::call_site()).token()); out.append(LitStr::new(&self.to_key(), Span::call_site()).token());
out.append(Punct::new(',', Alone));
} }
out out
})); }));

View file

@ -81,7 +81,7 @@ impl ToTokens for ExposeImpl {
], ],
_ => vec![], _ => vec![],
}; };
let values = variants.values(); let values = variants.iter().map(|(k, v)|ExposeArm(k.clone(), v.clone()));
let trait_impl = quote! { let trait_impl = quote! {
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> {
@ -97,6 +97,9 @@ impl ToTokens for ExposeImpl {
out.append(token); out.append(token);
} }
} }
//if exposed.len() > 0 {
//panic!("{}", quote! {#out});
//}
} }
} }
@ -122,9 +125,12 @@ impl ToTokens for ExposeArm {
})); }));
out.append(Punct::new('=', Joint)); out.append(Punct::new('=', Joint));
out.append(Punct::new('>', Alone)); out.append(Punct::new('>', Alone));
out.append(Ident::new("self", Span::call_site()));
out.append(Punct::new('.', Alone));
for token in quote! { #value } { for token in quote! { #value } {
out.append(token); out.append(token);
} }
out.append(Group::new(Delimiter::Parenthesis, TokenStream2::new()));
} }
} }