mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
dsl: modularize
This commit is contained in:
parent
4fc0db5777
commit
cf253c28f9
6 changed files with 224 additions and 397 deletions
118
dsl/src/dsl_ns.rs
Normal file
118
dsl/src/dsl_ns.rs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
use crate::*;
|
||||
/// Define a namespace:
|
||||
#[macro_export] macro_rules! dsl_ns (
|
||||
// Special form for numeric types
|
||||
(num |$state:ident : $State: ty| $($($num:lifetime)? $Type:ty $(=> { $(
|
||||
$pat:tt => $body:expr
|
||||
),* $(,)? })?;)+) => {
|
||||
$(dsl_ns!(num |$state: $State| -> $($num)? $Type { $( $($pat => $body),* )? });)+
|
||||
};
|
||||
// Special form for numeric types
|
||||
(num |$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 }}),*]);
|
||||
fn from <D: Dsl> (&'t self, dsl: D) -> Perhaps<$Type> {
|
||||
if let Ok(Some(src)) = dsl.src() {
|
||||
if let Ok(Some(num)) = src.num() {
|
||||
Ok(Some(to_number(num)? as $Type))
|
||||
} else if let Ok(Some(src)) = src.sym() {
|
||||
self.from_sym(src)
|
||||
} else {
|
||||
self.from_exp(src)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
// A namespace may resolve one or more types.
|
||||
(|$state:ident : $State: ty| $($Type:ty $(=> { $( $pat:tt => $body:expr ),* $(,)? })? ;)+) => {
|
||||
$(dsl_ns!(|$state: $State| -> $Type { $( $($pat => $body),* )? });)+
|
||||
};
|
||||
// Regular form for single type
|
||||
(|$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 }}),*]);
|
||||
}
|
||||
};
|
||||
// Symbols only.
|
||||
(@sym ($state:ident: $State:ty) -> $Type:ty {
|
||||
$sym:literal => $body:expr
|
||||
}) => {
|
||||
($sym, |$state|Ok(Some($body)))
|
||||
};
|
||||
// Expression handlers only.
|
||||
(@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))
|
||||
}) };
|
||||
// Nothing else in symbols.
|
||||
(@sym ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
||||
("", |_|Ok(None)) // FIXME don't emit at all
|
||||
};
|
||||
// Nothing else in expression handlers.
|
||||
(@exp ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
||||
("", |_, _|Ok(None)) // FIXME don't emit at all
|
||||
};
|
||||
|
||||
);
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
/// 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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue