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

@ -3,117 +3,144 @@ use crate::*;
// maybe their names should be switched around? // maybe their names should be switched around?
/// [Take]s instances of [Type] given [TokenIter]. /// [Take]s instances of [Type] given [TokenIter].
pub trait Give<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].
fn give <'source> (&self, words: &mut 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, 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> (
&self, words: &mut TokenIter<'source>, error: F &self, mut words: TokenIter<'source>, error: F
) -> Usually<Type> { ) -> Usually<Type> {
let next = words.peek().map(|x|x.value).clone();
if let Some(value) = Give::<Type>::give(self, words)? { if let Some(value) = Give::<Type>::give(self, words)? {
Ok(value) Ok(value)
} else { } else {
Result::Err(format!("give: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into()) Result::Err(format!("give: {}: {next:?}", error().into()).into())
} }
} }
} }
/// [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>;
/// Return custom error on [None].
fn take_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
state: &State, words: &mut TokenIter<'source>, error: F
) -> Usually<Self> {
if let Some(value) = Take::<State>::take(state, words)? {
Ok(value)
} else {
Result::Err(format!("take: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into())
}
}
}
#[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 { #[macro_export] macro_rules! give {
() => { () => {
impl<'n, Type: Take<'n, State>, State> Give<Type> for State { impl<'state, Type: Take<'state, State>, State> Give<'state, Type> for State {
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> { fn give <'source: 'state> (&self, mut words:TokenIter<'source>) -> Perhaps<Type> {
Type::take(self, words) Type::take(self, words)
} }
} }
}; };
(box) => { (box) => {
//impl<'n, T, Type: Take<'n, Box<T>>> Give<Box<T>> for Box<Type> { //impl<'state, T, Type: Take<'state, Box<T>>> Give<'state, Box<T>> for Box<Type> {
//fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Box<Type>> { //fn give (&self, mut words:TokenIter<'source>) -> Perhaps<Box<Type>> {
//Type::take(self, words) //Type::take(self, words)
//} //}
//} //}
}; };
($Type:ty: $State:ty) => { ($Type:ty: $State:ty) => {
impl<'n, $Type: Take<'n, $State>> Give<$Type> for $State { impl<'state, $Type: Take<'state, $State>> Give<'state, $Type> for $State {
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<$Type> { fn give <'source: 'state> (&self, mut words:TokenIter<'source>) -> Perhaps<$Type> {
$Type::take(self, words) $Type::take(self, words)
} }
} }
}; };
($Type:ty|$state:ident:$State:ident,$words:ident|$expr:expr) => { ($Type:ty|$state:ident:$State:ident,$words:ident|$expr:expr) => {
impl Give<$Type> for $State { impl<'state> Give<'state, $Type> for $State {
fn give <'source> (&self, $words: &mut TokenIter<'source>) -> Perhaps<$Type> { fn give <'source: 'state> (&self, mut $words:TokenIter<'source>) -> Perhaps<$Type> {
let $state = self; let $state = self;
$expr $expr
} }
} }
}; };
($Type:path$(,$Arg:ident)*|$state:ident,$words:ident|$expr:expr) => {
impl<'state, State: $(Give<'state, $Arg> +)* 'state $(, $Arg)*> Give<'state, $Type> for State {
fn give <'source: 'state> (&self, mut $words:TokenIter<'source>) -> Perhaps<$Type> {
let $state = self;
$expr
}
}
}
}
/// [Give]s instances of [Self] given [TokenIter].
pub trait Take<'state, State>: Sized {
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self>;
/// Return custom error on [None].
fn take_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
state: &State, mut words:TokenIter<'source>, error: F
) -> Usually<Self> {
let next = words.peek().map(|x|x.value).clone();
if let Some(value) = Take::<State>::take(state, words)? {
Ok(value)
} else {
Result::Err(format!("take: {}: {next:?}", error().into()).into())
}
}
}
#[macro_export] macro_rules! take {
() => {
impl<'state, Type: 'state, State: Give<'state, Type> + 'state> Take<'state, State> for Type {
fn take <'source: 'state> (state: &State, mut words:TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
(box) => {
impl<'state, T, State: Give<'state, Box<T>> + 'state> Take<'state, State> for Box<T> {
fn take <'source: 'state> (state: &State, mut words:TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
($Type:ty:$State:ty) => {
impl<'state> Take<'state, $State> for $Type {
fn take <'source: 'state> (state: &$State, mut words:TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
($Type:path$(,$Arg:ident)*|$state:ident,$words:ident|$expr:expr) => {
impl<'state,
State: Give<'state, bool> + $(Give<'state, $Arg> + )* 'state
$(, $Arg: Take<'state, State> + 'state)*
> Take<'state, State> for $Type {
fn take <'source: 'state> ($state: &State, mut $words:TokenIter<'source>) -> Perhaps<Self> {
$expr
}
}
};
($Type:path$(,$Arg:ident)*|$state:ident:$State:path,$words:ident|$expr:expr) => {
impl<'state $(, $Arg: 'state)*> Take<'state, $State> for $Type {
fn take <'source: 'state> ($state: &$State, mut $words:TokenIter<'source>) -> Perhaps<Self> {
$expr
}
}
};
}
#[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 /// Implement the [Give] trait, which boils down to
/// specifying two types and providing an expression. /// specifying two types and providing an expression.
#[macro_export] macro_rules! from_dsl { #[macro_export] macro_rules! from_dsl {
(@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => { (@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<'n, State, A: Take<'n, State>> Take<'n, State> for $T { impl<'state, State, A: Take<'state, State>> Take<'state, State> for $T {
fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> { fn take <'source: 'state> ($state: &State, mut $words:TokenIter<'source>) -> Perhaps<$T> {
$expr $expr
} }
} }
}; };
(@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => { (@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<'n, State, A: Take<'n, State>, B: Take<'n, State>> Take<'n, State> for $T { impl<'state, State, A: Take<'state, State>, B: Take<'state, State>> Take<'state, State> for $T {
fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> { fn take <'source: 'state> ($state: &State, mut $words:TokenIter<'source>) -> Perhaps<$T> {
$expr $expr
} }
} }
}; };
($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => { ($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => {
impl<'n> Take<'n, $S> for $T { impl<'state> Take<'state, $S> for $T {
fn take <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> { fn take <'source: 'state> ($state: &$S, mut $words:TokenIter<'source>) -> Perhaps<$T> {
$expr $expr
} }
} }
@ -122,16 +149,16 @@ pub trait Take<'n, State>: Sized + 'n {
// auto impl graveyard: // auto impl graveyard:
//impl<'n, State: Give<Type>, Type: 'n> Take<'n, State> for Type { //impl<'state, State: Give<Type>, Type: 'state> Take<'state, State> for Type {
//fn take <'source> (state: &State, words: &mut TokenIter<'source>) //fn take <'state> (state: &State, mut words:TokenIter<'source>)
//-> Perhaps<Self> //-> Perhaps<Self>
//{ //{
//state.take(words) //state.take(words)
//} //}
//} //}
//impl<'n, Type: Take<'n, State>, State> Give<Type> for State { //impl<'state, Type: Take<'state, State>, State> Give<Type> for State {
//fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> { //fn take <'state> (&self, mut words:TokenIter<'source>) -> Perhaps<Type> {
//Type::take(self, words) //Type::take(self, words)
//} //}
//} //}

View file

@ -22,7 +22,7 @@ impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for So
match exp_iter.next() { match exp_iter.next() {
Some(Token { value: Value::Sym(binding), .. }) => { Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) { if input.matches_dsl(binding) {
if let Some(command) = Take::take(state, &mut exp_iter)? { if let Some(command) = Take::take(state, exp_iter)? {
return Ok(Some(command)) return Ok(Some(command))
} }
} }
@ -48,7 +48,7 @@ impl<'k, S, C: Take<'k, S> + Command<S>, I: DslInput> KeyMap<'k, S, C, I> for To
match e.next() { match e.next() {
Some(Token { value: Value::Sym(binding), .. }) => { Some(Token { value: Value::Sym(binding), .. }) => {
if input.matches_dsl(binding) { if input.matches_dsl(binding) {
if let Some(command) = Take::take(state, &mut e)? { if let Some(command) = Take::take(state, e)? {
return Ok(Some(command)) return Ok(Some(command))
} }
} }

View file

@ -31,32 +31,38 @@ use crate::*;
#[derive(Debug, Copy, Clone, Default)] #[derive(Debug, Copy, Clone, Default)]
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W } pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A); pub struct Align<A>(Alignment, A);
#[cfg(feature = "dsl")]take!(Align<A>, A|state: Give<A>, words|Ok(Some(match words.peek() { #[cfg(feature = "dsl")]
Some(Token { value: Value::Key(key), .. }) => match key { impl<'state, State: Give<'state, A>, A: 'state> Take<'state, State> for Align<A> {
"align/c"|"align/x"|"align/y"| fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
"align/n"|"align/s"|"align/e"|"al;qign/w"| todo!()
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
let _ = words.next().unwrap();
let content = state.give_or_fail(&mut words.clone(), ||"expected content")?;
match key {
"align/c" => Self::c(content),
"align/x" => Self::x(content),
"align/y" => Self::y(content),
"align/n" => Self::n(content),
"align/s" => Self::s(content),
"align/e" => Self::e(content),
"align/w" => Self::w(content),
"align/nw" => Self::nw(content),
"align/ne" => Self::ne(content),
"align/sw" => Self::sw(content),
"align/se" => Self::se(content),
_ => unreachable!()
} }
}, }
_ => return Ok(None) //give!(Align<A>, A|state, words|Ok(Some(match words.peek() {
}, //Some(Token { value: Value::Key(key), .. }) => match key {
_ => return Ok(None) //"align/c"|"align/x"|"align/y"|
}))); //"align/n"|"align/s"|"align/e"|"al;qign/w"|
//"align/nw"|"align/sw"|"align/ne"|"align/se" => {
//let _ = words.next().unwrap();
//let content = Take::take_or_fail(state, &mut words.clone(), ||"expected content")?;
//match key {
//"align/c" => Self::c(content),
//"align/x" => Self::x(content),
//"align/y" => Self::y(content),
//"align/n" => Self::n(content),
//"align/s" => Self::s(content),
//"align/e" => Self::e(content),
//"align/w" => Self::w(content),
//"align/nw" => Self::nw(content),
//"align/ne" => Self::ne(content),
//"align/sw" => Self::sw(content),
//"align/se" => Self::se(content),
//_ => unreachable!()
//}
//},
//_ => return Ok(None)
//},
//_ => return Ok(None)
//})));
impl<A> Align<A> { impl<A> Align<A> {
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) } #[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }

View file

@ -20,10 +20,7 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
} }
} }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")] take!(Bsp<A, B>, A, B|state, words|Ok(if let Some(Token {
impl<'n, State: Give<A> + Give<B>, A: 'n, B: 'n> Take<'n, State> for Bsp<A, B> {
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
Ok(if let Some(Token {
value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"), value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"),
.. ..
}) = words.peek() { }) = words.peek() {
@ -45,9 +42,7 @@ impl<'n, State: Give<A> + Give<B>, A: 'n, B: 'n> Take<'n, State> for Bsp<A, B> {
} }
} else { } else {
None None
}) }));
}
}
impl<A, B> Bsp<A, B> { impl<A, B> Bsp<A, B> {
#[inline] pub const fn n (a: A, b: B) -> Self { Self(North, a, b) } #[inline] pub const fn n (a: A, b: B) -> Self { Self(North, a, b) }
#[inline] pub const fn s (a: A, b: B) -> Self { Self(South, a, b) } #[inline] pub const fn s (a: A, b: B) -> Self { Self(South, a, b) }

View file

@ -8,21 +8,18 @@ impl<A, B> Either<A, B> {
Self(c, a, b) Self(c, a, b)
} }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")] take!(Either<A, B>, A, B|state, words|Ok(
impl<'n, State: Give<bool> + Give<A> + Give<B>, A: 'n, B: 'n> Take<'n, State> for Either<A, B> { if let Some(Token { value: Value::Key("either"), .. }) = words.peek() {
fn take <'source> (state: &State, token: &mut TokenIter<'source>) -> Perhaps<Self> { let base = words.clone();
if let Some(Token { value: Value::Key("either"), .. }) = token.peek() { let _ = words.next().unwrap();
let base = token.clone();
let _ = token.next().unwrap();
return Ok(Some(Self( return Ok(Some(Self(
state.give_or_fail(token, ||"either: no condition")?, state.give_or_fail(words, ||"either: no condition")?,
state.give_or_fail(token, ||"either: no content 1")?, state.give_or_fail(words, ||"either: no content 1")?,
state.give_or_fail(token, ||"either: no content 2")?, state.give_or_fail(words, ||"either: no content 2")?,
))) )))
} } else {
Ok(None) None
} }));
}
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> { impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
fn layout (&self, to: E::Area) -> E::Area { fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, a, b) = self; let Self(cond, a, b) = self;

View file

@ -32,26 +32,19 @@ macro_rules! transform_xy {
#[inline] pub const fn y (item: A) -> Self { Self::Y(item) } #[inline] pub const fn y (item: A) -> Self { Self::Y(item) }
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) } #[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
impl<'n, A: 'n, T: Give<A>> Take<'n, T> for $Enum<A> { if let Some(Token { value: Value::Key(k), .. }) = words.peek() {
fn take <'source> (state: &T, token: &mut TokenIter<'source>) let mut base = words.clone();
-> Perhaps<Self> let content = state.give_or_fail(words, ||format!("{k}: no content"))?;
{ return Ok(Some(match words.next() {
if let Some(Token { value: Value::Key(k), .. }) = token.peek() { Some(Token{value: Value::Key($x),..}) => Self::x(content),
let mut base = token.clone(); Some(Token{value: Value::Key($y),..}) => Self::y(content),
return Ok(Some(match token.next() { Some(Token{value: Value::Key($xy),..}) => Self::xy(content),
Some(Token{value:Value::Key($x),..}) =>
Self::x(state.give_or_fail(token, ||"x: no content")?),
Some(Token{value:Value::Key($y),..}) =>
Self::y(state.give_or_fail(token, ||"y: no content")?),
Some(Token{value:Value::Key($xy),..}) =>
Self::xy(state.give_or_fail(token, ||"xy: no content")?),
_ => unreachable!() _ => unreachable!()
})) }))
} } else {
Ok(None) None
} }));
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> { impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
fn content (&self) -> impl Render<E> + '_ { fn content (&self) -> impl Render<E> + '_ {
match self { match self {
@ -78,56 +71,45 @@ macro_rules! transform_xy_unit {
#[inline] pub const fn y (y: U, item: A) -> Self { Self::Y(y, item) } #[inline] pub const fn y (y: U, item: A) -> Self { Self::Y(y, item) }
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) } #[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
} }
#[cfg(feature = "dsl")] #[cfg(feature = "dsl")] take!($Enum<U, A>, U, A|state, words|Ok(
impl<'n, A: 'n, U: Coordinate + 'n, T: Give<A> + Give<U>> Take<'n, T> for $Enum<U, A> { if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = words.peek() {
fn take <'source> ( let mut base = words.clone();
state: &T, token: &mut TokenIter<'source> Some(match words.next() {
) -> Perhaps<Self> {
Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = token.peek() {
let mut base = token.clone();
Some(match token.next() {
Some(Token { value: Value::Key($x), .. }) => Self::x( Some(Token { value: Value::Key($x), .. }) => Self::x(
state.give_or_fail(token, ||"x: no unit")?, state.give_or_fail(words, ||"x: no unit")?,
state.give_or_fail(token, ||"x: no content")?, state.give_or_fail(words, ||"x: no content")?,
), ),
Some(Token { value: Value::Key($y), .. }) => Self::y( Some(Token { value: Value::Key($y), .. }) => Self::y(
state.give_or_fail(token, ||"y: no unit")?, state.give_or_fail(words, ||"y: no unit")?,
state.give_or_fail(token, ||"y: no content")?, state.give_or_fail(words, ||"y: no content")?,
), ),
Some(Token { value: Value::Key($x), .. }) => Self::xy( Some(Token { value: Value::Key($x), .. }) => Self::xy(
state.give_or_fail(token, ||"xy: no unit x")?, state.give_or_fail(words, ||"xy: no unit x")?,
state.give_or_fail(token, ||"xy: no unit y")?, state.give_or_fail(words, ||"xy: no unit y")?,
state.give_or_fail(token, ||"xy: no content")? state.give_or_fail(words, ||"xy: no content")?
), ),
_ => unreachable!(), _ => unreachable!(),
}) })
} else { } else {
None None
}) }));
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> { impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
fn content (&self) -> impl Render<E> + '_ {
Some(match self {
Self::X(_, content) => content,
Self::Y(_, content) => content,
Self::XY(_, _, content) => content,
})
}
fn layout (&$self, $to: E::Area) -> E::Area { fn layout (&$self, $to: E::Area) -> E::Area {
$layout.into() $layout.into()
} }
fn content (&self) -> impl Render<E> + '_ {
use $Enum::*;
Some(match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, })
} }
impl<U: Copy + Coordinate, T> $Enum<U, T> { }
impl<U: Coordinate, T> $Enum<U, T> {
#[inline] pub fn dx (&self) -> U { #[inline] pub fn dx (&self) -> U {
match self { use $Enum::*;
Self::X(x, _) => *x, Self::Y(_, _) => 0.into(), Self::XY(x, _, _) => *x, match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
} }
#[inline] pub fn dy (&self) -> U { #[inline] pub fn dy (&self) -> U {
match self { use $Enum::*;
Self::X(_, _) => 0.into(), Self::Y(y, _) => *y, Self::XY(_, y, _) => *y, match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
} }
} }
} }

View file

@ -1,33 +1,22 @@
use crate::*; use crate::*;
/// Show an item only when a condition is true. /// Show an item only when a condition is true.
pub struct When<A>(pub bool, pub A); pub struct When<A>(pub bool, pub A);
impl<A> When<A> { impl<A> When<A> {
/// Create a binary condition. /// Create a binary condition.
pub const fn new (c: bool, a: A) -> Self { pub const fn new (c: bool, a: A) -> Self {
Self(c, a) Self(c, a)
} }
} }
#[cfg(feature = "dsl")]take!(When<A>, A|state, words|Ok(Some(match words.peek() {
#[cfg(feature = "dsl")] Some(Token { value: Value::Key("when"), .. }) => {
impl<'n, State: Give<bool>, A: Take<'n, State>> Take<'n, State> for When<A> {
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
Ok(if let Some(Token {
value: Value::Key("when"),
..
}) = words.peek() {
let _ = words.next(); let _ = words.next();
let base = words.clone(); let base = words.clone();
return Ok(Some(Self( let cond = state.give_or_fail(words, ||"cond: no condition")?;
state.give_or_fail(words, ||"cond: no condition")?, let cont = state.give_or_fail(words, ||"cond: no content")?;
A::take_or_fail(state, words, ||"cond: no content")?, Self(cond, cont)
))) },
} else { _ => return Ok(None)
None })));
})
}
}
impl<E: Output, A: Render<E>> Content<E> for When<A> { impl<E: Output, A: Render<E>> Content<E> for When<A> {
fn layout (&self, to: E::Area) -> E::Area { fn layout (&self, to: E::Area) -> E::Area {

View file

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

View file

@ -9,9 +9,6 @@ pub(crate) struct ExposeMeta;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct ExposeImpl(ItemImpl, BTreeMap<ExposeType, BTreeMap<String, Ident>>); pub(crate) struct ExposeImpl(ItemImpl, BTreeMap<ExposeType, BTreeMap<String, Ident>>);
#[derive(Debug, Clone)]
struct ExposeArm(String, Ident);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct ExposeSym(LitStr); struct ExposeSym(LitStr);
@ -82,13 +79,23 @@ impl ToTokens for ExposeImpl {
}, },
_ => quote! {}, _ => 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! { 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> ( fn take <'source: 'n> (
state: &#state, state: &#state, mut words: ::tengri::dsl::TokenIter<'source>
words: &mut ::tengri::dsl::TokenIter<'source>
) -> Perhaps<Self> { ) -> Perhaps<Self> {
Ok(Some(match words.next().map(|x|x.value) { Ok(Some(match words.next().map(|x|x.value) {
#predefined #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 From<LitStr> for ExposeSym { fn from (this: LitStr) -> Self { Self(this) } }
impl PartialOrd for ExposeSym { impl PartialOrd for ExposeSym {

View file

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