wip: ast/cst
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-25 22:48:29 +03:00
parent 31e84bf5b3
commit f1b24d436a
20 changed files with 1081 additions and 1103 deletions

View file

@ -149,10 +149,8 @@ impl ToTokens for CommandDef {
}
}
/// Generated by [tengri_proc::command].
impl<'state> ::tengri::dsl::Take<'state, #state> for #command_enum {
fn take <'source: 'state> (
state: &#state, mut words: ::tengri::dsl::TokenIter<'source>
) -> Perhaps<Self> {
impl ::tengri::dsl::Take<#state> for #command_enum {
fn take (state: &#state, mut words: ::tengri::dsl::Cst) -> Perhaps<Self> {
let mut words = words.clone();
let token = words.next();
todo!()//Ok(match token { #(#matchers)* _ => None })

View file

@ -85,10 +85,8 @@ impl ToTokens for ExposeImpl {
});
write_quote_to(out, quote! {
/// Generated by [tengri_proc::expose].
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
fn take <'source: 'n> (
state: &#state, mut words: ::tengri::dsl::TokenIter<'source>
) -> Perhaps<Self> {
impl ::tengri::dsl::Take<#state> for #t {
fn take (state: &#state, mut words: ::tengri::dsl::Cst) -> Perhaps<Self> {
Ok(Some(match words.next().map(|x|x.value) {
#predefined
#(#values)*

View file

@ -43,50 +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 builtin = builtins_with_boxes_output(quote! { #output });
let builtin = builtins_with_boxes_output(quote! { #output }).map(|builtin|quote! {
::tengri::dsl::Value::Exp(_, expr) => return Ok(Some(
#builtin::take_or_fail(state, expr, ||"failed to load builtin")?.boxed()
)),
});
// Symbols are handled by user-taked functions
// that take no parameters but `&self`.
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))))}));
::tengri::dsl::Value::Sym(#key) => return Ok(Some(
state.#value().boxed()
)),
}));
write_quote_to(out, quote! {
// Original user-taked implementation:
#block
/// Generated by [tengri_proc].
///
/// Makes [#self_ty] able to construct the [Render]able
/// which might correspond to a given [TokenStream],
/// while taking [#self_ty]'s state into consideration.
impl<'state: 'static> Take<'state, #self_ty> for Box<dyn Render<#output> + 'state> {
fn take <'source: 'state> (state: &#self_ty, mut words: TokenIter<'source>)
-> Perhaps<Box<dyn Render<#output> + 'state>>
{
//let state = self;
impl<'source, 'state: 'source>
Take<'state, 'source, #self_ty>
for Box<dyn Render<#output> + 'state>
{
fn take (state: &'state #self_ty, mut words: Cst<'source>) -> Perhaps<Self> {
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
match value {
#(::tengri::dsl::Value::Exp(_, expr) => {
Give::<'state, #builtin>::give(state, expr)?.map(|value|value.boxed())
},)*
#(
#exposed,
)*
_ => None
}
match value { #(#builtin)* #(#exposed)* _ => None }
} else {
None
})
}
}
/// Generated by [tengri_proc].
///
/// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,
/// which you will need to implement, e.g. passing a [TokenIter]
/// containing a layout and keybindings config from user dirs.
impl ::tengri::output::Content<#output> for #self_ty {
fn content (&self) -> impl Render<#output> + '_ {
#self_ty::view(self)
}
}
// Original user-taked implementation:
#block
})
}
}
@ -96,11 +84,11 @@ fn builtins_with_holes () -> impl Iterator<Item=TokenStream2> {
}
fn builtins_with_boxes () -> impl Iterator<Item=TokenStream2> {
builtins_with(quote! { _ }, quote! { Box<dyn Render<_>+'state> })
builtins_with(quote! { _ }, quote! { Box<dyn Render<_>> })
}
fn builtins_with_boxes_output (o: TokenStream2) -> impl Iterator<Item=TokenStream2> {
builtins_with(quote! { _ }, quote! { Box<dyn Render<#o>+'state> })
builtins_with(quote! { _ }, quote! { Box<dyn Render<#o>> })
}
fn builtins_with (n: TokenStream2, c: TokenStream2) -> impl Iterator<Item=TokenStream2> {