mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-07 12:16:44 +01:00
dsl: exp -> expr, sym -> word
This commit is contained in:
parent
cf253c28f9
commit
ddd162f225
8 changed files with 330 additions and 325 deletions
|
|
@ -10,18 +10,18 @@ use crate::*;
|
|||
// 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>> =
|
||||
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(Some(num)) = src.num() {
|
||||
Ok(Some(to_number(num)? as $Type))
|
||||
} else if let Ok(Some(src)) = src.sym() {
|
||||
self.from_sym(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_exp(src)
|
||||
self.from_expr(src)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
|
|
@ -36,17 +36,17 @@ use crate::*;
|
|||
// 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>> =
|
||||
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.
|
||||
(@sym ($state:ident: $State:ty) -> $Type:ty {
|
||||
$sym:literal => $body:expr
|
||||
(@word ($state:ident: $State:ty) -> $Type:ty {
|
||||
$word:literal => $body:expr
|
||||
}) => {
|
||||
($sym, |$state|Ok(Some($body)))
|
||||
($word, |$state|Ok(Some($body)))
|
||||
};
|
||||
// Expression handlers only.
|
||||
(@exp ($state:ident: $State:ty) -> $Type:ty {
|
||||
|
|
@ -54,17 +54,21 @@ use crate::*;
|
|||
}) => { ($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: {}", stringify!($arg)).into())
|
||||
return Err(format!("{}: missing argument: {} ({}); got: {tail}",
|
||||
$head,
|
||||
stringify!($arg),
|
||||
stringify!($Type),
|
||||
).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 }) => {
|
||||
(@word ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $body:expr }) => {
|
||||
("", |_|Ok(None)) // FIXME don't emit at all
|
||||
};
|
||||
// Nothing else in expression handlers.
|
||||
|
|
@ -75,42 +79,45 @@ use crate::*;
|
|||
);
|
||||
|
||||
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)
|
||||
if let Ok(Some(src)) = src.word() {
|
||||
self.from_word(src)
|
||||
} else {
|
||||
self.from_exp(src)
|
||||
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.
|
||||
pub struct DslNsMap<'t, T: 't>(pub &'t [(&'t str, T)]);
|
||||
impl<'t, T: 't> DslNsMap<'t, T> {
|
||||
#[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