mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
This commit is contained in:
parent
d7884f6289
commit
4c312ac8d7
2 changed files with 243 additions and 52 deletions
|
|
@ -1,9 +1,72 @@
|
|||
use crate::*;
|
||||
|
||||
/// Retrieve from namespace:
|
||||
pub fn dsl_arg <'t, T: DslSymNs<'t, U> + DslExpNs<'t, U> + 't, U: 't> (
|
||||
state: &'t T, dsl: &impl Dsl
|
||||
) -> Perhaps<U> {
|
||||
if let Ok(Some(sym)) = dsl.sym() {
|
||||
state.from_sym(sym)
|
||||
} else if let Ok(Some(exp)) = dsl.exp() {
|
||||
state.from_exp(exp)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Namespace.
|
||||
pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
|
||||
|
||||
impl<'t, T: 't> DslNs<'t, T> {
|
||||
pub const fn new (data: &'t [(&'t str, T)]) -> Self {
|
||||
Self(data) // TODO build search index
|
||||
}
|
||||
}
|
||||
|
||||
/// Define a namespace:
|
||||
#[macro_export] macro_rules! dsl_ns (
|
||||
(|$state:ident : $State: ty| -> $Type:ty { $($pat:tt => $val:expr),* $(,)? }) => {
|
||||
impl<'t> DslSymNs<'t, $Type> for $State {
|
||||
const SYMS: DslSymNsMap<'t, $State, $Type> = DslNs::new(&[$(
|
||||
dsl_ns! { @sym ($state: $State) -> $Type { $pat => $val } }
|
||||
),*]);
|
||||
}
|
||||
impl<'t> DslExpNs<'t, $Type> for $State {
|
||||
const EXPS: DslExpNsMap<'t, $State, $Type> = DslNs::new(&[$(
|
||||
dsl_ns! { @exp ($state: $State) -> $Type { $pat => $val } }
|
||||
),*]);
|
||||
}
|
||||
};
|
||||
(@sym ($state:ident: $State:ty) -> $Type:ty { $sym:literal => $val:expr }) => {
|
||||
($sym, |$state|Ok(Some($val)))
|
||||
};
|
||||
(@exp ($state:ident: $State:ty) -> $Type:ty {
|
||||
($head:literal $(,$arg:ident:$ty:ty)* $(,)*) => $val:expr
|
||||
}) => { ($head, |$state, tail: &str|{
|
||||
$(
|
||||
let head = tail.head()?.unwrap_or_default();
|
||||
let $arg: $ty = if let Some(arg) = dsl_arg($state, &head)? {
|
||||
arg
|
||||
} else {
|
||||
return Err(format!("missing argument: {}", stringify!($arg)).into())
|
||||
};
|
||||
let tail = tail.tail()?.unwrap_or_default();
|
||||
)*
|
||||
Ok(Some($val))
|
||||
}) };
|
||||
(@sym ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $val:expr }) => {
|
||||
("", |_|Ok(None))
|
||||
};
|
||||
(@exp ($state:ident: $State:ty) -> $Type:ty { $pat:tt => $val:expr }) => {
|
||||
("", |_, _|Ok(None))
|
||||
};
|
||||
);
|
||||
|
||||
pub type DslSymNsMap<'t, S, T> = DslNs<'t, fn (&'t S)->Perhaps<T>>;
|
||||
pub type DslExpNsMap<'t, S, T> = DslNs<'t, fn (&'t S, &str)->Perhaps<T>>;
|
||||
/// Namespace where keys are symbols.
|
||||
pub trait DslSymNs<'t, T: 't>: 't {
|
||||
/// Known symbold.
|
||||
const SYMS: DslNs<'t, fn (&'t Self)->Perhaps<T>> = DslNs(&[]);
|
||||
const SYMS: DslSymNsMap<'t, Self, T> = DslNs(&[]);
|
||||
/// Resolve a symbol if known.
|
||||
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||
if let Some(dsl) = dsl.sym()? {
|
||||
|
|
@ -12,63 +75,20 @@ pub trait DslSymNs<'t, T: 't>: 't {
|
|||
return Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Define a symbol namespace.
|
||||
#[macro_export] macro_rules! dsl_sym_ns (
|
||||
(|$state:ident:$State:ty| -> $Type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{
|
||||
impl<'t> DslSymNs<'t, $Type> for $State {
|
||||
const SYMS: DslNs<'t, fn (&'t $State)->Perhaps<$Type>> = DslNs(&[
|
||||
$(($lit, |$state: &$State|Ok(Some($exp)))),*
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
/// Namespace where the keys are heads of expressions.
|
||||
pub trait DslExpNs<'t, T: 't>: 't {
|
||||
/// Known expressions.
|
||||
const EXPS: DslNs<'t, fn (&'t Self, &str)->Perhaps<T>> = DslNs(&[]);
|
||||
const EXPS: DslExpNsMap<'t, Self, T> = DslNs(&[]);
|
||||
/// Resolve an expression if known.
|
||||
fn from_exp <D: Dsl> (&'t self, dsl: D) -> Perhaps<T> {
|
||||
if let Some(exp) = dsl.exp()? {
|
||||
//panic!("exp\n{:?}\n{:?}\n{:?}\n{:?}",
|
||||
//exp.src(), exp.head(), exp.head().src(), Self::EXPS.0);
|
||||
for (key, value) in Self::EXPS.0.iter() {
|
||||
println!("{key} {:?}", exp.head());
|
||||
//println!("{key} {:?}", exp.head());
|
||||
if exp.head() == Ok(Some(key)) { return value(self, exp.tail()?.unwrap_or("")) }
|
||||
}
|
||||
}
|
||||
return Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Define an expression namespace.
|
||||
#[macro_export] macro_rules! dsl_exp_ns (
|
||||
(|$state:ident:$State:ty|->$Type:ty { $( (
|
||||
$key:literal $(/ $sub:ident : $Sub:ty)? $(, $arg:ident:$ty:ty)* $(, ..$tail:ident)?
|
||||
) => $body:expr),* $(,)? }) => {
|
||||
impl<'t> DslExpNs<'t, $Type> for $State {
|
||||
const EXPS: DslNs<'t, fn (&'t $State, &str)->Perhaps<$Type>> = DslNs(&[ $(
|
||||
($key, |$state: &$State, mut tail: &str|{
|
||||
$(let $arg: $ty = if let Some(val) = dsl_next(&mut tail)? {
|
||||
val
|
||||
} else {
|
||||
return Err(format!("missing arg: {}", stringify!($arg)).into())
|
||||
};)*
|
||||
$(let $tail = tail;)?
|
||||
Ok(Some($body))
|
||||
})
|
||||
),* ]);
|
||||
}
|
||||
});
|
||||
|
||||
pub fn dsl_next <T> (dsl: &mut impl Dsl) -> DslPerhaps<T> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Namespace.
|
||||
///
|
||||
/// Currently linearly searched.
|
||||
/// Maybe it's better to use a trie.
|
||||
/// I don't think there's a `const` trie in Rust?
|
||||
/// Oughtta be.
|
||||
pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
|
||||
|
|
|
|||
|
|
@ -9,12 +9,6 @@ use crate::*;
|
|||
})=>{
|
||||
pub trait $T: Dsl { $($trait)* }
|
||||
impl<D: Dsl> $T for D {}
|
||||
dsl_peek_seek!($peek, $peek_only, $seek, $seek_start, $seek_length);
|
||||
pub const fn $seek_start ($source1: &str) -> DslPerhaps<usize> $body1
|
||||
pub const fn $seek_length ($source2: &str) -> DslPerhaps<usize> $body2
|
||||
});
|
||||
|
||||
#[macro_export] macro_rules! dsl_peek_seek(($peek:ident, $peek_only:ident, $seek:ident, $seek_start:ident, $seek_length:ident)=>{
|
||||
/// Find a slice corrensponding to a syntax token.
|
||||
pub const fn $peek (source: &str) -> DslPerhaps<&str> {
|
||||
match $seek(source) {
|
||||
|
|
@ -48,4 +42,181 @@ use crate::*;
|
|||
},
|
||||
}
|
||||
}
|
||||
pub const fn $seek_start ($source1: &str) -> DslPerhaps<usize> $body1
|
||||
pub const fn $seek_length ($source2: &str) -> DslPerhaps<usize> $body2
|
||||
});
|
||||
|
||||
pub const fn is_exp_start (c: char) -> bool { c == '(' }
|
||||
pub const fn is_exp_end (c: char) -> bool { c == ')' }
|
||||
dsl_type!(DslExp {
|
||||
fn exp (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(exp_peek_inner_only))}
|
||||
fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))}
|
||||
fn tail (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek_tail))}
|
||||
/// my other car is a cdr :<
|
||||
fn each (&self, mut cb: impl FnMut(&str)->Usually<()>) -> Usually<()> {
|
||||
Ok(if let Some(head) = self.head()? {
|
||||
cb(head)?;
|
||||
if let Some(tail) = self.tail()? {
|
||||
tail.each(cb)?;
|
||||
}
|
||||
})
|
||||
}
|
||||
} {
|
||||
pub const fn exp_peek [generated];
|
||||
pub const fn exp_peek_only [generated];
|
||||
pub const fn exp_seek [generated];
|
||||
pub const fn exp_seek_start (src) {
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_exp_start(c) { return Ok(Some(i)) } else
|
||||
if !is_space(c) { return Err(Unexpected(c, Some(i), Some("expected expression start"))) });
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn exp_seek_length (src) {
|
||||
let mut depth = 0;
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_exp_start(c) { depth += 1; } else
|
||||
if is_exp_end(c) {
|
||||
if depth == 0 {
|
||||
return Err(Unexpected(c, Some(i), Some("expected expression end")))
|
||||
} else if depth == 1 {
|
||||
return Ok(Some(i + 1))
|
||||
} else {
|
||||
depth -= 1;
|
||||
}
|
||||
});
|
||||
Err(Incomplete)
|
||||
}
|
||||
});
|
||||
|
||||
pub const fn exp_peek_inner (src: &str) -> DslPerhaps<&str> {
|
||||
match exp_peek(src) {
|
||||
Ok(Some(peeked)) => {
|
||||
let len = peeked.len();
|
||||
let start = if len > 0 { 1 } else { 0 };
|
||||
Ok(Some(str_range(src, start, start + len.saturating_sub(2))))
|
||||
},
|
||||
e => e
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn exp_peek_inner_only (src: &str) -> DslPerhaps<&str> {
|
||||
match exp_seek(src) {
|
||||
Err(e) => Err(e),
|
||||
Ok(None) => Ok(None),
|
||||
Ok(Some((start, length))) => {
|
||||
if let Err(e) = no_trailing_non_space(src, start + length, Some("exp_peek_inner_only")) { return Err(e) }
|
||||
let peeked = str_range(src, start, start + length);
|
||||
let len = peeked.len();
|
||||
let start = if len > 0 { 1 } else { 0 };
|
||||
Ok(Some(str_range(peeked, start, start + len.saturating_sub(2))))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn is_sym_start (c: char) -> bool { matches!(c, ':'|'@') }
|
||||
pub const fn is_sym_char (c: char) -> bool { is_sym_start(c) || matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-'|'/') }
|
||||
pub const fn is_sym_end (c: char) -> bool { is_space(c) || matches!(c, ')') }
|
||||
dsl_type!(DslSym {
|
||||
fn sym (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(sym_peek_only))}
|
||||
} {
|
||||
pub const fn sym_peek [generated];
|
||||
pub const fn sym_peek_only [generated];
|
||||
pub const fn sym_seek [generated];
|
||||
pub const fn sym_seek_start (src) {
|
||||
for_each!((i, c) in char_indices(src) => if
|
||||
is_sym_start(c) { return Ok(Some(i)) } else
|
||||
if !is_space(c) { return Err(Unexpected(c, Some(i), Some("sym_seek_start"))) });
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn sym_seek_length (src) {
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_sym_end(c) { return Ok(Some(i)) } else
|
||||
if !is_sym_char(c) { return Err(Unexpected(c, Some(i), Some("sym_seek_length"))) });
|
||||
Ok(Some(src.len()))
|
||||
}
|
||||
});
|
||||
|
||||
pub const fn is_text_start (c: char) -> bool { matches!(c, '"') }
|
||||
pub const fn is_text_end (c: char) -> bool { matches!(c, '"') }
|
||||
dsl_type!(DslText {
|
||||
fn text (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(text_peek_only)) }
|
||||
} {
|
||||
pub const fn text_peek [generated];
|
||||
pub const fn text_peek_only [generated];
|
||||
pub const fn text_seek [generated];
|
||||
pub const fn text_seek_start (src) {
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_text_start(c) { return Ok(Some(i)) } else
|
||||
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn text_seek_length (src) {
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_text_end(c) { return Ok(Some(i)) });
|
||||
Ok(None)
|
||||
}
|
||||
});
|
||||
|
||||
pub const fn is_key_start (c: char) -> bool { matches!(c, '/'|('a'..='z')) }
|
||||
pub const fn is_key_char (c: char) -> bool { is_key_start(c) || matches!(c, '0'..='9'|'-') }
|
||||
pub const fn is_key_end (c: char) -> bool { !is_key_char(c) }
|
||||
dsl_type!(DslKey {
|
||||
fn key (&self) -> DslPerhaps<&str> { ok_flat(self.src()?.map(key_peek_only)) }
|
||||
} {
|
||||
pub const fn key_peek [generated];
|
||||
pub const fn key_peek_only [generated];
|
||||
pub const fn key_seek [generated];
|
||||
pub const fn key_seek_start (src) {
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_key_start(c) { return Ok(Some(i)) } else
|
||||
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn key_seek_length (src) {
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_key_end(c) { return Ok(Some(i)) } else
|
||||
if !is_key_char(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||
Ok(Some(src.len()))
|
||||
}
|
||||
});
|
||||
|
||||
dsl_type!(DslNum {
|
||||
fn num (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(num_peek_only))}
|
||||
} {
|
||||
pub const fn num_peek [generated];
|
||||
pub const fn num_peek_only [generated];
|
||||
pub const fn num_seek [generated];
|
||||
pub const fn num_seek_start (src) {
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_digit(c) { return Ok(Some(i)); } else
|
||||
if !is_space(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn num_seek_length (src) {
|
||||
for_each!((i, c) in char_indices(src) =>
|
||||
if is_num_end(c) { return Ok(Some(i)) } else
|
||||
if !is_digit(c) { return Err(Unexpected(c, Some(i), None)) });
|
||||
Ok(None)
|
||||
}
|
||||
});
|
||||
pub const fn is_digit (c: char) -> bool { matches!(c, '0'..='9') }
|
||||
pub const fn is_num_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
|
||||
pub const fn to_number <D: Dsl> (digits: &str) -> Result<usize, DslError> {
|
||||
let mut iter = char_indices(digits);
|
||||
let mut value = 0;
|
||||
while let Some(((_, c), next)) = iter.next() {
|
||||
match to_digit(c) {
|
||||
Ok(digit) => value = 10 * value + digit,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
iter = next;
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
pub const fn to_digit (c: char) -> Result<usize, DslError> {
|
||||
Ok(match c {
|
||||
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
|
||||
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
|
||||
_ => return Err(Unexpected(c, None, Some("parse digit")))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue