mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
parent
08a8dff93d
commit
cb47c4d0ff
11 changed files with 513 additions and 492 deletions
|
|
@ -1,13 +1,16 @@
|
|||
use crate::*;
|
||||
use std::sync::Arc;
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct Ast(pub Value<Arc<str>, AstIter>);
|
||||
|
||||
impl<'source> From<CstToken<'source>> for Ast {
|
||||
fn from (token: CstToken<'source>) -> Self {
|
||||
token.value().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> From<CstValue<'source>> for Ast {
|
||||
fn from (value: CstValue<'source>) -> Self {
|
||||
use Value::*;
|
||||
|
|
@ -23,10 +26,21 @@ impl<'source> From<CstValue<'source>> for Ast {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct AstIter; // TODO
|
||||
impl<'source> From<SourceIter<'source>> for AstIter {
|
||||
fn from (source: SourceIter<'source>) -> Self {
|
||||
Self // TODO
|
||||
impl DslValue for Ast {
|
||||
type Str = Arc<str>;
|
||||
type Exp = AstIter;
|
||||
fn value (&self) -> &Value<Arc<str>, AstIter> {
|
||||
self.0.value()
|
||||
}
|
||||
}
|
||||
|
||||
impl DslToken for Ast {
|
||||
type Value = Self;
|
||||
type Meta = ();
|
||||
fn value (&self) -> &Self::Value {
|
||||
self
|
||||
}
|
||||
fn meta (&self) -> &Self::Meta {
|
||||
&()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,183 @@
|
|||
use crate::*;
|
||||
|
||||
/// CST stores strings as source references and expressions as new [SourceIter] instances.
|
||||
pub type CstValue<'source> = Value<&'source str, SourceIter<'source>>;
|
||||
/// Token sharing memory with source reference.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct CstToken<'source>(pub CstValue<'source>, pub CstMeta<'source>);
|
||||
|
||||
/// Reference to the source slice.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
|
||||
pub source: &'source str,
|
||||
pub start: usize,
|
||||
pub length: usize,
|
||||
}
|
||||
|
||||
/// Token sharing memory with source reference.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct CstToken<'source>(pub CstValue<'source>, pub CstMeta<'source>);
|
||||
|
||||
impl<'source> CstToken<'source> {
|
||||
pub const fn new (
|
||||
source: &'source str, start: usize, length: usize, value: CstValue<'source>
|
||||
) -> Self {
|
||||
Self(value, CstMeta { source, start, length })
|
||||
}
|
||||
pub const fn end (&self) -> usize {
|
||||
self.1.start.saturating_add(self.1.length)
|
||||
}
|
||||
pub const fn slice (&'source self) -> &'source str {
|
||||
self.slice_source(self.1.source)
|
||||
}
|
||||
pub const fn slice_source <'range> (&'source self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.1.start, self.end())
|
||||
}
|
||||
pub const fn slice_source_exp <'range> (&'source self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.1.start.saturating_add(1), self.end())
|
||||
}
|
||||
pub const fn with_value (self, value: CstValue<'source>) -> Self {
|
||||
Self(value, self.1)
|
||||
}
|
||||
pub const fn value (&self) -> CstValue<'source> {
|
||||
self.0
|
||||
}
|
||||
pub const fn error (self, error: DslError) -> Self {
|
||||
Self(Value::Err(error), self.1)
|
||||
}
|
||||
pub const fn grow (self) -> Self {
|
||||
Self(self.0, CstMeta { length: self.1.length.saturating_add(1), ..self.1 })
|
||||
}
|
||||
pub const fn grow_num (self, m: usize, c: char) -> Self {
|
||||
use Value::*;
|
||||
match to_digit(c) {
|
||||
Result::Ok(n) => Self(Num(10*m+n), self.grow().1),
|
||||
Result::Err(e) => Self(Err(e), self.grow().1),
|
||||
}
|
||||
}
|
||||
pub const fn grow_key (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Key(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_sym (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Sym(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_str (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Str(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_exp (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
if let Exp(depth, _) = token.value() {
|
||||
token.with_value(Exp(depth, SourceIter::new(token.slice_source_exp(self.1.source))))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_in (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let Value::Exp(depth, source) = token.value() {
|
||||
token.with_value(Value::Exp(depth.saturating_add(1), source))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_out (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let Value::Exp(depth, source) = token.value() {
|
||||
if depth > 0 {
|
||||
token.with_value(Value::Exp(depth - 1, source))
|
||||
} else {
|
||||
return self.error(Unexpected(')'))
|
||||
}
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn to_number (digits: &str) -> DslResult<usize> {
|
||||
let mut value = 0;
|
||||
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
|
||||
Ok(digit) => value = 10 * value + digit,
|
||||
Result::Err(e) => return Result::Err(e)
|
||||
});
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub const fn to_digit (c: char) -> DslResult<usize> {
|
||||
Ok(match c {
|
||||
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
|
||||
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
|
||||
_ => return Result::Err(Unexpected(c))
|
||||
})
|
||||
}
|
||||
|
||||
/// Static iteration helper.
|
||||
#[macro_export] macro_rules! iterate {
|
||||
($expr:expr => $arg: pat => $body:expr) => {
|
||||
let mut iter = $expr;
|
||||
while let Some(($arg, next)) = iter.next() {
|
||||
$body;
|
||||
iter = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||
use Value::*;
|
||||
let mut token: CstToken<'a> = CstToken::new(source, 0, 0, Nil);
|
||||
iterate!(char_indices(source) => (start, c) => token = match token.value() {
|
||||
Err(_) => return Some(token),
|
||||
Nil => match c {
|
||||
' '|'\n'|'\r'|'\t' =>
|
||||
token.grow(),
|
||||
'(' =>
|
||||
CstToken::new(source, start, 1, Exp(1, SourceIter::new(str_range(source, start, start + 1)))),
|
||||
'"' =>
|
||||
CstToken::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
||||
':'|'@' =>
|
||||
CstToken::new(source, start, 1, Sym(str_range(source, start, start + 1))),
|
||||
'/'|'a'..='z' =>
|
||||
CstToken::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
||||
'0'..='9' =>
|
||||
CstToken::new(source, start, 1, match to_digit(c) {
|
||||
Ok(c) => Value::Num(c),
|
||||
Result::Err(e) => Value::Err(e)
|
||||
}),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Str(_) => match c {
|
||||
'"' => return Some(token),
|
||||
_ => token.grow_str(),
|
||||
},
|
||||
Num(n) => match c {
|
||||
'0'..='9' => token.grow_num(n, c),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Sym(_) => match c {
|
||||
'a'..='z'|'A'..='Z'|'0'..='9'|'-' => token.grow_sym(),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Key(_) => match c {
|
||||
'a'..='z'|'0'..='9'|'-'|'/' => token.grow_key(),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Exp(depth, _) => match depth {
|
||||
0 => return Some(token.grow_exp()),
|
||||
_ => match c {
|
||||
')' => token.grow_out(),
|
||||
'(' => token.grow_in(),
|
||||
_ => token.grow_exp(),
|
||||
}
|
||||
},
|
||||
});
|
||||
match token.value() {
|
||||
Nil => None,
|
||||
_ => Some(token),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@ pub trait Eval<Input, Output> {
|
|||
|
||||
/// May construct [Self] from token stream.
|
||||
pub trait Dsl<State>: Sized {
|
||||
fn try_provide (state: &State, source: impl DslValue) -> Perhaps<Self>;
|
||||
fn try_provide (state: &State, source: Ast) -> Perhaps<Self>;
|
||||
fn provide <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||
state: &State, source: impl DslValue, error: F
|
||||
state: &State, source: Ast, error: F
|
||||
) -> Usually<Self> {
|
||||
let next = format!("{source:?}");
|
||||
if let Some(value) = Self::try_provide(state, source)? {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,29 @@ pub trait DslIter {
|
|||
type Token: DslToken;
|
||||
fn peek (&self) -> Option<<Self::Token as DslToken>::Value>;
|
||||
fn next (&mut self) -> Option<<Self::Token as DslToken>::Value>;
|
||||
fn rest (&self) -> Option<&[<Self::Token as DslToken>::Value]>;
|
||||
fn rest (self) -> Vec<Self::Token>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct AstIter(std::collections::VecDeque<Ast>);
|
||||
|
||||
impl DslIter for AstIter {
|
||||
type Token = Ast;
|
||||
fn peek (&self) -> Option<Ast> {
|
||||
self.0.get(0).cloned()
|
||||
}
|
||||
fn next (&mut self) -> Option<Ast> {
|
||||
self.0.pop_front()
|
||||
}
|
||||
fn rest (self) -> Vec<Ast> {
|
||||
self.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> From<SourceIter<'source>> for AstIter {
|
||||
fn from (source: SourceIter<'source>) -> Self {
|
||||
Self(source.map(Into::into).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides a native [Iterator] API over [SourceConstIter],
|
||||
|
|
|
|||
124
dsl/src/dsl_test.rs
Normal file
124
dsl/src/dsl_test.rs
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
#[cfg(test)] mod test_token_iter {
|
||||
use crate::*;
|
||||
//use proptest::prelude::*;
|
||||
#[test] fn test_iters () {
|
||||
let mut iter = crate::SourceIter::new(&":foo :bar");
|
||||
let _ = iter.next();
|
||||
let mut iter = crate::TokenIter::new(&":foo :bar");
|
||||
let _ = iter.next();
|
||||
}
|
||||
#[test] const fn test_const_iters () {
|
||||
let mut iter = crate::SourceIter::new(&":foo :bar");
|
||||
let _ = iter.next();
|
||||
}
|
||||
#[test] fn test_num () {
|
||||
let digit = to_digit('0');
|
||||
let digit = to_digit('x');
|
||||
let number = to_number(&"123");
|
||||
let number = to_number(&"12asdf3");
|
||||
}
|
||||
//proptest! {
|
||||
//#[test] fn proptest_source_iter (
|
||||
//source in "\\PC*"
|
||||
//) {
|
||||
//let mut iter = crate::SourceIter::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 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 = crate::Token {
|
||||
source: &source,
|
||||
start,
|
||||
length,
|
||||
value: crate::Value::Nil
|
||||
};
|
||||
let _ = token.slice();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
|
||||
let source = ":f00";
|
||||
let mut token = Token { source, start: 0, length: 1, value: Sym(":") };
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Token { source, start: 0, length: 2, value: Sym(":f") });
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Token { source, start: 0, length: 3, value: Sym(":f0") });
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Token { source, start: 0, length: 4, value: Sym(":f00") });
|
||||
|
||||
let src = "";
|
||||
assert_eq!(None, SourceIter(src).next());
|
||||
|
||||
let src = " \n \r \t ";
|
||||
assert_eq!(None, SourceIter(src).next());
|
||||
|
||||
let src = "7";
|
||||
assert_eq!(Num(7), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " 100 ";
|
||||
assert_eq!(Num(100), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " 9a ";
|
||||
assert_eq!(Err(Unexpected('a')), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " :123foo ";
|
||||
assert_eq!(Sym(":123foo"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " \r\r\r\n\n\n@bar456\t\t\t\t\t\t";
|
||||
assert_eq!(Sym("@bar456"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = "foo123";
|
||||
assert_eq!(Key("foo123"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = "foo/bar";
|
||||
assert_eq!(Key("foo/bar"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = "\"foo/bar\"";
|
||||
assert_eq!(Str("foo/bar"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " \"foo/bar\" ";
|
||||
assert_eq!(Str("foo/bar"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//#[cfg(test)] #[test] fn test_examples () -> Result<(), DslError> {
|
||||
//// 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, SourceIter(&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(())
|
||||
//}
|
||||
|
|
@ -1,67 +1,5 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(PartialEq, Clone, Default, Debug)]
|
||||
pub enum Value<
|
||||
S: PartialEq + Clone + Default + Debug,
|
||||
X: PartialEq + Clone + Default + Debug,
|
||||
> {
|
||||
#[default] Nil, Err(DslError), Num(usize), Sym(S), Key(S), Str(S), Exp(usize, X),
|
||||
}
|
||||
|
||||
impl<
|
||||
S: Copy + PartialEq + Clone + Default + Debug,
|
||||
X: Copy + PartialEq + Clone + Default + Debug,
|
||||
> Copy for Value<S, X> {}
|
||||
|
||||
pub trait DslValue: PartialEq + Clone + Default + Debug {
|
||||
type Err: PartialEq + Clone + Debug;
|
||||
type Num: PartialEq + Copy + Clone + Default + Debug;
|
||||
type Str: PartialEq + Clone + Default + Debug;
|
||||
type Exp: PartialEq + Clone + Default + Debug;
|
||||
fn nil (&self) -> bool;
|
||||
fn err (&self) -> Option<&Self::Err>;
|
||||
fn num (&self) -> Option<Self::Num>;
|
||||
fn sym (&self) -> Option<&Self::Str>;
|
||||
fn key (&self) -> Option<&Self::Str>;
|
||||
fn str (&self) -> Option<&Self::Str>;
|
||||
fn exp (&self) -> Option<&Self::Exp>;
|
||||
fn exp_depth (&self) -> Option<usize> { None } // TODO
|
||||
fn exp_head (&self) -> Option<&Self> { None } // TODO
|
||||
fn exp_tail (&self) -> Option<&[Self]> { None } // TODO
|
||||
}
|
||||
|
||||
impl<
|
||||
Str: PartialEq + Clone + Default + Debug,
|
||||
Exp: PartialEq + Clone + Default + Debug,
|
||||
> DslValue for Value<Str, Exp> {
|
||||
type Err = DslError;
|
||||
type Num = usize;
|
||||
type Str = Str;
|
||||
type Exp = Exp;
|
||||
fn nil (&self) -> bool {
|
||||
matches!(self, Self::Nil)
|
||||
}
|
||||
fn err (&self) -> Option<&DslError> {
|
||||
if let Self::Err(e) = self { Some(e) } else { None }
|
||||
}
|
||||
fn num (&self) -> Option<usize> {
|
||||
if let Self::Num(n) = self { Some(*n) } else { None }
|
||||
}
|
||||
fn sym (&self) -> Option<&Str> {
|
||||
if let Self::Sym(s) = self { Some(s) } else { None }
|
||||
}
|
||||
fn key (&self) -> Option<&Str> {
|
||||
if let Self::Key(k) = self { Some(k) } else { None }
|
||||
}
|
||||
fn str (&self) -> Option<&Str> {
|
||||
if let Self::Str(s) = self { Some(s) } else { None }
|
||||
}
|
||||
fn exp (&self) -> Option<&Exp> {
|
||||
if let Self::Exp(_, x) = self { Some(x) } else { None }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Clone, Default, Debug)]
|
||||
pub struct Token<
|
||||
V: PartialEq + Clone + Default + Debug,
|
||||
|
|
@ -85,186 +23,3 @@ impl<V: DslValue, M: PartialEq + Clone + Default + Debug> DslToken for Token<V,
|
|||
&self.1
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> CstToken<'source> {
|
||||
pub const fn new (
|
||||
source: &'source str, start: usize, length: usize, value: CstValue<'source>
|
||||
) -> Self {
|
||||
Self(value, CstMeta { source, start, length })
|
||||
}
|
||||
pub const fn end (&self) -> usize {
|
||||
self.1.start.saturating_add(self.1.length)
|
||||
}
|
||||
pub const fn slice (&'source self) -> &'source str {
|
||||
self.slice_source(self.1.source)
|
||||
}
|
||||
pub const fn slice_source <'range> (&'source self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.1.start, self.end())
|
||||
}
|
||||
pub const fn slice_source_exp <'range> (&'source self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.1.start.saturating_add(1), self.end())
|
||||
}
|
||||
pub const fn with_value (self, value: CstValue<'source>) -> Self {
|
||||
Self(value, self.1)
|
||||
}
|
||||
pub const fn value (&self) -> CstValue<'source> {
|
||||
self.0
|
||||
}
|
||||
pub const fn error (self, error: DslError) -> Self {
|
||||
Self(Value::Err(error), self.1)
|
||||
}
|
||||
pub const fn grow (self) -> Self {
|
||||
Self(self.0, CstMeta { length: self.1.length.saturating_add(1), ..self.1 })
|
||||
}
|
||||
pub const fn grow_num (self, m: usize, c: char) -> Self {
|
||||
use Value::*;
|
||||
match to_digit(c) {
|
||||
Result::Ok(n) => Self(Num(10*m+n), self.grow().1),
|
||||
Result::Err(e) => Self(Err(e), self.grow().1),
|
||||
}
|
||||
}
|
||||
pub const fn grow_key (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Key(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_sym (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Sym(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_str (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Str(token.slice_source(self.1.source)))
|
||||
}
|
||||
pub const fn grow_exp (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
if let Exp(depth, _) = token.value() {
|
||||
token.with_value(Exp(depth, SourceIter::new(token.slice_source_exp(self.1.source))))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_in (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let Value::Exp(depth, source) = token.value() {
|
||||
token.with_value(Value::Exp(depth.saturating_add(1), source))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_out (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let Value::Exp(depth, source) = token.value() {
|
||||
if depth > 0 {
|
||||
token.with_value(Value::Exp(depth - 1, source))
|
||||
} else {
|
||||
return self.error(Unexpected(')'))
|
||||
}
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn to_number (digits: &str) -> DslResult<usize> {
|
||||
let mut value = 0;
|
||||
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
|
||||
Ok(digit) => value = 10 * value + digit,
|
||||
Result::Err(e) => return Result::Err(e)
|
||||
});
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub const fn to_digit (c: char) -> DslResult<usize> {
|
||||
Ok(match c {
|
||||
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
|
||||
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
|
||||
_ => return Result::Err(Unexpected(c))
|
||||
})
|
||||
}
|
||||
|
||||
/// Static iteration helper.
|
||||
#[macro_export] macro_rules! iterate {
|
||||
($expr:expr => $arg: pat => $body:expr) => {
|
||||
let mut iter = $expr;
|
||||
while let Some(($arg, next)) = iter.next() {
|
||||
$body;
|
||||
iter = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||
use Value::*;
|
||||
let mut token: CstToken<'a> = CstToken::new(source, 0, 0, Nil);
|
||||
iterate!(char_indices(source) => (start, c) => token = match token.value() {
|
||||
Err(_) => return Some(token),
|
||||
Nil => match c {
|
||||
' '|'\n'|'\r'|'\t' =>
|
||||
token.grow(),
|
||||
'(' =>
|
||||
CstToken::new(source, start, 1, Exp(1, SourceIter::new(str_range(source, start, start + 1)))),
|
||||
'"' =>
|
||||
CstToken::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
||||
':'|'@' =>
|
||||
CstToken::new(source, start, 1, Sym(str_range(source, start, start + 1))),
|
||||
'/'|'a'..='z' =>
|
||||
CstToken::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
||||
'0'..='9' =>
|
||||
CstToken::new(source, start, 1, match to_digit(c) {
|
||||
Ok(c) => Value::Num(c),
|
||||
Result::Err(e) => Value::Err(e)
|
||||
}),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Str(_) => match c {
|
||||
'"' => return Some(token),
|
||||
_ => token.grow_str(),
|
||||
},
|
||||
Num(n) => match c {
|
||||
'0'..='9' => token.grow_num(n, c),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Sym(_) => match c {
|
||||
'a'..='z'|'A'..='Z'|'0'..='9'|'-' => token.grow_sym(),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Key(_) => match c {
|
||||
'a'..='z'|'0'..='9'|'-'|'/' => token.grow_key(),
|
||||
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
Exp(depth, _) => match depth {
|
||||
0 => return Some(token.grow_exp()),
|
||||
_ => match c {
|
||||
')' => token.grow_out(),
|
||||
'(' => token.grow_in(),
|
||||
_ => token.grow_exp(),
|
||||
}
|
||||
},
|
||||
});
|
||||
match token.value() {
|
||||
Nil => None,
|
||||
_ => Some(token),
|
||||
}
|
||||
}
|
||||
|
||||
//impl<S1, S2: From<S1>, X1, X2: From<S2>> From<Value<S1, X1>> for Value<S2, X2> {
|
||||
//fn from (other: Value<S2, X2>) -> Self {
|
||||
//use Value::*;
|
||||
//match other {
|
||||
//Nil => Nil,
|
||||
//Err(e) => Err(e),
|
||||
//Num(u) => Num(u),
|
||||
//Sym(s) => Sym(s.into()),
|
||||
//Key(s) => Key(s.into()),
|
||||
//Str(s) => Str(s.into()),
|
||||
//Exp(x) => Exp(x.into()),
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
|
|
|
|||
84
dsl/src/dsl_value.rs
Normal file
84
dsl/src/dsl_value.rs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
use crate::*;
|
||||
use std::fmt::Display;
|
||||
pub trait DslValue: PartialEq + Clone + Default + Debug {
|
||||
type Str: AsRef<str> + PartialEq + Clone + Default + Debug;
|
||||
type Exp: PartialEq + Clone + Default + Debug;
|
||||
fn value (&self) -> &Value<Self::Str, Self::Exp>;
|
||||
fn nil (&self) -> bool {
|
||||
matches!(self.value(), Value::Nil)
|
||||
}
|
||||
fn err (&self) -> Option<&DslError> {
|
||||
if let Value::Err(e) = self.value() { Some(e) } else { None }
|
||||
}
|
||||
fn num (&self) -> Option<usize> {
|
||||
if let Value::Num(n) = self.value() { Some(*n) } else { None }
|
||||
}
|
||||
fn sym (&self) -> Option<&str> {
|
||||
if let Value::Sym(s) = self.value() { Some(s.as_ref()) } else { None }
|
||||
}
|
||||
fn key (&self) -> Option<&str> {
|
||||
if let Value::Key(k) = self.value() { Some(k.as_ref()) } else { None }
|
||||
}
|
||||
fn str (&self) -> Option<&str> {
|
||||
if let Value::Str(s) = self.value() { Some(s.as_ref()) } else { None }
|
||||
}
|
||||
fn exp (&self) -> Option<&Self::Exp> {
|
||||
if let Value::Exp(_, x) = self.value() { Some(x) } else { None }
|
||||
}
|
||||
fn exp_depth (&self) -> Option<usize> { None } // TODO
|
||||
fn exp_head (&self) -> Option<&Self> { None } // TODO
|
||||
fn exp_tail (&self) -> Option<&[Self]> { None } // TODO
|
||||
}
|
||||
impl<
|
||||
Str: AsRef<str> + PartialEq + Clone + Default + Debug,
|
||||
Exp: PartialEq + Clone + Default + Debug,
|
||||
> DslValue for Value<Str, Exp> {
|
||||
type Str = Str;
|
||||
type Exp = Exp;
|
||||
fn value (&self) -> &Value<Str, Exp> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Value<S, X> { Nil, Err(DslError), Num(usize), Sym(S), Key(S), Str(S), Exp(usize, X), }
|
||||
impl<S, X> Default for Value<S, X> { fn default () -> Self { Self:: Nil } }
|
||||
impl<S: PartialEq, X: PartialEq,> PartialEq for Value<S, X> {
|
||||
fn eq (&self, other: &Self) -> bool {
|
||||
use Value::*;
|
||||
match (self, other) {
|
||||
(Nil, Nil) => true,
|
||||
(Err(e1), Err(e2)) if e1 == e2 => true,
|
||||
(Num(n1), Num(n2)) if n1 == n2 => true,
|
||||
(Sym(s1), Sym(s2)) if s1 == s2 => true,
|
||||
(Key(s1), Key(s2)) if s1 == s2 => true,
|
||||
(Str(s1), Str(s2)) if s1 == s2 => true,
|
||||
(Exp(d1, e1), Exp(d2, e2)) if d1 == d2 && e1 == e2 => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<S: Clone, X: Clone,> Clone for Value<S, X> {
|
||||
fn clone (&self) -> Self {
|
||||
use Value::*;
|
||||
match self {
|
||||
Nil => Nil,
|
||||
Err(e) => Err(e.clone()),
|
||||
Num(n) => Num(*n),
|
||||
Sym(s) => Sym(s.clone()),
|
||||
Key(s) => Key(s.clone()),
|
||||
Str(s) => Str(s.clone()),
|
||||
Exp(d, e) => Exp(*d, e.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<S: Copy, X: Copy,> Copy for Value<S, X> {}
|
||||
impl<S: Debug, X: Debug,> Debug for Value<S, X> {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl<S: Display, X: Display,> Display for Value<S, X> {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
137
dsl/src/lib.rs
137
dsl/src/lib.rs
|
|
@ -1,7 +1,3 @@
|
|||
#![feature(adt_const_params)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_fn_trait_return)]
|
||||
|
||||
//! [Token]s are parsed substrings with an associated [Value].
|
||||
//!
|
||||
//! * [ ] FIXME: Value may be [Err] which may shadow [Result::Err]
|
||||
|
|
@ -35,15 +31,15 @@
|
|||
//! assert_eq!(view.0.0, src);
|
||||
//! assert_eq!(view.peek(), view.0.peek())
|
||||
//! ```
|
||||
|
||||
#![feature(adt_const_params)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_fn_trait_return)]
|
||||
pub(crate) use ::tengri_core::*;
|
||||
|
||||
pub(crate) use std::fmt::Debug;
|
||||
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::*;
|
||||
|
||||
mod dsl_ast; pub use self::dsl_ast::*;
|
||||
mod dsl_cst; pub use self::dsl_cst::*;
|
||||
mod dsl_display; pub use self::dsl_display::*;
|
||||
|
|
@ -51,128 +47,5 @@ mod dsl_domain; pub use self::dsl_domain::*;
|
|||
mod dsl_error; pub use self::dsl_error::*;
|
||||
mod dsl_iter; pub use self::dsl_iter::*;
|
||||
mod dsl_token; pub use self::dsl_token::*;
|
||||
|
||||
#[cfg(test)] mod test_token_iter {
|
||||
use crate::*;
|
||||
//use proptest::prelude::*;
|
||||
#[test] fn test_iters () {
|
||||
let mut iter = crate::SourceIter::new(&":foo :bar");
|
||||
let _ = iter.next();
|
||||
let mut iter = crate::TokenIter::new(&":foo :bar");
|
||||
let _ = iter.next();
|
||||
}
|
||||
#[test] const fn test_const_iters () {
|
||||
let mut iter = crate::SourceIter::new(&":foo :bar");
|
||||
let _ = iter.next();
|
||||
}
|
||||
#[test] fn test_num () {
|
||||
let digit = to_digit('0');
|
||||
let digit = to_digit('x');
|
||||
let number = to_number(&"123");
|
||||
let number = to_number(&"12asdf3");
|
||||
}
|
||||
//proptest! {
|
||||
//#[test] fn proptest_source_iter (
|
||||
//source in "\\PC*"
|
||||
//) {
|
||||
//let mut iter = crate::SourceIter::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 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 = crate::Token {
|
||||
source: &source,
|
||||
start,
|
||||
length,
|
||||
value: crate::Value::Nil
|
||||
};
|
||||
let _ = token.slice();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
|
||||
let source = ":f00";
|
||||
let mut token = Token { source, start: 0, length: 1, value: Sym(":") };
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Token { source, start: 0, length: 2, value: Sym(":f") });
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Token { source, start: 0, length: 3, value: Sym(":f0") });
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Token { source, start: 0, length: 4, value: Sym(":f00") });
|
||||
|
||||
let src = "";
|
||||
assert_eq!(None, SourceIter(src).next());
|
||||
|
||||
let src = " \n \r \t ";
|
||||
assert_eq!(None, SourceIter(src).next());
|
||||
|
||||
let src = "7";
|
||||
assert_eq!(Num(7), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " 100 ";
|
||||
assert_eq!(Num(100), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " 9a ";
|
||||
assert_eq!(Err(Unexpected('a')), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " :123foo ";
|
||||
assert_eq!(Sym(":123foo"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " \r\r\r\n\n\n@bar456\t\t\t\t\t\t";
|
||||
assert_eq!(Sym("@bar456"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = "foo123";
|
||||
assert_eq!(Key("foo123"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = "foo/bar";
|
||||
assert_eq!(Key("foo/bar"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = "\"foo/bar\"";
|
||||
assert_eq!(Str("foo/bar"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
let src = " \"foo/bar\" ";
|
||||
assert_eq!(Str("foo/bar"), SourceIter(src).next().unwrap().0.value);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//#[cfg(test)] #[test] fn test_examples () -> Result<(), DslError> {
|
||||
//// 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, SourceIter(&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(())
|
||||
//}
|
||||
mod dsl_value; pub use self::dsl_value::*;
|
||||
#[cfg(test)] mod dsl_test;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue