mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
This commit is contained in:
parent
cbd28a5934
commit
5a2177cc77
4 changed files with 131 additions and 94 deletions
|
|
@ -17,13 +17,6 @@ pub trait Give<Type> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'n, T: Take<'n, U>, U> Give<T> for U {
|
||||
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<T> {
|
||||
T::take(self, words)
|
||||
}
|
||||
}
|
||||
|
||||
/// [Give]s instances of [Self] given [TokenIter].
|
||||
pub trait Take<'n, State>: Sized + 'n {
|
||||
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self>;
|
||||
|
|
@ -39,6 +32,68 @@ pub trait Take<'n, State>: Sized + 'n {
|
|||
}
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! take {
|
||||
() => {
|
||||
impl<'n, Type: 'n, State: Give<Type> + 'n> Take<'n, State> for Type {
|
||||
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
state.give(words)
|
||||
}
|
||||
}
|
||||
};
|
||||
(box) => {
|
||||
impl<'n, T, State: Give<Box<T>> + 'n> Take<'n, State> for Box<T> {
|
||||
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
state.give(words)
|
||||
}
|
||||
}
|
||||
};
|
||||
($Type:ty:$State:ty) => {
|
||||
impl<'n> Take<'n, $State> for $Type {
|
||||
fn take <'source> (state: &$State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
state.give(words)
|
||||
}
|
||||
}
|
||||
};
|
||||
($Type:path$(,$Arg:ident)*|$state:ident:$State:path,$words:ident|$expr:expr) => {
|
||||
impl<'n, State: $State + 'n $(, $Arg: 'n)*> Take<'n, State> for $Type {
|
||||
fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
$expr
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
#[macro_export] macro_rules! give {
|
||||
() => {
|
||||
impl<'n, Type: Take<'n, State>, State> Give<Type> for State {
|
||||
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
|
||||
Type::take(self, words)
|
||||
}
|
||||
}
|
||||
};
|
||||
(box) => {
|
||||
//impl<'n, T, Type: Take<'n, Box<T>>> Give<Box<T>> for Box<Type> {
|
||||
//fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Box<Type>> {
|
||||
//Type::take(self, words)
|
||||
//}
|
||||
//}
|
||||
};
|
||||
($Type:ty: $State:ty) => {
|
||||
impl<'n, $Type: Take<'n, $State>> Give<$Type> for $State {
|
||||
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<$Type> {
|
||||
$Type::take(self, words)
|
||||
}
|
||||
}
|
||||
};
|
||||
($Type:ty|$state:ident:$State:ident,$words:ident|$expr:expr) => {
|
||||
impl Give<$Type> for $State {
|
||||
fn give <'source> (&self, $words: &mut TokenIter<'source>) -> Perhaps<$Type> {
|
||||
let $state = self;
|
||||
$expr
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Implement the [Give] trait, which boils down to
|
||||
/// specifying two types and providing an expression.
|
||||
#[macro_export] macro_rules! from_dsl {
|
||||
|
|
|
|||
|
|
@ -27,25 +27,18 @@
|
|||
//! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]);
|
||||
//! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
|
||||
//! ```
|
||||
|
||||
use crate::*;
|
||||
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
|
||||
|
||||
pub struct Align<A>(Alignment, A);
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'n, State: Give<A>, A: 'n> Take<'n, State> for Align<A> {
|
||||
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
if let Some(Token { value: Value::Key(key), .. }) = words.peek() {
|
||||
match key {
|
||||
#[cfg(feature = "dsl")]take!(Align<A>, A|state: Give<A>, words|Ok(Some(match words.peek() {
|
||||
Some(Token { value: Value::Key(key), .. }) => match key {
|
||||
"align/c"|"align/x"|"align/y"|
|
||||
"align/n"|"align/s"|"align/e"|"align/w"|
|
||||
"align/n"|"align/s"|"align/e"|"al;qign/w"|
|
||||
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
|
||||
let _ = words.next().unwrap();
|
||||
let content = state.give_or_fail(&mut words.clone(), ||"expected content")?;
|
||||
return Ok(Some(match key {
|
||||
match key {
|
||||
"align/c" => Self::c(content),
|
||||
"align/x" => Self::x(content),
|
||||
"align/y" => Self::y(content),
|
||||
|
|
@ -58,14 +51,12 @@ impl<'n, State: Give<A>, A: 'n> Take<'n, State> for Align<A> {
|
|||
"align/sw" => Self::sw(content),
|
||||
"align/se" => Self::se(content),
|
||||
_ => unreachable!()
|
||||
}))
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
_ => return Ok(None)
|
||||
},
|
||||
_ => return Ok(None)
|
||||
})));
|
||||
|
||||
impl<A> Align<A> {
|
||||
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
|
||||
|
|
|
|||
|
|
@ -121,11 +121,7 @@ impl ToTokens for ExposeArm {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<LitStr> for ExposeSym {
|
||||
fn from (this: LitStr) -> Self {
|
||||
Self(this)
|
||||
}
|
||||
}
|
||||
impl From<LitStr> for ExposeSym { fn from (this: LitStr) -> Self { Self(this) } }
|
||||
|
||||
impl PartialOrd for ExposeSym {
|
||||
fn partial_cmp (&self, other: &Self) -> Option<Ordering> {
|
||||
|
|
@ -153,11 +149,7 @@ impl PartialEq for ExposeSym {
|
|||
|
||||
impl Eq for ExposeSym {}
|
||||
|
||||
impl From<Type> for ExposeType {
|
||||
fn from (this: Type) -> Self {
|
||||
Self(Box::new(this))
|
||||
}
|
||||
}
|
||||
impl From<Type> for ExposeType { fn from (this: Type) -> Self { Self(Box::new(this)) } }
|
||||
|
||||
impl PartialOrd for ExposeType {
|
||||
fn partial_cmp (&self, other: &Self) -> Option<Ordering> {
|
||||
|
|
|
|||
|
|
@ -4,9 +4,7 @@ use crate::*;
|
|||
pub(crate) struct ViewDef(pub(crate) ViewMeta, pub(crate) ViewImpl);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ViewMeta {
|
||||
pub(crate) output: Ident,
|
||||
}
|
||||
pub(crate) struct ViewMeta { pub(crate) output: Ident }
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ViewImpl {
|
||||
|
|
@ -43,20 +41,18 @@ impl ToTokens for ViewDef {
|
|||
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||
let Self(ViewMeta { output }, ViewImpl { block, exposed }) = self;
|
||||
let self_ty = &block.self_ty;
|
||||
let builtins: Vec<_> = builtins_with_types()
|
||||
.iter()
|
||||
.map(|ty|write_quote(quote! {
|
||||
let value: Option<#ty> = Give::<#ty>::give(self, &mut exp.clone())?;
|
||||
if let Some(value) = value {
|
||||
return Ok(Some(value.boxed()))
|
||||
}
|
||||
}))
|
||||
.collect();
|
||||
let exposed: Vec<_> = exposed
|
||||
.iter()
|
||||
.map(|(key, value)|write_quote(quote! {
|
||||
#key => {
|
||||
Some(Box::new(Thunk::new(move||#self_ty::#value(self))))//Box::new("todo"))
|
||||
// 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();
|
||||
// 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();
|
||||
write_quote_to(out, quote! {
|
||||
|
|
@ -65,31 +61,34 @@ impl ToTokens for ViewDef {
|
|||
/// 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.
|
||||
impl ::tengri::dsl::Give<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
|
||||
})
|
||||
}
|
||||
}
|
||||
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
|
||||
//})
|
||||
//}
|
||||
//}
|
||||
/// Generated by [tengri_proc].
|
||||
///
|
||||
/// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue