mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
This commit is contained in:
parent
1ef898ac32
commit
2557a0d253
7 changed files with 605 additions and 342 deletions
|
|
@ -1,125 +0,0 @@
|
|||
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 WORDS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
|
||||
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||
const EXPRS: 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(num) = to_number(src) {
|
||||
Ok(Some(num as $Type))
|
||||
} else if let Ok(Some(src)) = src.word() {
|
||||
self.from_word(src)
|
||||
} else {
|
||||
self.from_expr(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 WORDS: DslNsMap<'t, fn (&'t $State)->Perhaps<$Type>> =
|
||||
DslNsMap::new(&[$(dsl_ns!{@word ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||
const EXPRS: DslNsMap<'t, fn (&'t $State, &str)->Perhaps<$Type>> =
|
||||
DslNsMap::new(&[$(dsl_ns!{@exp ($state: $State) -> $Type { $pat => $body }}),*]);
|
||||
}
|
||||
};
|
||||
// Symbols only.
|
||||
(@word ($state:ident: $State:ty) -> $Type:ty {
|
||||
$word:literal => $body:expr
|
||||
}) => {
|
||||
($word, |$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 tail = tail.tail()?.unwrap_or_default();
|
||||
let $arg: $ty = if let Some(arg) = $state.from(&head)? {
|
||||
arg
|
||||
} else {
|
||||
return Err(format!("{}: missing argument: {} ({}); got: {tail}",
|
||||
$head,
|
||||
stringify!($arg),
|
||||
stringify!($Type),
|
||||
).into())
|
||||
};
|
||||
)*
|
||||
Ok(Some($body))
|
||||
}) };
|
||||
// Nothing else in symbols.
|
||||
(@word ($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 {
|
||||
/// 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.word() {
|
||||
self.from_word(src)
|
||||
} else {
|
||||
self.from_expr(src)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
/// Resolve a symbol if known.
|
||||
fn from_word <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||
if let Some(dsl) = dsl.word()? {
|
||||
for (word, get) in Self::WORDS.0 { if dsl == *word { return get(self) } }
|
||||
}
|
||||
return Ok(None)
|
||||
}
|
||||
/// Resolve an expression if known.
|
||||
fn from_expr <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||
if let Some(head) = dsl.expr().head()? {
|
||||
for (key, value) in Self::EXPRS.0.iter() {
|
||||
if head == *key {
|
||||
return value(self, dsl.expr().tail()?.unwrap_or(""))
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(None)
|
||||
}
|
||||
/// Known symbols.
|
||||
const WORDS: DslNsMap<'t, fn (&'t Self)->Perhaps<T>> = DslNsMap::new(&[]);
|
||||
/// Known expressions.
|
||||
const EXPRS: DslNsMap<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNsMap::new(&[]);
|
||||
}
|
||||
/// Namespace mapping.
|
||||
#[derive(Debug)]
|
||||
pub struct DslNsMap<'t, T: Debug + 't>(pub &'t [(&'t str, T)]);
|
||||
impl<'t, T: Debug + '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