extact dsl_token; flip Dsl; try to obviate ViewContext

This commit is contained in:
🪞👃🪞 2025-05-20 16:27:05 +03:00
parent f08593f0f8
commit f797a7143d
12 changed files with 264 additions and 209 deletions

View file

@ -1,12 +1,49 @@
use crate::*;
pub trait Dsl<State>: Sized {
fn take_from <'state, 'source: 'state> (state: &'state State, _: &mut TokenIter<'source>)
-> Perhaps<Self>
{
unimplemented!()
}
fn take_from_or_fail <'state, 'source: 'state> (
state: &'state State,
token: &mut TokenIter<'source>,
error: impl Into<Box<dyn std::error::Error>>
) -> Usually<Self> {
if let Some(value) = Dsl::<State>::take_from(state, token)? {
Ok(value)
} else {
Result::Err(error.into())
}
}
}
impl<T, U: Dsl<T>> DslFrom<U> for T {}
pub trait DslFrom<T: Dsl<Self>>: Sized {
fn take <'state, 'source: 'state> (&'state self, token: &mut TokenIter<'source>)
-> Perhaps<T>
{
T::take_from(self, token)
}
fn take_or_fail <'state, 'source: 'state> (
&'state self,
token: &mut TokenIter<'source>,
error: impl Into<Box<dyn std::error::Error>>
) -> Usually<T> {
T::take_from_or_fail(self, token, error)
}
}
/// Implement the [Dsl] trait, which boils down to
/// specifying two types and providing an expression.
#[macro_export] macro_rules! dsl {
($T:ty: |$self:ident:$S:ty, $iter:ident|$expr:expr) => {
impl ::tengri::dsl::Dsl<$T> for $S {
fn take <'state, 'source> (
&'state $self, $iter: &mut ::tengri::dsl::TokenIter<'source>,
impl ::tengri::dsl::Dsl<$S> for $T {
fn take_from <'state, 'source: 'state> (
state: &'state $S,
$iter: &mut ::tengri::dsl::TokenIter<'source>,
) -> ::tengri::Perhaps<$T> {
$expr
}
@ -14,43 +51,24 @@ use crate::*;
}
}
/// Maps a sequencer of EDN tokens to parameters of supported types
/// for a given context.
pub trait Dsl<Value>: Sized {
fn take <'state, 'source> (&'state self, _: &mut TokenIter<'source>) -> Perhaps<Value> {
unimplemented!()
}
fn take_or_fail <'state, 'source> (
&'state self,
token: &mut TokenIter<'source>,
error: impl Into<Box<dyn std::error::Error>>
) -> Usually<Value> {
if let Some(value) = Dsl::<Value>::take(self, token)? {
Ok(value)
} else {
Result::Err(error.into())
}
}
}
pub trait FromDsl<'state, State>: Sized {
fn take_from <'source: 'state> (state: &'state State, _token: &mut TokenIter<'source>)
-> Perhaps<Self>
{
unimplemented!()
}
fn take_from_or_fail <'source: 'state> (
state: &'state State,
token: &mut TokenIter<'source>,
error: impl Into<Box<dyn std::error::Error>>
) -> Usually<Self> {
if let Some(value) = FromDsl::<State>::take_from(state, token)? {
Ok(value)
} else {
Result::Err(error.into())
}
}
}
///// Maps a sequencer of EDN tokens to parameters of supported types
///// for a given context.
//pub trait Dsl<Value>: Sized {
//fn take <'state, 'source> (&'state self, _: &mut TokenIter<'source>) -> Perhaps<Value> {
//unimplemented!()
//}
//fn take_or_fail <'state, 'source> (
//&'state self,
//token: &mut TokenIter<'source>,
//error: impl Into<Box<dyn std::error::Error>>
//) -> Usually<Value> {
//if let Some(value) = Dsl::<Value>::take(self, token)? {
//Ok(value)
//} else {
//Result::Err(error.into())
//}
//}
//}
//impl<T: Dsl<U>, U> Dsl<U> for &T {
//fn take <'state, 'source> (&'state self, iter: &mut TokenIter<'source>) -> Perhaps<U> {
@ -64,8 +82,8 @@ pub trait FromDsl<'state, State>: Sized {
//}
//}
impl<'state, X, Y> Dsl<X> for Y where Y: FromDsl<'state, X> {
}
//impl<T, U: FromDsl<T>> Dsl<U> for T {
//impl<'state, X, Y> Dsl<X> for Y where Y: Dsl<'state, X> {
//}
//impl<T, U: Dsl<T>> Dsl<U> for T {
//}