wip: dsl, output, input, proc, tui: sorting out give and take
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-24 23:57:12 +03:00
parent 5a2177cc77
commit 3e1084555b
10 changed files with 273 additions and 301 deletions

View file

@ -149,10 +149,9 @@ impl ToTokens for CommandDef {
}
}
/// Generated by [tengri_proc::command].
impl<'n> ::tengri::dsl::Take<'n, #state> for #command_enum {
fn take <'source> (
state: &#state,
words: &mut ::tengri::dsl::TokenIter<'source>
impl<'state> ::tengri::dsl::Take<'state, #state> for #command_enum {
fn take <'source: 'state> (
state: &#state, mut words: ::tengri::dsl::TokenIter<'source>
) -> Perhaps<Self> {
let mut words = words.clone();
let token = words.next();

View file

@ -9,9 +9,6 @@ pub(crate) struct ExposeMeta;
#[derive(Debug, Clone)]
pub(crate) struct ExposeImpl(ItemImpl, BTreeMap<ExposeType, BTreeMap<String, Ident>>);
#[derive(Debug, Clone)]
struct ExposeArm(String, Ident);
#[derive(Debug, Clone)]
struct ExposeSym(LitStr);
@ -82,13 +79,23 @@ impl ToTokens for ExposeImpl {
},
_ => quote! {},
};
let values = variants.iter().map(ExposeArm::from);
let values = variants.iter().map(|(key, value)|{
let key = LitStr::new(&key, Span::call_site());
quote! { Some(::tengri::dsl::Value::Sym(#key)) => state.#value(), }
});
write_quote_to(out, quote! {
/// Generated by [tengri_proc::expose].
impl<'n> ::tengri::dsl::Give<'n, #t> for #state {
fn give <'source: 'n> (
&self, words: ::tengri::dsl::TokenIter<'source>
) -> Perhaps<#t> {
Take::take(self, words)
}
}
/// Generated by [tengri_proc::expose].
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
fn take <'source> (
state: &#state,
words: &mut ::tengri::dsl::TokenIter<'source>
fn take <'source: 'n> (
state: &#state, mut words: ::tengri::dsl::TokenIter<'source>
) -> Perhaps<Self> {
Ok(Some(match words.next().map(|x|x.value) {
#predefined
@ -105,22 +112,6 @@ impl ToTokens for ExposeImpl {
}
}
impl From<(&String, &Ident)> for ExposeArm {
fn from ((a, b): (&String, &Ident)) -> Self {
Self(a.clone(), b.clone())
}
}
impl ToTokens for ExposeArm {
fn to_tokens (&self, out: &mut TokenStream2) {
let Self(key, value) = self;
let key = LitStr::new(&key, Span::call_site());
write_quote_to(out, quote! {
Some(::tengri::dsl::Value::Sym(#key)) => state.#value(),
})
}
}
impl From<LitStr> for ExposeSym { fn from (this: LitStr) -> Self { Self(this) } }
impl PartialOrd for ExposeSym {

View file

@ -43,52 +43,38 @@ impl ToTokens for ViewDef {
let self_ty = &block.self_ty;
// Expressions are handled by built-in functions
// that operate over constants and symbols.
let builtins: Vec<_> = builtins_with_types().iter().map(|ty|write_quote(quote! {
::tengri::dsl::Value::Exp(_, expr) => {
Give::<#ty>::give(&mut expr.clone()).map(|value|value.boxed())
},
})).collect();
let builtin = builtins_with_types().map(|ty|write_quote(quote! { #ty }));
// Symbols are handled by user-taked functions
// that take no parameters but `&self`.
let exposed: Vec<_> = exposed.iter().map(|(key, value)|write_quote(quote! {
::tengri::dsl::Value::Sym(#key) => {
Some(Box::new(Thunk::new(move||#self_ty::#value(self))))
},
})).collect();
let exposed = exposed.iter().map(|(key, value)|write_quote(quote! {
::tengri::dsl::Value::Sym(#key) => Some(Box::new(Thunk::new(
move||#self_ty::#value(state))))}));
write_quote_to(out, quote! {
/// Generated by [tengri_proc].
///
/// Gives [#self_ty] the ability to construct the [Render]able
/// which might corresponds to a given [TokenStream],
/// while taking [#self_ty]'s state into consideration.
give!(Box<dyn Render<#output>>|state:#self_ty, words|Ok(None));
//impl <'n, State: ::tengri::dsl::Give<Box<dyn Render<#output>>>>
//Take<'n, Box<dyn Render<#output>> for #self_ty
//{
//fn give <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>)
//-> Perhaps<Box<dyn Render<#output>>>
//{
//Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
//match value {
//// Expressions are handled by built-in functions
//// that operate over constants and symbols.
//::tengri::dsl::Value::Exp(_, exp) => {
//#(#builtins)*
//None
//},
//// Symbols are handled by user-taked functions
//// that take no parameters but `&self`.
//::tengri::dsl::Value::Sym(sym) => match sym {
//#(#exposed)*
//_ => None
//},
//_ => None
//}
//} else {
//None
//})
//}
//}
impl<'state> Give<'state, Box<dyn Render<#output> + 'state>> for #self_ty {
fn give <'source: 'state> (&self, mut words: TokenIter<'source>)
-> Perhaps<Box<dyn Render<#output> + 'state>>
{
let state = self;
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
match value {
#(::tengri::dsl::Value::Exp(_, expr) => {
#builtin::take(state, expr)?.map(|value|value.boxed())
},)*
#(
#exposed,
)*
_ => None
}
} else {
None
})
}
}
/// Generated by [tengri_proc].
///
/// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,
@ -105,21 +91,21 @@ impl ToTokens for ViewDef {
}
}
fn builtins_with_types () -> [TokenStream2;14] {
fn builtins_with_types () -> impl Iterator<Item=TokenStream2> {
[
quote! { When< Box<dyn Render<_> + '_> > },
quote! { Either< Box<dyn Render<_> + '_>, Box<dyn Render<_> + '_>> },
quote! { Align< Box<dyn Render<_> + '_> > },
quote! { Bsp< Box<dyn Render<_> + '_>, Box<dyn Render<_> + '_>> },
quote! { Fill< Box<dyn Render<_> + '_> > },
quote! { Fixed<_, Box<dyn Render<_> + '_> > },
quote! { Min<_, Box<dyn Render<_> + '_> > },
quote! { Max<_, Box<dyn Render<_> + '_> > },
quote! { Shrink<_, Box<dyn Render<_> + '_> > },
quote! { Expand<_, Box<dyn Render<_> + '_> > },
quote! { Push<_, Box<dyn Render<_> + '_> > },
quote! { Pull<_, Box<dyn Render<_> + '_> > },
quote! { Margin<_, Box<dyn Render<_> + '_> > },
quote! { Padding<_, Box<dyn Render<_> + '_> > },
]
quote! { When::< Box<dyn Render<_>> > },
quote! { Either::< Box<dyn Render<_>>, Box<dyn Render<_>>> },
quote! { Align::< Box<dyn Render<_>> > },
quote! { Bsp::< Box<dyn Render<_>>, Box<dyn Render<_>>> },
quote! { Fill::< Box<dyn Render<_>> > },
quote! { Fixed::<_, Box<dyn Render<_>> > },
quote! { Min::<_, Box<dyn Render<_>> > },
quote! { Max::<_, Box<dyn Render<_>> > },
quote! { Shrink::<_, Box<dyn Render<_>> > },
quote! { Expand::<_, Box<dyn Render<_>> > },
quote! { Push::<_, Box<dyn Render<_>> > },
quote! { Pull::<_, Box<dyn Render<_>> > },
quote! { Margin::<_, Box<dyn Render<_>> > },
quote! { Padding::<_, Box<dyn Render<_>> > },
].into_iter()
}