mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
This commit is contained in:
parent
c8827b43c3
commit
cd4df6e222
15 changed files with 749 additions and 611 deletions
31
dsl/src/ast.rs
Normal file
31
dsl/src/ast.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use crate::*;
|
||||
|
||||
//#[derive(Debug, Clone, Default, PartialEq)]
|
||||
//pub struct Ast(pub AstValue);
|
||||
|
||||
/// The abstract syntax tree (AST) can be produced from the CST
|
||||
/// by cloning source slices into owned [Arc] values.
|
||||
pub type Ast = DslValue<Arc<str>, VecDeque<Ast>>;
|
||||
|
||||
//#[derive(Debug, Clone, Default, PartialEq)]
|
||||
//pub struct AstIter();
|
||||
|
||||
impl<'src> From<Cst<'src>> for Ast {
|
||||
fn from (token: Cst<'src>) -> Self {
|
||||
token.value().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> From<CstValue<'src>> for Ast {
|
||||
fn from (value: CstValue<'src>) -> Self {
|
||||
match value {
|
||||
DslValue::Nil => DslValue::Nil,
|
||||
DslValue::Err(e) => DslValue::Err(e),
|
||||
DslValue::Num(u) => DslValue::Num(u),
|
||||
DslValue::Sym(s) => DslValue::Sym(s.into()),
|
||||
DslValue::Key(s) => DslValue::Key(s.into()),
|
||||
DslValue::Str(s) => DslValue::Str(s.into()),
|
||||
DslValue::Exp(d, x) => DslValue::Exp(d, x.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
206
dsl/src/cst.rs
Normal file
206
dsl/src/cst.rs
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
//! 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.
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// Implement the const iterator pattern.
|
||||
macro_rules! const_iter {
|
||||
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
||||
impl$(<$l>)? Iterator for $Struct {
|
||||
type Item = $Item;
|
||||
fn next (&mut $self) -> Option<$Item> { $expr }
|
||||
}
|
||||
impl$(<$l>)? ConstIntoIter for $Struct {
|
||||
type Kind = IsIteratorKind;
|
||||
type Item = $Item;
|
||||
type IntoIter = Self;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Static iteration helper used by [cst].
|
||||
macro_rules! iterate {
|
||||
($expr:expr => $arg: pat => $body:expr) => {
|
||||
let mut iter = $expr;
|
||||
while let Some(($arg, next)) = iter.next() {
|
||||
$body;
|
||||
iter = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod cst_const; pub use self::cst_const::*;
|
||||
mod cst_iter; pub use self::cst_iter::*;
|
||||
mod cst_token; pub use self::cst_token::*;
|
||||
|
||||
/// CST stores strings as source references and expressions as [SourceIter] instances.
|
||||
pub type CstValue<'source> = DslValue<&'source str, SourceIter<'source>>;
|
||||
|
||||
/// Token sharing memory with source reference.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct Cst<'src> {
|
||||
/// Reference to source text.
|
||||
pub source: &'src str,
|
||||
/// Index of 1st character of token.
|
||||
pub start: usize,
|
||||
/// Length of token.
|
||||
pub length: usize,
|
||||
/// Meaning of token.
|
||||
pub value: CstValue<'src>,
|
||||
}
|
||||
|
||||
impl<'src> Cst<'src> {
|
||||
pub const fn new (
|
||||
source: &'src str,
|
||||
start: usize,
|
||||
length: usize,
|
||||
value: CstValue<'src>
|
||||
) -> Self {
|
||||
Self { source, start, length, value }
|
||||
}
|
||||
pub const fn end (&self) -> usize {
|
||||
self.start.saturating_add(self.length)
|
||||
}
|
||||
pub const fn slice (&'src self) -> &'src str {
|
||||
self.slice_source(self.source)
|
||||
}
|
||||
pub const fn slice_source <'range> (&'src self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.start, self.end())
|
||||
}
|
||||
pub const fn slice_source_exp <'range> (&'src self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.start.saturating_add(1), self.end())
|
||||
}
|
||||
pub const fn with_value (self, value: CstValue<'src>) -> Self {
|
||||
Self { value, ..self }
|
||||
}
|
||||
pub const fn value (&self) -> CstValue<'src> {
|
||||
self.value
|
||||
}
|
||||
pub const fn error (self, error: DslError) -> Self {
|
||||
Self { value: DslValue::Err(error), ..self }
|
||||
}
|
||||
pub const fn grow (self) -> Self {
|
||||
Self { length: self.length.saturating_add(1), ..self }
|
||||
}
|
||||
pub const fn grow_num (self, m: usize, c: char) -> Self {
|
||||
match to_digit(c) {
|
||||
Result::Ok(n) => Self { value: DslValue::Num(10*m+n), ..self.grow() },
|
||||
Result::Err(e) => Self { value: DslValue::Err(e), ..self.grow() },
|
||||
}
|
||||
}
|
||||
pub const fn grow_key (self) -> Self {
|
||||
let token = self.grow();
|
||||
token.with_value(DslValue::Key(token.slice_source(self.source)))
|
||||
}
|
||||
pub const fn grow_sym (self) -> Self {
|
||||
let token = self.grow();
|
||||
token.with_value(DslValue::Sym(token.slice_source(self.source)))
|
||||
}
|
||||
pub const fn grow_str (self) -> Self {
|
||||
let token = self.grow();
|
||||
token.with_value(DslValue::Str(token.slice_source(self.source)))
|
||||
}
|
||||
pub const fn grow_exp (self) -> Self {
|
||||
let token = self.grow();
|
||||
if let DslValue::Exp(depth, _) = token.value() {
|
||||
token.with_value(DslValue::Exp(depth, SourceIter::new(token.slice_source_exp(self.source))))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_in (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let DslValue::Exp(depth, source) = token.value() {
|
||||
token.with_value(DslValue::Exp(depth.saturating_add(1), source))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_out (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let DslValue::Exp(depth, source) = token.value() {
|
||||
if depth > 0 {
|
||||
token.with_value(DslValue::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))
|
||||
})
|
||||
}
|
||||
|
||||
pub const fn peek_src <'src> (source: &'src str) -> Option<Cst<'src>> {
|
||||
use DslValue::*;
|
||||
let mut token: Cst<'src> = Cst::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(),
|
||||
'(' =>
|
||||
Cst::new(source, start, 1, Exp(1, SourceIter::new(str_range(source, start, start + 1)))),
|
||||
'"' =>
|
||||
Cst::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
||||
':'|'@' =>
|
||||
Cst::new(source, start, 1, Sym(str_range(source, start, start + 1))),
|
||||
'/'|'a'..='z' =>
|
||||
Cst::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
||||
'0'..='9' =>
|
||||
Cst::new(source, start, 1, match to_digit(c) {
|
||||
Ok(c) => DslValue::Num(c),
|
||||
Result::Err(e) => DslValue::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),
|
||||
}
|
||||
}
|
||||
44
dsl/src/cst/cst_const.rs
Normal file
44
dsl/src/cst/cst_const.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use crate::*;
|
||||
|
||||
/// Owns a reference to the source text.
|
||||
/// [SourceConstIter::next] emits subsequent pairs of:
|
||||
/// * a [Cst] and
|
||||
/// * the source text remaining
|
||||
/// * [ ] TODO: maybe [SourceConstIter::next] should wrap the remaining source in `Self` ?
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct SourceConstIter<'src>(pub &'src str);
|
||||
|
||||
impl<'src> From<SourceConstIter<'src>> for SourceIter<'src> {
|
||||
fn from (source: SourceConstIter<'src>) -> Self{
|
||||
Self(source)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> From<&'src str> for SourceConstIter<'src> {
|
||||
fn from (source: &'src str) -> Self{
|
||||
Self::new(source)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> SourceConstIter<'src> {
|
||||
pub const fn new (source: &'src str) -> Self {
|
||||
Self(source)
|
||||
}
|
||||
pub const fn chomp (&self, index: usize) -> Self {
|
||||
Self(split_at(self.0, index).1)
|
||||
}
|
||||
pub const fn next (mut self) -> Option<(Cst<'src>, Self)> {
|
||||
Self::next_mut(&mut self)
|
||||
}
|
||||
pub const fn peek (&self) -> Option<Cst<'src>> {
|
||||
peek_src(self.0)
|
||||
}
|
||||
pub const fn next_mut (&mut self) -> Option<(Cst<'src>, Self)> {
|
||||
match self.peek() {
|
||||
Some(token) => Some((token, self.chomp(token.end()))),
|
||||
None => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const_iter!(<'src>|self: SourceConstIter<'src>| => Cst<'src> => self.next_mut().map(|(result, _)|result));
|
||||
46
dsl/src/cst/cst_iter.rs
Normal file
46
dsl/src/cst/cst_iter.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use crate::*;
|
||||
|
||||
/// Provides a native [Iterator] API over [SourceConstIter],
|
||||
/// emitting [Cst] items.
|
||||
///
|
||||
/// [Cst::next] returns just the [Cst] and mutates `self`,
|
||||
/// instead of returning an updated version of the struct as [SourceConstIter::next] does.
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct SourceIter<'src>(pub SourceConstIter<'src>);
|
||||
|
||||
impl<'src> SourceIter<'src> {
|
||||
pub const fn new (source: &'src str) -> Self {
|
||||
Self(SourceConstIter::new(source))
|
||||
}
|
||||
pub const fn peek (&self) -> Option<Cst<'src>> {
|
||||
self.0.peek()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> Iterator for SourceIter<'src> {
|
||||
type Item = Cst<'src>;
|
||||
fn next (&mut self) -> Option<Cst<'src>> {
|
||||
self.0.next().map(|(item, rest)|{
|
||||
self.0 = rest;
|
||||
item
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> From<&'src str> for SourceIter<'src> {
|
||||
fn from (source: &'src str) -> Self{
|
||||
Self(SourceConstIter(source))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> Into<Vec<Cst<'src>>> for SourceIter<'src> {
|
||||
fn into (self) -> Vec<Cst<'src>> {
|
||||
self.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> Into<Vec<Ast>> for SourceIter<'src> {
|
||||
fn into (self) -> Vec<Ast> {
|
||||
self.map(Into::into).collect()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,93 +1,90 @@
|
|||
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 struct Cst<'src> {
|
||||
/// Reference to source text.
|
||||
pub source: &'src str,
|
||||
/// Index of 1st character of token.
|
||||
pub start: usize,
|
||||
/// Length of token.
|
||||
pub length: usize,
|
||||
/// Meaning of token.
|
||||
pub value: CstValue<'src>,
|
||||
}
|
||||
|
||||
impl<'source> CstToken<'source> {
|
||||
impl<'src> Cst<'src> {
|
||||
pub const fn new (
|
||||
source: &'source str, start: usize, length: usize, value: CstValue<'source>
|
||||
source: &'src str,
|
||||
start: usize,
|
||||
length: usize,
|
||||
value: CstValue<'src>
|
||||
) -> Self {
|
||||
Self(value, CstMeta { source, start, length })
|
||||
Self { source, start, length, value }
|
||||
}
|
||||
pub const fn end (&self) -> usize {
|
||||
self.1.start.saturating_add(self.1.length)
|
||||
self.start.saturating_add(self.length)
|
||||
}
|
||||
pub const fn slice (&'source self) -> &'source str {
|
||||
self.slice_source(self.1.source)
|
||||
pub const fn slice (&'src self) -> &'src str {
|
||||
self.slice_source(self.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 <'range> (&'src self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.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 slice_source_exp <'range> (&'src self, source: &'range str) -> &'range str {
|
||||
str_range(source, self.start.saturating_add(1), self.end())
|
||||
}
|
||||
pub const fn with_value (self, value: CstValue<'source>) -> Self {
|
||||
Self(value, self.1)
|
||||
pub const fn with_value (self, value: CstValue<'src>) -> Self {
|
||||
Self { value, ..self }
|
||||
}
|
||||
pub const fn value (&self) -> CstValue<'source> {
|
||||
self.0
|
||||
pub const fn value (&self) -> CstValue<'src> {
|
||||
self.value
|
||||
}
|
||||
pub const fn error (self, error: DslError) -> Self {
|
||||
Self(Value::Err(error), self.1)
|
||||
Self { value: DslValue::Err(error), ..self }
|
||||
}
|
||||
pub const fn grow (self) -> Self {
|
||||
Self(self.0, CstMeta { length: self.1.length.saturating_add(1), ..self.1 })
|
||||
Self { length: self.length.saturating_add(1), ..self }
|
||||
}
|
||||
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),
|
||||
Result::Ok(n) => Self { value: DslValue::Num(10*m+n), ..self.grow() },
|
||||
Result::Err(e) => Self { value: DslValue::Err(e), ..self.grow() },
|
||||
}
|
||||
}
|
||||
pub const fn grow_key (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Key(token.slice_source(self.1.source)))
|
||||
token.with_value(DslValue::Key(token.slice_source(self.source)))
|
||||
}
|
||||
pub const fn grow_sym (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Sym(token.slice_source(self.1.source)))
|
||||
token.with_value(DslValue::Sym(token.slice_source(self.source)))
|
||||
}
|
||||
pub const fn grow_str (self) -> Self {
|
||||
use Value::*;
|
||||
let token = self.grow();
|
||||
token.with_value(Str(token.slice_source(self.1.source)))
|
||||
token.with_value(DslValue::Str(token.slice_source(self.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))))
|
||||
if let DslValue::Exp(depth, _) = token.value() {
|
||||
token.with_value(DslValue::Exp(depth, SourceIter::new(token.slice_source_exp(self.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))
|
||||
if let DslValue::Exp(depth, source) = token.value() {
|
||||
token.with_value(DslValue::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 let DslValue::Exp(depth, source) = token.value() {
|
||||
if depth > 0 {
|
||||
token.with_value(Value::Exp(depth - 1, source))
|
||||
token.with_value(DslValue::Exp(depth - 1, source))
|
||||
} else {
|
||||
return self.error(Unexpected(')'))
|
||||
}
|
||||
|
|
@ -114,37 +111,26 @@ pub const fn to_digit (c: char) -> DslResult<usize> {
|
|||
})
|
||||
}
|
||||
|
||||
/// 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);
|
||||
pub const fn peek_src <'src> (source: &'src str) -> Option<Cst<'src>> {
|
||||
use DslValue::*;
|
||||
let mut token: Cst<'src> = Cst::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)))),
|
||||
Cst::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))),
|
||||
Cst::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
||||
':'|'@' =>
|
||||
CstToken::new(source, start, 1, Sym(str_range(source, start, start + 1))),
|
||||
Cst::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))),
|
||||
Cst::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)
|
||||
Cst::new(source, start, 1, match to_digit(c) {
|
||||
Ok(c) => DslValue::Num(c),
|
||||
Result::Err(e) => DslValue::Err(e)
|
||||
}),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
142
dsl/src/dsl.rs
Normal file
142
dsl/src/dsl.rs
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Error, Debug, Copy, Clone, PartialEq)] pub enum DslError {
|
||||
#[error("parse failed: not implemented")]
|
||||
Unimplemented,
|
||||
#[error("parse failed: empty")]
|
||||
Empty,
|
||||
#[error("parse failed: incomplete")]
|
||||
Incomplete,
|
||||
#[error("parse failed: unexpected character '{0}'")]
|
||||
Unexpected(char),
|
||||
#[error("parse failed: error #{0}")]
|
||||
Code(u8),
|
||||
}
|
||||
|
||||
/// Thing that may construct itself from `State` and [DslValue].
|
||||
pub trait FromDsl<State>: Sized {
|
||||
fn try_provide (state: &State, value: DslValue<impl DslStr, impl DslExp>) -> Perhaps<Self>;
|
||||
fn provide (
|
||||
state: &State,
|
||||
value: DslValue<impl DslStr, impl DslExp>,
|
||||
error: impl Fn()->Box<dyn std::error::Error>
|
||||
) -> Usually<Self> {
|
||||
match Self::try_provide(state, value)? {
|
||||
Some(value) => Ok(value),
|
||||
_ => Err(error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type DslResult<T> = Result<T, DslError>;
|
||||
|
||||
/// Marker trait for supported string types.
|
||||
trait DslStr: PartialEq + Clone + Default + Debug + AsRef<str> {}
|
||||
impl<T: PartialEq + Clone + Default + Debug + AsRef<str>> DslStr for T {}
|
||||
|
||||
/// Marker trait for supported expression types.
|
||||
trait DslExp: PartialEq + Clone + Default + Debug {}
|
||||
impl<T: PartialEq + Clone + Default + Debug> DslExp for T {}
|
||||
|
||||
/// A DSL value generic over string and expression types.
|
||||
/// See [CstValue] and [AstValue].
|
||||
pub enum DslValue<Str: DslStr, Exp: DslExp> {
|
||||
Nil,
|
||||
Err(DslError),
|
||||
Num(usize),
|
||||
Sym(Str),
|
||||
Key(Str),
|
||||
Str(Str),
|
||||
Exp(usize, Exp),
|
||||
}
|
||||
|
||||
impl<Str: DslStr, Exp: DslExp> DslValue<Str, Exp> {
|
||||
fn nil (&self) -> bool {
|
||||
matches!(self, DslValue::Nil)
|
||||
}
|
||||
fn err (&self) -> Option<&DslError> {
|
||||
if let DslValue::Err(e) = self { Some(e) } else { None }
|
||||
}
|
||||
fn num (&self) -> Option<usize> {
|
||||
if let DslValue::Num(n) = self { Some(*n) } else { None }
|
||||
}
|
||||
fn sym (&self) -> Option<&str> {
|
||||
if let DslValue::Sym(s) = self { Some(s.as_ref()) } else { None }
|
||||
}
|
||||
fn key (&self) -> Option<&str> {
|
||||
if let DslValue::Key(k) = self { Some(k.as_ref()) } else { None }
|
||||
}
|
||||
fn str (&self) -> Option<&str> {
|
||||
if let DslValue::Str(s) = self { Some(s.as_ref()) } else { None }
|
||||
}
|
||||
fn exp (&self) -> Option<&Exp> {
|
||||
if let DslValue::Exp(_, x) = self { Some(x) } else { None }
|
||||
}
|
||||
fn exp_depth (&self) -> Option<usize> {
|
||||
todo!()
|
||||
}
|
||||
fn exp_head (&self) -> Option<&Self> {
|
||||
todo!()
|
||||
} // TODO
|
||||
fn exp_tail (&self) -> Option<&[Self]> {
|
||||
todo!()
|
||||
} // TODO
|
||||
fn peek (&self) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
fn next (&mut self) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
fn rest (self) -> Vec<Self> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Str: DslStr, Exp: DslExp> Default for DslValue<Str, Exp> {
|
||||
fn default () -> Self { Self:: Nil }
|
||||
}
|
||||
|
||||
impl<Str: DslStr, Exp: DslExp> PartialEq for DslValue<Str, Exp> {
|
||||
fn eq (&self, other: &Self) -> bool {
|
||||
use DslValue::*;
|
||||
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<Str: DslStr, Exp: DslExp> Clone for DslValue<Str, Exp> {
|
||||
fn clone (&self) -> Self {
|
||||
use DslValue::*;
|
||||
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<Str: DslStr, Exp: DslExp> Debug for DslValue<Str, Exp> {
|
||||
fn fmt (&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Str: DslStr, Exp: DslExp> Display for DslValue<Str, Exp> {
|
||||
fn fmt (&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Str: DslStr + Copy, Exp: DslExp + Copy> Copy for DslValue<Str, Exp> {}
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
use crate::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[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::*;
|
||||
Self(match value {
|
||||
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(d, x) => Exp(d, x.into()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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,29 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
impl Display for Ast {
|
||||
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||
match &self.0 {
|
||||
Value::Nil => Ok(()),
|
||||
Value::Err(e) => write!(out, "[error: {e}]"),
|
||||
Value::Num(n) => write!(out, "{n}"),
|
||||
Value::Sym(s) => write!(out, "{s}"),
|
||||
Value::Key(s) => write!(out, "{s}"),
|
||||
Value::Str(s) => write!(out, "{s}"),
|
||||
Value::Exp(_, e) => write!(out, "{e:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> Display for CstValue<'source> {
|
||||
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||
match self {
|
||||
Value::Nil => Ok(()),
|
||||
Value::Err(e) => write!(out, "[error: {e}]"),
|
||||
Value::Num(n) => write!(out, "{n}"),
|
||||
Value::Sym(s) => write!(out, "{s}"),
|
||||
Value::Key(s) => write!(out, "{s}"),
|
||||
Value::Str(s) => write!(out, "{s}"),
|
||||
Value::Exp(_, e) => write!(out, "{e:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
pub type DslResult<T> = Result<T, DslError>;
|
||||
|
||||
#[derive(Error, Debug, Copy, Clone, PartialEq)] pub enum DslError {
|
||||
#[error("parse failed: not implemented")]
|
||||
Unimplemented,
|
||||
#[error("parse failed: empty")]
|
||||
Empty,
|
||||
#[error("parse failed: incomplete")]
|
||||
Incomplete,
|
||||
#[error("parse failed: unexpected character '{0}'")]
|
||||
Unexpected(char),
|
||||
#[error("parse failed: error #{0}")]
|
||||
Code(u8),
|
||||
}
|
||||
|
|
@ -1,130 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
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) -> Vec<Self::Token>;
|
||||
}
|
||||
|
||||
/// Implement the const iterator pattern.
|
||||
#[macro_export] macro_rules! const_iter {
|
||||
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
||||
impl$(<$l>)? Iterator for $Struct {
|
||||
type Item = $Item;
|
||||
fn next (&mut $self) -> Option<$Item> { $expr }
|
||||
}
|
||||
impl$(<$l>)? ConstIntoIter for $Struct {
|
||||
type Kind = IsIteratorKind;
|
||||
type Item = $Item;
|
||||
type IntoIter = Self;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Owns a reference to the source text.
|
||||
/// [SourceConstIter::next] emits subsequent pairs of:
|
||||
/// * a [CstToken] and
|
||||
/// * the source text remaining
|
||||
/// * [ ] TODO: maybe [SourceConstIter::next] should wrap the remaining source in `Self` ?
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct SourceConstIter<'source>(pub &'source str);
|
||||
|
||||
impl<'source> From<SourceConstIter<'source>> for SourceIter<'source> {
|
||||
fn from (source: SourceConstIter<'source>) -> Self{
|
||||
Self(source)
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides a native [Iterator] API over [SourceConstIter],
|
||||
/// emitting [CstToken] items.
|
||||
///
|
||||
/// [Cst::next] returns just the [CstToken] and mutates `self`,
|
||||
/// instead of returning an updated version of the struct as [SourceConstIter::next] does.
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct SourceIter<'source>(pub SourceConstIter<'source>);
|
||||
|
||||
impl<'source> SourceIter<'source> {
|
||||
pub const fn new (source: &'source str) -> Self {
|
||||
Self(SourceConstIter::new(source))
|
||||
}
|
||||
pub const fn peek (&self) -> Option<CstToken<'source>> {
|
||||
self.0.peek()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> Iterator for SourceIter<'source> {
|
||||
type Item = CstToken<'source>;
|
||||
fn next (&mut self) -> Option<CstToken<'source>> {
|
||||
self.0.next().map(|(item, rest)|{self.0 = rest; item})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> From<&'source str> for SourceIter<'source> {
|
||||
fn from (source: &'source str) -> Self{
|
||||
Self(SourceConstIter(source))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> Into<Vec<CstToken<'source>>> for SourceIter<'source> {
|
||||
fn into (self) -> Vec<CstToken<'source>> {
|
||||
self.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> Into<Vec<Ast>> for SourceIter<'source> {
|
||||
fn into (self) -> Vec<Ast> {
|
||||
self.map(Into::into).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> From<&'source str> for SourceConstIter<'source> {
|
||||
fn from (source: &'source str) -> Self{
|
||||
Self::new(source)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'source> SourceConstIter<'source> {
|
||||
pub const fn new (source: &'source str) -> Self {
|
||||
Self(source)
|
||||
}
|
||||
pub const fn chomp (&self, index: usize) -> Self {
|
||||
Self(split_at(self.0, index).1)
|
||||
}
|
||||
pub const fn next (mut self) -> Option<(CstToken<'source>, Self)> {
|
||||
Self::next_mut(&mut self)
|
||||
}
|
||||
pub const fn peek (&self) -> Option<CstToken<'source>> {
|
||||
peek_src(self.0)
|
||||
}
|
||||
pub const fn next_mut (&mut self) -> Option<(CstToken<'source>, Self)> {
|
||||
match self.peek() {
|
||||
Some(token) => Some((token, self.chomp(token.end()))),
|
||||
None => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const_iter!(<'source>|self: SourceConstIter<'source>| => CstToken<'source> => self.next_mut().map(|(result, _)|result));
|
||||
|
||||
#[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())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,111 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
#[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();
|
||||
}
|
||||
#[test] const fn test_const_iters () {
|
||||
let iter = crate::SourceConstIter::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 crate::{CstToken, 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 = CstToken(Nil, CstMeta { source: &source, start, length });
|
||||
//let _ = token.slice();
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
|
||||
#[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
|
||||
use crate::Value::*;
|
||||
let source = ":f00";
|
||||
let mut token = CstToken(Sym(":"), CstMeta { source, start: 0, length: 1 });
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, CstToken(Sym(":f"), CstMeta { source, start: 0, length: 2, }));
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, CstToken(Sym(":f0"), CstMeta { source, start: 0, length: 3, }));
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, CstToken(Sym(":f00"), CstMeta { source, start: 0, length: 4, }));
|
||||
|
||||
assert_eq!(None,
|
||||
SourceIter::new("").next());
|
||||
assert_eq!(None,
|
||||
SourceIter::new(" \n \r \t ").next());
|
||||
assert_eq!(&Num(7),
|
||||
SourceIter::new("7").next().unwrap().0.value());
|
||||
assert_eq!(&Num(100),
|
||||
SourceIter::new(" 100 ").next().unwrap().0.value());
|
||||
assert_eq!(&Err(Unexpected('a')),
|
||||
SourceIter::new(" 9a ").next().unwrap().0.value());
|
||||
assert_eq!(&Sym(":123foo"),
|
||||
SourceIter::new(" :123foo ").next().unwrap().0.value());
|
||||
assert_eq!(&Sym("@bar456"),
|
||||
SourceIter::new(" \r\r\r\n\n\n@bar456\t\t\t\t\t\t").next().unwrap().0.value());
|
||||
assert_eq!(&Key("foo123"),
|
||||
SourceIter::new("foo123").next().unwrap().0.value());
|
||||
assert_eq!(&Key("foo/bar"),
|
||||
SourceIter::new("foo/bar").next().unwrap().0.value());
|
||||
assert_eq!(&Str("foo/bar"),
|
||||
SourceIter::new("\"foo/bar\"").next().unwrap().0.value());
|
||||
assert_eq!(&Str("foo/bar"),
|
||||
SourceIter::new(" \"foo/bar\" ").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::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(())
|
||||
//}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(PartialEq, Clone, Default, Debug)]
|
||||
pub struct Token<
|
||||
V: PartialEq + Clone + Default + Debug,
|
||||
M: PartialEq + Clone + Default + Debug,
|
||||
>(pub V, pub M);
|
||||
|
||||
pub trait DslToken: PartialEq + Clone + Default + Debug {
|
||||
type Value: DslValue;
|
||||
type Meta: Clone + Default + Debug;
|
||||
fn value (&self) -> &Self::Value;
|
||||
fn meta (&self) -> &Self::Meta;
|
||||
}
|
||||
|
||||
impl<V: DslValue, M: PartialEq + Clone + Default + Debug> DslToken for Token<V, M> {
|
||||
type Value = V;
|
||||
type Meta = M;
|
||||
fn value (&self) -> &Self::Value {
|
||||
&self.0
|
||||
}
|
||||
fn meta (&self) -> &Self::Meta {
|
||||
&self.1
|
||||
}
|
||||
}
|
||||
|
|
@ -1,116 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
/// Thing that may construct itself from state and [DslValue].
|
||||
pub trait Dsl<State>: Sized {
|
||||
fn try_provide (state: &State, value: impl DslValue) -> Perhaps<Self>;
|
||||
fn provide (
|
||||
state: &State,
|
||||
value: impl DslValue,
|
||||
error: impl Fn()->Box<dyn std::error::Error>
|
||||
) -> Usually<Self> {
|
||||
match Self::try_provide(state, value)? {
|
||||
Some(value) => Ok(value),
|
||||
_ => Err(error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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!()
|
||||
}
|
||||
}
|
||||
127
dsl/src/lib.rs
127
dsl/src/lib.rs
|
|
@ -35,17 +35,124 @@
|
|||
#![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 std::fmt::{Display, Formatter, Error as FormatError};
|
||||
pub(crate) use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
||||
pub(crate) use std::sync::Arc;
|
||||
pub(crate) use std::collections::VecDeque;
|
||||
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::*;
|
||||
mod dsl_error; pub use self::dsl_error::*;
|
||||
mod dsl_iter; pub use self::dsl_iter::*;
|
||||
mod dsl_token; pub use self::dsl_token::*;
|
||||
mod dsl_value; pub use self::dsl_value::*;
|
||||
#[cfg(test)] mod dsl_test;
|
||||
|
||||
mod dsl; pub use self::dsl::*;
|
||||
mod ast; pub use self::ast::*;
|
||||
mod cst; pub use self::cst::*;
|
||||
|
||||
#[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();
|
||||
}
|
||||
#[test] const fn test_const_iters () {
|
||||
let iter = crate::SourceConstIter::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 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();
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
|
||||
#[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
|
||||
use crate::Value::*;
|
||||
let source = ":f00";
|
||||
let mut token = Cst::new(source, 0, 1, Sym(":"));
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Cst::new(source, 0, 2, Sym(":f")));
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Cst::new(source, 0, 3, Sym(":f0")));
|
||||
token = token.grow_sym();
|
||||
assert_eq!(token, Cst::new(source, 0, 4, Sym(":f00")));
|
||||
|
||||
assert_eq!(None,
|
||||
SourceIter::new("").next());
|
||||
assert_eq!(None,
|
||||
SourceIter::new(" \n \r \t ").next());
|
||||
assert_eq!(&Num(7),
|
||||
SourceIter::new("7").next().unwrap().0.value());
|
||||
assert_eq!(&Num(100),
|
||||
SourceIter::new(" 100 ").next().unwrap().0.value());
|
||||
assert_eq!(&Err(Unexpected('a')),
|
||||
SourceIter::new(" 9a ").next().unwrap().0.value());
|
||||
assert_eq!(&Sym(":123foo"),
|
||||
SourceIter::new(" :123foo ").next().unwrap().0.value());
|
||||
assert_eq!(&Sym("@bar456"),
|
||||
SourceIter::new(" \r\r\r\n\n\n@bar456\t\t\t\t\t\t").next().unwrap().0.value());
|
||||
assert_eq!(&Key("foo123"),
|
||||
SourceIter::new("foo123").next().unwrap().0.value());
|
||||
assert_eq!(&Key("foo/bar"),
|
||||
SourceIter::new("foo/bar").next().unwrap().0.value());
|
||||
assert_eq!(&Str("foo/bar"),
|
||||
SourceIter::new("\"foo/bar\"").next().unwrap().0.value());
|
||||
assert_eq!(&Str("foo/bar"),
|
||||
SourceIter::new(" \"foo/bar\" ").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::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(())
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -1,91 +1,65 @@
|
|||
use crate::*;
|
||||
use Value::*;
|
||||
|
||||
fn exp_match <S: DslValue, E: DslIter, T> (source: S, namespace: &str, cb: impl Fn(&str, E)) -> Perhaps<T> {
|
||||
if let Some(exp) = source.exp()
|
||||
&& let Some(Value::Key(key)) = exp.next()
|
||||
&& key.starts_with(namespace) {
|
||||
cb(key.split_at(namespace.len()).1, exp)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
||||
fn try_provide (state: &S, source: impl DslValue) -> Perhaps<Self> {
|
||||
if let Exp(_, mut exp) = source.value()
|
||||
&& let Some(Ast(Key(id))) = exp.peek() && *id == *"when" {
|
||||
let _ = exp.next();
|
||||
return Ok(Some(Self(
|
||||
state.eval(exp.next().unwrap(), ||"when: expected condition")?,
|
||||
state.eval(exp.next().unwrap(), ||"when: expected content")?,
|
||||
)))
|
||||
}
|
||||
Ok(None)
|
||||
exp_match::<S, Self>(source, "when", |_, tail|Ok(Some(Self(
|
||||
tail.eval(0, ||"no condition")?,
|
||||
tail.eval(1, ||"no content")?,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, A, B> Dsl<S> for Either<A, B> where S: Eval<Ast, bool> + Eval<Ast, A> + Eval<Ast, B> {
|
||||
fn try_provide (state: &S, source: impl DslValue) -> Perhaps<Self> {
|
||||
if let Exp(_, mut exp) = source.value()
|
||||
&& let Some(Ast(Key(id))) = exp.peek() && *id == *"either" {
|
||||
let _ = exp.next();
|
||||
return Ok(Some(Self(
|
||||
state.eval(exp.next().unwrap(), ||"either: expected condition")?,
|
||||
state.eval(exp.next().unwrap(), ||"either: expected content 1")?,
|
||||
state.eval(exp.next().unwrap(), ||"either: expected content 2")?,
|
||||
)))
|
||||
}
|
||||
Ok(None)
|
||||
exp_match::<S, Self>(source, "either", |_, tail|Ok(Some(Self(
|
||||
tail.eval(0, ||"no condition")?,
|
||||
tail.eval(1, ||"no content 1")?,
|
||||
tail.eval(2, ||"no content 2")?,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
|
||||
fn try_provide (state: &S, source: impl DslValue) -> Perhaps<Self> {
|
||||
if let Exp(_, source) = source.value() {
|
||||
let mut rest = source.clone();
|
||||
return Ok(Some(match rest.next().as_ref().and_then(|x|x.key()) {
|
||||
Some("align/c") => Self::c(state.eval(rest.next(), ||"align/c: expected content")?),
|
||||
Some("align/x") => Self::x(state.eval(rest.next(), ||"align/x: expected content")?),
|
||||
Some("align/y") => Self::y(state.eval(rest.next(), ||"align/y: expected content")?),
|
||||
Some("align/n") => Self::n(state.eval(rest.next(), ||"align/n: expected content")?),
|
||||
Some("align/s") => Self::s(state.eval(rest.next(), ||"align/s: expected content")?),
|
||||
Some("align/e") => Self::e(state.eval(rest.next(), ||"align/e: expected content")?),
|
||||
Some("align/w") => Self::w(state.eval(rest.next(), ||"align/w: expected content")?),
|
||||
Some("align/nw") => Self::nw(state.eval(rest.next(), ||"align/nw: expected content")?),
|
||||
Some("align/ne") => Self::ne(state.eval(rest.next(), ||"align/ne: expected content")?),
|
||||
Some("align/sw") => Self::sw(state.eval(rest.next(), ||"align/sw: expected content")?),
|
||||
Some("align/se") => Self::se(state.eval(rest.next(), ||"align/se: expected content")?),
|
||||
_ => return Ok(None),
|
||||
}))
|
||||
}
|
||||
Ok(None)
|
||||
exp_match::<S, Self>(source, "align/", |head, tail|Ok(Some(match head {
|
||||
"c" => Self::c(tail.eval(0, ||"no content")?),
|
||||
"x" => Self::x(tail.eval(0, ||"no content")?),
|
||||
"y" => Self::y(tail.eval(0, ||"no content")?),
|
||||
"n" => Self::n(tail.eval(0, ||"no content")?),
|
||||
"s" => Self::s(tail.eval(0, ||"no content")?),
|
||||
"e" => Self::e(tail.eval(0, ||"no content")?),
|
||||
"w" => Self::w(tail.eval(0, ||"no content")?),
|
||||
"nw" => Self::nw(tail.eval(0, ||"no content")?),
|
||||
"ne" => Self::ne(tail.eval(0, ||"no content")?),
|
||||
"sw" => Self::sw(tail.eval(0, ||"no content")?),
|
||||
"se" => Self::se(tail.eval(0, ||"no content")?),
|
||||
_ => return Err("invalid align variant")
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, A, B> Dsl<S> for Bsp<A, B> where S: Eval<Option<Ast>, A> + Eval<Option<Ast>, B> {
|
||||
fn try_provide (state: &S, source: impl DslValue) -> Perhaps<Self> {
|
||||
if let Exp(_, exp) = source.value() {
|
||||
let mut rest = exp.clone();
|
||||
return Ok(Some(match rest.next().as_ref().and_then(|x|x.key()) {
|
||||
Some("bsp/n") => Self::n(
|
||||
state.eval(rest.next(), ||"bsp/n: expected content 1")?,
|
||||
state.eval(rest.next(), ||"bsp/n: expected content 2")?,
|
||||
),
|
||||
Some("bsp/s") => Self::s(
|
||||
state.eval(rest.next(), ||"bsp/s: expected content 1")?,
|
||||
state.eval(rest.next(), ||"bsp/s: expected content 2")?,
|
||||
),
|
||||
Some("bsp/e") => Self::e(
|
||||
state.eval(rest.next(), ||"bsp/e: expected content 1")?,
|
||||
state.eval(rest.next(), ||"bsp/e: expected content 2")?,
|
||||
),
|
||||
Some("bsp/w") => Self::w(
|
||||
state.eval(rest.next(), ||"bsp/w: expected content 1")?,
|
||||
state.eval(rest.next(), ||"bsp/w: expected content 2")?,
|
||||
),
|
||||
Some("bsp/a") => Self::a(
|
||||
state.eval(rest.next(), ||"bsp/a: expected content 1")?,
|
||||
state.eval(rest.next(), ||"bsp/a: expected content 2")?,
|
||||
),
|
||||
Some("bsp/b") => Self::b(
|
||||
state.eval(rest.next(), ||"bsp/b: expected content 1")?,
|
||||
state.eval(rest.next(), ||"bsp/b: expected content 2")?,
|
||||
),
|
||||
exp_match::<S, Self>(source, "bsp/", |head, tail|Ok(Some(match head {
|
||||
"n" => Self::n(tail.eval(0, ||"no content 1"), tail.eval(1, ||"no content 2")),
|
||||
"s" => Self::s(tail.eval(0, ||"no content 1"), tail.eval(1, ||"no content 2")),
|
||||
"e" => Self::e(tail.eval(0, ||"no content 1"), tail.eval(1, ||"no content 2")),
|
||||
"w" => Self::w(tail.eval(0, ||"no content 1"), tail.eval(1, ||"no content 2")),
|
||||
"a" => Self::a(tail.eval(0, ||"no content 1"), tail.eval(1, ||"no content 2")),
|
||||
"b" => Self::b(tail.eval(0, ||"no content 1"), tail.eval(1, ||"no content 2")),
|
||||
_ => return Ok(None),
|
||||
}))
|
||||
}
|
||||
Ok(None)
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -124,3 +98,77 @@ impl<S, A, B> Dsl<S> for Bsp<A, B> where S: Eval<Option<Ast>, A> + Eval<Option<A
|
|||
//} else {
|
||||
//None
|
||||
//}));
|
||||
//if let Exp(_, exp) = source.value() {
|
||||
//let mut rest = exp.clone();
|
||||
//return Ok(Some(match rest.next().as_ref().and_then(|x|x.key()) {
|
||||
//Some("bsp/n") => Self::n(
|
||||
//state.eval(rest.next(), ||"bsp/n: no content 1")?,
|
||||
//state.eval(rest.next(), ||"bsp/n: no content 2")?,
|
||||
//),
|
||||
//Some("bsp/s") => Self::s(
|
||||
//state.eval(rest.next(), ||"bsp/s: no content 1")?,
|
||||
//state.eval(rest.next(), ||"bsp/s: no content 2")?,
|
||||
//),
|
||||
//Some("bsp/e") => Self::e(
|
||||
//state.eval(rest.next(), ||"bsp/e: no content 1")?,
|
||||
//state.eval(rest.next(), ||"bsp/e: no content 2")?,
|
||||
//),
|
||||
//Some("bsp/w") => Self::w(
|
||||
//state.eval(rest.next(), ||"bsp/w: no content 1")?,
|
||||
//state.eval(rest.next(), ||"bsp/w: no content 2")?,
|
||||
//),
|
||||
//Some("bsp/a") => Self::a(
|
||||
//state.eval(rest.next(), ||"bsp/a: no content 1")?,
|
||||
//state.eval(rest.next(), ||"bsp/a: no content 2")?,
|
||||
//),
|
||||
//Some("bsp/b") => Self::b(
|
||||
//state.eval(rest.next(), ||"bsp/b: no content 1")?,
|
||||
//state.eval(rest.next(), ||"bsp/b: no content 2")?,
|
||||
//),
|
||||
//_ => return Ok(None),
|
||||
//}))
|
||||
//}
|
||||
//Ok(None)
|
||||
//if let Exp(_, source) = source.value() {
|
||||
//let mut rest = source.clone();
|
||||
//return Ok(Some(match rest.next().as_ref().and_then(|x|x.key()) {
|
||||
//Some("align/c") => Self::c(state.eval(rest.next(), ||"align/c: no content")?),
|
||||
//Some("align/x") => Self::x(state.eval(rest.next(), ||"align/x: no content")?),
|
||||
//Some("align/y") => Self::y(state.eval(rest.next(), ||"align/y: no content")?),
|
||||
//Some("align/n") => Self::n(state.eval(rest.next(), ||"align/n: no content")?),
|
||||
//Some("align/s") => Self::s(state.eval(rest.next(), ||"align/s: no content")?),
|
||||
//Some("align/e") => Self::e(state.eval(rest.next(), ||"align/e: no content")?),
|
||||
//Some("align/w") => Self::w(state.eval(rest.next(), ||"align/w: no content")?),
|
||||
//Some("align/nw") => Self::nw(state.eval(rest.next(), ||"align/nw: no content")?),
|
||||
//Some("align/ne") => Self::ne(state.eval(rest.next(), ||"align/ne: no content")?),
|
||||
//Some("align/sw") => Self::sw(state.eval(rest.next(), ||"align/sw: no content")?),
|
||||
//Some("align/se") => Self::se(state.eval(rest.next(), ||"align/se: no content")?),
|
||||
//_ => return Ok(None),
|
||||
//}))
|
||||
//}
|
||||
//Ok(None)
|
||||
//Ok(match source.exp_head().and_then(|e|e.key()) {
|
||||
//Some("either") => Some(Self(
|
||||
//source.exp_tail().and_then(|t|t.get(0)).map(|x|state.eval(x, ||"when: no condition"))?,
|
||||
//source.exp_tail().and_then(|t|t.get(1)).map(|x|state.eval(x, ||"when: no content 1"))?,
|
||||
//source.exp_tail().and_then(|t|t.get(2)).map(|x|state.eval(x, ||"when: no content 2"))?,
|
||||
//)),
|
||||
//_ => None
|
||||
//})
|
||||
//if let Exp(_, mut exp) = source.value()
|
||||
//&& let Some(Ast(Key(id))) = exp.peek() && *id == *"either" {
|
||||
//let _ = exp.next();
|
||||
//return Ok(Some(Self(
|
||||
//state.eval(exp.next().unwrap(), ||"either: no condition")?,
|
||||
//state.eval(exp.next().unwrap(), ||"either: no content 1")?,
|
||||
//state.eval(exp.next().unwrap(), ||"either: no content 2")?,
|
||||
//)))
|
||||
//}
|
||||
//Ok(None)
|
||||
//Ok(match source.exp_head().and_then(|e|e.key()) {
|
||||
//Some("when") => Some(Self(
|
||||
//source.exp_tail().and_then(|t|t.get(0)).map(|x|state.eval(x, ||"when: no condition"))?,
|
||||
//source.exp_tail().and_then(|t|t.get(1)).map(|x|state.eval(x, ||"when: no content"))?,
|
||||
//)),
|
||||
//_ => None
|
||||
//})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue