wip: ast/cst
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-25 22:48:29 +03:00
parent 31e84bf5b3
commit f1b24d436a
20 changed files with 1081 additions and 1103 deletions

70
dsl/src/dsl_ast.rs Normal file
View file

@ -0,0 +1,70 @@
use crate::*;
use std::sync::Arc;
use std::fmt::{Debug, Display, Formatter};
/// Emits tokens.
pub trait Ast: Debug {
fn peek (&self) -> Option<AstValue>;
fn next (&mut self) -> Option<AstValue>;
fn rest (self) -> Option<Box<dyn Ast>>;
}
/// A [Cst] can be used as an [Ast].
impl<'source: 'static> Ast for Cst<'source> {
fn peek (&self) -> Option<AstValue> {
Cst::peek(self).map(|token|token.value.into())
}
fn next (&mut self) -> Option<AstValue> {
Iterator::next(self).map(|token|token.value.into())
}
fn rest (self) -> Option<Box<dyn Ast>> {
self.peek().is_some().then(||Box::new(self) as Box<dyn Ast>)
}
}
#[derive(Clone, Default, Debug)]
pub enum AstValue {
#[default] Nil,
Err(DslError),
Num(usize),
Sym(Arc<str>),
Key(Arc<str>),
Str(Arc<str>),
Exp(Arc<Box<dyn Ast>>),
}
impl std::fmt::Display for AstValue {
fn fmt (&self, out: &mut Formatter) -> Result<(), std::fmt::Error> {
use AstValue::*;
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 AstValue {
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 Ast>> for Cst<'source> {
fn into (self) -> Box<dyn Ast> {
Box::new(self)
}
}

291
dsl/src/dsl_cst.rs Normal file
View file

@ -0,0 +1,291 @@
use crate::*;
/// Provides a native [Iterator] API over [CstConstIter],
/// emitting [CstToken] items.
///
/// [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.
/// [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 start: usize,
pub length: usize,
pub value: CstValue<'source>,
}
/// The meaning of a CST token. Strip the source from this to get an [AstValue].
#[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> {
pub const fn new (source: &'a str) -> Self {
Self(CstConstIter::new(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:?}"),
})
}
}

64
dsl/src/dsl_domain.rs Normal file
View file

@ -0,0 +1,64 @@
use crate::*;
pub trait Eval<Input, Output> {
fn eval (&self, input: Input) -> Perhaps<Output>;
}
/// May construct [Self] from token stream.
pub trait Dsl: Sized {
type State;
fn try_provide (state: Self::State, source: impl Ast) -> Perhaps<Self>;
fn provide <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
state: Self::State, source: impl Ast, error: F
) -> Usually<Self> {
let next = source.peek().clone();
if let Some(value) = Self::try_provide(state, source)? {
Ok(value)
} else {
Result::Err(format!("dsl: {}: {next:?}", error().into()).into())
}
}
}
//pub trait Give<'state, 'source, Type> {
///// Implement this to be able to [Give] [Type] from the [Cst].
///// Advance the stream if returning `Ok<Some<Type>>`.
//fn give (&'state self, words: Cst<'source>) -> Perhaps<Type>;
///// Return custom error on [None].
//fn give_or_fail <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
//&'state self, mut words: Cst<'source>, error: F
//) -> Usually<Type> {
//let next = words.peek().map(|x|x.value).clone();
//if let Some(value) = Give::<Type>::give(self, words)? {
//Ok(value)
//} else {
//Result::Err(format!("give: {}: {next:?}", error().into()).into())
//}
//}
//}
//#[macro_export] macro_rules! give {
//($Type:ty|$state:ident:$State:ident,$words:ident|$expr:expr) => {
//impl Give<$Type> for $State {
//fn give (&self, mut $words: Cst) -> Perhaps<$Type> {
//let $state = self;
//$expr
//}
//}
//};
//($Type:path$(,$Arg:ident)*|$state:ident,$words:ident|$expr:expr) => {
//impl<State: $(Give<$Arg>)++ $(, $Arg)*> Give<$Type> for State {
//fn give (&self, mut $words: Cst) -> Perhaps<$Type> {
//let $state = self;
//$expr
//}
//}
//}
//}
/////// Implement the [Give] trait, which boils down to
/////// specifying two types and providing an expression.
//#[macro_export] macro_rules! from_dsl {
//($Type:ty: |$state:ident:$State:ty, $words:ident|$expr:expr) => {
//give! { $Type|$state:$State,$words|$expr }
//};
//}

View file

@ -1,173 +0,0 @@
use crate::*;
/// 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;
}
}
}
/// Provides a native [Iterator] API over the [ConstIntoIter] [SourceIter]
/// [TokenIter::next] returns just the [Token] and mutates `self`,
/// instead of returning an updated version of the struct as [SourceIter::next] does.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct TokenIter<'a>(
pub SourceIter<'a>
);
impl<'a> TokenIter<'a> {
pub const fn new (source: &'a str) -> Self {
Self(SourceIter::new(source))
}
pub const fn peek (&self) -> Option<Token<'a>> {
self.0.peek()
}
}
impl<'a> Iterator for TokenIter<'a> {
type Item = Token<'a>;
fn next (&mut self) -> Option<Token<'a>> {
self.0.next().map(|(item, rest)|{self.0 = rest; item})
}
}
impl<'a> From<&'a str> for TokenIter<'a> {
fn from (source: &'a str) -> Self{
Self(SourceIter(source))
}
}
impl<'a> From<SourceIter<'a>> for TokenIter<'a> {
fn from (source: SourceIter<'a>) -> Self{
Self(source)
}
}
/// Owns a reference to the source text.
/// [SourceIter::next] emits subsequent pairs of:
/// * a [Token] and
/// * the source text remaining
/// * [ ] TODO: maybe [SourceIter::next] should wrap the remaining source in `Self` ?
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct SourceIter<'a>(pub &'a str);
const_iter!(<'a>|self: SourceIter<'a>| => Token<'a> => self.next_mut().map(|(result, _)|result));
impl<'a> From<&'a str> for SourceIter<'a> {
fn from (source: &'a str) -> Self{
Self::new(source)
}
}
impl<'a> SourceIter<'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<(Token<'a>, Self)> {
Self::next_mut(&mut self)
}
pub const fn peek (&self) -> Option<Token<'a>> {
peek_src(self.0)
}
pub const fn next_mut (&mut self) -> Option<(Token<'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<Token<'a>> {
let mut token: Token<'a> = Token::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(),
'(' =>
Token::new(source, start, 1, Exp(1, TokenIter::new(str_range(source, start, start + 1)))),
'"' =>
Token::new(source, start, 1, Str(str_range(source, start, start + 1))),
':'|'@' =>
Token::new(source, start, 1, Sym(str_range(source, start, start + 1))),
'/'|'a'..='z' =>
Token::new(source, start, 1, Key(str_range(source, start, start + 1))),
'0'..='9' =>
Token::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),
}
}
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))
})
}

View file

@ -1,147 +0,0 @@
use crate::*;
///// Implement the [Give] trait, which boils down to
///// specifying two types and providing an expression.
#[macro_export] macro_rules! from_dsl {
($Type:ty: |$state:ident:$State:ty, $words:ident|$expr:expr) => {
take! { $Type|$state:$State,$words|$expr }
};
}
/// [Take]s instances of [Type] given [TokenIter].
pub trait Give<'state, Type> {
/// Implement this to be able to [Give] [Type] from the [TokenIter].
/// Advance the stream if returning `Ok<Some<Type>>`.
fn give <'source: 'state> (&self, words: TokenIter<'source>) -> Perhaps<Type>;
/// Return custom error on [None].
fn give_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
&self, mut words: TokenIter<'source>, error: F
) -> Usually<Type> {
let next = words.peek().map(|x|x.value).clone();
if let Some(value) = Give::<Type>::give(self, words)? {
Ok(value)
} else {
Result::Err(format!("give: {}: {next:?}", error().into()).into())
}
}
}
#[macro_export] macro_rules! give {
() => {
impl<'state, Type: Take<'state, State>, State> Give<'state, Type> for State {
fn give <'source: 'state> (&self, mut words:TokenIter<'source>) -> Perhaps<Type> {
Type::take(self, words)
}
}
};
(box) => {
//impl<'state, T, Type: Take<'state, Box<T>>> Give<'state, Box<T>> for Box<Type> {
//fn give (&self, mut words:TokenIter<'source>) -> Perhaps<Box<Type>> {
//Type::take(self, words)
//}
//}
};
($Type:ty: $State:ty) => {
impl<'state, $Type: Take<'state, $State>> Give<'state, $Type> for $State {
fn give <'source: 'state> (&self, mut words:TokenIter<'source>) -> Perhaps<$Type> {
$Type::take(self, words)
}
}
};
($Type:ty|$state:ident:$State:ident,$words:ident|$expr:expr) => {
impl<'state> Give<'state, $Type> for $State {
fn give <'source: 'state> (&self, mut $words:TokenIter<'source>) -> Perhaps<$Type> {
let $state = self;
$expr
}
}
};
($Type:path$(,$Arg:ident)*|$state:ident,$words:ident|$expr:expr) => {
impl<'state, State: $(Give<'state, $Arg> +)* 'state $(, $Arg)*> Give<'state, $Type> for State {
fn give <'source: 'state> (&self, mut $words:TokenIter<'source>) -> Perhaps<$Type> {
let $state = self;
$expr
}
}
}
}
/// [Give]s instances of [Self] given [TokenIter].
pub trait Take<'state, State>: Sized {
/// Implement this to be able to [Take] [Self] from the [TokenIter].
/// Advance the stream if returning `Ok<Some<Self>>`.
fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self>;
/// Return custom error on [None].
fn take_or_fail <'source: 'state, E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
state: &State, mut words:TokenIter<'source>, error: F
) -> Usually<Self> {
let next = words.peek().map(|x|x.value).clone();
if let Some(value) = Take::<State>::take(state, words)? {
Ok(value)
} else {
Result::Err(format!("take: {}: {next:?}", error().into()).into())
}
}
}
#[macro_export] macro_rules! take {
() => {
impl<'state, Type: 'state, State: Give<'state, Type> + 'state> Take<'state, State> for Type {
fn take <'source: 'state> (state: &State, mut words:TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
(box) => {
impl<'state, T, State: Give<'state, Box<T>> + 'state> Take<'state, State> for Box<T> {
fn take <'source: 'state> (state: &State, mut words:TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
($Type:ty:$State:ty) => {
impl<'state> Take<'state, $State> for $Type {
fn take <'source: 'state> (state: &$State, mut words:TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
($Type:path$(,$Arg:ident)*|$state:ident,$words:ident|$expr:expr) => {
impl<'state,
State: Give<'state, bool> + $(Give<'state, $Arg> + )* 'state
$(, $Arg: Take<'state, State> + 'state)*
> Take<'state, State> for $Type {
fn take <'source: 'state> ($state: &State, mut $words:TokenIter<'source>) -> Perhaps<Self> {
$expr
}
}
};
($Type:path$(,$Arg:ident)*|$state:ident:$State:path,$words:ident|$expr:expr) => {
impl<'state $(, $Arg: 'state)*> Take<'state, $State> for $Type {
fn take <'source: 'state> ($state: &$State, mut $words:TokenIter<'source>) -> Perhaps<Self> {
$expr
}
}
};
}
// auto impl graveyard:
//impl<'state, State: Give<Type>, Type: 'state> Take<'state, State> for Type {
//fn take <'state> (state: &State, mut words:TokenIter<'source>)
//-> Perhaps<Self>
//{
//state.take(words)
//}
//}
//#[cfg(feature="dsl")]
//impl<'state, E: 'state, State: Give<'state, Box<dyn Render<E> + 'state>>>
//Take<'state, State> for Box<dyn Render<E> + 'state> {
//fn take <'source: 'state> (state: &State, words: TokenIter<'source>) -> Perhaps<Self> {
//state.give(words)
//}
//}
impl<'state, Type: Take<'state, State>, State> Give<'state, Type> for State {
fn give <'source: 'state> (&self, words: TokenIter<'source>) -> Perhaps<Type> {
Type::take(self, words)
}
}

View file

@ -1,110 +0,0 @@
use crate::*;
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct Token<'source> {
pub source: &'source str,
pub start: usize,
pub length: usize,
pub value: Value<'source>,
}
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum Value<'source> {
#[default] Nil,
Err(DslError),
Num(usize),
Sym(&'source str),
Key(&'source str),
Str(&'source str),
Exp(usize, TokenIter<'source>),
}
impl<'source> std::fmt::Display for Value<'source> {
fn fmt (&self, out: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
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> Token<'source> {
pub const fn new (
source: &'source str, start: usize, length: usize, value: Value<'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: Value<'source>) -> Self {
Self { value, ..self }
}
pub const fn value (&self) -> Value {
self.value
}
pub const fn error (self, error: DslError) -> Self {
Self { value: Value::Err(error), ..self }
}
pub const fn grow (self) -> Self {
Self { length: self.length.saturating_add(1), ..self }
}
pub const fn grow_num (self, m: usize, c: char) -> Self {
match to_digit(c) {
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 {
let token = self.grow();
token.with_value(Key(token.slice_source(self.source)))
}
pub const fn grow_sym (self) -> Self {
let token = self.grow();
token.with_value(Sym(token.slice_source(self.source)))
}
pub const fn grow_str (self) -> Self {
let token = self.grow();
token.with_value(Str(token.slice_source(self.source)))
}
pub const fn grow_exp (self) -> Self {
let token = self.grow();
if let Exp(depth, _) = token.value {
token.with_value(Exp(depth, TokenIter::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!()
}
}
}

View file

@ -42,13 +42,12 @@ pub(crate) use std::fmt::Debug;
pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
pub(crate) use konst::string::{split_at, str_range, char_indices};
pub(crate) use thiserror::Error;
pub(crate) use self::Value::*;
pub(crate) use self::DslError::*;
mod dsl_ast; pub use self::dsl_ast::*;
mod dsl_cst; pub use self::dsl_cst::*;
mod dsl_domain; pub use self::dsl_domain::*;
mod dsl_error; pub use self::dsl_error::*;
mod dsl_token; pub use self::dsl_token::*;
mod dsl_parse; pub use self::dsl_parse::*;
mod dsl_provide; pub use self::dsl_provide::*;
#[cfg(test)] mod test_token_iter {
use crate::*;