once again, why did i begin to refactor this

This commit is contained in:
🪞👃🪞 2025-01-18 00:13:36 +01:00
parent 297f9b30df
commit 798de37172
15 changed files with 582 additions and 602 deletions

View file

@ -1,9 +1,14 @@
use crate::*;
//use std::iter::IntoIterator;
use konst::iter::{ConstIntoIter, IsIteratorKind};
use konst::string::{split_at, str_range, char_indices};
use itertools::join;
use self::ParseError::*;
use self::TokenKind::*;
#[derive(Debug)] pub enum ParseError { Unimplemented, Empty, Incomplete, Unexpected(char), Code(u8), }
type TokenResult<'a> = Result<Token<'a>, ParseError>;
#[derive(Debug)] pub enum ParseError {
Unimplemented, Empty, Incomplete, Unexpected(char), Code(u8),
}
impl std::fmt::Display for ParseError {
fn fmt (&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
@ -16,7 +21,6 @@ impl std::fmt::Display for ParseError {
}
}
impl std::error::Error for ParseError {}
/// Iterator helper macro because I can't find the canonical one in [konst] docs.
macro_rules! iterate {
($expr:expr => $arg: pat => $body:expr) => {
let mut iter = $expr;
@ -26,7 +30,11 @@ macro_rules! iterate {
}
}
}
pub struct TokenIterator<'a>(&'a str);
#[derive(Clone, PartialEq)] pub struct TokenIterator<'a>(&'a str);
impl<'a> Iterator for TokenIterator<'a> {
type Item = TokenResult<'a>;
fn next (&mut self) -> Option<TokenResult<'a>> { self.next_mut().map(|(result, _)|result) }
}
impl<'a> ConstIntoIter for TokenIterator<'a> {
type Kind = IsIteratorKind;
type Item = Token<'a>;
@ -34,16 +42,18 @@ impl<'a> ConstIntoIter for TokenIterator<'a> {
}
impl<'a> TokenIterator<'a> {
pub const fn new (source: &'a str) -> Self { Self(source) }
pub const fn split (self, index: usize) -> Self { Self(split_at(self.0, index).1) }
pub const fn next (self) -> Option<(Result<Token<'a>, ParseError>, Self)> {
let src = self.0;
let mut token: Token<'a> = Token::new(src, Nil, 0, 0, 0);
iterate!(char_indices(src) => (index, c) => token = match token.kind() {
pub const fn split (&self, index: usize) -> Self { Self(split_at(self.0, index).1) }
pub const fn next (mut self) -> Option<(TokenResult<'a>, Self)> {
Self::next_mut(&mut self)
}
pub const fn next_mut (&mut self) -> Option<(TokenResult<'a>, Self)> {
let mut token: Token<'a> = Token::new(self.0, Nil, 0, 0, 0);
iterate!(char_indices(self.0) => (index, c) => token = match token.kind() {
Nil => match c {
'(' => Token::new(src, Exp, index, 1, 1),
':'|'@' => Token::new(src, Sym, index, 1, 0),
'0'..='9' => Token::new(src, Num, index, 1, 0),
'/'|'a'..='z' => Token::new(src, Key, index, 1, 0),
'(' => Token::new(self.0, Exp, index, 1, 1),
':'|'@' => Token::new(self.0, Sym, index, 1, 0),
'0'..='9' => Token::new(self.0, Num, index, 1, 0),
'/'|'a'..='z' => Token::new(self.0, Key, index, 1, 0),
' '|'\n'|'\r'|'\t' => token.grow(),
_ => return Some((Err(Unexpected(c)), self.split(token.end())))
},
@ -63,7 +73,7 @@ impl<'a> TokenIterator<'a> {
_ => return Some((Err(Unexpected(c)), self.split(token.end())))
},
Exp => match token.depth {
0 => return Some((Ok(token), Self(split_at(src, token.end()).1))),
0 => return Some((Ok(token), Self(split_at(self.0, token.end()).1))),
_ => match c {
')' => match token.grow_out() {
Ok(token) => token,
@ -77,31 +87,10 @@ impl<'a> TokenIterator<'a> {
match token.kind() { Nil => None, _ => Some((Err(ParseError::Incomplete), self.split(token.end()))) }
}
}
pub struct AtomIterator<'a>(TokenIterator<'a>);
impl<'a> ConstIntoIter for AtomIterator<'a> {
type Kind = IsIteratorKind;
type Item = Atom<&'a str>;
type IntoIter = Self;
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum TokenKind {
#[default] Nil, Num, Sym, Key, Exp
}
impl<'a> AtomIterator<'a> {
pub const fn new (tokens: TokenIterator<'a>) -> Self { Self(tokens) }
pub const fn next (mut self) -> Option<(Result<Atom<&'a str>, ParseError>, Self)> {
match self.0.next() {
None => None,
Some((result, next)) => match result {
Err(e) => Some((Err(e), Self(next))),
Ok(token) => match token.to_ref_atom() {
Err(e) => Some((Err(e), Self(next))),
Ok(atom) => Some((Ok(atom), Self(next))),
}
}
}
}
}
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub enum TokenKind { #[default] Nil, Num, Sym, Key, Exp }
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct Token<'a> {
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct Token<'a> {
source: &'a str,
kind: TokenKind,
start: usize,
@ -115,7 +104,13 @@ impl<'a> Token<'a> {
Self { source, kind, start, length, depth }
}
pub const fn end (&self) -> usize { self.start + self.length }
pub const fn slice (&self) -> &str { str_range(self.source, self.start, self.end()) }
pub const fn slice (&'a self) -> &'a str {
self.slice_source(self.source)
//str_range(self.source, self.start, self.end())
}
pub const fn slice_source <'b> (&'a self, source: &'b str) -> &'b str {
str_range(source, self.start, self.end())
}
pub const fn kind (&self) -> TokenKind { Nil }
pub const fn grow (self) -> Self {
Self { length: self.length + 1, ..self }
@ -129,27 +124,15 @@ impl<'a> Token<'a> {
d => Ok(Self { length: self.length + 1, depth: d - 1, ..self })
}
}
pub const fn to_ref_atom (&'a self) -> Result<Atom<&'a str>, ParseError> {
pub const fn to_ref_atom (&'a self) -> Result<RefAtom<'a>, ParseError> {
Ok(match self.kind {
Nil => return Err(ParseError::Empty),
Num => match to_number(self.slice()) {
Ok(n) => Atom::Num(n),
Ok(n) => RefAtom::Num(n),
Err(e) => return Err(e)
},
Sym => Atom::Sym(self.slice()),
Key => Atom::Key(self.slice()),
Exp => todo!()
})
}
pub fn to_arc_atom (&self) -> Result<Atom<Arc<str>>, ParseError> {
Ok(match self.kind {
Nil => return Err(ParseError::Empty),
Num => match to_number(self.slice()) {
Ok(n) => Atom::Num(n),
Err(e) => return Err(e)
},
Sym => Atom::Sym(self.slice().into()),
Key => Atom::Key(self.slice().into()),
Sym => RefAtom::Sym(self.slice()),
Key => RefAtom::Key(self.slice()),
Exp => todo!()
})
}
@ -169,156 +152,225 @@ const fn to_digit (c: char) -> Result<usize, ParseError> {
_ => return Err(Unexpected(c))
})
}
#[derive(Clone, PartialEq)] pub enum Atom<T> { Num(usize), Sym(T), Key(T), Exp(Vec<Atom<T>>) }
impl<'a, T: 'a> Atom<T> {
pub fn transform <U: 'a, F: Fn(&'a T)->U + Clone> (&'a self, f: F) -> Atom<U> {
use Atom::*;
pub trait Atom<'a>: Sized {
fn kind (&self) -> TokenKind;
fn text (&self) -> &str;
fn read_all (source: &'a str) -> impl Iterator<Item = Result<Self, ParseError>>;
}
#[derive(Clone, PartialEq)] pub enum RefAtom<'a> {
Num(usize),
Sym(&'a str),
Key(&'a str),
Exp(TokenIterator<'a>),
}
impl<'a> RefAtom<'a> {
pub fn to_arc_atom (&self) -> ArcAtom {
todo!()
}
}
impl<'a> Atom<'a> for RefAtom<'a> {
fn kind (&self) -> TokenKind {
match self {
Num(n) => Num(*n),
Sym(t) => Sym(f(t)),
Key(t) => Key(f(t)),
Exp(e) => Exp(e.iter().map(|i|i.transform(f.clone())).collect())
Self::Num(_) => TokenKind::Num,
Self::Sym(_) => TokenKind::Sym,
Self::Key(_) => TokenKind::Key,
Self::Exp(_) => TokenKind::Exp,
}
}
}
impl<'a, T: AsRef<str>> Atom<T> {
pub fn to_ref (&'a self) -> Atom<&'a str> {
self.transform(|t|t.as_ref())
fn text (&self) -> &str {
match self {
Self::Num(_)|Self::Exp(_) => "",
Self::Sym(s) => s,
Self::Key(k) => k,
}
}
pub fn to_arc (&'a self) -> Atom<Arc<str>> {
self.transform(|t|t.as_ref().into())
fn read_all (source: &'a str) -> impl Iterator<Item = Result<Self, ParseError>> {
TokenIterator::new(source).map(move |result: TokenResult<'a>|match result{
Err(e) => Err(e),
Ok(token) => match token.kind {
Nil => Err(ParseError::Empty),
Num => match to_number(token.slice_source(source)) {
Ok(n) => Ok(RefAtom::Num(n)),
Err(e) => return Err(e)
},
Sym => Ok(RefAtom::Sym(token.slice_source(source))),
Key => Ok(RefAtom::Key(token.slice_source(source))),
Exp => Ok(RefAtom::Exp(TokenIterator::new(token.slice_source(source))))
},
})
}
}
impl<'a> Atom<&'a str> {
pub const fn read_all_ref (_: &'a str) -> Result<Vec<Self>, ParseError> {
Err(Unimplemented)
}
}
impl<T: Debug> Debug for Atom<T> {
impl<'a> Debug for RefAtom<'a> {
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
use Atom::*;
match self {
Num(u) => write!(f, "(num {u})"),
Sym(u) => write!(f, "(sym {u:?})"),
Key(u) => write!(f, "(key {u:?})"),
Exp(e) => write!(f, "(exp {})",
itertools::join(e.iter().map(|i|format!("{:?}", i)), ","))
RefAtom::Num(n) => write!(f, "(num {n})"),
RefAtom::Sym(t) => write!(f, "(sym {t:?})"),
RefAtom::Key(t) => write!(f, "(key {t:?})"),
RefAtom::Exp(u) => write!(f, "(exp {})", join(u.clone().map(|t|format!("{t:?}")), ","))
}
}
}
impl<T: Display> Display for Atom<T> {
#[derive(Clone, PartialEq)] pub enum ArcAtom {
Num(usize),
Sym(Arc<str>),
Key(Arc<str>),
Exp(Vec<ArcAtom>),
}
impl<'a> Atom<'a> for ArcAtom {
fn kind (&self) -> TokenKind {
match self {
Self::Num(_) => TokenKind::Num,
Self::Sym(_) => TokenKind::Sym,
Self::Key(_) => TokenKind::Key,
Self::Exp(_) => TokenKind::Exp,
}
}
fn text (&self) -> &str {
match self {
Self::Num(_)|Self::Exp(_) => "",
Self::Sym(s) => s.as_ref(),
Self::Key(k) => k.as_ref(),
}
}
fn read_all (source: &'a str) -> impl Iterator<Item = Result<Self, ParseError>> {
TokenIterator::new(source).map(move |result: TokenResult<'a>|match result{
Err(e) => Err(e),
Ok(token) => match token.kind {
Nil => Err(ParseError::Empty),
Num => match to_number(token.slice_source(source).into()) {
Ok(n) => Ok(ArcAtom::Num(n)),
Err(e) => return Err(e)
},
Sym => Ok(ArcAtom::Sym(token.slice_source(source).into())),
Key => Ok(ArcAtom::Key(token.slice_source(source).into())),
Exp => Ok(ArcAtom::Exp({
let mut iter = Self::read_all(token.slice_source(source));
let mut atoms = vec![];
while let Some(atom) = iter.next() {
atoms.push(atom?);
}
atoms
}))
},
})
}
}
impl<'a> Debug for ArcAtom {
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
use Atom::*;
use itertools::join;
match self {
Num(u) => write!(f, "{u}"),
Sym(u) => write!(f, "{u}"),
Key(u) => write!(f, "{u}"),
Exp(e) => write!(f, "({})", join(e.iter().map(|i|format!("{}", i)), " "))
Self::Num(n) => write!(f, "(num {n})"),
Self::Sym(t) => write!(f, "(sym {t:?})"),
Self::Key(t) => write!(f, "(key {t:?})"),
Self::Exp(u) => write!(f, "(exp {u:?})"),
}
}
}
//impl<'a> Token<'a> {
//pub const fn chomp_one (source: &'a str) -> Result<Token<'a>, ParseError> {
//match Self::chomp(source) {
//Ok((_, token)) => Ok(token),
//Err(e) => Err(e)
//}
//}
//pub const fn from_nil (c: char) -> Result<(&'a str, Token<'a>), ParseError> {
//match c {
//' '|'\n'|'\r'|'\t' => Nil,
//'(' => Exp(source, index, 1, 1),
//':'|'@' => Sym(source, index, 1),
//'0'..='9' => Num(source, index, 1),
//'a'..='z' => Key(source, index, 1),
//_ => return Err(Unexpected(c))
//}
//}
//pub const fn chomp (source: &'a str) -> Result<(&'a str, Token<'a>), ParseError> {
//let mut state = Self::Nil;
//let mut chars = char_indices(source);
//while let Some(((index, c), next)) = chars.next() {
//state = match state {
//// must begin expression
//Nil => Self::from_nil(c),
//Num(_, _, 0) => unreachable!(),
//Sym(_, _, 0) => unreachable!(),
//Key(_, _, 0) => unreachable!(),
//Num(src, idx, len) => match c {
//'0'..='9' => Num(src, idx, len + 1),
//' '|'\n'|'\r'|'\t' => return Ok((split(src, idx+len).1, Num(src, idx, len))),
//_ => return Err(Unexpected(c))
//},
//Sym(src, idx, len) => match c {
//'a'..='z'|'0'..='9'|'-' => Sym(src, idx, len + 1),
//' '|'\n'|'\r'|'\t' => return Ok((split(src, idx+len).1, Sym(src, idx, len))),
//_ => return Err(Unexpected(c))
//},
//Key(src, idx, len) => match c {
//'a'..='z'|'0'..='9'|'-'|'/' => Key(src, idx, len + 1),
//' '|'\n'|'\r'|'\t' => return Ok((split(src, idx+len).1, Key(src, idx, len))),
//_ => return Err(Unexpected(c))
//},
//Exp(src, idx, len, 0) => match c {
//' '|'\n'|'\r'|'\t' => return Ok((split(src, idx+len).1, Exp(src, idx, len, 0))),
//_ => return Err(Unexpected(c))
//},
//Exp(src, idx, len, balance) => match c {
//')' => Exp(src, idx, len + 1, balance - 1),
//'(' => Exp(src, idx, len + 1, balance + 1),
//_ => Exp(src, idx, len + 1, balance)
//},
//};
//chars = next
//}
//Ok(("", state))
//}
//pub fn src (&self) -> &str {
//match self {
//Self::Nil => "",
//Self::Num(src, _, _) => src,
//Self::Sym(src, _, _) => src,
//Self::Key(src, _, _) => src,
//Self::Exp(src, _, _, _) => src,
//}
//}
//pub fn str (&self) -> &str {
//match self {
//Self::Nil => "",
//Self::Num(src, start, len) => &src[*start..start+len],
//Self::Sym(src, start, len) => &src[*start..start+len],
//Self::Key(src, start, len) => &src[*start..start+len],
//Self::Exp(src, start, len, 0) => &src[*start..(start+len)],
//Self::Exp(src, start, len, d) => panic!(
//"unclosed delimiter with depth {d} in:\n{}",
//&src[*start..(start+len)]
//)
//}
//}
//pub fn to_atom_ref (&'a self) -> Result<Atom<&'a str>, ParseError> {
//use Atom::*;
//Ok(match self {
//Token::Nil => Nil,
//Token::Num(_, _, _) => Num(Token::number(self.str())),
//Token::Sym(_, _, _) => Sym(self.str().into()),
//Token::Key(_, _, _) => Key(self.str().into()),
//Token::Exp(_, _, _, _) => Exp(match Atom::read_all_ref(self.str()) {
//Ok(exp) => exp,
//Err(e) => return Err(e)
//}),
//})
//}
//}
impl<'a> Display for ArcAtom {
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
match self {
Self::Num(n) => write!(f, "{n}"),
Self::Sym(t) => write!(f, "{t}"),
Self::Key(t) => write!(f, "{t}"),
Self::Exp(u) => write!(f, "({})", join(u.iter().map(|i|format!("{}", &i)), " "))
}
}
}
/// Map EDN tokens to parameters of a given type for a given context
pub trait Context<'a, U>: Sized {
fn get (&'a self, _edn: &'a impl Atom<'a>) -> Option<U> {
None
}
fn get_or_fail (&'a self, edn: &'a impl Atom<'a>) -> U {
self.get(edn).expect("no value")
}
}
impl<'a, T: Context<'a, U>, U> Context<'a, U> for &T {
fn get (&'a self, edn: &'a impl Atom<'a>) -> Option<U> {
(*self).get(edn)
}
fn get_or_fail (&'a self, edn: &'a impl Atom<'a>) -> U {
(*self).get_or_fail(edn)
}
}
impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
fn get (&'a self, edn: &'a impl Atom<'a>) -> Option<U> {
self.as_ref().map(|s|s.get(edn)).flatten()
}
fn get_or_fail (&'a self, edn: &'a impl Atom<'a>) -> U {
self.as_ref().map(|s|s.get_or_fail(edn)).expect("no provider")
}
}
/// Implement `Context` for a context and type.
#[macro_export] macro_rules! edn_provide {
// Provide a value to the EDN template
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> Context<'a, $type> for $State {
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<$type> {
use RefAtom::*;
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
// Provide a value more generically
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$lt> Context<$lt, $type> for $State {
fn get (&$lt $self, edn: &$lt impl Atom<'a>) -> Option<$type> {
use RefAtom::*;
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
}
/// Implement `Context` for a context and numeric type.
///
/// This enables support for numeric literals.
#[macro_export] macro_rules! edn_provide_num {
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a, $T: $Trait> Context<'a, $type> for $T {
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<$type> {
use RefAtom::*;
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* Num(n) => n as $type, _ => return None })
}
}
};
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> Context<'a, $type> for $State {
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<$type> {
use RefAtom::*;
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* Num(n) => n as $type, _ => return None })
}
}
};
}
/// Implement `Context` for a context and content type.
///
/// This enables support for layout expressions.
#[macro_export] macro_rules! edn_provide_content {
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<Box<dyn Render<E> + 'a>> {
use RefAtom::*;
Some(match edn.to_ref() { $(RefAtom::Sym($pat) => $expr),*, _ => return None })
}
}
};
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<Box<dyn Render<$Output> + 'a>> {
use RefAtom::*;
Some(match edn.to_ref() { $(Sym($pat) => $expr),*, _ => return None })
}
}
}
}
pub trait TryFromEdn<'a, T>: Sized {
fn try_from_edn (state: &'a T, head: &impl Atom<'a>, tail: &'a [impl Atom<'a>]) ->
Option<Self>;
}
#[cfg(test)] #[test] fn test_edn_token () -> Result<(), Box<dyn std::error::Error>> {
use Token::*;
assert_eq!(Nil, Token::chomp_one("")?);
assert_eq!(Nil, Token::chomp_one(" \n \r \t ")?);
assert_eq!(Num("8", 0, 1), Token::chomp_one("8")?);
assert_eq!(Num(" 8 ", 3, 1), Token::chomp_one(" 8 ")?);
assert_eq!(Sym(":foo", 0, 4), Token::chomp_one(":foo")?);
assert_eq!(Sym("@bar", 0, 4), Token::chomp_one("@bar")?);
assert_eq!(Key("foo/bar", 0, 7), Token::chomp_one("foo/bar")?);
Ok(())
pub trait TryIntoEdn<'a, T>: Sized {
fn try_from_edn (state: &'a T, head: &impl Atom<'a>, tail: &'a [impl Atom<'a>]) ->
Option<Self>;
}