mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-01-31 19:06:41 +01:00
Compare commits
5 commits
93b1cf1a5c
...
cb47c4d0ff
| Author | SHA1 | Date | |
|---|---|---|---|
| cb47c4d0ff | |||
| 08a8dff93d | |||
| e9d4c7e0bc | |||
| f77139c8fd | |||
| d6e8be6ce5 |
12 changed files with 642 additions and 539 deletions
|
|
@ -1,70 +1,46 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[derive(Clone, Default, Debug)]
|
#[derive(Debug, Clone, Default, PartialEq)]
|
||||||
pub enum Ast {
|
pub struct Ast(pub Value<Arc<str>, AstIter>);
|
||||||
#[default] Nil,
|
|
||||||
Err(DslError),
|
|
||||||
Num(usize),
|
|
||||||
Sym(Arc<str>),
|
|
||||||
Key(Arc<str>),
|
|
||||||
Str(Arc<str>),
|
|
||||||
Exp(Arc<Box<dyn AstIter>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Emits tokens.
|
impl<'source> From<CstToken<'source>> for Ast {
|
||||||
pub trait AstIter: Debug {
|
fn from (token: CstToken<'source>) -> Self {
|
||||||
fn peek (&self) -> Option<Ast>;
|
token.value().into()
|
||||||
fn next (&mut self) -> Option<Ast>;
|
|
||||||
fn rest (self) -> Option<Box<dyn AstIter>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A [Cst] can be used as an [Ast].
|
|
||||||
impl<'source: 'static> AstIter for Cst<'source> {
|
|
||||||
fn peek (&self) -> Option<Ast> {
|
|
||||||
Cst::peek(self).map(|token|token.value.into())
|
|
||||||
}
|
|
||||||
fn next (&mut self) -> Option<Ast> {
|
|
||||||
Iterator::next(self).map(|token|token.value.into())
|
|
||||||
}
|
|
||||||
fn rest (self) -> Option<Box<dyn AstIter>> {
|
|
||||||
self.peek().is_some().then(||Box::new(self) as Box<dyn AstIter>)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Ast {
|
impl<'source> From<CstValue<'source>> for Ast {
|
||||||
fn fmt (&self, out: &mut Formatter) -> Result<(), std::fmt::Error> {
|
fn from (value: CstValue<'source>) -> Self {
|
||||||
use Ast::*;
|
use Value::*;
|
||||||
write!(out, "{}", match self {
|
Self(match value {
|
||||||
Nil => String::new(),
|
Nil => Nil,
|
||||||
Err(e) => format!("[error: {e}]"),
|
Err(e) => Err(e),
|
||||||
Num(n) => format!("{n}"),
|
Num(u) => Num(u),
|
||||||
Sym(s) => format!("{s}"),
|
Sym(s) => Sym(s.into()),
|
||||||
Key(s) => format!("{s}"),
|
Key(s) => Key(s.into()),
|
||||||
Str(s) => format!("{s}"),
|
Str(s) => Str(s.into()),
|
||||||
Exp(e) => format!("{e:?}"),
|
Exp(d, x) => Exp(d, x.into()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'source: 'static> From<CstValue<'source>> for Ast {
|
impl DslValue for Ast {
|
||||||
fn from (other: CstValue<'source>) -> Self {
|
type Str = Arc<str>;
|
||||||
use CstValue::*;
|
type Exp = AstIter;
|
||||||
match other {
|
fn value (&self) -> &Value<Arc<str>, AstIter> {
|
||||||
Nil => Self::Nil,
|
self.0.value()
|
||||||
Err(e) => Self::Err(e),
|
|
||||||
Num(u) => Self::Num(u),
|
|
||||||
Sym(s) => Self::Sym(s.into()),
|
|
||||||
Key(s) => Self::Key(s.into()),
|
|
||||||
Str(s) => Self::Str(s.into()),
|
|
||||||
Exp(_, s) => Self::Exp(Arc::new(s.into())),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'source: 'static> Into<Box<dyn AstIter>> for Cst<'source> {
|
impl DslToken for Ast {
|
||||||
fn into (self) -> Box<dyn AstIter> {
|
type Value = Self;
|
||||||
Box::new(self)
|
type Meta = ();
|
||||||
|
fn value (&self) -> &Self::Value {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
fn meta (&self) -> &Self::Meta {
|
||||||
|
&()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,110 +1,117 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
/// Provides a native [Iterator] API over [CstConstIter],
|
/// CST stores strings as source references and expressions as new [SourceIter] instances.
|
||||||
/// emitting [CstToken] items.
|
pub type CstValue<'source> = Value<&'source str, SourceIter<'source>>;
|
||||||
///
|
|
||||||
/// [Cst::next] returns just the [CstToken] and mutates `self`,
|
|
||||||
/// instead of returning an updated version of the struct as [CstConstIter::next] does.
|
|
||||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
|
||||||
pub struct Cst<'a>(pub CstConstIter<'a>);
|
|
||||||
|
|
||||||
/// Owns a reference to the source text.
|
/// Reference to the source slice.
|
||||||
/// [CstConstIter::next] emits subsequent pairs of:
|
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
|
||||||
/// * a [CstToken] and
|
|
||||||
/// * the source text remaining
|
|
||||||
/// * [ ] TODO: maybe [CstConstIter::next] should wrap the remaining source in `Self` ?
|
|
||||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
|
||||||
pub struct CstConstIter<'a>(pub &'a str);
|
|
||||||
|
|
||||||
/// A CST token, with reference to the source slice.
|
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstToken<'source> {
|
|
||||||
pub source: &'source str,
|
pub source: &'source str,
|
||||||
pub start: usize,
|
pub start: usize,
|
||||||
pub length: usize,
|
pub length: usize,
|
||||||
pub value: CstValue<'source>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The meaning of a CST token. Strip the source from this to get an [AstValue].
|
/// Token sharing memory with source reference.
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum CstValue<'source> {
|
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||||
#[default] Nil,
|
pub struct CstToken<'source>(pub CstValue<'source>, pub CstMeta<'source>);
|
||||||
Err(DslError),
|
|
||||||
Num(usize),
|
|
||||||
Sym(&'source str),
|
|
||||||
Key(&'source str),
|
|
||||||
Str(&'source str),
|
|
||||||
Exp(usize, Cst<'source>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Cst<'a> {
|
impl<'source> CstToken<'source> {
|
||||||
pub const fn new (source: &'a str) -> Self {
|
pub const fn new (
|
||||||
Self(CstConstIter::new(source))
|
source: &'source str, start: usize, length: usize, value: CstValue<'source>
|
||||||
|
) -> Self {
|
||||||
|
Self(value, CstMeta { source, start, length })
|
||||||
}
|
}
|
||||||
pub const fn peek (&self) -> Option<CstToken<'a>> {
|
pub const fn end (&self) -> usize {
|
||||||
self.0.peek()
|
self.1.start.saturating_add(self.1.length)
|
||||||
}
|
}
|
||||||
}
|
pub const fn slice (&'source self) -> &'source str {
|
||||||
|
self.slice_source(self.1.source)
|
||||||
impl<'a> Iterator for Cst<'a> {
|
|
||||||
type Item = CstToken<'a>;
|
|
||||||
fn next (&mut self) -> Option<CstToken<'a>> {
|
|
||||||
self.0.next().map(|(item, rest)|{self.0 = rest; item})
|
|
||||||
}
|
}
|
||||||
}
|
pub const fn slice_source <'range> (&'source self, source: &'range str) -> &'range str {
|
||||||
|
str_range(source, self.1.start, self.end())
|
||||||
impl<'a> From<&'a str> for Cst<'a> {
|
|
||||||
fn from (source: &'a str) -> Self{
|
|
||||||
Self(CstConstIter(source))
|
|
||||||
}
|
}
|
||||||
}
|
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())
|
||||||
impl<'a> From<CstConstIter<'a>> for Cst<'a> {
|
|
||||||
fn from (source: CstConstIter<'a>) -> Self{
|
|
||||||
Self(source)
|
|
||||||
}
|
}
|
||||||
}
|
pub const fn with_value (self, value: CstValue<'source>) -> Self {
|
||||||
|
Self(value, self.1)
|
||||||
/// 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 {
|
pub const fn value (&self) -> CstValue<'source> {
|
||||||
type Kind = IsIteratorKind;
|
self.0
|
||||||
type Item = $Item;
|
}
|
||||||
type IntoIter = Self;
|
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!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iter!(<'a>|self: CstConstIter<'a>| => CstToken<'a> => self.next_mut().map(|(result, _)|result));
|
pub const fn to_number (digits: &str) -> DslResult<usize> {
|
||||||
|
let mut value = 0;
|
||||||
impl<'a> From<&'a str> for CstConstIter<'a> {
|
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
|
||||||
fn from (source: &'a str) -> Self{
|
Ok(digit) => value = 10 * value + digit,
|
||||||
Self::new(source)
|
Result::Err(e) => return Result::Err(e)
|
||||||
}
|
});
|
||||||
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CstConstIter<'a> {
|
pub const fn to_digit (c: char) -> DslResult<usize> {
|
||||||
pub const fn new (source: &'a str) -> Self {
|
Ok(match c {
|
||||||
Self(source)
|
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
|
||||||
}
|
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
|
||||||
pub const fn chomp (&self, index: usize) -> Self {
|
_ => return Result::Err(Unexpected(c))
|
||||||
Self(split_at(self.0, index).1)
|
})
|
||||||
}
|
|
||||||
pub const fn next (mut self) -> Option<(CstToken<'a>, Self)> {
|
|
||||||
Self::next_mut(&mut self)
|
|
||||||
}
|
|
||||||
pub const fn peek (&self) -> Option<CstToken<'a>> {
|
|
||||||
peek_src(self.0)
|
|
||||||
}
|
|
||||||
pub const fn next_mut (&mut self) -> Option<(CstToken<'a>, Self)> {
|
|
||||||
match self.peek() {
|
|
||||||
Some(token) => Some((token, self.chomp(token.end()))),
|
|
||||||
None => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Static iteration helper.
|
/// Static iteration helper.
|
||||||
|
|
@ -119,7 +126,7 @@ impl<'a> CstConstIter<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||||
use CstValue::*;
|
use Value::*;
|
||||||
let mut token: CstToken<'a> = CstToken::new(source, 0, 0, Nil);
|
let mut token: CstToken<'a> = CstToken::new(source, 0, 0, Nil);
|
||||||
iterate!(char_indices(source) => (start, c) => token = match token.value() {
|
iterate!(char_indices(source) => (start, c) => token = match token.value() {
|
||||||
Err(_) => return Some(token),
|
Err(_) => return Some(token),
|
||||||
|
|
@ -127,7 +134,7 @@ pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||||
' '|'\n'|'\r'|'\t' =>
|
' '|'\n'|'\r'|'\t' =>
|
||||||
token.grow(),
|
token.grow(),
|
||||||
'(' =>
|
'(' =>
|
||||||
CstToken::new(source, start, 1, Exp(1, Cst::new(str_range(source, start, start + 1)))),
|
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, Str(str_range(source, start, start + 1))),
|
||||||
':'|'@' =>
|
':'|'@' =>
|
||||||
|
|
@ -136,8 +143,8 @@ pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||||
CstToken::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
CstToken::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
||||||
'0'..='9' =>
|
'0'..='9' =>
|
||||||
CstToken::new(source, start, 1, match to_digit(c) {
|
CstToken::new(source, start, 1, match to_digit(c) {
|
||||||
Ok(c) => CstValue::Num(c),
|
Ok(c) => Value::Num(c),
|
||||||
Result::Err(e) => CstValue::Err(e)
|
Result::Err(e) => Value::Err(e)
|
||||||
}),
|
}),
|
||||||
_ => token.error(Unexpected(c))
|
_ => token.error(Unexpected(c))
|
||||||
},
|
},
|
||||||
|
|
@ -174,118 +181,3 @@ pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||||
_ => Some(token),
|
_ => Some(token),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'source> CstToken<'source> {
|
|
||||||
pub const fn new (
|
|
||||||
source: &'source str, start: usize, length: usize, value: CstValue<'source>
|
|
||||||
) -> Self {
|
|
||||||
Self { source, start, length, value }
|
|
||||||
}
|
|
||||||
pub const fn end (&self) -> usize {
|
|
||||||
self.start.saturating_add(self.length)
|
|
||||||
}
|
|
||||||
pub const fn slice (&'source self) -> &'source str {
|
|
||||||
self.slice_source(self.source)
|
|
||||||
}
|
|
||||||
pub const fn slice_source <'range> (&'source 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.start.saturating_add(1), self.end())
|
|
||||||
}
|
|
||||||
pub const fn with_value (self, value: CstValue<'source>) -> Self {
|
|
||||||
Self { value, ..self }
|
|
||||||
}
|
|
||||||
pub const fn value (&self) -> CstValue {
|
|
||||||
self.value
|
|
||||||
}
|
|
||||||
pub const fn error (self, error: DslError) -> Self {
|
|
||||||
Self { value: CstValue::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 {
|
|
||||||
use CstValue::*;
|
|
||||||
match to_digit(c) {
|
|
||||||
Ok(n) => Self { value: Num(10*m+n), ..self.grow() },
|
|
||||||
Result::Err(e) => Self { value: Err(e), ..self.grow() },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub const fn grow_key (self) -> Self {
|
|
||||||
use CstValue::*;
|
|
||||||
let token = self.grow();
|
|
||||||
token.with_value(Key(token.slice_source(self.source)))
|
|
||||||
}
|
|
||||||
pub const fn grow_sym (self) -> Self {
|
|
||||||
use CstValue::*;
|
|
||||||
let token = self.grow();
|
|
||||||
token.with_value(Sym(token.slice_source(self.source)))
|
|
||||||
}
|
|
||||||
pub const fn grow_str (self) -> Self {
|
|
||||||
use CstValue::*;
|
|
||||||
let token = self.grow();
|
|
||||||
token.with_value(Str(token.slice_source(self.source)))
|
|
||||||
}
|
|
||||||
pub const fn grow_exp (self) -> Self {
|
|
||||||
use CstValue::*;
|
|
||||||
let token = self.grow();
|
|
||||||
if let Exp(depth, _) = token.value {
|
|
||||||
token.with_value(Exp(depth, Cst::new(token.slice_source_exp(self.source))))
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub const fn grow_in (self) -> Self {
|
|
||||||
let token = self.grow_exp();
|
|
||||||
if let CstValue::Exp(depth, source) = token.value {
|
|
||||||
token.with_value(CstValue::Exp(depth.saturating_add(1), source))
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub const fn grow_out (self) -> Self {
|
|
||||||
let token = self.grow_exp();
|
|
||||||
if let CstValue::Exp(depth, source) = token.value {
|
|
||||||
if depth > 0 {
|
|
||||||
token.with_value(CstValue::Exp(depth - 1, source))
|
|
||||||
} else {
|
|
||||||
return self.error(Unexpected(')'))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'source> std::fmt::Display for CstValue<'source> {
|
|
||||||
fn fmt (&self, out: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
|
||||||
use CstValue::*;
|
|
||||||
write!(out, "{}", match self {
|
|
||||||
Nil => String::new(),
|
|
||||||
Err(e) => format!("[error: {e}]"),
|
|
||||||
Num(n) => format!("{n}"),
|
|
||||||
Sym(s) => format!("{s}"),
|
|
||||||
Key(s) => format!("{s}"),
|
|
||||||
Str(s) => format!("{s}"),
|
|
||||||
Exp(_, e) => format!("{e:?}"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
30
dsl/src/dsl_display.rs
Normal file
30
dsl/src/dsl_display.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
use crate::*;
|
||||||
|
use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
||||||
|
impl Display for Ast {
|
||||||
|
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||||
|
use Value::*;
|
||||||
|
write!(out, "{}", match &self.0 {
|
||||||
|
Nil => String::new(),
|
||||||
|
Err(e) => format!("[error: {e}]"),
|
||||||
|
Num(n) => format!("{n}"),
|
||||||
|
Sym(s) => format!("{s}"),
|
||||||
|
Key(s) => format!("{s}"),
|
||||||
|
Str(s) => format!("{s}"),
|
||||||
|
Exp(_, e) => format!("{e:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'source> Display for CstValue<'source> {
|
||||||
|
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||||
|
use Value::*;
|
||||||
|
write!(out, "{}", match self {
|
||||||
|
Nil => String::new(),
|
||||||
|
Err(e) => format!("[error: {e}]"),
|
||||||
|
Num(n) => format!("{n}"),
|
||||||
|
Sym(s) => format!("{s}"),
|
||||||
|
Key(s) => format!("{s}"),
|
||||||
|
Str(s) => format!("{s}"),
|
||||||
|
Exp(_, e) => format!("{e:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,13 +13,25 @@ pub trait Eval<Input, Output> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//impl<S: Eval<I, O>, I, O> Eval<I, O> for &S {
|
||||||
|
//fn try_eval (&self, input: I) -> Perhaps<O> {
|
||||||
|
//(*self).try_eval(input)
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
|
||||||
|
//impl<S: Eval<I, O>, I: Ast, O: Dsl<S>> Eval<I, O> for S {
|
||||||
|
//fn try_eval (&self, input: I) -> Perhaps<O> {
|
||||||
|
//Dsl::try_provide(self, input)
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
|
||||||
/// May construct [Self] from token stream.
|
/// May construct [Self] from token stream.
|
||||||
pub trait Dsl<State>: Sized {
|
pub trait Dsl<State>: Sized {
|
||||||
fn try_provide (state: State, source: Ast) -> Perhaps<Self>;
|
fn try_provide (state: &State, source: Ast) -> Perhaps<Self>;
|
||||||
fn provide <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
fn provide <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||||
state: State, source: Ast, error: F
|
state: &State, source: Ast, error: F
|
||||||
) -> Usually<Self> {
|
) -> Usually<Self> {
|
||||||
let next = source.clone();
|
let next = format!("{source:?}");
|
||||||
if let Some(value) = Self::try_provide(state, source)? {
|
if let Some(value) = Self::try_provide(state, source)? {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -28,10 +40,6 @@ pub trait Dsl<State>: Sized {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Dsl<S>, S> Eval<Ast, T> for S {
|
|
||||||
fn try_eval (&self, input: Ast) -> Perhaps<T> { todo!() }
|
|
||||||
}
|
|
||||||
|
|
||||||
//pub trait Give<'state, 'source, Type> {
|
//pub trait Give<'state, 'source, Type> {
|
||||||
///// Implement this to be able to [Give] [Type] from the [Cst].
|
///// Implement this to be able to [Give] [Type] from the [Cst].
|
||||||
///// Advance the stream if returning `Ok<Some<Type>>`.
|
///// Advance the stream if returning `Ok<Some<Type>>`.
|
||||||
|
|
|
||||||
130
dsl/src/dsl_iter.rs
Normal file
130
dsl/src/dsl_iter.rs
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
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>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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],
|
||||||
|
/// 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
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(())
|
||||||
|
//}
|
||||||
25
dsl/src/dsl_token.rs
Normal file
25
dsl/src/dsl_token.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
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!()
|
||||||
|
}
|
||||||
|
}
|
||||||
140
dsl/src/lib.rs
140
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].
|
//! [Token]s are parsed substrings with an associated [Value].
|
||||||
//!
|
//!
|
||||||
//! * [ ] FIXME: Value may be [Err] which may shadow [Result::Err]
|
//! * [ ] FIXME: Value may be [Err] which may shadow [Result::Err]
|
||||||
|
|
@ -35,141 +31,21 @@
|
||||||
//! assert_eq!(view.0.0, src);
|
//! assert_eq!(view.0.0, src);
|
||||||
//! assert_eq!(view.peek(), view.0.peek())
|
//! 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 ::tengri_core::*;
|
||||||
|
|
||||||
pub(crate) use std::fmt::Debug;
|
pub(crate) use std::fmt::Debug;
|
||||||
pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
|
pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
|
||||||
pub(crate) use konst::string::{split_at, str_range, char_indices};
|
pub(crate) use konst::string::{split_at, str_range, char_indices};
|
||||||
pub(crate) use thiserror::Error;
|
pub(crate) use thiserror::Error;
|
||||||
pub(crate) use self::DslError::*;
|
pub(crate) use self::DslError::*;
|
||||||
|
|
||||||
mod dsl_ast; pub use self::dsl_ast::*;
|
mod dsl_ast; pub use self::dsl_ast::*;
|
||||||
mod dsl_cst; pub use self::dsl_cst::*;
|
mod dsl_cst; pub use self::dsl_cst::*;
|
||||||
|
mod dsl_display; pub use self::dsl_display::*;
|
||||||
mod dsl_domain; pub use self::dsl_domain::*;
|
mod dsl_domain; pub use self::dsl_domain::*;
|
||||||
mod dsl_error; pub use self::dsl_error::*;
|
mod dsl_error; pub use self::dsl_error::*;
|
||||||
|
mod dsl_iter; pub use self::dsl_iter::*;
|
||||||
#[cfg(test)] mod test_token_iter {
|
mod dsl_token; pub use self::dsl_token::*;
|
||||||
use crate::*;
|
mod dsl_value; pub use self::dsl_value::*;
|
||||||
//use proptest::prelude::*;
|
#[cfg(test)] mod dsl_test;
|
||||||
#[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,14 +1,12 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
/// List of input layers with optional conditional filters.
|
|
||||||
#[derive(Default, Debug)] pub struct InputLayers<S>(Vec<InputLayer<S>>);
|
#[derive(Default, Debug)] pub struct InputLayers(Vec<InputLayer>);
|
||||||
#[derive(Default, Debug)] pub struct InputLayer<S>{
|
|
||||||
__: PhantomData<S>,
|
#[derive(Default, Debug)] pub struct InputLayer(Option<Ast>, Ast);
|
||||||
condition: Option<Ast>,
|
|
||||||
binding: Ast,
|
impl InputLayers {
|
||||||
}
|
|
||||||
impl<S> InputLayers<S> {
|
|
||||||
pub fn new (layer: Ast) -> Self {
|
pub fn new (layer: Ast) -> Self {
|
||||||
Self(vec![]).layer(layer)
|
Self(vec![]).layer(layer)
|
||||||
}
|
}
|
||||||
|
|
@ -22,31 +20,31 @@ impl<S> InputLayers<S> {
|
||||||
self.add_layer_if(None, layer.into()); self
|
self.add_layer_if(None, layer.into()); self
|
||||||
}
|
}
|
||||||
pub fn add_layer_if (&mut self, condition: Option<Ast>, binding: Ast) -> &mut Self {
|
pub fn add_layer_if (&mut self, condition: Option<Ast>, binding: Ast) -> &mut Self {
|
||||||
self.0.push(InputLayer { condition, binding, __: Default::default() });
|
self.0.push(InputLayer(condition, binding));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
pub fn handle <S: Eval<Ast, bool> + Eval<Ast, O>, I: Eval<Ast, bool>, O: Command<S>> (&self, state: &mut S, input: I) -> Perhaps<O> {
|
||||||
|
InputHandle(state, self.0.as_slice()).try_eval(input)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl<S: Eval<Ast, bool> + Eval<Ast, C>, C: Command<S>, I: Debug + Eval<Ast, bool>> Eval<(S, I), C> for InputLayers<S> {
|
|
||||||
fn try_eval (&self, (state, input): (S, I)) -> Perhaps<C> {
|
pub struct InputHandle<'a, S>(&'a mut S, &'a [InputLayer]);
|
||||||
for InputLayer { condition, binding, .. } in self.0.iter() {
|
|
||||||
|
impl<'a, S: Eval<Ast, bool> + Eval<Ast, O>, I: Eval<Ast, bool>, O: Command<S>> Eval<I, O> for InputHandle<'a, S> {
|
||||||
|
fn try_eval (&self, input: I) -> Perhaps<O> {
|
||||||
|
let Self(state, layers) = self;
|
||||||
|
for InputLayer(condition, binding) in layers.iter() {
|
||||||
let mut matches = true;
|
let mut matches = true;
|
||||||
if let Some(condition) = condition {
|
if let Some(condition) = condition {
|
||||||
matches = state.eval(condition.clone(), ||"input: no condition")?;
|
matches = state.eval(condition.clone(), ||"input: no condition")?;
|
||||||
}
|
}
|
||||||
if matches {
|
if matches
|
||||||
if let Ast::Exp(e) = binding {
|
&& let Some(exp) = binding.exp()
|
||||||
if let Some(ast) = e.peek() {
|
&& let Some(ast) = exp.peek()
|
||||||
if input.eval(ast.clone(), ||"InputLayers: input.eval(binding) failed")?
|
&& input.eval(ast.clone(), ||"InputLayers: input.eval(binding) failed")?
|
||||||
&& let Some(command) = state.try_eval(ast)? {
|
&& let Some(command) = state.try_eval(ast)? {
|
||||||
return Ok(Some(command))
|
return Ok(Some(command))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
unreachable!("InputLayer")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!("InputLayer: expected expression, got: {input:?}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#![feature(associated_type_defaults)]
|
#![feature(associated_type_defaults)]
|
||||||
|
#![feature(if_let_guard)]
|
||||||
|
|
||||||
pub(crate) use tengri_core::*;
|
pub(crate) use tengri_core::*;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,159 +1,118 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
use Value::*;
|
||||||
|
|
||||||
impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
||||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
fn try_provide (state: &S, source: Ast) -> Perhaps<Self> {
|
||||||
if let Ast::Exp(source) = source {
|
if let Exp(_, exp) = source.0 && let Some(Ast(Key(id))) = exp.peek() && *id == *"when" {
|
||||||
if let Some(Ast::Key(id)) = source.next() && *id == *"when" {
|
let _ = exp.next();
|
||||||
return Ok(Some(Self(
|
return Ok(Some(Self(
|
||||||
state.eval(source.next().unwrap(), ||"when: expected condition")?,
|
state.eval(exp.next().unwrap(), ||"when: expected condition")?,
|
||||||
state.eval(source.next().unwrap(), ||"when: expected content")?,
|
state.eval(exp.next().unwrap(), ||"when: expected content")?,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A, B> Dsl<S> for Either<A, B> where S: Eval<Ast, bool> + Eval<Ast, A> + Eval<Ast, B> {
|
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, mut source: Ast) -> Perhaps<Self> {
|
fn try_provide (state: &S, source: Ast) -> Perhaps<Self> {
|
||||||
if let Ast::Exp(mut source) = source {
|
if let Exp(_, exp) = source.0 && let Some(Ast(Key(id))) = exp.peek() && *id == *"either" {
|
||||||
if let Some(Ast::Key(id)) = source.next() && *id == *"either" {
|
let _ = exp.next();
|
||||||
return Ok(Some(Self(
|
return Ok(Some(Self(
|
||||||
state.eval(source.next().unwrap(), ||"either: expected condition")?,
|
state.eval(exp.next().unwrap(), ||"either: expected condition")?,
|
||||||
state.eval(source.next().unwrap(), ||"either: expected content 1")?,
|
state.eval(exp.next().unwrap(), ||"either: expected content 1")?,
|
||||||
state.eval(source.next().unwrap(), ||"either: expected content 2")?,
|
state.eval(exp.next().unwrap(), ||"either: expected content 2")?,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A, B> Dsl<S> for Bsp<A, B>
|
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
|
||||||
where S: Eval<Option<Ast>, A> + Eval<Option<Ast>, B> {
|
fn try_provide (state: &S, source: Ast) -> Perhaps<Self> {
|
||||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
if let Exp(_, source) = source.0 {
|
||||||
Ok(Some(if let Ast::Exp(source) = source {
|
let mut rest = source.clone();
|
||||||
match source.next() {
|
return Ok(Some(match rest.next().and_then(|x|x.key()) {
|
||||||
Some(Value::Key("bsp/n")) => {
|
Some("align/c") => Self::c(state.eval(rest.next(), ||"align/c: expected content")?),
|
||||||
let a: A = state.eval(source.next(), ||"bsp/n: expected content 1")?;
|
Some("align/x") => Self::x(state.eval(rest.next(), ||"align/x: expected content")?),
|
||||||
let b: B = state.eval(source.next(), ||"bsp/n: expected content 2")?;
|
Some("align/y") => Self::y(state.eval(rest.next(), ||"align/y: expected content")?),
|
||||||
Self::n(a, b)
|
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(Value::Key("bsp/s")) => {
|
Some("align/e") => Self::e(state.eval(rest.next(), ||"align/e: expected content")?),
|
||||||
let a: A = state.eval(source.next(), ||"bsp/s: expected content 1")?;
|
Some("align/w") => Self::w(state.eval(rest.next(), ||"align/w: expected content")?),
|
||||||
let b: B = state.eval(source.next(), ||"bsp/s: expected content 2")?;
|
Some("align/nw") => Self::nw(state.eval(rest.next(), ||"align/nw: expected content")?),
|
||||||
Self::s(a, b)
|
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(Value::Key("bsp/e")) => {
|
Some("align/se") => Self::se(state.eval(rest.next(), ||"align/se: expected content")?),
|
||||||
let a: A = state.eval(source.next(), ||"bsp/e: expected content 1")?;
|
|
||||||
let b: B = state.eval(source.next(), ||"bsp/e: expected content 2")?;
|
|
||||||
Self::e(a, b)
|
|
||||||
},
|
|
||||||
Some(Value::Key("bsp/w")) => {
|
|
||||||
let a: A = state.eval(source.next(), ||"bsp/w: expected content 1")?;
|
|
||||||
let b: B = state.eval(source.next(), ||"bsp/w: expected content 2")?;
|
|
||||||
Self::w(a, b)
|
|
||||||
},
|
|
||||||
Some(Value::Key("bsp/a")) => {
|
|
||||||
let a: A = state.eval(source.next(), ||"bsp/a: expected content 1")?;
|
|
||||||
let b: B = state.eval(source.next(), ||"bsp/a: expected content 2")?;
|
|
||||||
Self::a(a, b)
|
|
||||||
},
|
|
||||||
Some(Value::Key("bsp/b")) => {
|
|
||||||
let a: A = state.eval(source.next(), ||"bsp/b: expected content 1")?;
|
|
||||||
let b: B = state.eval(source.next(), ||"bsp/b: expected content 2")?;
|
|
||||||
Self::b(a, b)
|
|
||||||
},
|
|
||||||
_ => return Ok(None),
|
_ => return Ok(None),
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Ok(None)
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
|
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: Ast) -> Perhaps<Self> {
|
fn try_provide (state: &S, source: Ast) -> Perhaps<Self> {
|
||||||
Ok(Some(if let Ast::Exp(source) = source {
|
if let Exp(_, exp) = source.0 {
|
||||||
match source.next() {
|
let mut rest = exp.clone();
|
||||||
Some(Value::Key("align/c")) => {
|
return Ok(Some(match rest.next().and_then(|x|x.key()) {
|
||||||
let content: A = state.eval(source.next(), ||"align/c: expected content")?;
|
Some("bsp/n") => Self::n(
|
||||||
Self::c(content)
|
state.eval(rest.next(), ||"bsp/n: expected content 1")?,
|
||||||
},
|
state.eval(rest.next(), ||"bsp/n: expected content 2")?,
|
||||||
Some(Value::Key("align/x")) => {
|
),
|
||||||
let content: A = state.eval(source.next(), ||"align/x: expected content")?;
|
Some("bsp/s") => Self::s(
|
||||||
Self::x(content)
|
state.eval(rest.next(), ||"bsp/s: expected content 1")?,
|
||||||
},
|
state.eval(rest.next(), ||"bsp/s: expected content 2")?,
|
||||||
Some(Value::Key("align/y")) => {
|
),
|
||||||
let content: A = state.eval(source.next(), ||"align/y: expected content")?;
|
Some("bsp/e") => Self::e(
|
||||||
Self::y(content)
|
state.eval(rest.next(), ||"bsp/e: expected content 1")?,
|
||||||
},
|
state.eval(rest.next(), ||"bsp/e: expected content 2")?,
|
||||||
Some(Value::Key("align/n")) => {
|
),
|
||||||
let content: A = state.eval(source.next(), ||"align/n: expected content")?;
|
Some("bsp/w") => Self::w(
|
||||||
Self::n(content)
|
state.eval(rest.next(), ||"bsp/w: expected content 1")?,
|
||||||
},
|
state.eval(rest.next(), ||"bsp/w: expected content 2")?,
|
||||||
Some(Value::Key("align/s")) => {
|
),
|
||||||
let content: A = state.eval(source.next(), ||"align/s: expected content")?;
|
Some("bsp/a") => Self::a(
|
||||||
Self::s(content)
|
state.eval(rest.next(), ||"bsp/a: expected content 1")?,
|
||||||
},
|
state.eval(rest.next(), ||"bsp/a: expected content 2")?,
|
||||||
Some(Value::Key("align/e")) => {
|
),
|
||||||
let content: A = state.eval(source.next(), ||"align/e: expected content")?;
|
Some("bsp/b") => Self::b(
|
||||||
Self::e(content)
|
state.eval(rest.next(), ||"bsp/b: expected content 1")?,
|
||||||
},
|
state.eval(rest.next(), ||"bsp/b: expected content 2")?,
|
||||||
Some(Value::Key("align/w")) => {
|
),
|
||||||
let content: A = state.eval(source.next(), ||"align/w: expected content")?;
|
|
||||||
Self::w(content)
|
|
||||||
},
|
|
||||||
Some(Value::Key("align/nw")) => {
|
|
||||||
let content: A = state.eval(source.next(), ||"align/nw: expected content")?;
|
|
||||||
Self::nw(content)
|
|
||||||
},
|
|
||||||
Some(Value::Key("align/ne")) => {
|
|
||||||
let content: A = state.eval(source.next(), ||"align/ne: expected content")?;
|
|
||||||
Self::ne(content)
|
|
||||||
},
|
|
||||||
Some(Value::Key("align/sw")) => {
|
|
||||||
let content: A = state.eval(source.next(), ||"align/sw: expected content")?;
|
|
||||||
Self::sw(content)
|
|
||||||
},
|
|
||||||
Some(Value::Key("align/se")) => {
|
|
||||||
let content: A = state.eval(source.next(), ||"align/se: expected content")?;
|
|
||||||
Self::se(content)
|
|
||||||
},
|
|
||||||
_ => return Ok(None),
|
_ => return Ok(None),
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Ok(None)
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//#[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
|
//#[cfg(feature = "dsl")] take!($Enum<A>, A|state, words|Ok(
|
||||||
//if let Some(Token { value: Value::Key(k), .. }) = words.peek() {
|
//if let Some(Token { value: Key(k), .. }) = words.peek() {
|
||||||
//let mut base = words.clone();
|
//let mut base = words.clone();
|
||||||
//let content = state.give_or_fail(words, ||format!("{k}: no content"))?;
|
//let content = state.give_or_fail(words, ||format!("{k}: no content"))?;
|
||||||
//return Ok(Some(match words.next() {
|
//return Ok(Some(match words.next() {
|
||||||
//Some(Token{value: Value::Key($x),..}) => Self::x(content),
|
//Some(Token{value: Key($x),..}) => Self::x(content),
|
||||||
//Some(Token{value: Value::Key($y),..}) => Self::y(content),
|
//Some(Token{value: Key($y),..}) => Self::y(content),
|
||||||
//Some(Token{value: Value::Key($xy),..}) => Self::xy(content),
|
//Some(Token{value: Key($xy),..}) => Self::xy(content),
|
||||||
//_ => unreachable!()
|
//_ => unreachable!()
|
||||||
//}))
|
//}))
|
||||||
//} else {
|
//} else {
|
||||||
//None
|
//None
|
||||||
//}));
|
//}));
|
||||||
//#[cfg(feature = "dsl")] take!($Enum<U, A>, U, A|state, words|Ok(
|
//#[cfg(feature = "dsl")] take!($Enum<U, A>, U, A|state, words|Ok(
|
||||||
//if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = words.peek() {
|
//if let Some(Token { value: Key($x|$y|$xy), .. }) = words.peek() {
|
||||||
//let mut base = words.clone();
|
//let mut base = words.clone();
|
||||||
//Some(match words.next() {
|
//Some(match words.next() {
|
||||||
//Some(Token { value: Value::Key($x), .. }) => Self::x(
|
//Some(Token { value: Key($x), .. }) => Self::x(
|
||||||
//state.give_or_fail(words, ||"x: no unit")?,
|
//state.give_or_fail(words, ||"x: no unit")?,
|
||||||
//state.give_or_fail(words, ||"x: no content")?,
|
//state.give_or_fail(words, ||"x: no content")?,
|
||||||
//),
|
//),
|
||||||
//Some(Token { value: Value::Key($y), .. }) => Self::y(
|
//Some(Token { value: Key($y), .. }) => Self::y(
|
||||||
//state.give_or_fail(words, ||"y: no unit")?,
|
//state.give_or_fail(words, ||"y: no unit")?,
|
||||||
//state.give_or_fail(words, ||"y: no content")?,
|
//state.give_or_fail(words, ||"y: no content")?,
|
||||||
//),
|
//),
|
||||||
//Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
//Some(Token { value: Key($x), .. }) => Self::xy(
|
||||||
//state.give_or_fail(words, ||"xy: no unit x")?,
|
//state.give_or_fail(words, ||"xy: no unit x")?,
|
||||||
//state.give_or_fail(words, ||"xy: no unit y")?,
|
//state.give_or_fail(words, ||"xy: no unit y")?,
|
||||||
//state.give_or_fail(words, ||"xy: no content")?
|
//state.give_or_fail(words, ||"xy: no content")?
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue