dsl: reduce number of lifetime arguments
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-21 02:50:21 +03:00
parent 2048dd2263
commit 776cea6f1b
9 changed files with 126 additions and 108 deletions

View file

@ -1,30 +1,29 @@
use crate::*;
pub trait Dsl<Other> {
fn take <'state, 'source: 'state> (
&'state self,
token: &mut TokenIter<'source>
)
-> Perhaps<Other>;
fn take_or_fail <'state, 'source: 'state> (
&'state self,
token: &mut TokenIter<'source>,
error: impl Into<Box<dyn std::error::Error>>
) -> Usually<Other> {
if let Some(value) = Dsl::<Other>::take(self, token)? {
pub trait Dsl<Type> {
fn take <'source> (&self, token: &mut TokenIter<'source>) -> Perhaps<Type>;
fn take_or_fail <'source> (
&self, token: &mut TokenIter<'source>, error: impl Into<Box<dyn std::error::Error>>
) -> Usually<Type> {
if let Some(value) = Dsl::<Type>::take(self, token)? {
Ok(value)
} else {
Result::Err(error.into())
}
}
}
pub trait Namespace<'source, State>: Sized {
fn take_from <'state> (state: &'state State, token: &mut TokenIter<'source>)
impl<Type: Namespace<State>, State> Dsl<Type> for State {
fn take <'source> (&self, token: &mut TokenIter<'source>) -> Perhaps<Type> {
Namespace::take_from(self, token)
}
}
pub trait Namespace<State>: Sized {
fn take_from <'source> (state: &State, token: &mut TokenIter<'source>)
-> Perhaps<Self>;
fn take_from_or_fail <'state> (
state: &'state State,
token: &mut TokenIter<'source>,
error: impl Into<Box<dyn std::error::Error>>
fn take_from_or_fail <'source> (
state: &State, token: &mut TokenIter<'source>, error: impl Into<Box<dyn std::error::Error>>
) -> Usually<Self> {
if let Some(value) = Namespace::<State>::take_from(state, token)? {
Ok(value)
@ -33,6 +32,7 @@ pub trait Namespace<'source, State>: Sized {
}
}
}
//impl<State: Dsl<T>, T> Namespace<State> for T {
//fn take_from <'state, 'source: 'state> (state: &'state State, token: &mut TokenIter<'source>)
//-> Perhaps<Self>
@ -45,9 +45,9 @@ pub trait Namespace<'source, State>: Sized {
/// specifying two types and providing an expression.
#[macro_export] macro_rules! from_dsl {
(@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<'source, State: Dsl<A>, A> Namespace<'source, State> for $T {
fn take_from <'state> (
$state: &'state State,
impl<State: Dsl<A>, A> Namespace<State> for $T {
fn take_from <'source> (
$state: &State,
$words: &mut TokenIter<'source>,
) -> Perhaps<$T> {
$expr
@ -55,9 +55,9 @@ pub trait Namespace<'source, State>: Sized {
}
};
(@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => {
impl<'source, State: Dsl<A> + Dsl<B>, A, B> Namespace<'source, State> for $T {
fn take_from <'state> (
$state: &'state State,
impl<State: Dsl<A> + Dsl<B>, A, B> Namespace<State> for $T {
fn take_from <'source> (
$state: &State,
$words: &mut TokenIter<'source>,
) -> Perhaps<$T> {
$expr
@ -65,9 +65,9 @@ pub trait Namespace<'source, State>: Sized {
}
};
($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => {
impl<'source> Namespace<'source, $S> for $T {
fn take_from <'state> (
$state: &'state $S,
impl Namespace<$S> for $T {
fn take_from <'source> (
$state: &$S,
$words: &mut TokenIter<'source>,
) -> Perhaps<$T> {
$expr