mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
parent
3e1084555b
commit
31e84bf5b3
3 changed files with 55 additions and 68 deletions
|
|
@ -1,10 +1,17 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
// maybe their names should be switched around?
|
///// Implement the [Give] trait, which boils down to
|
||||||
|
///// specifying two types and providing an expression.
|
||||||
|
#[macro_export] macro_rules! from_dsl {
|
||||||
|
($Type:ty: |$state:ident:$State:ty, $words:ident|$expr:expr) => {
|
||||||
|
take! { $Type|$state:$State,$words|$expr }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// [Take]s instances of [Type] given [TokenIter].
|
/// [Take]s instances of [Type] given [TokenIter].
|
||||||
pub trait Give<'state, Type> {
|
pub trait Give<'state, Type> {
|
||||||
/// Implement this to be able to [Give] [Type] from the [TokenIter].
|
/// Implement this to be able to [Give] [Type] from the [TokenIter].
|
||||||
|
/// Advance the stream if returning `Ok<Some<Type>>`.
|
||||||
fn give <'source: 'state> (&self, words: TokenIter<'source>) -> Perhaps<Type>;
|
fn give <'source: 'state> (&self, words: TokenIter<'source>) -> Perhaps<Type>;
|
||||||
/// Return custom error on [None].
|
/// Return custom error on [None].
|
||||||
fn give_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
fn give_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||||
|
|
@ -59,6 +66,8 @@ pub trait Give<'state, Type> {
|
||||||
}
|
}
|
||||||
/// [Give]s instances of [Self] given [TokenIter].
|
/// [Give]s instances of [Self] given [TokenIter].
|
||||||
pub trait Take<'state, State>: Sized {
|
pub trait Take<'state, State>: Sized {
|
||||||
|
/// Implement this to be able to [Take] [Self] from the [TokenIter].
|
||||||
|
/// Advance the stream if returning `Ok<Some<Self>>`.
|
||||||
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self>;
|
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self>;
|
||||||
/// Return custom error on [None].
|
/// Return custom error on [None].
|
||||||
fn take_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
fn take_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||||
|
|
@ -113,40 +122,6 @@ pub trait Take<'state, State>: Sized {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature="dsl")]
|
|
||||||
impl<'state, E: 'state, State: Give<'state, Box<dyn Render<E> + 'state>>>
|
|
||||||
Take<'state, State> for Box<dyn Render<E> + 'state> {
|
|
||||||
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
|
|
||||||
state.give(words)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Implement the [Give] trait, which boils down to
|
|
||||||
/// specifying two types and providing an expression.
|
|
||||||
#[macro_export] macro_rules! from_dsl {
|
|
||||||
(@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
|
|
||||||
impl<'state, State, A: Take<'state, State>> Take<'state, State> for $T {
|
|
||||||
fn take <'source: 'state> ($state: &State, mut $words:TokenIter<'source>) -> Perhaps<$T> {
|
|
||||||
$expr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
(@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
|
|
||||||
impl<'state, State, A: Take<'state, State>, B: Take<'state, State>> Take<'state, State> for $T {
|
|
||||||
fn take <'source: 'state> ($state: &State, mut $words:TokenIter<'source>) -> Perhaps<$T> {
|
|
||||||
$expr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => {
|
|
||||||
impl<'state> Take<'state, $S> for $T {
|
|
||||||
fn take <'source: 'state> ($state: &$S, mut $words:TokenIter<'source>) -> Perhaps<$T> {
|
|
||||||
$expr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// auto impl graveyard:
|
// auto impl graveyard:
|
||||||
|
|
||||||
//impl<'state, State: Give<Type>, Type: 'state> Take<'state, State> for Type {
|
//impl<'state, State: Give<Type>, Type: 'state> Take<'state, State> for Type {
|
||||||
|
|
@ -157,8 +132,16 @@ Take<'state, State> for Box<dyn Render<E> + 'state> {
|
||||||
//}
|
//}
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//impl<'state, Type: Take<'state, State>, State> Give<Type> for State {
|
//#[cfg(feature="dsl")]
|
||||||
//fn take <'state> (&self, mut words:TokenIter<'source>) -> Perhaps<Type> {
|
//impl<'state, E: 'state, State: Give<'state, Box<dyn Render<E> + 'state>>>
|
||||||
//Type::take(self, words)
|
//Take<'state, State> for Box<dyn Render<E> + 'state> {
|
||||||
|
//fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
|
||||||
|
//state.give(words)
|
||||||
//}
|
//}
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
impl<'state, Type: Take<'state, State>, State> Give<'state, Type> for State {
|
||||||
|
fn give <'source: 'state> (&self, words: TokenIter<'source>) -> Perhaps<Type> {
|
||||||
|
Type::take(self, words)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -84,14 +84,6 @@ impl ToTokens for ExposeImpl {
|
||||||
quote! { Some(::tengri::dsl::Value::Sym(#key)) => state.#value(), }
|
quote! { Some(::tengri::dsl::Value::Sym(#key)) => state.#value(), }
|
||||||
});
|
});
|
||||||
write_quote_to(out, quote! {
|
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].
|
/// Generated by [tengri_proc::expose].
|
||||||
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
|
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
|
||||||
fn take <'source: 'n> (
|
fn take <'source: 'n> (
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ impl ToTokens for ViewDef {
|
||||||
let self_ty = &block.self_ty;
|
let self_ty = &block.self_ty;
|
||||||
// Expressions are handled by built-in functions
|
// Expressions are handled by built-in functions
|
||||||
// that operate over constants and symbols.
|
// that operate over constants and symbols.
|
||||||
let builtin = builtins_with_types().map(|ty|write_quote(quote! { #ty }));
|
let builtin = builtins_with_boxes_output(quote! { #output });
|
||||||
// Symbols are handled by user-taked functions
|
// Symbols are handled by user-taked functions
|
||||||
// that take no parameters but `&self`.
|
// that take no parameters but `&self`.
|
||||||
let exposed = exposed.iter().map(|(key, value)|write_quote(quote! {
|
let exposed = exposed.iter().map(|(key, value)|write_quote(quote! {
|
||||||
|
|
@ -52,18 +52,18 @@ impl ToTokens for ViewDef {
|
||||||
write_quote_to(out, quote! {
|
write_quote_to(out, quote! {
|
||||||
/// Generated by [tengri_proc].
|
/// Generated by [tengri_proc].
|
||||||
///
|
///
|
||||||
/// Gives [#self_ty] the ability to construct the [Render]able
|
/// Makes [#self_ty] able to construct the [Render]able
|
||||||
/// which might corresponds to a given [TokenStream],
|
/// which might correspond to a given [TokenStream],
|
||||||
/// while taking [#self_ty]'s state into consideration.
|
/// while taking [#self_ty]'s state into consideration.
|
||||||
impl<'state> Give<'state, Box<dyn Render<#output> + 'state>> for #self_ty {
|
impl<'state: 'static> Take<'state, #self_ty> for Box<dyn Render<#output> + 'state> {
|
||||||
fn give <'source: 'state> (&self, mut words: TokenIter<'source>)
|
fn take <'source: 'state> (state: &#self_ty, mut words: TokenIter<'source>)
|
||||||
-> Perhaps<Box<dyn Render<#output> + 'state>>
|
-> Perhaps<Box<dyn Render<#output> + 'state>>
|
||||||
{
|
{
|
||||||
let state = self;
|
//let state = self;
|
||||||
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
|
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
|
||||||
match value {
|
match value {
|
||||||
#(::tengri::dsl::Value::Exp(_, expr) => {
|
#(::tengri::dsl::Value::Exp(_, expr) => {
|
||||||
#builtin::take(state, expr)?.map(|value|value.boxed())
|
Give::<'state, #builtin>::give(state, expr)?.map(|value|value.boxed())
|
||||||
},)*
|
},)*
|
||||||
#(
|
#(
|
||||||
#exposed,
|
#exposed,
|
||||||
|
|
@ -91,21 +91,33 @@ impl ToTokens for ViewDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn builtins_with_types () -> impl Iterator<Item=TokenStream2> {
|
fn builtins_with_holes () -> impl Iterator<Item=TokenStream2> {
|
||||||
|
builtins_with(quote! { _ }, quote! { _ })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn builtins_with_boxes () -> impl Iterator<Item=TokenStream2> {
|
||||||
|
builtins_with(quote! { _ }, quote! { Box<dyn Render<_>+'state> })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn builtins_with_boxes_output (o: TokenStream2) -> impl Iterator<Item=TokenStream2> {
|
||||||
|
builtins_with(quote! { _ }, quote! { Box<dyn Render<#o>+'state> })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn builtins_with (n: TokenStream2, c: TokenStream2) -> impl Iterator<Item=TokenStream2> {
|
||||||
[
|
[
|
||||||
quote! { When::< Box<dyn Render<_>> > },
|
quote! { When::< #c > },
|
||||||
quote! { Either::< Box<dyn Render<_>>, Box<dyn Render<_>>> },
|
quote! { Either::< #c, #c> },
|
||||||
quote! { Align::< Box<dyn Render<_>> > },
|
quote! { Align::< #c > },
|
||||||
quote! { Bsp::< Box<dyn Render<_>>, Box<dyn Render<_>>> },
|
quote! { Bsp::< #c, #c> },
|
||||||
quote! { Fill::< Box<dyn Render<_>> > },
|
quote! { Fill::< #c > },
|
||||||
quote! { Fixed::<_, Box<dyn Render<_>> > },
|
quote! { Fixed::<#n, #c > },
|
||||||
quote! { Min::<_, Box<dyn Render<_>> > },
|
quote! { Min::<#n, #c > },
|
||||||
quote! { Max::<_, Box<dyn Render<_>> > },
|
quote! { Max::<#n, #c > },
|
||||||
quote! { Shrink::<_, Box<dyn Render<_>> > },
|
quote! { Shrink::<#n, #c > },
|
||||||
quote! { Expand::<_, Box<dyn Render<_>> > },
|
quote! { Expand::<#n, #c > },
|
||||||
quote! { Push::<_, Box<dyn Render<_>> > },
|
quote! { Push::<#n, #c > },
|
||||||
quote! { Pull::<_, Box<dyn Render<_>> > },
|
quote! { Pull::<#n, #c > },
|
||||||
quote! { Margin::<_, Box<dyn Render<_>> > },
|
quote! { Margin::<#n, #c > },
|
||||||
quote! { Padding::<_, Box<dyn Render<_>> > },
|
quote! { Padding::<#n, #c > },
|
||||||
].into_iter()
|
].into_iter()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue