mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
dsl: add ns
This commit is contained in:
parent
35a5784d23
commit
ab0dc3fae0
2 changed files with 39 additions and 1 deletions
|
|
@ -81,7 +81,7 @@ pub const fn peek_tail (src: &str) -> DslPerhaps<&str> {
|
||||||
Ok(None) => Ok(None),
|
Ok(None) => Ok(None),
|
||||||
Ok(Some((start, length))) => {
|
Ok(Some((start, length))) => {
|
||||||
let tail = str_range(src, start + length, src.len());
|
let tail = str_range(src, start + length, src.len());
|
||||||
for_each!((i, c) in char_indices(tail) => if !is_space(c) { return Ok(Some(tail)) });
|
for_each!((_i, c) in char_indices(tail) => if !is_space(c) { return Ok(Some(tail)) });
|
||||||
Ok(None)
|
Ok(None)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,43 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
|
/// Namespace. Currently linearly searched.
|
||||||
|
pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
|
||||||
|
|
||||||
|
/// Namespace where keys are symbols.
|
||||||
|
pub trait DslSymNs<'t, T: 't>: 't {
|
||||||
|
const SYMS: DslNs<'t, fn (&'t Self)->T>;
|
||||||
|
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Usually<T> {
|
||||||
|
if let Some(dsl) = dsl.sym()? {
|
||||||
|
for (sym, get) in Self::SYMS.0 {
|
||||||
|
if dsl == *sym {
|
||||||
|
return Ok(get(self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err(format!("not found: sym: {dsl:?}").into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export] macro_rules! dsl_sym (
|
||||||
|
(|$state:ident:$State:ty| -> $type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{
|
||||||
|
impl<'t> DslSymNs<'t, $type> for $State {
|
||||||
|
const SYMS: DslNs<'t, fn (&'t $State)->$type> =
|
||||||
|
DslNs(&[$(($lit, |$state: &$State|$exp)),*]); } });
|
||||||
|
|
||||||
|
pub trait DslExpNs<'t, T: 't>: 't {
|
||||||
|
const EXPS: DslNs<'t, fn (&'t Self, &str)->T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export] macro_rules! dsl_exp (
|
||||||
|
(|$state:ident:$State:ty|->$type:ty { $(
|
||||||
|
[$key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*] => $body:expr
|
||||||
|
),* $(,)? }) => {
|
||||||
|
impl<'t> DslExpNs<'t, $type> for $State {
|
||||||
|
const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> =
|
||||||
|
DslNs(&[]); } });
|
||||||
|
|
||||||
|
// TODO DEPRECATE:
|
||||||
|
|
||||||
/// `T` + [Dsl] -> `Self`.
|
/// `T` + [Dsl] -> `Self`.
|
||||||
pub trait FromDsl<T>: Sized {
|
pub trait FromDsl<T>: Sized {
|
||||||
fn from_dsl (state: &T, dsl: &impl Dsl) -> Perhaps<Self>;
|
fn from_dsl (state: &T, dsl: &impl Dsl) -> Perhaps<Self>;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue