mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
dsl gets the gordian treatment
This commit is contained in:
parent
93b1cf1a5c
commit
d6e8be6ce5
9 changed files with 540 additions and 456 deletions
|
|
@ -1,70 +1,36 @@
|
||||||
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)]
|
/// Owns its values, and has no metadata.
|
||||||
pub enum Ast {
|
pub type AstToken = Token<AstValue, AstMeta>;
|
||||||
#[default] Nil,
|
|
||||||
Err(DslError),
|
|
||||||
Num(usize),
|
|
||||||
Sym(Arc<str>),
|
|
||||||
Key(Arc<str>),
|
|
||||||
Str(Arc<str>),
|
|
||||||
Exp(Arc<Box<dyn AstIter>>),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Emits tokens.
|
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||||
pub trait AstIter: Debug {
|
pub struct AstMeta;
|
||||||
fn peek (&self) -> Option<Ast>;
|
|
||||||
fn next (&mut self) -> Option<Ast>;
|
|
||||||
fn rest (self) -> Option<Box<dyn AstIter>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A [Cst] can be used as an [Ast].
|
pub type AstValue = Value<Arc<str>, AstExp>;
|
||||||
impl<'source: 'static> AstIter for Cst<'source> {
|
impl<'source> From<CstValue<'source>> for AstValue {
|
||||||
fn peek (&self) -> Option<Ast> {
|
fn from (value: CstValue<'source>) -> Self {
|
||||||
Cst::peek(self).map(|token|token.value.into())
|
use Value::*;
|
||||||
}
|
match value {
|
||||||
fn next (&mut self) -> Option<Ast> {
|
Nil => Nil,
|
||||||
Iterator::next(self).map(|token|token.value.into())
|
Err(e) => Err(e),
|
||||||
}
|
Num(u) => Num(u),
|
||||||
fn rest (self) -> Option<Box<dyn AstIter>> {
|
Sym(s) => Sym(s.into()),
|
||||||
self.peek().is_some().then(||Box::new(self) as Box<dyn AstIter>)
|
Key(s) => Key(s.into()),
|
||||||
}
|
Str(s) => Str(s.into()),
|
||||||
}
|
Exp(x) => Exp(x.into()),
|
||||||
|
|
||||||
impl std::fmt::Display for Ast {
|
|
||||||
fn fmt (&self, out: &mut Formatter) -> Result<(), std::fmt::Error> {
|
|
||||||
use Ast::*;
|
|
||||||
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:?}"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'source: 'static> From<CstValue<'source>> for Ast {
|
|
||||||
fn from (other: CstValue<'source>) -> Self {
|
|
||||||
use CstValue::*;
|
|
||||||
match other {
|
|
||||||
Nil => Self::Nil,
|
|
||||||
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> {
|
#[derive(Clone, Default, PartialEq)]
|
||||||
fn into (self) -> Box<dyn AstIter> {
|
pub struct AstExp(pub Vec<AstToken>);
|
||||||
Box::new(self)
|
impl<'source> From<CstExp<'source>> for AstExp {
|
||||||
|
fn from (exp: CstExp<'source>) -> AstExp {
|
||||||
|
AstExp(exp.words.map(|token|Token(
|
||||||
|
AstValue::from(token.value()),
|
||||||
|
AstMeta,
|
||||||
|
)).collect())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,291 +1,17 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
/// Provides a native [Iterator] API over [CstConstIter],
|
/// Keeps the reference to the source slice.
|
||||||
/// emitting [CstToken] items.
|
pub type CstToken<'source> = Token<CstValue<'source>, CstMeta<'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.
|
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
|
||||||
/// [CstConstIter::next] emits subsequent pairs of:
|
|
||||||
/// * 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].
|
pub type CstValue<'source> = Value<&'source str, CstExp<'source>>;
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum CstValue<'source> {
|
|
||||||
#[default] Nil,
|
|
||||||
Err(DslError),
|
|
||||||
Num(usize),
|
|
||||||
Sym(&'source str),
|
|
||||||
Key(&'source str),
|
|
||||||
Str(&'source str),
|
|
||||||
Exp(usize, Cst<'source>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Cst<'a> {
|
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstExp<'source> {
|
||||||
pub const fn new (source: &'a str) -> Self {
|
pub depth: usize,
|
||||||
Self(CstConstIter::new(source))
|
pub words: SourceIter<'source>
|
||||||
}
|
|
||||||
pub const fn peek (&self) -> Option<CstToken<'a>> {
|
|
||||||
self.0.peek()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a str> for Cst<'a> {
|
|
||||||
fn from (source: &'a str) -> Self{
|
|
||||||
Self(CstConstIter(source))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<CstConstIter<'a>> for Cst<'a> {
|
|
||||||
fn from (source: CstConstIter<'a>) -> Self{
|
|
||||||
Self(source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const_iter!(<'a>|self: CstConstIter<'a>| => CstToken<'a> => self.next_mut().map(|(result, _)|result));
|
|
||||||
|
|
||||||
impl<'a> From<&'a str> for CstConstIter<'a> {
|
|
||||||
fn from (source: &'a str) -> Self{
|
|
||||||
Self::new(source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> CstConstIter<'a> {
|
|
||||||
pub const fn new (source: &'a 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<'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.
|
|
||||||
#[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 CstValue::*;
|
|
||||||
let mut token: CstToken<'a> = CstToken::new(source, 0, 0, Nil);
|
|
||||||
iterate!(char_indices(source) => (start, c) => token = match token.value() {
|
|
||||||
Err(_) => return Some(token),
|
|
||||||
Nil => match c {
|
|
||||||
' '|'\n'|'\r'|'\t' =>
|
|
||||||
token.grow(),
|
|
||||||
'(' =>
|
|
||||||
CstToken::new(source, start, 1, Exp(1, Cst::new(str_range(source, start, start + 1)))),
|
|
||||||
'"' =>
|
|
||||||
CstToken::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
|
||||||
':'|'@' =>
|
|
||||||
CstToken::new(source, start, 1, Sym(str_range(source, start, start + 1))),
|
|
||||||
'/'|'a'..='z' =>
|
|
||||||
CstToken::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
|
||||||
'0'..='9' =>
|
|
||||||
CstToken::new(source, start, 1, match to_digit(c) {
|
|
||||||
Ok(c) => CstValue::Num(c),
|
|
||||||
Result::Err(e) => CstValue::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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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:?}"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
39
dsl/src/dsl_display.rs
Normal file
39
dsl/src/dsl_display.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
||||||
|
|
||||||
|
impl Display for AstValue {
|
||||||
|
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:?}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for AstExp {
|
||||||
|
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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: impl DslValue) -> 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: impl DslValue, 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>>`.
|
||||||
|
|
|
||||||
96
dsl/src/dsl_iter.rs
Normal file
96
dsl/src/dsl_iter.rs
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
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) -> Option<&[<Self::Token as DslToken>::Value]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<'a>(pub SourceConstIter<'a>);
|
||||||
|
|
||||||
|
impl<'a> SourceIter<'a> {
|
||||||
|
pub const fn new (source: &'a str) -> Self {
|
||||||
|
Self(SourceConstIter::new(source))
|
||||||
|
}
|
||||||
|
pub const fn peek (&self) -> Option<CstToken<'a>> {
|
||||||
|
self.0.peek()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for SourceIter<'a> {
|
||||||
|
type Item = CstToken<'a>;
|
||||||
|
fn next (&mut self) -> Option<CstToken<'a>> {
|
||||||
|
self.0.next().map(|(item, rest)|{self.0 = rest; item})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for SourceIter<'a> {
|
||||||
|
fn from (source: &'a str) -> Self{
|
||||||
|
Self(SourceConstIter(source))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<'a>(pub &'a str);
|
||||||
|
|
||||||
|
impl<'a> From<SourceConstIter<'a>> for SourceIter<'a> {
|
||||||
|
fn from (source: SourceConstIter<'a>) -> Self{
|
||||||
|
Self(source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for SourceConstIter<'a> {
|
||||||
|
fn from (source: &'a str) -> Self{
|
||||||
|
Self::new(source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> SourceConstIter<'a> {
|
||||||
|
pub const fn new (source: &'a 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<'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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const_iter!(<'a>|self: SourceConstIter<'a>| => CstToken<'a> => self.next_mut().map(|(result, _)|result));
|
||||||
264
dsl/src/dsl_token.rs
Normal file
264
dsl/src/dsl_token.rs
Normal file
|
|
@ -0,0 +1,264 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Clone, Default, Debug)]
|
||||||
|
pub enum Value<
|
||||||
|
S: PartialEq + Clone + Default + Debug,
|
||||||
|
X: PartialEq + Clone + Default + Debug,
|
||||||
|
> {
|
||||||
|
#[default] Nil, Err(DslError), Num(usize), Sym(S), Key(S), Str(S), Exp(X),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait DslValue: PartialEq + Clone + Default + Debug {
|
||||||
|
type Err: PartialEq + Clone + Debug;
|
||||||
|
type Num: PartialEq + Copy + Clone + Default + Debug;
|
||||||
|
type Str: PartialEq + Clone + Default + Debug;
|
||||||
|
type Exp: PartialEq + Clone + Default + Debug;
|
||||||
|
fn nil (&self) -> bool;
|
||||||
|
fn err (&self) -> Option<&Self::Err>;
|
||||||
|
fn num (&self) -> Option<Self::Num>;
|
||||||
|
fn sym (&self) -> Option<&Self::Str>;
|
||||||
|
fn key (&self) -> Option<&Self::Str>;
|
||||||
|
fn str (&self) -> Option<&Self::Str>;
|
||||||
|
fn exp (&self) -> Option<&Self::Exp>;
|
||||||
|
fn exp_head (&self) -> Option<&Self> { None } // TODO
|
||||||
|
fn exp_tail (&self) -> Option<&[Self]> { None } // TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<
|
||||||
|
Str: PartialEq + Clone + Default + Debug,
|
||||||
|
Exp: PartialEq + Clone + Default + Debug,
|
||||||
|
> DslValue for Value<Str, Exp> {
|
||||||
|
type Err = DslError;
|
||||||
|
type Num = usize;
|
||||||
|
type Str = Str;
|
||||||
|
type Exp = Exp;
|
||||||
|
fn nil (&self) -> bool {
|
||||||
|
matches!(self, Self::Nil)
|
||||||
|
}
|
||||||
|
fn err (&self) -> Option<&DslError> {
|
||||||
|
if let Self::Err(e) = self { Some(e) } else { None }
|
||||||
|
}
|
||||||
|
fn num (&self) -> Option<usize> {
|
||||||
|
if let Self::Num(n) = self { Some(*n) } else { None }
|
||||||
|
}
|
||||||
|
fn sym (&self) -> Option<&Str> {
|
||||||
|
if let Self::Sym(s) = self { Some(s) } else { None }
|
||||||
|
}
|
||||||
|
fn key (&self) -> Option<&Str> {
|
||||||
|
if let Self::Key(k) = self { Some(k) } else { None }
|
||||||
|
}
|
||||||
|
fn str (&self) -> Option<&Str> {
|
||||||
|
if let Self::Str(s) = self { Some(s) } else { None }
|
||||||
|
}
|
||||||
|
fn exp (&self) -> Option<&Exp> {
|
||||||
|
if let Self::Exp(x) = self { Some(x) } else { None }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(PartialEq, Clone, Default, Debug)]
|
||||||
|
pub struct Token<
|
||||||
|
V: PartialEq + Clone + Default + Debug,
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'source> CstToken<'source> {
|
||||||
|
pub const fn new (
|
||||||
|
source: &'source str, start: usize, length: usize, value: CstValue<'source>
|
||||||
|
) -> Self {
|
||||||
|
Self(value, CstMeta { source, start, length })
|
||||||
|
}
|
||||||
|
pub const fn end (&self) -> usize {
|
||||||
|
self.1.start.saturating_add(self.1.length)
|
||||||
|
}
|
||||||
|
pub const fn slice (&'source self) -> &'source str {
|
||||||
|
self.slice_source(self.1.source)
|
||||||
|
}
|
||||||
|
pub const fn slice_source <'range> (&'source self, source: &'range str) -> &'range str {
|
||||||
|
str_range(source, self.1.start, self.end())
|
||||||
|
}
|
||||||
|
pub const fn slice_source_exp <'range> (&'source self, source: &'range str) -> &'range str {
|
||||||
|
str_range(source, self.1.start.saturating_add(1), self.end())
|
||||||
|
}
|
||||||
|
pub const fn with_value (self, value: CstValue<'source>) -> Self {
|
||||||
|
Self(value, self.1)
|
||||||
|
}
|
||||||
|
pub const fn value (&self) -> CstValue<'source> {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
pub const fn error (self, error: DslError) -> Self {
|
||||||
|
Self(Value::Err(error), self.1)
|
||||||
|
}
|
||||||
|
pub const fn grow (self) -> Self {
|
||||||
|
Self(self.0, CstMeta { length: self.1.length.saturating_add(1), ..self.1 })
|
||||||
|
}
|
||||||
|
pub const fn grow_num (self, m: usize, c: char) -> Self {
|
||||||
|
use Value::*;
|
||||||
|
match to_digit(c) {
|
||||||
|
Result::Ok(n) => Self(Num(10*m+n), self.grow().1),
|
||||||
|
Result::Err(e) => Self(Err(e), self.grow().1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub const fn grow_key (self) -> Self {
|
||||||
|
use Value::*;
|
||||||
|
let token = self.grow();
|
||||||
|
token.with_value(Key(token.slice_source(self.1.source)))
|
||||||
|
}
|
||||||
|
pub const fn grow_sym (self) -> Self {
|
||||||
|
use Value::*;
|
||||||
|
let token = self.grow();
|
||||||
|
token.with_value(Sym(token.slice_source(self.1.source)))
|
||||||
|
}
|
||||||
|
pub const fn grow_str (self) -> Self {
|
||||||
|
use Value::*;
|
||||||
|
let token = self.grow();
|
||||||
|
token.with_value(Str(token.slice_source(self.1.source)))
|
||||||
|
}
|
||||||
|
pub const fn grow_exp (self) -> Self {
|
||||||
|
use Value::*;
|
||||||
|
let token = self.grow();
|
||||||
|
if let Exp(depth, _) = token.value() {
|
||||||
|
token.with_value(Exp(depth, Cst::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))
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub const fn grow_out (self) -> Self {
|
||||||
|
let token = self.grow_exp();
|
||||||
|
if let Value::Exp(depth, source) = token.value {
|
||||||
|
if depth > 0 {
|
||||||
|
token.with_value(Value::Exp(depth - 1, source))
|
||||||
|
} else {
|
||||||
|
return self.error(Unexpected(')'))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn to_number (digits: &str) -> DslResult<usize> {
|
||||||
|
let mut value = 0;
|
||||||
|
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
|
||||||
|
Ok(digit) => value = 10 * value + digit,
|
||||||
|
Result::Err(e) => return Result::Err(e)
|
||||||
|
});
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn to_digit (c: char) -> DslResult<usize> {
|
||||||
|
Ok(match c {
|
||||||
|
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
|
||||||
|
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
|
||||||
|
_ => return Result::Err(Unexpected(c))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Static iteration helper.
|
||||||
|
#[macro_export] macro_rules! iterate {
|
||||||
|
($expr:expr => $arg: pat => $body:expr) => {
|
||||||
|
let mut iter = $expr;
|
||||||
|
while let Some(($arg, next)) = iter.next() {
|
||||||
|
$body;
|
||||||
|
iter = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||||
|
use Value::*;
|
||||||
|
let mut token: CstToken<'a> = CstToken::new(source, 0, 0, Nil);
|
||||||
|
iterate!(char_indices(source) => (start, c) => token = match token.value() {
|
||||||
|
Err(_) => return Some(token),
|
||||||
|
Nil => match c {
|
||||||
|
' '|'\n'|'\r'|'\t' =>
|
||||||
|
token.grow(),
|
||||||
|
'(' =>
|
||||||
|
CstToken::new(source, start, 1, Exp(1, Cst::new(str_range(source, start, start + 1)))),
|
||||||
|
'"' =>
|
||||||
|
CstToken::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
||||||
|
':'|'@' =>
|
||||||
|
CstToken::new(source, start, 1, Sym(str_range(source, start, start + 1))),
|
||||||
|
'/'|'a'..='z' =>
|
||||||
|
CstToken::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
||||||
|
'0'..='9' =>
|
||||||
|
CstToken::new(source, start, 1, match to_digit(c) {
|
||||||
|
Ok(c) => Value::Num(c),
|
||||||
|
Result::Err(e) => Value::Err(e)
|
||||||
|
}),
|
||||||
|
_ => token.error(Unexpected(c))
|
||||||
|
},
|
||||||
|
Str(_) => match c {
|
||||||
|
'"' => return Some(token),
|
||||||
|
_ => token.grow_str(),
|
||||||
|
},
|
||||||
|
Num(n) => match c {
|
||||||
|
'0'..='9' => token.grow_num(n, c),
|
||||||
|
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||||
|
_ => token.error(Unexpected(c))
|
||||||
|
},
|
||||||
|
Sym(_) => match c {
|
||||||
|
'a'..='z'|'A'..='Z'|'0'..='9'|'-' => token.grow_sym(),
|
||||||
|
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||||
|
_ => token.error(Unexpected(c))
|
||||||
|
},
|
||||||
|
Key(_) => match c {
|
||||||
|
'a'..='z'|'0'..='9'|'-'|'/' => token.grow_key(),
|
||||||
|
' '|'\n'|'\r'|'\t'|')' => return Some(token),
|
||||||
|
_ => token.error(Unexpected(c))
|
||||||
|
},
|
||||||
|
Exp(depth, _) => match depth {
|
||||||
|
0 => return Some(token.grow_exp()),
|
||||||
|
_ => match c {
|
||||||
|
')' => token.grow_out(),
|
||||||
|
'(' => token.grow_in(),
|
||||||
|
_ => token.grow_exp(),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
match token.value() {
|
||||||
|
Nil => None,
|
||||||
|
_ => Some(token),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//impl<S1, S2: From<S1>, X1, X2: From<S2>> From<Value<S1, X1>> for Value<S2, X2> {
|
||||||
|
//fn from (other: Value<S2, X2>) -> Self {
|
||||||
|
//use Value::*;
|
||||||
|
//match other {
|
||||||
|
//Nil => Nil,
|
||||||
|
//Err(e) => Err(e),
|
||||||
|
//Num(u) => Num(u),
|
||||||
|
//Sym(s) => Sym(s.into()),
|
||||||
|
//Key(s) => Key(s.into()),
|
||||||
|
//Str(s) => Str(s.into()),
|
||||||
|
//Exp(x) => Exp(x.into()),
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
|
@ -46,8 +46,11 @@ 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::*;
|
||||||
|
mod dsl_token; pub use self::dsl_token::*;
|
||||||
|
|
||||||
#[cfg(test)] mod test_token_iter {
|
#[cfg(test)] mod test_token_iter {
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,47 @@
|
||||||
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<Cst<'static>>, Cst<'static>);
|
||||||
condition: Option<Ast>,
|
|
||||||
binding: Ast,
|
impl InputLayers {
|
||||||
}
|
pub fn new (layer: Cst<'static>) -> Self {
|
||||||
impl<S> InputLayers<S> {
|
|
||||||
pub fn new (layer: Ast) -> Self {
|
|
||||||
Self(vec![]).layer(layer)
|
Self(vec![]).layer(layer)
|
||||||
}
|
}
|
||||||
pub fn layer (mut self, layer: Ast) -> Self {
|
pub fn layer (mut self, layer: Cst<'static>) -> Self {
|
||||||
self.add_layer(layer); self
|
self.add_layer(layer); self
|
||||||
}
|
}
|
||||||
pub fn layer_if (mut self, condition: Ast, layer: Ast) -> Self {
|
pub fn layer_if (mut self, condition: Cst<'static>, layer: Cst<'static>) -> Self {
|
||||||
self.add_layer_if(Some(condition), layer); self
|
self.add_layer_if(Some(condition), layer); self
|
||||||
}
|
}
|
||||||
pub fn add_layer (&mut self, layer: Ast) -> &mut Self {
|
pub fn add_layer (&mut self, layer: Cst<'static>) -> &mut Self {
|
||||||
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<Cst<'static>>, binding: Cst<'static>) -> &mut Self {
|
||||||
self.0.push(InputLayer { condition, binding, __: Default::default() });
|
self.0.push(InputLayer { condition, binding, __: Default::default() });
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
pub fn handle <S: Eval<dyn Ast, bool>, I, 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<&'a dyn Ast, bool>, I, 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, ||"input: no condition")?;
|
||||||
}
|
}
|
||||||
if matches {
|
if matches {
|
||||||
if let Ast::Exp(e) = binding {
|
if let AstValue::Exp(e) = binding.peek() {
|
||||||
if let Some(ast) = e.peek() {
|
if let Some(ast) = e.peek() {
|
||||||
if input.eval(ast.clone(), ||"InputLayers: input.eval(binding) failed")?
|
if input.eval(ast.clone(), ||"InputLayers: input.eval(binding) failed")?
|
||||||
&& let Some(command) = state.try_eval(ast)? {
|
&& let Some(command) = state.try_eval(ast)? {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
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: impl Ast) -> Perhaps<Self> {
|
||||||
if let Ast::Exp(source) = source {
|
if let Ast::Exp(exp) = source {
|
||||||
if let Some(Ast::Key(id)) = source.next() && *id == *"when" {
|
let mut iter = source.clone();
|
||||||
|
if let Some(Ast::Key(id)) = iter.next() && *id == *"when" {
|
||||||
return Ok(Some(Self(
|
return Ok(Some(Self(
|
||||||
state.eval(source.next().unwrap(), ||"when: expected condition")?,
|
state.eval(iter.next().unwrap(), ||"when: expected condition")?,
|
||||||
state.eval(source.next().unwrap(), ||"when: expected content")?,
|
state.eval(iter.next().unwrap(), ||"when: expected content")?,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,13 +16,14 @@ impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
||||||
}
|
}
|
||||||
|
|
||||||
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: impl Ast) -> Perhaps<Self> {
|
||||||
if let Ast::Exp(mut source) = source {
|
if let Ast::Exp(source) = source {
|
||||||
if let Some(Ast::Key(id)) = source.next() && *id == *"either" {
|
let mut iter = source.clone();
|
||||||
|
if let Some(Ast::Key(id)) = iter.next() && *id == *"either" {
|
||||||
return Ok(Some(Self(
|
return Ok(Some(Self(
|
||||||
state.eval(source.next().unwrap(), ||"either: expected condition")?,
|
state.eval(iter.next().unwrap(), ||"either: expected condition")?,
|
||||||
state.eval(source.next().unwrap(), ||"either: expected content 1")?,
|
state.eval(iter.next().unwrap(), ||"either: expected content 1")?,
|
||||||
state.eval(source.next().unwrap(), ||"either: expected content 2")?,
|
state.eval(iter.next().unwrap(), ||"either: expected content 2")?,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -29,41 +31,35 @@ impl<S, A, B> Dsl<S> for Either<A, B> where S: Eval<Ast, bool> + Eval<Ast, A> +
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A, B> Dsl<S> for Bsp<A, B>
|
impl<S, A, B> Dsl<S> for Bsp<A, B> where S: Eval<Option<Ast>, A> + Eval<Option<Ast>, B> {
|
||||||
where S: Eval<Option<Ast>, A> + Eval<Option<Ast>, B> {
|
fn try_provide (state: S, source: impl Ast) -> Perhaps<Self> {
|
||||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
|
||||||
Ok(Some(if let Ast::Exp(source) = source {
|
Ok(Some(if let Ast::Exp(source) = source {
|
||||||
match source.next() {
|
let mut iter = source.clone();
|
||||||
Some(Value::Key("bsp/n")) => {
|
match iter.next() {
|
||||||
let a: A = state.eval(source.next(), ||"bsp/n: expected content 1")?;
|
Some(Value::Key("bsp/n")) => Self::n(
|
||||||
let b: B = state.eval(source.next(), ||"bsp/n: expected content 2")?;
|
state.eval(iter.next(), ||"bsp/n: expected content 1")?,
|
||||||
Self::n(a, b)
|
state.eval(iter.next(), ||"bsp/n: expected content 2")?,
|
||||||
},
|
),
|
||||||
Some(Value::Key("bsp/s")) => {
|
Some(Value::Key("bsp/s")) => Self::s(
|
||||||
let a: A = state.eval(source.next(), ||"bsp/s: expected content 1")?;
|
state.eval(iter.next(), ||"bsp/s: expected content 1")?,
|
||||||
let b: B = state.eval(source.next(), ||"bsp/s: expected content 2")?;
|
state.eval(iter.next(), ||"bsp/s: expected content 2")?,
|
||||||
Self::s(a, b)
|
),
|
||||||
},
|
Some(Value::Key("bsp/e")) => Self::e(
|
||||||
Some(Value::Key("bsp/e")) => {
|
state.eval(iter.next(), ||"bsp/e: expected content 1")?,
|
||||||
let a: A = state.eval(source.next(), ||"bsp/e: expected content 1")?;
|
state.eval(iter.next(), ||"bsp/e: expected content 2")?,
|
||||||
let b: B = state.eval(source.next(), ||"bsp/e: expected content 2")?;
|
),
|
||||||
Self::e(a, b)
|
Some(Value::Key("bsp/w")) => Self::w(
|
||||||
},
|
state.eval(iter.next(), ||"bsp/w: expected content 1")?,
|
||||||
Some(Value::Key("bsp/w")) => {
|
state.eval(iter.next(), ||"bsp/w: expected content 2")?,
|
||||||
let a: A = state.eval(source.next(), ||"bsp/w: expected content 1")?;
|
),
|
||||||
let b: B = state.eval(source.next(), ||"bsp/w: expected content 2")?;
|
Some(Value::Key("bsp/a")) => Self::a(
|
||||||
Self::w(a, b)
|
state.eval(iter.next(), ||"bsp/a: expected content 1")?,
|
||||||
},
|
state.eval(iter.next(), ||"bsp/a: expected content 2")?,
|
||||||
Some(Value::Key("bsp/a")) => {
|
),
|
||||||
let a: A = state.eval(source.next(), ||"bsp/a: expected content 1")?;
|
Some(Value::Key("bsp/b")) => Self::b(
|
||||||
let b: B = state.eval(source.next(), ||"bsp/a: expected content 2")?;
|
state.eval(iter.next(), ||"bsp/b: expected content 1")?,
|
||||||
Self::a(a, b)
|
state.eval(iter.next(), ||"bsp/b: expected content 2")?,
|
||||||
},
|
),
|
||||||
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 {
|
} else {
|
||||||
|
|
@ -73,53 +69,32 @@ where S: Eval<Option<Ast>, A> + Eval<Option<Ast>, B> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
|
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
|
||||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
fn try_provide (state: S, source: impl Ast) -> Perhaps<Self> {
|
||||||
Ok(Some(if let Ast::Exp(source) = source {
|
Ok(Some(if let Ast::Exp(source) = source {
|
||||||
match source.next() {
|
let mut iter = source.clone();
|
||||||
Some(Value::Key("align/c")) => {
|
match iter.next() {
|
||||||
let content: A = state.eval(source.next(), ||"align/c: expected content")?;
|
Some(Value::Key("align/c")) =>
|
||||||
Self::c(content)
|
Self::c(state.eval(iter.next(), ||"align/c: expected content")?),
|
||||||
},
|
Some(Value::Key("align/x")) =>
|
||||||
Some(Value::Key("align/x")) => {
|
Self::x(state.eval(iter.next(), ||"align/x: expected content")?),
|
||||||
let content: A = state.eval(source.next(), ||"align/x: expected content")?;
|
Some(Value::Key("align/y")) =>
|
||||||
Self::x(content)
|
Self::y(state.eval(iter.next(), ||"align/y: expected content")?),
|
||||||
},
|
Some(Value::Key("align/n")) =>
|
||||||
Some(Value::Key("align/y")) => {
|
Self::n(state.eval(iter.next(), ||"align/n: expected content")?),
|
||||||
let content: A = state.eval(source.next(), ||"align/y: expected content")?;
|
Some(Value::Key("align/s")) =>
|
||||||
Self::y(content)
|
Self::s(state.eval(iter.next(), ||"align/s: expected content")?),
|
||||||
},
|
Some(Value::Key("align/e")) =>
|
||||||
Some(Value::Key("align/n")) => {
|
Self::e(state.eval(iter.next(), ||"align/e: expected content")?),
|
||||||
let content: A = state.eval(source.next(), ||"align/n: expected content")?;
|
Some(Value::Key("align/w")) =>
|
||||||
Self::n(content)
|
Self::w(state.eval(iter.next(), ||"align/w: expected content")?),
|
||||||
},
|
Some(Value::Key("align/nw")) =>
|
||||||
Some(Value::Key("align/s")) => {
|
Self::nw(state.eval(iter.next(), ||"align/nw: expected content")?),
|
||||||
let content: A = state.eval(source.next(), ||"align/s: expected content")?;
|
Some(Value::Key("align/ne")) =>
|
||||||
Self::s(content)
|
Self::ne(state.eval(iter.next(), ||"align/ne: expected content")?),
|
||||||
},
|
Some(Value::Key("align/sw")) =>
|
||||||
Some(Value::Key("align/e")) => {
|
Self::sw(state.eval(iter.next(), ||"align/sw: expected content")?),
|
||||||
let content: A = state.eval(source.next(), ||"align/e: expected content")?;
|
Some(Value::Key("align/se")) =>
|
||||||
Self::e(content)
|
Self::se(state.eval(iter.next(), ||"align/se: expected content")?),
|
||||||
},
|
|
||||||
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 {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue