use crate::*; pub trait DslNs<'a, T: 'a>: 'a { /// Known symbols. const WORDS: DslWords<'a, Self, T> = &[]; /// Known expressions. const EXPRS: DslExprs<'a, Self, T> = &[]; /// Resolve an expression or symbol. fn from (&'a self, dsl: impl Dsl + 'a) -> Perhaps { if let Ok(Some(literal)) = self.from_literal(&dsl) { Ok(Some(literal)) } else if let Ok(Some(meaning)) = self.from_word(&dsl) { Ok(Some(meaning)) } else { self.from_expr(dsl) } } /// Resolve as literal if valid. fn from_literal (&self, _: impl Dsl) -> Perhaps { Ok(None) } /// Resolve a symbol if known. fn from_word (&'a self, dsl: impl DslWord) -> Perhaps { if let Some(dsl) = dsl.word()? { for (key, get) in Self::WORDS.iter() { if dsl == *key { let value = get(self); return value } } } return Ok(None) } /// Resolve an expression if known. fn from_expr (&'a self, dsl: impl DslExpr + 'a) -> Perhaps { let head = dsl.head()?; for (key, get) in Self::EXPRS.iter() { if Some(*key) == head { return get(self, dsl.tail()?.unwrap_or("")) } } return Ok(None) } } /// Define a DSL namespace that provides values to words and expressions. #[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<'a> DslNs<'a, $Type> for $State { const WORDS: DslWords<'a, $State, $Type> = &[ $(dsl_ns!{@word 'a ($state: $State) -> $Type { $pat => $body }}),*]; const EXPRS: DslExprs<'a, $State, $Type> = &[ $(dsl_ns!{@exp 'a ($state: $State) -> $Type { $pat => $body }}),*]; fn from_literal (&self, dsl: impl Dsl) -> Perhaps<$Type> { Ok(if let Some(src) = dsl.src()? { Some(to_number(src)? as $Type) } else { 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<'a> DslNs<'a, $Type> for $State { const WORDS: DslWords<'a, $State, $Type> = &[$(dsl_ns!{@word 'a ($state: $State) -> $Type { $pat => $body }}),*]; const EXPRS: DslExprs<'a, $State, $Type> = &[$(dsl_ns!{@exp 'a ($state: $State) -> $Type { $pat => $body }}),*]; } }; // Symbols only. (@word $l:lifetime ($state:ident: $State:ty) -> $Type:ty { $word:literal => $body:expr }) => {{ let get: GetDslWord<$l, Self, $Type> = (|$state: &$State|Ok(Some($body))); ($word, get) }}; // Expression handlers only. (@exp $l:lifetime ($state:ident: $State:ty) -> $Type:ty { ($head:literal $(,$arg:ident:$ty:ty)* $(,)*) => $body:expr }) => {{ let get: GetDslExpr<$l, Self, $Type> = |$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)) }; ($head, get) }}; // Nothing else in symbols. (@word $l:lifetime ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => { ("", ((|_|Ok(None)) as GetDslWord<$l, Self, $Type>)) // FIXME don't emit at all }; // Nothing else in expression handlers. (@exp $l:lifetime ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => { ("", ((|_, _|Ok(None)) as GetDslExpr<$l, Self, $Type>)) // FIXME don't emit at all }; );