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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue