implement TokensIterator::peek

This commit is contained in:
🪞👃🪞 2025-01-18 03:47:18 +01:00
parent a949117017
commit 92fcb0af8f
7 changed files with 196 additions and 198 deletions

View file

@ -5,7 +5,6 @@ use konst::string::{split_at, str_range, char_indices};
use itertools::join;
use self::ParseError::*;
use self::TokenKind::*;
type TokenResult<'a> = Result<Token<'a>, ParseError>;
#[derive(Debug)] pub enum ParseError {
Unimplemented, Empty, Incomplete, Unexpected(char), Code(u8),
}
@ -31,6 +30,7 @@ macro_rules! iterate {
}
}
#[derive(Copy, Clone, PartialEq)] pub struct TokensIterator<'a>(&'a str);
type TokenResult<'a> = Result<Token<'a>, ParseError>;
impl<'a> Iterator for TokensIterator<'a> {
type Item = TokenResult<'a>;
fn next (&mut self) -> Option<TokenResult<'a>> { self.next_mut().map(|(result, _)|result) }
@ -47,6 +47,13 @@ impl<'a> TokensIterator<'a> {
Self::next_mut(&mut self)
}
pub const fn next_mut (&mut self) -> Option<(TokenResult<'a>, Self)> {
match self.peek() {
Some(Ok(token)) => Some((Ok(token), self.split(token.end()))),
Some(Err((l, e))) => Some((Err(e), self.split(l))),
None => None
}
}
pub const fn peek (&self) -> Option<Result<Token<'a>, (usize, ParseError)>> {
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 {
@ -55,36 +62,39 @@ impl<'a> TokensIterator<'a> {
'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())))
_ => return Some(Err((token.end(), Unexpected(c))))
},
Num => match c {
'0'..='9' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some((Ok(token), self.split(token.end()))),
_ => return Some((Err(Unexpected(c)), self.split(token.end())))
' '|'\n'|'\r'|'\t' => return Some(Ok(token)),
_ => return Some(Err((token.end(), Unexpected(c))))
},
Sym => match c {
'a'..='z'|'0'..='9'|'-' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some((Ok(token), self.split(token.end()))),
_ => return Some((Err(Unexpected(c)), self.split(token.end())))
' '|'\n'|'\r'|'\t' => return Some(Ok(token)),
_ => return Some(Err((token.end(), Unexpected(c))))
},
Key => match c {
'a'..='z'|'0'..='9'|'-'|'/' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some((Ok(token), self.split(token.end()))),
_ => return Some((Err(Unexpected(c)), self.split(token.end())))
' '|'\n'|'\r'|'\t' => return Some(Ok(token)),
_ => return Some(Err((token.end(), Unexpected(c))))
},
Exp => match token.depth {
0 => return Some((Ok(token), Self(split_at(self.0, token.end()).1))),
0 => return Some(Ok(token)),
_ => match c {
')' => match token.grow_out() {
Ok(token) => token,
Err(e) => return Some((Err(e), self.split(token.end())))
Err(e) => return Some(Err((token.end(), e)))
},
'(' => token.grow_in(),
_ => token.grow(),
}
},
});
match token.kind() { Nil => None, _ => Some((Err(ParseError::Incomplete), self.split(token.end()))) }
match token.kind() {
Nil => None,
_ => Some(Err((token.end(), ParseError::Incomplete)))
}
}
}
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum TokenKind {
@ -154,7 +164,7 @@ const fn to_digit (c: char) -> Result<usize, ParseError> {
_ => return Err(Unexpected(c))
})
}
pub trait Atom: Sized {
pub trait Atom: Sized + Send + Sync {
fn kind (&self) -> TokenKind;
fn text (&self) -> &str;
fn num (&self) -> usize;
@ -296,27 +306,27 @@ impl<'a> Display for ArcAtom {
}
}
/// Map EDN tokens to parameters of a given type for a given context
pub trait Context<'a, U>: Sized {
fn get (&'a self, _atom: &'a impl Atom) -> Option<U> {
pub trait Context<U>: Sized {
fn get (&self, _atom: &impl Atom) -> Option<U> {
None
}
fn get_or_fail (&'a self, atom: &'a impl Atom) -> U {
fn get_or_fail (&self, atom: &impl Atom) -> U {
self.get(atom).expect("no value")
}
}
impl<'a, T: Context<'a, U>, U> Context<'a, U> for &T {
fn get (&'a self, atom: &'a impl Atom) -> Option<U> {
impl<T: Context<U>, U> Context<U> for &T {
fn get (&self, atom: &impl Atom) -> Option<U> {
(*self).get(atom)
}
fn get_or_fail (&'a self, atom: &'a impl Atom) -> U {
fn get_or_fail (&self, atom: &impl Atom) -> U {
(*self).get_or_fail(atom)
}
}
impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
fn get (&'a self, atom: &'a impl Atom) -> Option<U> {
impl<T: Context<U>, U> Context<U> for Option<T> {
fn get (&self, atom: &impl Atom) -> Option<U> {
self.as_ref().map(|s|s.get(atom)).flatten()
}
fn get_or_fail (&'a self, atom: &'a impl Atom) -> U {
fn get_or_fail (&self, atom: &impl Atom) -> U {
self.as_ref().map(|s|s.get_or_fail(atom)).expect("no provider")
}
}
@ -324,8 +334,8 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
#[macro_export] macro_rules! 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, atom: &'a impl Atom) -> Option<$type> {
impl Context<$type> for $State {
fn get (&$self, atom: &impl Atom) -> Option<$type> {
Some(match (atom.kind(), atom.text()) {
$((TokenKind::Sym, $pat) => $expr,)*
_ => return None
@ -336,7 +346,7 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
// 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, atom: &$lt impl Atom) -> Option<$type> {
fn get (&$lt $self, atom: &impl Atom) -> Option<$type> {
Some(match (atom.kind(), atom.text()) {
$((TokenKind::Sym, $pat) => $expr,)*
_ => return None
@ -351,8 +361,8 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
#[macro_export] macro_rules! 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, atom: &'a impl Atom) -> Option<$type> {
impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, atom: &impl Atom) -> Option<$type> {
Some(match (atom.kind(), atom.text()) {
$((TokenKind::Sym, $pat) => $expr,)*
(TokenKind::Num, _) => atom.num() as $type,
@ -363,8 +373,8 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
};
// 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, atom: &'a impl Atom) -> Option<$type> {
impl Context<$type> for $State {
fn get (&$self, atom: &impl Atom) -> Option<$type> {
Some(match (atom.kind(), atom.text()) {
$((TokenKind::Sym, $pat) => $expr,)*
(TokenKind::Num, _) => atom.num() as $type,
@ -395,12 +405,3 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
}
}
}
#[macro_export] macro_rules! try_delegate {
($s:ident, $atom:expr, $T:ty) => {
if let [head, tail @ ..] = $atom.as_slice() {
if let Some(value) = <$T>::try_from_atoms($s, head, tail) {
return Some(value.boxed())
}
}
}
}