mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-08 12:46: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)
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue