mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
This commit is contained in:
parent
b52c1f5828
commit
24ac52d807
10 changed files with 238 additions and 203 deletions
|
|
@ -17,12 +17,14 @@ mod dsl_conv; pub use self::dsl_conv::*;
|
|||
pub type DslResult<T> = Result<T, DslError>;
|
||||
/// DSL-specific optional result type.
|
||||
pub type DslPerhaps<T> = Result<Option<T>, DslError>;
|
||||
// Some things that can be DSL source:
|
||||
impl Dsl for String { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } }
|
||||
impl Dsl for Arc<str> { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } }
|
||||
impl<'s> Dsl for &'s str { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } }
|
||||
// Designates a string as parsable DSL.
|
||||
flex_trait!(Dsl: Debug + Send + Sync + Sized {
|
||||
fn src (&self) -> DslPerhaps<&str> { unreachable!("Dsl::src default impl") }
|
||||
});
|
||||
impl Dsl for Arc<str> { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } }
|
||||
impl<'s> Dsl for &'s str { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } }
|
||||
impl<D: Dsl> Dsl for Option<D> {
|
||||
fn src (&self) -> DslPerhaps<&str> {Ok(if let Some(dsl) = self { dsl.src()? } else { None })}
|
||||
}
|
||||
|
|
@ -45,12 +47,11 @@ impl<D: Dsl> DslExp for D {} pub trait DslExp: Dsl {
|
|||
fn exp (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(exp_peek_inner_only))}
|
||||
fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))}
|
||||
fn tail (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek_tail))}
|
||||
/// my other car is a cdr :<
|
||||
fn each (&self, mut cb: impl FnMut(&str)->Usually<()>) -> Usually<()> {
|
||||
Ok(if let Some(head) = self.head()? {
|
||||
println!("| HEAD ->\n{head}");
|
||||
cb(head)?;
|
||||
if let Some(tail) = self.tail()? {
|
||||
println!("| TAIL ->\n{tail}");
|
||||
tail.each(cb)?;
|
||||
}
|
||||
})
|
||||
|
|
@ -194,7 +195,7 @@ pub const fn exp_seek_length (source: &str) -> DslPerhaps<usize> {
|
|||
}
|
||||
def_peek_seek!(sym_peek, sym_peek_only, sym_seek, sym_seek_start, sym_seek_length);
|
||||
pub const fn is_sym_start (c: char) -> bool { matches!(c, ':'|'@') }
|
||||
pub const fn is_sym_char (c: char) -> bool { matches!(c, ':'|'a'..='z'|'A'..='Z'|'0'..='9'|'-'|'/') }
|
||||
pub const fn is_sym_char (c: char) -> bool { is_sym_start(c) || matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-'|'/') }
|
||||
pub const fn is_sym_end (c: char) -> bool { is_space(c) || matches!(c, ')') }
|
||||
pub const fn sym_seek_start (source: &str) -> DslPerhaps<usize> {
|
||||
for_each!((i, c) in char_indices(source) => if is_sym_start(c) {
|
||||
|
|
|
|||
|
|
@ -12,24 +12,24 @@ pub trait FromDsl<T>: Sized {
|
|||
}
|
||||
|
||||
/// `self` + [Dsl] -> `T`
|
||||
pub trait DslInto<'s, T> {
|
||||
fn dsl_into (&'s self, dsl: &impl Dsl) -> Perhaps<T>;
|
||||
fn dsl_into_or (&'s self, dsl: &impl Dsl, err: Box<dyn Error>) -> Usually<T> {
|
||||
pub trait DslInto<T> {
|
||||
fn dsl_into (&self, dsl: &impl Dsl) -> Perhaps<T>;
|
||||
fn dsl_into_or (&self, dsl: &impl Dsl, err: Box<dyn Error>) -> Usually<T> {
|
||||
self.dsl_into(dsl)?.ok_or(err)
|
||||
}
|
||||
fn dsl_into_or_else (&'s self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<T> {
|
||||
fn dsl_into_or_else (&self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<T> {
|
||||
self.dsl_into(dsl)?.ok_or_else(err)
|
||||
}
|
||||
}
|
||||
|
||||
/// `self` + `T` -> [Dsl]
|
||||
pub trait DslFrom<T> {
|
||||
fn dsl_from (&self, dsl: &T) -> Perhaps<impl Dsl>;
|
||||
fn dsl_from_or (&self, dsl: &T, err: Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(dsl)?.ok_or(err)
|
||||
fn dsl_from (&self, val: &T) -> Perhaps<impl Dsl>;
|
||||
fn dsl_from_or (&self, val: &T, err: Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(val)?.ok_or(err)
|
||||
}
|
||||
fn dsl_from_or_else (&self, dsl: &T, err: impl Fn()->Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(dsl)?.ok_or_else(err)
|
||||
fn dsl_from_or_else (&self, val: &T, err: impl Fn()->Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(val)?.ok_or_else(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -44,14 +44,14 @@ pub trait IntoDsl<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'s, T: FromDsl<U>, U> DslInto<'s, T> for U {
|
||||
fn dsl_into (&self, dsl: &impl Dsl) -> Perhaps<T> {
|
||||
T::from_dsl(self, dsl)
|
||||
}
|
||||
}
|
||||
//impl<'s, T: FromDsl<U>, U> DslInto<'s, T> for U {
|
||||
//fn dsl_into (&self, dsl: &impl Dsl) -> Perhaps<T> {
|
||||
//T::from_dsl(self, dsl)
|
||||
//}
|
||||
//}
|
||||
|
||||
impl<T: DslFrom<U>, U> IntoDsl<T> for U {
|
||||
fn into_dsl (&self, state: &T) -> Perhaps<impl Dsl> {
|
||||
T::dsl_from(state, self)
|
||||
}
|
||||
}
|
||||
//impl<T: DslFrom<U>, U> IntoDsl<T> for U {
|
||||
//fn into_dsl (&self, state: &T) -> Perhaps<impl Dsl> {
|
||||
//T::dsl_from(state, self)
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue