mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
simplify
This commit is contained in:
parent
291b917970
commit
238ac2e888
25 changed files with 1018 additions and 1147 deletions
|
|
@ -5,6 +5,7 @@
|
|||
extern crate const_panic;
|
||||
use const_panic::{concat_panic, PanicFmt};
|
||||
pub(crate) use ::tengri_core::*;
|
||||
use std::ops::Deref;
|
||||
pub(crate) use std::error::Error;
|
||||
pub(crate) use std::fmt::Debug;
|
||||
pub(crate) use std::sync::Arc;
|
||||
|
|
@ -13,7 +14,80 @@ pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
|
|||
pub(crate) use konst::string::{split_at, str_range, char_indices};
|
||||
pub(crate) use thiserror::Error;
|
||||
pub(crate) use self::DslError::*;
|
||||
#[cfg(test)] mod test;
|
||||
#[cfg(test)] mod dsl_test;
|
||||
|
||||
#[macro_export] macro_rules! dsl_read_advance (($exp:ident, $pat:pat => $val:expr)=>{{
|
||||
let (head, tail) = $exp.advance(); $exp = tail; match head {
|
||||
Some($pat) => $val, _ => Err(format!("(e4) unexpected {head:?}").into()) }}});
|
||||
|
||||
/// Coerce to [Val] for predefined [Self::Str] and [Self::Exp].
|
||||
pub trait Dsl: Clone + Debug {
|
||||
/// The string representation for a dizzle.
|
||||
type Str: DslStr;
|
||||
/// The string representation for a dizzle.
|
||||
type Exp: DslExp;
|
||||
/// Request the top-level DSL [Val]ue.
|
||||
/// May perform cloning or parsing.
|
||||
fn val (&self) -> Val<Self::Str, Self::Exp>;
|
||||
fn err (&self) -> Option<DslError> {self.val().err()}
|
||||
fn nil (&self) -> bool {self.val().nil()}
|
||||
fn num (&self) -> Option<usize> {self.val().num()}
|
||||
fn sym (&self) -> Option<Self::Str> {self.val().sym()}
|
||||
fn key (&self) -> Option<Self::Str> {self.val().key()}
|
||||
fn str (&self) -> Option<Self::Str> {self.val().str()}
|
||||
fn exp (&self) -> Option<Self::Exp> {self.val().exp()}
|
||||
fn exp_depth (&self) -> Option<isize> {self.val().exp_depth()}
|
||||
fn exp_head (&self) -> Val<Self::Str, Self::Exp> {self.val().exp_head()}
|
||||
fn exp_tail (&self) -> Self::Exp {self.val().exp_tail()}
|
||||
fn exp_each (&self, f: impl Fn(&Self) -> Usually<()>) -> Usually<()> {
|
||||
todo!()
|
||||
}
|
||||
fn advance (&self) -> (Val<Self::Str, Self::Exp>, Self::Exp) {
|
||||
(self.exp_head(), self.exp_tail())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Dsl for &'s str {
|
||||
type Str = &'s str;
|
||||
type Exp = &'s str;
|
||||
fn val (&self) -> Val<Self::Str, Self::Exp> { Val::Exp(0, self) }
|
||||
}
|
||||
|
||||
impl<'s> Dsl for Cst<'s> {
|
||||
type Str = &'s str;
|
||||
type Exp = Cst<'s>;
|
||||
fn val (&self) -> Val<Self::Str, Self::Exp> { Val::Exp(0, *self) }
|
||||
}
|
||||
|
||||
impl Dsl for Ast {
|
||||
type Str = Arc<str>;
|
||||
type Exp = Ast;
|
||||
fn val (&self) -> Val<Self::Str, Ast> { Val::Exp(0, self.clone()) }
|
||||
}
|
||||
|
||||
impl<Str: DslStr, Exp: DslExp> Dsl for Token<Str, Exp> {
|
||||
type Str = Str;
|
||||
type Exp = Exp;
|
||||
fn val (&self) -> Val<Str, Exp> { self.value.clone() }
|
||||
}
|
||||
|
||||
impl<Str: DslStr, Exp: DslExp> Dsl for Val<Str, Exp> {
|
||||
type Str = Str;
|
||||
type Exp = Exp;
|
||||
fn val (&self) -> Val<Str, Exp> { self.clone() }
|
||||
}
|
||||
|
||||
/// The expression representation for a [Dsl] implementation.
|
||||
/// [Cst] uses [CstIter]. [Ast] uses [VecDeque].
|
||||
pub trait DslExp: PartialEq + Clone + Debug + Dsl {}
|
||||
impl<T: PartialEq + Clone + Debug + Dsl> DslExp for T {}
|
||||
/// The string representation for a [Dsl] implementation.
|
||||
/// [Cst] uses `&'s str`. [Ast] uses `Arc<str>`.
|
||||
pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef<str> + Deref<Target = str> {
|
||||
fn as_str (&self) -> &str { self.as_ref() }
|
||||
fn as_arc (&self) -> Arc<str> { self.as_ref().into() }
|
||||
}
|
||||
impl<Str: PartialEq + Clone + Default + Debug + AsRef<str> + Deref<Target = str>> DslStr for Str {}
|
||||
/// Enumeration of values that may figure in an expression.
|
||||
/// Generic over string and expression storage.
|
||||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
|
|
@ -39,31 +113,20 @@ pub enum Val<Str, Exp> {
|
|||
Error(DslError),
|
||||
}
|
||||
impl<Str: Copy, Exp: Copy> Copy for Val<Str, Exp> {}
|
||||
impl<Str: DslStr, Exp: DslExp> Val<Str, Exp> {
|
||||
pub fn convert <T: Dsl> (&self) -> Val<T::Str, T::Exp> where
|
||||
T::Str: for<'a> From<&'a Str>,
|
||||
T::Exp: for<'a> From<&'a Exp>
|
||||
{
|
||||
impl<S1, E1> Val<S1, E1> {
|
||||
pub fn convert_to <S2, E2> (&self, to_str: impl Fn(&S1)->S2, to_exp: impl Fn(&E1)->E2) -> Val<S2, E2> {
|
||||
match self {
|
||||
Val::Nil => Val::Nil,
|
||||
Val::Num(u) => Val::Num(*u),
|
||||
Val::Sym(s) => Val::Sym(s.into()),
|
||||
Val::Key(s) => Val::Key(s.into()),
|
||||
Val::Str(s) => Val::Str(s.into()),
|
||||
Val::Exp(d, x) => Val::Exp(*d, x.into()),
|
||||
Val::Sym(s) => Val::Sym(to_str(s)),
|
||||
Val::Key(s) => Val::Key(to_str(s)),
|
||||
Val::Str(s) => Val::Str(to_str(s)),
|
||||
Val::Exp(d, x) => Val::Exp(*d, to_exp(x)),
|
||||
Val::Error(e) => Val::Error(*e)
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The expression representation for a [Dsl] implementation.
|
||||
/// [Cst] uses [CstIter]. [Ast] uses [VecDeque].
|
||||
pub trait DslExp: PartialEq + Clone + Default + Debug + Dsl {}
|
||||
impl<T: PartialEq + Clone + Default + Debug + Dsl> DslExp for T {}
|
||||
/// The string representation for a [Dsl] implementation.
|
||||
/// [Cst] uses `&'s str`. [Ast] uses `Arc<str>`.
|
||||
pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef<str> + std::ops::Deref<Target = str> {}
|
||||
impl<T: PartialEq + Clone + Default + Debug + AsRef<str> + std::ops::Deref<Target = str>> DslStr for T {}
|
||||
impl<Str: DslStr, Exp: DslExp + Dsl<Str=Str, Exp=Exp>> Val<Str, Exp> {
|
||||
impl<Str: DslStr, Exp: DslExp + Dsl<Str=Str>> Val<Str, Exp> {
|
||||
pub const fn err (&self) -> Option<DslError> {match self{Val::Error(e)=>Some(*e), _=>None}}
|
||||
pub const fn nil (&self) -> bool {match self{Val::Nil=>true, _=>false}}
|
||||
pub const fn num (&self) -> Option<usize> {match self{Val::Num(n)=>Some(*n), _=>None}}
|
||||
|
|
@ -85,7 +148,9 @@ pub struct Token<Str, Exp> {
|
|||
/// Length of span.
|
||||
pub length: usize,
|
||||
}
|
||||
/// Tokens are copiable where possible.
|
||||
impl<Str: Copy, Exp: Copy> Copy for Token<Str, Exp> {}
|
||||
/// Token methods.
|
||||
impl<Str, Exp> Token<Str, Exp> {
|
||||
pub const fn end (&self) -> usize {
|
||||
self.start.saturating_add(self.length) }
|
||||
|
|
@ -99,83 +164,85 @@ impl<Str, Exp> Token<Str, Exp> {
|
|||
Self { value: self.value, ..*self }
|
||||
}
|
||||
}
|
||||
/// To the [Dsl], a token is equivalent to its `value` field.
|
||||
impl<Str: DslStr, Exp: DslExp> Dsl for Token<Str, Exp> {
|
||||
type Str = Str; type Exp = Exp;
|
||||
fn dsl (&self) -> Val<Str, Exp> { self.value.clone() }
|
||||
}
|
||||
/// Coerce to [Val] for predefined [Self::Str] and [Self::Exp].
|
||||
pub trait Dsl: Clone + Debug {
|
||||
/// The string representation for a dizzle.
|
||||
type Str: DslStr;
|
||||
/// The expression representation for a dizzle.
|
||||
type Exp: DslExp;
|
||||
/// Request the top-level DSL [Val]ue.
|
||||
/// May perform cloning or parsing.
|
||||
fn dsl (&self) -> Val<Self::Str, Self::Exp>;
|
||||
fn err (&self) -> Option<DslError> {self.dsl().err()}
|
||||
fn nil (&self) -> bool {self.dsl().nil()}
|
||||
fn num (&self) -> Option<usize> {self.dsl().num()}
|
||||
fn sym (&self) -> Option<Self::Str> {self.dsl().sym()}
|
||||
fn key (&self) -> Option<Self::Str> {self.dsl().key()}
|
||||
fn str (&self) -> Option<Self::Str> {self.dsl().str()}
|
||||
fn exp (&self) -> Option<Self::Exp> {self.dsl().exp()}
|
||||
fn exp_depth (&self) -> Option<isize> {self.dsl().exp_depth()}
|
||||
fn exp_head (&self) -> Val<Self::Str, Self::Exp> {self.dsl().exp_head()}
|
||||
fn exp_tail (&self) -> Self::Exp {self.dsl().exp_tail()}
|
||||
fn exp_each (&self, f: impl Fn(&Self) -> Usually<()>) -> Usually<()> { todo!() }
|
||||
}
|
||||
/// The most basic implementor of the [Dsl] trait.
|
||||
impl<Str: DslStr, Exp: DslExp> Dsl for Val<Str, Exp> {
|
||||
type Str = Str; type Exp = Exp;
|
||||
fn dsl (&self) -> Val<Str, Exp> { self.clone() }
|
||||
}
|
||||
/// The abstract syntax tree (AST) can be produced from the CST
|
||||
/// by cloning source slices into owned ([Arc]) string slices.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct Ast(Arc<VecDeque<Arc<Token<Arc<str>, Ast>>>>);
|
||||
pub type AstVal = Val<Arc<str>, Ast>;
|
||||
pub type AstToken = Token<Arc<str>, Ast>;
|
||||
impl Dsl for Ast {
|
||||
type Str = Arc<str>; type Exp = Ast;
|
||||
fn dsl (&self) -> Val<Arc<str>, Ast> { Val::Exp(0, Ast(self.0.clone())) }
|
||||
}
|
||||
impl<'s> From<&'s str> for Ast {
|
||||
fn from (source: &'s str) -> Self {
|
||||
let source: Arc<str> = source.into();
|
||||
Self(CstIter(CstConstIter(source.as_ref()))
|
||||
.map(|token|Arc::new(Token {
|
||||
source: source.clone(),
|
||||
start: token.start,
|
||||
length: token.length,
|
||||
value: match token.value {
|
||||
Val::Nil => Val::Nil,
|
||||
Val::Num(u) => Val::Num(u),
|
||||
Val::Sym(s) => Val::Sym(s.into()),
|
||||
Val::Key(s) => Val::Key(s.into()),
|
||||
Val::Str(s) => Val::Str(s.into()),
|
||||
Val::Exp(d, x) => Val::Exp(d, x.into()),
|
||||
Val::Error(e) => Val::Error(e.into())
|
||||
},
|
||||
}))
|
||||
.collect::<VecDeque<_>>()
|
||||
.into())
|
||||
}
|
||||
}
|
||||
impl<'s> From<Cst<'s>> for Ast {
|
||||
fn from (cst: Cst<'s>) -> Self {
|
||||
let mut tokens: VecDeque<_> = Default::default();
|
||||
Self(tokens.into())
|
||||
}
|
||||
}
|
||||
/// The concrete syntax tree (CST) implements zero-copy
|
||||
/// parsing of the DSL from a string reference. CST items
|
||||
/// preserve info about their location in the source.
|
||||
/// CST stores strings as source references and expressions as [CstIter] instances.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct Cst<'s>(pub CstIter<'s>);
|
||||
pub type CstVal<'s> = Val<&'s str, Cst<'s>>;
|
||||
pub type CstToken<'s> = Token<&'s str, Cst<'s>>;
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Default)]
|
||||
pub enum Cst<'s> {
|
||||
#[default] __,
|
||||
Source(&'s str),
|
||||
Token(CstToken<'s>),
|
||||
Val(CstVal<'s>),
|
||||
Iter(CstIter<'s>),
|
||||
ConstIter(CstConstIter<'s>),
|
||||
}
|
||||
impl<'s> From<&'s str> for Cst<'s> {
|
||||
fn from (src: &'s str) -> Self {
|
||||
Cst::Source(src)
|
||||
}
|
||||
}
|
||||
impl<'s> From<CstToken<'s>> for Cst<'s> {
|
||||
fn from (token: CstToken<'s>) -> Self {
|
||||
Cst::Token(token)
|
||||
}
|
||||
}
|
||||
impl<'s> From<CstVal<'s>> for Cst<'s> {
|
||||
fn from (val: CstVal<'s>) -> Self {
|
||||
Cst::Val(val)
|
||||
}
|
||||
}
|
||||
impl<'s> From<CstIter<'s>> for Cst<'s> {
|
||||
fn from (iter: CstIter<'s>) -> Self {
|
||||
Cst::Iter(iter)
|
||||
}
|
||||
}
|
||||
impl<'s> From<CstConstIter<'s>> for Cst<'s> {
|
||||
fn from (iter: CstConstIter<'s>) -> Self {
|
||||
Cst::ConstIter(iter)
|
||||
}
|
||||
}
|
||||
impl<'s> From<Cst<'s>> for Ast {
|
||||
fn from (cst: Cst<'s>) -> Self {
|
||||
match cst {
|
||||
Cst::Source(source) | Cst::Iter(CstIter(CstConstIter(source))) |
|
||||
Cst::ConstIter(CstConstIter(source)) =>
|
||||
Ast::Source(source.into()),
|
||||
Cst::Val(value) =>
|
||||
Ast::Val(value.convert_to(|s|(*s).into(), |e|Box::new((*e).into()))),
|
||||
Cst::Token(Token { source, start, length, value }) => {
|
||||
let source = AsRef::<str>::as_ref(source).into();
|
||||
let value = value.convert_to(|s|(*s).into(), |e|Box::new((*e).into()));
|
||||
Ast::Token(Token { source, start, length, value })
|
||||
},
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The abstract syntax tree (AST) can be produced from the CST
|
||||
/// by cloning source slices into owned ([Arc]) string slices.
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub enum Ast {
|
||||
#[default] __,
|
||||
Source(Arc<str>),
|
||||
Token(Token<Arc<str>, Box<Ast>>),
|
||||
Val(Val<Arc<str>, Box<Ast>>),
|
||||
Exp(Arc<VecDeque<Arc<Token<Arc<str>, Ast>>>>),
|
||||
}
|
||||
impl<'s> From<&'s str> for Ast {
|
||||
fn from (src: &'s str) -> Self {
|
||||
Self::Source(src.into())
|
||||
}
|
||||
}
|
||||
impl<'s, Str: DslStr, Exp: DslExp + Into<Ast> + 's> From<Token<Str, Exp>> for Ast {
|
||||
fn from (Token { source, start, length, value }: Token<Str, Exp>) -> Self {
|
||||
let source: Arc<str> = source.as_ref().into();
|
||||
let value = value.convert_to(|s|s.as_ref().into(), |e|Box::new(e.clone().into()));
|
||||
Self::Token(Token { source, start, length, value })
|
||||
}
|
||||
}
|
||||
pub type CstVal<'s> = Val<&'s str, &'s str>;
|
||||
pub type CstToken<'s> = Token<&'s str, &'s str>;
|
||||
impl<'s> CstToken<'s> {
|
||||
pub const fn slice (&self) -> &str {
|
||||
str_range(self.source, self.start, self.end()) }
|
||||
|
|
@ -188,19 +255,32 @@ impl<'s> CstToken<'s> {
|
|||
self
|
||||
}
|
||||
pub const fn grow_exp (&'s mut self, depth: isize, source: &'s str) -> &mut Self {
|
||||
self.value = Val::Exp(depth, Cst(CstIter(CstConstIter(source))));
|
||||
self.value = Val::Exp(depth, source);
|
||||
self
|
||||
}
|
||||
}
|
||||
impl<'s> Dsl for Cst<'s> {
|
||||
type Str = &'s str; type Exp = Cst<'s>;
|
||||
fn dsl (&self) -> Val<Self::Str, Self::Exp> { Val::Exp(0, Cst(self.0)) }
|
||||
}
|
||||
impl<'s> From<&'s str> for Cst<'s> {
|
||||
fn from (source: &'s str) -> Self {
|
||||
Self(CstIter(CstConstIter(source)))
|
||||
}
|
||||
}
|
||||
//impl<'s> From<&'s str> for Ast {
|
||||
//fn from (source: &'s str) -> Ast {
|
||||
//let source: Arc<str> = source.into();
|
||||
//Ast(CstIter(CstConstIter(source.as_ref()))
|
||||
//.map(|token|Arc::new(Token {
|
||||
//source: source.clone(),
|
||||
//start: token.start,
|
||||
//length: token.length,
|
||||
//value: match token.value {
|
||||
//Val::Nil => Val::Nil,
|
||||
//Val::Num(u) => Val::Num(u),
|
||||
//Val::Sym(s) => Val::Sym(s.into()),
|
||||
//Val::Key(s) => Val::Key(s.into()),
|
||||
//Val::Str(s) => Val::Str(s.into()),
|
||||
//Val::Exp(d, x) => Val::Exp(d, x.into()),
|
||||
//Val::Error(e) => Val::Error(e.into())
|
||||
//},
|
||||
//}))
|
||||
//.collect::<VecDeque<_>>()
|
||||
//.into())
|
||||
//}
|
||||
//}
|
||||
/// DSL-specific error codes.
|
||||
#[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)] pub enum DslError {
|
||||
#[error("parse failed: not implemented")]
|
||||
|
|
@ -283,20 +363,12 @@ pub const fn peek <'s> (mut value: CstVal<'s>, source: &'s str) -> CstToken<'s>
|
|||
}
|
||||
start = i;
|
||||
length = 1;
|
||||
if is_exp_start(c) {
|
||||
value = Exp(1, Cst(CstIter(CstConstIter(str_range(source, i, i+1)))));
|
||||
} else if is_str_start(c) {
|
||||
value = Str(str_range(source, i, i+1));
|
||||
} else if is_sym_start(c) {
|
||||
value = Sym(str_range(source, i, i+1));
|
||||
} else if is_key_start(c) {
|
||||
value = Key(str_range(source, i, i+1));
|
||||
} else if is_digit(c) {
|
||||
value = match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) };
|
||||
} else {
|
||||
value = Error(Unexpected(c));
|
||||
break
|
||||
}
|
||||
value = if is_exp_start(c) { Exp(1, str_range(source, i, i+1)) }
|
||||
else if is_str_start(c) { Str(str_range(source, i, i+1)) }
|
||||
else if is_sym_start(c) { Sym(str_range(source, i, i+1)) }
|
||||
else if is_key_start(c) { Key(str_range(source, i, i+1)) }
|
||||
else if is_digit(c) { match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) } }
|
||||
else { value = Error(Unexpected(c)); break }
|
||||
} else if matches!(value, Str(_)) {
|
||||
if is_str_end(c) {
|
||||
break
|
||||
|
|
@ -323,13 +395,13 @@ pub const fn peek <'s> (mut value: CstVal<'s>, source: &'s str) -> CstToken<'s>
|
|||
}
|
||||
} else if let Exp(depth, exp) = value {
|
||||
if depth == 0 {
|
||||
value = Exp(0, Cst(CstIter(CstConstIter(str_range(source, start, start + length)))));
|
||||
value = Exp(0, str_range(source, start, start + length));
|
||||
break
|
||||
}
|
||||
length += 1;
|
||||
value = Exp(
|
||||
if c == ')' { depth-1 } else if c == '(' { depth+1 } else { depth },
|
||||
Cst(CstIter(CstConstIter(str_range(source, start, start + length))))
|
||||
str_range(source, start, start + length)
|
||||
);
|
||||
} else if let Num(m) = value {
|
||||
if is_num_end(c) {
|
||||
|
|
@ -378,22 +450,6 @@ pub const fn to_digit (c: char) -> Result<usize, DslError> {
|
|||
_ => return Err(Unexpected(c))
|
||||
})
|
||||
}
|
||||
/// `State` + [Dsl] -> `Self`.
|
||||
pub trait FromDsl<State>: Sized {
|
||||
fn try_from_dsl (state: &State, dsl: &impl Dsl) -> Perhaps<Self>;
|
||||
fn from_dsl (state: &State, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Self> {
|
||||
match Self::try_from_dsl(state, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } }
|
||||
}
|
||||
/// `self` + `Options` -> [Dsl]
|
||||
pub trait IntoDsl { /*TODO*/ }
|
||||
/// `self` + [Dsl] -> `Item`
|
||||
pub trait DslInto<Item> {
|
||||
fn try_dsl_into (&self, dsl: &impl Dsl) -> Perhaps<Item>;
|
||||
fn dsl_into (&self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Item> {
|
||||
match Self::try_dsl_into(self, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } }
|
||||
}
|
||||
/// `self` + `Item` -> [Dsl]
|
||||
pub trait DslFrom { /*TODO*/ }
|
||||
|
||||
/// Implement type conversions.
|
||||
macro_rules! from(($($Struct:ty { $(
|
||||
|
|
@ -421,3 +477,53 @@ macro_rules! from(($($Struct:ty { $(
|
|||
//<'s> (iter: CstIter<'s>) Ast(iter.map(|x|x.value.into()).collect::<VecDeque<_>>().into());
|
||||
//<D: Dsl> (token: Token<D>) Ast(VecDeque::from([dsl_val(token.val())]).into()); }
|
||||
//}
|
||||
|
||||
/// `T` + [Dsl] -> `Self`.
|
||||
pub trait FromDsl<T>: Sized {
|
||||
fn from_dsl (state: &T, dsl: &impl Dsl) -> Perhaps<Self>;
|
||||
fn from_dsl_or (state: &T, dsl: &impl Dsl, err: Box<dyn Error>) -> Usually<Self> {
|
||||
Self::from_dsl(state, dsl)?.ok_or(err)
|
||||
}
|
||||
fn from_dsl_or_else (state: &T, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Self> {
|
||||
Self::from_dsl(state, dsl)?.ok_or_else(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromDsl<U>, U> DslInto<T> for U {
|
||||
fn dsl_into (&self, dsl: &impl Dsl) -> Perhaps<T> {
|
||||
T::from_dsl(self, dsl)
|
||||
}
|
||||
}
|
||||
|
||||
/// `self` + [Dsl] -> `T`
|
||||
pub trait DslInto<T> {
|
||||
fn dsl_into (&self, dsl: &impl Dsl) -> Perhaps<T>;
|
||||
fn dsl_into_or (&self, dsl: &impl Dsl, err: Box<dyn Error>) -> Usually<T> {
|
||||
self.dsl_into(dsl)?.ok_or(err)
|
||||
}
|
||||
fn dsl_into_or_else (&self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<T> {
|
||||
self.dsl_into(dsl)?.ok_or_else(err)
|
||||
}
|
||||
}
|
||||
|
||||
/// `self` + `T` + -> [Dsl]
|
||||
pub trait IntoDsl<T> {
|
||||
fn into_dsl (&self, state: &T) -> Perhaps<impl Dsl>;
|
||||
fn into_dsl_or (&self, state: &T, err: Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.into_dsl(state)?.ok_or(err)
|
||||
}
|
||||
fn into_dsl_or_else (&self, state: &T, err: impl Fn()->Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.into_dsl(state)?.ok_or_else(err)
|
||||
}
|
||||
}
|
||||
|
||||
/// `self` + `T` -> [Dsl]
|
||||
pub trait DslFrom<T> {
|
||||
fn dsl_from (&self, dsl: &impl Dsl) -> Perhaps<impl Dsl>;
|
||||
fn dsl_from_or (&self, dsl: &impl Dsl, err: Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(dsl)?.ok_or(err)
|
||||
}
|
||||
fn dsl_from_or_else (&self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<impl Dsl> {
|
||||
self.dsl_from(dsl)?.ok_or_else(err)
|
||||
}
|
||||
}
|
||||
0
dsl/src/dsl_test.rs
Normal file
0
dsl/src/dsl_test.rs
Normal file
104
dsl/src/test.rs
104
dsl/src/test.rs
|
|
@ -1,104 +0,0 @@
|
|||
use crate::*;
|
||||
use proptest::prelude::*;
|
||||
|
||||
#[test] fn test_iters () {
|
||||
let mut iter = crate::CstIter::new(&":foo :bar");
|
||||
let _ = iter.next();
|
||||
}
|
||||
|
||||
#[test] const fn test_const_iters () {
|
||||
let iter = crate::CstConstIter::new(&":foo :bar");
|
||||
let _ = iter.next();
|
||||
}
|
||||
|
||||
#[test] fn test_num () {
|
||||
let _digit = Token::to_digit('0');
|
||||
let _digit = Token::to_digit('x');
|
||||
let _number = Token::to_number(&"123");
|
||||
let _number = Token::to_number(&"12asdf3");
|
||||
}
|
||||
//proptest! {
|
||||
//#[test] fn proptest_source_iter (
|
||||
//source in "\\PC*"
|
||||
//) {
|
||||
//let mut iter = crate::CstIter::new(&source);
|
||||
////let _ = iter.next()
|
||||
//}
|
||||
//#[test] fn proptest_token_iter (
|
||||
//source in "\\PC*"
|
||||
//) {
|
||||
//let mut iter = crate::TokenIter::new(&source);
|
||||
////let _ = iter.next();
|
||||
//}
|
||||
//}
|
||||
|
||||
//#[cfg(test)] mod test_token_prop {
|
||||
//use crate::{Cst, CstMeta, Value::*};
|
||||
//use proptest::prelude::*;
|
||||
//proptest! {
|
||||
//#[test] fn test_token_prop (
|
||||
//source in "\\PC*",
|
||||
//start in usize::MIN..usize::MAX,
|
||||
//length in usize::MIN..usize::MAX,
|
||||
//) {
|
||||
//let token = Cst(Nil, CstMeta { source: &source, start, length });
|
||||
//let _ = token.slice();
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
|
||||
#[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
|
||||
use crate::Val::*;
|
||||
let source = ":f00";
|
||||
let mut token = CstToken::new(source, 0, 1, Sym(":"));
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, CstToken::new(source, 0, 2, Sym(":f")));
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, CstToken::new(source, 0, 3, Sym(":f0")));
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, CstToken::new(source, 0, 4, Sym(":f00")));
|
||||
|
||||
assert_eq!(None, CstIter::new("").next());
|
||||
assert_eq!(None, CstIter::new(" \n \r \t ").next());
|
||||
|
||||
assert_eq!(Err(Unexpected('a')), CstIter::new(" 9a ").next().unwrap().value());
|
||||
|
||||
assert_eq!(Num(7), CstIter::new("7").next().unwrap().value());
|
||||
assert_eq!(Num(100), CstIter::new(" 100 ").next().unwrap().value());
|
||||
|
||||
assert_eq!(Sym(":123foo"), CstIter::new(" :123foo ").next().unwrap().value());
|
||||
assert_eq!(Sym("@bar456"), CstIter::new(" \r\r\n\n@bar456\t\t\t").next().unwrap().value());
|
||||
|
||||
assert_eq!(Key("foo123"), CstIter::new("foo123").next().unwrap().value());
|
||||
assert_eq!(Key("foo/bar"), CstIter::new("foo/bar").next().unwrap().value());
|
||||
|
||||
//assert_eq!(Str("foo/bar"), CstIter::new("\"foo/bar\"").next().unwrap().value());
|
||||
//assert_eq!(Str("foo/bar"), CstIter::new(" \"foo/bar\" ").next().unwrap().value());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//#[cfg(test)] #[test] fn test_examples () -> Result<(), DslErr> {
|
||||
//// Let's pretend to render some view.
|
||||
//let source = include_str!("../../tek/src/view_arranger.edn");
|
||||
//// The token iterator allows you to get the tokens represented by the source text.
|
||||
//let mut view = TokenIter(source);
|
||||
//// The token iterator wraps a const token+source iterator.
|
||||
//assert_eq!(view.0.0, source);
|
||||
//let mut expr = view.peek();
|
||||
//assert_eq!(view.0.0, source);
|
||||
//assert_eq!(expr, Some(Token {
|
||||
//source, start: 0, length: source.len() - 1, value: Exp(0, CstIter::new(&source[1..]))
|
||||
//}));
|
||||
////panic!("{view:?}");
|
||||
////panic!("{:#?}", expr);
|
||||
////for example in [
|
||||
////include_str!("../../tui/examples/edn01.edn"),
|
||||
////include_str!("../../tui/examples/edn02.edn"),
|
||||
////] {
|
||||
//////let items = Dsl::read_all(example)?;
|
||||
//////panic!("{layout:?}");
|
||||
//////let content = <dyn ViewContext<::tengri_engine::tui::Tui>>::from(&layout);
|
||||
////}
|
||||
//Ok(())
|
||||
//}
|
||||
Loading…
Add table
Add a link
Reference in a new issue