mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
dsl eval and ns again
This commit is contained in:
parent
d7884f6289
commit
4fc0db5777
3 changed files with 256 additions and 81 deletions
|
|
@ -16,13 +16,91 @@ pub(crate) use ::{
|
|||
};
|
||||
pub(crate) use self::DslError::*;
|
||||
mod dsl_conv; pub use self::dsl_conv::*;
|
||||
mod dsl_ns; pub use self::dsl_ns::*;
|
||||
mod dsl_type;
|
||||
#[cfg(test)] mod dsl_test;
|
||||
/// DSL-specific result type.
|
||||
pub type DslResult<T> = Result<T, DslError>;
|
||||
/// DSL-specific optional result type.
|
||||
pub type DslPerhaps<T> = Result<Option<T>, DslError>;
|
||||
/// Namespace mapping.
|
||||
pub struct DslNsMap<'t, T: 't>(pub &'t [(&'t str, T)]);
|
||||
impl<'t, T: 't> DslNsMap<'t, T> {
|
||||
/// Populate a namespace.
|
||||
pub const fn new (data: &'t [(&'t str, T)]) -> Self {
|
||||
Self(data) // TODO build search index
|
||||
}
|
||||
}
|
||||
pub trait DslNs<'t, T: 't>: 't {
|
||||
/// Known symbols.
|
||||
const SYMS: DslNsMap<'t, fn (&'t Self)->Perhaps<T>> = DslNsMap::new(&[]);
|
||||
/// Known expressions.
|
||||
const EXPS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNsMap::new(&[]);
|
||||
/// Resolve a symbol if known.
|
||||
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||
if let Some(dsl) = dsl.sym()? {
|
||||
for (sym, get) in Self::SYMS.0 { if dsl == *sym { return get(self) } }
|
||||
}
|
||||
return Ok(None)
|
||||
}
|
||||
/// Resolve an expression if known.
|
||||
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||
if let Some(exp) = dsl.exp()? {
|
||||
for (key, value) in Self::EXPS.0.iter() {
|
||||
if exp.head() == Ok(Some(key)) { return value(self, exp.tail()?.unwrap_or("")) }
|
||||
}
|
||||
}
|
||||
return Ok(None)
|
||||
}
|
||||
/// Resolve an expression or symbol.
|
||||
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||
if let Ok(Some(src)) = dsl.src() {
|
||||
if let Ok(Some(src)) = src.sym() {
|
||||
self.from_sym(src)
|
||||
} else {
|
||||
self.from_exp(src)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Define a namespace:
|
||||
#[macro_export] macro_rules! dsl_ns (
|
||||
(|$state:ident : $State: ty| $($Type:ty $(=> { $($pat:tt => $body:expr),* $(,)? })?;)+) => {
|
||||
$(dsl_ns!(|$state: $State| -> $Type { $( $($pat => $body),* )? });)+
|
||||
};
|
||||
(|$state:ident : $State: ty| -> $Type:ty { $($pat:tt => $body:expr),* $(,)? }) => {
|
||||
impl<'t> DslNs<'t, $Type> for $State {
|
||||
const SYMS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
|
||||
DslNsMap::new(&[$(dsl_ns!{@sym ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||
const EXPS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
||||
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||
}
|
||||
};
|
||||
(@sym ($state:ident: $State:ty) -> $Type:ty { $sym:literal => $body:expr }) => {
|
||||
($sym, |$state|Ok(Some($body)))
|
||||
};
|
||||
(@exp ($state:ident: $State:ty) -> $Type:ty {
|
||||
($head:literal $(,$arg:ident:$ty:ty)* $(,)*) => $body:expr
|
||||
}) => { ($head, |$state, tail: &str|{
|
||||
$(
|
||||
let head = tail.head()?.unwrap_or_default();
|
||||
let $arg: $ty = if let Some(arg) = $state.from(&head)? {
|
||||
arg
|
||||
} else {
|
||||
return Err(format!("missing argument: {}", stringify!($arg)).into())
|
||||
};
|
||||
let tail = tail.tail()?.unwrap_or_default();
|
||||
)*
|
||||
Ok(Some($body))
|
||||
}) };
|
||||
(@sym ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
||||
("", |_|Ok(None))
|
||||
};
|
||||
(@exp ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
||||
("", |_, _|Ok(None))
|
||||
};
|
||||
);
|
||||
// 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())) } }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue