mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
wip: dsl, output, input, proc, tui: sorting out give and take
Some checks are pending
/ build (push) Waiting to run
Some checks are pending
/ build (push) Waiting to run
This commit is contained in:
parent
5a2177cc77
commit
3e1084555b
10 changed files with 273 additions and 301 deletions
|
|
@ -3,117 +3,144 @@ use crate::*;
|
|||
// maybe their names should be switched around?
|
||||
|
||||
/// [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].
|
||||
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].
|
||||
fn give_or_fail <'source, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||
&self, words: &mut TokenIter<'source>, error: F
|
||||
fn give_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||
&self, mut words: TokenIter<'source>, error: F
|
||||
) -> Usually<Type> {
|
||||
let next = words.peek().map(|x|x.value).clone();
|
||||
if let Some(value) = Give::<Type>::give(self, words)? {
|
||||
Ok(value)
|
||||
} 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 {
|
||||
() => {
|
||||
impl<'n, Type: Take<'n, State>, State> Give<Type> for State {
|
||||
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
|
||||
impl<'state, Type: Take<'state, State>, State> Give<'state, Type> for State {
|
||||
fn give <'source: 'state> (&self, mut words: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>> {
|
||||
//impl<'state, T, Type: Take<'state, Box<T>>> Give<'state, Box<T>> for Box<Type> {
|
||||
//fn give (&self, mut words: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> {
|
||||
impl<'state, $Type: Take<'state, $State>> Give<'state, $Type> for $State {
|
||||
fn give <'source: 'state> (&self, mut words: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> {
|
||||
impl<'state> Give<'state, $Type> for $State {
|
||||
fn give <'source: 'state> (&self, mut $words:TokenIter<'source>) -> Perhaps<$Type> {
|
||||
let $state = self;
|
||||
$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
|
||||
/// specifying two types and providing an expression.
|
||||
#[macro_export] macro_rules! from_dsl {
|
||||
(@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
|
||||
impl<'n, State, A: Take<'n, State>> Take<'n, State> for $T {
|
||||
fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
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<'n, State, A: Take<'n, State>, B: Take<'n, State>> Take<'n, State> for $T {
|
||||
fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
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<'n> Take<'n, $S> for $T {
|
||||
fn take <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> {
|
||||
impl<'state> Take<'state, $S> for $T {
|
||||
fn take <'source: 'state> ($state: &$S, mut $words:TokenIter<'source>) -> Perhaps<$T> {
|
||||
$expr
|
||||
}
|
||||
}
|
||||
|
|
@ -122,16 +149,16 @@ pub trait Take<'n, State>: Sized + 'n {
|
|||
|
||||
// auto impl graveyard:
|
||||
|
||||
//impl<'n, State: Give<Type>, Type: 'n> Take<'n, State> for Type {
|
||||
//fn take <'source> (state: &State, words: &mut TokenIter<'source>)
|
||||
//impl<'state, State: Give<Type>, Type: 'state> Take<'state, State> for Type {
|
||||
//fn take <'state> (state: &State, mut words:TokenIter<'source>)
|
||||
//-> Perhaps<Self>
|
||||
//{
|
||||
//state.take(words)
|
||||
//}
|
||||
//}
|
||||
|
||||
//impl<'n, Type: Take<'n, State>, State> Give<Type> for State {
|
||||
//fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
|
||||
//impl<'state, Type: Take<'state, State>, State> Give<Type> for State {
|
||||
//fn take <'state> (&self, mut words:TokenIter<'source>) -> Perhaps<Type> {
|
||||
//Type::take(self, words)
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
Some(Token { value: Value::Sym(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))
|
||||
}
|
||||
}
|
||||
|
|
@ -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() {
|
||||
Some(Token { value: Value::Sym(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))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,32 +31,38 @@ 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")]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"|"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")?;
|
||||
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!()
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'state, State: Give<'state, A>, A: 'state> Take<'state, State> for Align<A> {
|
||||
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
|
||||
todo!()
|
||||
}
|
||||
},
|
||||
_ => return Ok(None)
|
||||
},
|
||||
_ => return Ok(None)
|
||||
})));
|
||||
}
|
||||
//give!(Align<A>, A|state, 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"|"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> {
|
||||
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
|
||||
|
|
|
|||
|
|
@ -20,10 +20,7 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
|
|||
}
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
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 {
|
||||
#[cfg(feature = "dsl")] take!(Bsp<A, B>, A, B|state, words|Ok(if let Some(Token {
|
||||
value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"),
|
||||
..
|
||||
}) = 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 {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
}));
|
||||
impl<A, B> Bsp<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) }
|
||||
|
|
|
|||
|
|
@ -8,21 +8,18 @@ impl<A, B> Either<A, B> {
|
|||
Self(c, a, b)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'n, State: Give<bool> + Give<A> + Give<B>, A: 'n, B: 'n> Take<'n, State> for Either<A, B> {
|
||||
fn take <'source> (state: &State, token: &mut TokenIter<'source>) -> Perhaps<Self> {
|
||||
if let Some(Token { value: Value::Key("either"), .. }) = token.peek() {
|
||||
let base = token.clone();
|
||||
let _ = token.next().unwrap();
|
||||
#[cfg(feature = "dsl")] take!(Either<A, B>, A, B|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key("either"), .. }) = words.peek() {
|
||||
let base = words.clone();
|
||||
let _ = words.next().unwrap();
|
||||
return Ok(Some(Self(
|
||||
state.give_or_fail(token, ||"either: no condition")?,
|
||||
state.give_or_fail(token, ||"either: no content 1")?,
|
||||
state.give_or_fail(token, ||"either: no content 2")?,
|
||||
state.give_or_fail(words, ||"either: no condition")?,
|
||||
state.give_or_fail(words, ||"either: no content 1")?,
|
||||
state.give_or_fail(words, ||"either: no content 2")?,
|
||||
)))
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
let Self(cond, a, b) = self;
|
||||
|
|
|
|||
|
|
@ -32,26 +32,19 @@ macro_rules! transform_xy {
|
|||
#[inline] pub const fn y (item: A) -> Self { Self::Y(item) }
|
||||
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'n, A: 'n, T: Give<A>> Take<'n, T> for $Enum<A> {
|
||||
fn take <'source> (state: &T, token: &mut TokenIter<'source>)
|
||||
-> Perhaps<Self>
|
||||
{
|
||||
if let Some(Token { value: Value::Key(k), .. }) = token.peek() {
|
||||
let mut base = token.clone();
|
||||
return Ok(Some(match token.next() {
|
||||
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")?),
|
||||
#[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key(k), .. }) = words.peek() {
|
||||
let mut base = words.clone();
|
||||
let content = state.give_or_fail(words, ||format!("{k}: no content"))?;
|
||||
return Ok(Some(match words.next() {
|
||||
Some(Token{value: Value::Key($x),..}) => Self::x(content),
|
||||
Some(Token{value: Value::Key($y),..}) => Self::y(content),
|
||||
Some(Token{value: Value::Key($xy),..}) => Self::xy(content),
|
||||
_ => unreachable!()
|
||||
}))
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}));
|
||||
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
|
||||
fn content (&self) -> impl Render<E> + '_ {
|
||||
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 xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
|
||||
}
|
||||
#[cfg(feature = "dsl")]
|
||||
impl<'n, A: 'n, U: Coordinate + 'n, T: Give<A> + Give<U>> Take<'n, T> for $Enum<U, A> {
|
||||
fn take <'source> (
|
||||
state: &T, token: &mut TokenIter<'source>
|
||||
) -> Perhaps<Self> {
|
||||
Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = token.peek() {
|
||||
let mut base = token.clone();
|
||||
Some(match token.next() {
|
||||
#[cfg(feature = "dsl")] take!($Enum<U, A>, U, A|state, words|Ok(
|
||||
if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = words.peek() {
|
||||
let mut base = words.clone();
|
||||
Some(match words.next() {
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::x(
|
||||
state.give_or_fail(token, ||"x: no unit")?,
|
||||
state.give_or_fail(token, ||"x: no content")?,
|
||||
state.give_or_fail(words, ||"x: no unit")?,
|
||||
state.give_or_fail(words, ||"x: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
||||
state.give_or_fail(token, ||"y: no unit")?,
|
||||
state.give_or_fail(token, ||"y: no content")?,
|
||||
state.give_or_fail(words, ||"y: no unit")?,
|
||||
state.give_or_fail(words, ||"y: no content")?,
|
||||
),
|
||||
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
||||
state.give_or_fail(token, ||"xy: no unit x")?,
|
||||
state.give_or_fail(token, ||"xy: no unit y")?,
|
||||
state.give_or_fail(token, ||"xy: no content")?
|
||||
state.give_or_fail(words, ||"xy: no unit x")?,
|
||||
state.give_or_fail(words, ||"xy: no unit y")?,
|
||||
state.give_or_fail(words, ||"xy: no content")?
|
||||
),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
}));
|
||||
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 {
|
||||
$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 {
|
||||
match self {
|
||||
Self::X(x, _) => *x, Self::Y(_, _) => 0.into(), Self::XY(x, _, _) => *x,
|
||||
}
|
||||
use $Enum::*;
|
||||
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
|
||||
}
|
||||
#[inline] pub fn dy (&self) -> U {
|
||||
match self {
|
||||
Self::X(_, _) => 0.into(), Self::Y(y, _) => *y, Self::XY(_, y, _) => *y,
|
||||
}
|
||||
use $Enum::*;
|
||||
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,33 +1,22 @@
|
|||
use crate::*;
|
||||
|
||||
/// Show an item only when a condition is true.
|
||||
pub struct When<A>(pub bool, pub A);
|
||||
|
||||
impl<A> When<A> {
|
||||
/// Create a binary condition.
|
||||
pub const fn new (c: bool, a: A) -> Self {
|
||||
Self(c, a)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "dsl")]
|
||||
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() {
|
||||
#[cfg(feature = "dsl")]take!(When<A>, A|state, words|Ok(Some(match words.peek() {
|
||||
Some(Token { value: Value::Key("when"), .. }) => {
|
||||
let _ = words.next();
|
||||
let base = words.clone();
|
||||
return Ok(Some(Self(
|
||||
state.give_or_fail(words, ||"cond: no condition")?,
|
||||
A::take_or_fail(state, words, ||"cond: no content")?,
|
||||
)))
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
let cond = state.give_or_fail(words, ||"cond: no condition")?;
|
||||
let cont = state.give_or_fail(words, ||"cond: no content")?;
|
||||
Self(cond, cont)
|
||||
},
|
||||
_ => return Ok(None)
|
||||
})));
|
||||
|
||||
impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
||||
fn layout (&self, to: E::Area) -> E::Area {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue