mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-07 20:26:44 +01:00
This commit is contained in:
parent
9e0b7be9a9
commit
d2095111f1
4 changed files with 247 additions and 87 deletions
|
|
@ -8,7 +8,8 @@ use const_panic::PanicFmt;
|
|||
use std::fmt::Debug;
|
||||
pub(crate) use std::error::Error;
|
||||
pub(crate) use std::sync::Arc;
|
||||
pub(crate) use konst::string::{str_range, char_indices};
|
||||
pub(crate) use konst::iter::for_each;
|
||||
pub(crate) use konst::string::{str_from, str_range, char_indices};
|
||||
pub(crate) use thiserror::Error;
|
||||
pub(crate) use ::tengri_core::*;
|
||||
pub(crate) use self::DslError::*;
|
||||
|
|
@ -16,77 +17,52 @@ mod dsl_conv; pub use self::dsl_conv::*;
|
|||
mod dsl_parse;
|
||||
pub(crate) use self::dsl_parse::*;
|
||||
pub mod parse { pub use crate::dsl_parse::*; }
|
||||
|
||||
#[cfg(test)]
|
||||
mod dsl_test;
|
||||
|
||||
#[cfg(test)] mod dsl_test;
|
||||
flex_trait!(Dsl: Debug + Send + Sync + Sized {
|
||||
fn src (&self) -> &str {
|
||||
unreachable!("Dsl::src default impl")
|
||||
}
|
||||
fn src (&self) -> DslPerhaps<&str> { unreachable!("Dsl::src default impl") }
|
||||
});
|
||||
impl Dsl for Arc<str> {
|
||||
fn src (&self) -> &str { self.as_ref() }
|
||||
fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) }
|
||||
}
|
||||
impl<'s> Dsl for &'s str {
|
||||
fn src (&self) -> &str { self.as_ref() }
|
||||
fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) }
|
||||
}
|
||||
impl<D: Dsl> Dsl for Option<D> {
|
||||
fn src (&self) -> &str { if let Some(dsl) = self { dsl.src() } else { "" } }
|
||||
fn src (&self) -> DslPerhaps<&str> {
|
||||
Ok(if let Some(dsl) = self { dsl.src()? } else { None })
|
||||
}
|
||||
}
|
||||
impl<D: Dsl> Dsl for Result<D, DslError> {
|
||||
fn src (&self) -> DslPerhaps<&str> {
|
||||
match self { Ok(dsl) => Ok(dsl.src()?), Err(e) => Err(*e) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslExp for D {}
|
||||
pub trait DslExp: Dsl {
|
||||
fn exp (&self) -> DslPerhaps<&str> {
|
||||
Ok(exp_peek(self.src())?)
|
||||
}
|
||||
fn head (&self) -> DslPerhaps<&str> {
|
||||
let start = 1;
|
||||
let src = self.src();
|
||||
let src = &src[start.min(src.len().saturating_sub(1))..];
|
||||
peek(src)
|
||||
}
|
||||
fn tail (&self) -> DslPerhaps<&str> {
|
||||
let start = 1;
|
||||
let src = self.src();
|
||||
let src = &src[start.min(src.len().saturating_sub(1))..];
|
||||
Ok(if let Some((head_start, head_len)) = seek(src)? {
|
||||
let start = 1 + head_start + head_len;
|
||||
let src = self.src();
|
||||
let src = &src[start.min(src.len().saturating_sub(1))..];
|
||||
peek(src)?
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
fn exp (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(exp_peek_inner)) }
|
||||
fn head (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(peek)) }
|
||||
fn tail (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(peek_tail(self.head()))) }
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslSym for D {}
|
||||
pub trait DslSym: Dsl {
|
||||
fn sym (&self) -> DslPerhaps<&str> { crate::parse::sym_peek(self.src()) }
|
||||
fn sym (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(sym_peek_only)) }
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslKey for D {}
|
||||
pub trait DslKey: Dsl {
|
||||
fn key (&self) -> DslPerhaps<&str> { crate::parse::key_peek(self.src()) }
|
||||
fn key (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(key_peek_only)) }
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslText for D {}
|
||||
pub trait DslText: Dsl {
|
||||
fn text (&self) -> DslPerhaps<&str> { crate::parse::text_peek(self.src()) }
|
||||
fn text (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(text_peek_only)) }
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslNum for D {}
|
||||
pub trait DslNum: Dsl {
|
||||
fn num (&self) -> DslPerhaps<&str> { crate::parse::num_peek(self.src()) }
|
||||
fn num (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(num_peek_only)) }
|
||||
}
|
||||
|
||||
/// DSL-specific result type.
|
||||
pub type DslResult<T> = Result<T, DslError>;
|
||||
|
||||
/// DSL-specific optional result type.
|
||||
pub type DslPerhaps<T> = Result<Option<T>, DslError>;
|
||||
|
||||
/// DSL-specific error codes.
|
||||
#[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)]
|
||||
pub enum DslError {
|
||||
|
|
@ -97,3 +73,28 @@ pub enum DslError {
|
|||
#[error("parse failed: error #{0}")] Code(u8),
|
||||
#[error("end reached")] End
|
||||
}
|
||||
#[macro_export] macro_rules! dsl_for_each (($dsl:expr => |$next:ident|$body:expr)=>{
|
||||
let mut dsl: Arc<str> = $dsl.src().into();
|
||||
let mut $next: Option<Arc<str>> = dsl.next()?.map(Into::into);
|
||||
let mut rest: Option<Arc<str>> = dsl.rest()?.map(Into::into);
|
||||
loop {
|
||||
if let Some($next) = $next { $body; } else { break }
|
||||
if let Some(next) = rest {
|
||||
$next = next.next()?.map(Into::into);
|
||||
rest = next.rest()?.map(Into::into);
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
});
|
||||
fn ok_flat <T> (x: Option<DslPerhaps<T>>) -> DslPerhaps<T> { Ok(x.transpose()?.flatten()) }
|
||||
fn peek_tail <'a> (head: DslPerhaps<&'a str>) -> impl Fn(&'a str)->DslPerhaps<&'a str> {
|
||||
move|src|match head {
|
||||
Ok(Some(next)) => {
|
||||
let src = &src[src.len().min(1 + next.len())..];
|
||||
for c in src.chars() { if !is_whitespace(c) { return Ok(Some(src)) } }
|
||||
Ok(None)
|
||||
},
|
||||
e => e
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue