mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
fixed up some parsing and removed some edn mentions
This commit is contained in:
parent
5e7b867aba
commit
452bdf9598
15 changed files with 290 additions and 292 deletions
163
edn/src/token.rs
163
edn/src/token.rs
|
|
@ -30,17 +30,17 @@ macro_rules! iterate {
|
|||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone, PartialEq)] pub struct TokenIterator<'a>(&'a str);
|
||||
impl<'a> Iterator for TokenIterator<'a> {
|
||||
#[derive(Clone, PartialEq)] pub struct TokensIterator<'a>(&'a str);
|
||||
impl<'a> Iterator for TokensIterator<'a> {
|
||||
type Item = TokenResult<'a>;
|
||||
fn next (&mut self) -> Option<TokenResult<'a>> { self.next_mut().map(|(result, _)|result) }
|
||||
}
|
||||
impl<'a> ConstIntoIter for TokenIterator<'a> {
|
||||
impl<'a> ConstIntoIter for TokensIterator<'a> {
|
||||
type Kind = IsIteratorKind;
|
||||
type Item = Token<'a>;
|
||||
type IntoIter = Self;
|
||||
}
|
||||
impl<'a> TokenIterator<'a> {
|
||||
impl<'a> TokensIterator<'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 (mut self) -> Option<(TokenResult<'a>, Self)> {
|
||||
|
|
@ -124,17 +124,19 @@ impl<'a> Token<'a> {
|
|||
d => Ok(Self { length: self.length + 1, depth: d - 1, ..self })
|
||||
}
|
||||
}
|
||||
pub const fn to_ref_atom (&'a self) -> Result<RefAtom, ParseError> {
|
||||
Ok(match self.kind {
|
||||
Nil => return Err(ParseError::Empty),
|
||||
Num => match to_number(self.slice()) {
|
||||
Ok(n) => RefAtom::Num(n),
|
||||
Err(e) => return Err(e)
|
||||
pub const fn to_ref_atom (&self, source: &'a str) -> Result<RefAtom<'a>, ParseError> {
|
||||
match self.kind {
|
||||
Nil => Err(ParseError::Empty),
|
||||
Num => match to_number(self.slice_source(source)) {
|
||||
Err(e) => Err(e),
|
||||
Ok(n) => Ok(RefAtom::Num(n)),
|
||||
},
|
||||
Sym => RefAtom::Sym(self.slice()),
|
||||
Key => RefAtom::Key(self.slice()),
|
||||
Exp => todo!()
|
||||
})
|
||||
Sym => Ok(RefAtom::Sym(self.slice_source(source))),
|
||||
Key => Ok(RefAtom::Key(self.slice_source(source))),
|
||||
Exp => Ok(
|
||||
RefAtom::Exp(RefAtomsIterator(TokensIterator::new(self.slice_source(source))))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
const fn to_number (digits: &str) -> Result<usize, ParseError> {
|
||||
|
|
@ -155,32 +157,21 @@ const fn to_digit (c: char) -> Result<usize, ParseError> {
|
|||
pub trait Atom: Sized {
|
||||
fn kind (&self) -> TokenKind;
|
||||
fn text (&self) -> &str;
|
||||
fn num (&self) -> usize;
|
||||
}
|
||||
#[derive(Clone, PartialEq)] pub enum RefAtom<'a> {
|
||||
Num(usize),
|
||||
Sym(&'a str),
|
||||
Key(&'a str),
|
||||
Exp(RefAtomIterator<'a>),
|
||||
Exp(RefAtomsIterator<'a>),
|
||||
}
|
||||
type RefAtomResult<'a> = Result<RefAtom<'a>, ParseError>;
|
||||
#[derive(Clone, PartialEq)] pub struct RefAtomIterator<'a>(TokenIterator<'a>);
|
||||
impl<'a> Iterator for RefAtomIterator<'a> {
|
||||
#[derive(Clone, PartialEq)] pub struct RefAtomsIterator<'a>(TokensIterator<'a>);
|
||||
impl<'a> Iterator for RefAtomsIterator<'a> {
|
||||
type Item = RefAtomResult<'a>;
|
||||
fn next (&mut self) -> Option<RefAtomResult<'a>> {
|
||||
Some(if let Some(result) = Iterator::next(&mut self.0) {
|
||||
match result {
|
||||
Err(e) => Err(e),
|
||||
Ok(token) => match token.kind {
|
||||
Nil => Err(ParseError::Empty),
|
||||
Num => match to_number(token.slice_source(self.0.0)) {
|
||||
Ok(n) => Ok(RefAtom::Num(n)),
|
||||
Err(e) => Err(e)
|
||||
},
|
||||
Sym => Ok(RefAtom::Sym(token.slice_source(self.0.0))),
|
||||
Key => Ok(RefAtom::Key(token.slice_source(self.0.0))),
|
||||
Exp => todo!()
|
||||
}
|
||||
}
|
||||
match result { Ok(token) => token.to_ref_atom(self.0.0), Err(e) => Err(e), }
|
||||
} else {
|
||||
return None
|
||||
})
|
||||
|
|
@ -193,20 +184,8 @@ impl<'a> RefAtom<'a> {
|
|||
pub fn read_all <'b: 'a> (source: &'b str)
|
||||
-> impl Iterator<Item = Result<RefAtom<'b>, ParseError>> + 'b
|
||||
{
|
||||
TokenIterator::new(source).map(move |result|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(RefAtomIterator(TokenIterator::new(token.slice_source(source))))
|
||||
)
|
||||
},
|
||||
TokensIterator::new(source).map(move |result|match result {
|
||||
Ok(token) => token.to_ref_atom(source), Err(e) => Err(e),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -226,6 +205,12 @@ impl<'a> Atom for RefAtom<'a> {
|
|||
Self::Key(k) => k,
|
||||
}
|
||||
}
|
||||
fn num (&self) -> usize {
|
||||
match self {
|
||||
Self::Num(n) => *n,
|
||||
_ => 0
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a> Debug for RefAtom<'a> {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
|
|
@ -245,7 +230,7 @@ impl<'a> Debug for RefAtom<'a> {
|
|||
}
|
||||
impl ArcAtom {
|
||||
pub fn read_all <'a> (source: &'a str) -> impl Iterator<Item = Result<ArcAtom, ParseError>> + 'a {
|
||||
TokenIterator::new(source).map(move |result: TokenResult<'a>|match result{
|
||||
TokensIterator::new(source).map(move |result: TokenResult<'a>|match result{
|
||||
Err(e) => Err(e),
|
||||
Ok(token) => match token.kind {
|
||||
Nil => Err(ParseError::Empty),
|
||||
|
|
@ -283,6 +268,12 @@ impl<'a> Atom for ArcAtom {
|
|||
Self::Key(k) => k.as_ref(),
|
||||
}
|
||||
}
|
||||
fn num (&self) -> usize {
|
||||
match self {
|
||||
Self::Num(n) => *n,
|
||||
_ => 0
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a> Debug for ArcAtom {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
|
|
@ -306,27 +297,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, _edn: &'a impl Atom) -> Option<U> {
|
||||
fn get (&'a self, _atom: &'a impl Atom) -> Option<U> {
|
||||
None
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom) -> U {
|
||||
self.get(edn).expect("no value")
|
||||
fn get_or_fail (&'a self, atom: &'a 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, edn: &'a impl Atom) -> Option<U> {
|
||||
(*self).get(edn)
|
||||
fn get (&'a self, atom: &'a impl Atom) -> Option<U> {
|
||||
(*self).get(atom)
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom) -> U {
|
||||
(*self).get_or_fail(edn)
|
||||
fn get_or_fail (&'a self, atom: &'a 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, edn: &'a impl Atom) -> Option<U> {
|
||||
self.as_ref().map(|s|s.get(edn)).flatten()
|
||||
fn get (&'a self, atom: &'a impl Atom) -> Option<U> {
|
||||
self.as_ref().map(|s|s.get(atom)).flatten()
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a impl Atom) -> U {
|
||||
self.as_ref().map(|s|s.get_or_fail(edn)).expect("no provider")
|
||||
fn get_or_fail (&'a self, atom: &'a impl Atom) -> U {
|
||||
self.as_ref().map(|s|s.get_or_fail(atom)).expect("no provider")
|
||||
}
|
||||
}
|
||||
/// Implement `Context` for a context and type.
|
||||
|
|
@ -334,18 +325,22 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
// 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) -> Option<$type> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* _ => return None })
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::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) -> Option<$type> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* _ => return None })
|
||||
fn get (&$lt $self, atom: &$lt impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -357,18 +352,24 @@ 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 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) -> Option<$type> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* Num(n) => n as $type, _ => return None })
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
(TokenKind::Num, _) => atom.num() 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) -> Option<$type> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* Num(n) => n as $type, _ => return None })
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<$type> {
|
||||
Some(match (atom.kind(), atom.text()) {
|
||||
$((TokenKind::Sym, $pat) => $expr,)*
|
||||
(TokenKind::Num, _) => atom.num() as $type,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -379,27 +380,35 @@ impl<'a, T: Context<'a, U>, U> Context<'a, U> for Option<T> {
|
|||
#[macro_export] macro_rules! 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) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(RefAtom::Sym($pat) => $expr),*, _ => return None })
|
||||
Some(match atom.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) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
use RefAtom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr),*, _ => return None })
|
||||
Some(match atom.to_ref() { $(Sym($pat) => $expr),*, _ => return None })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub trait TryFromEdn<'a, T>: Sized {
|
||||
fn try_from_edn (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) ->
|
||||
#[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_atom($s, head, tail) {
|
||||
return Some(value.boxed())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub trait TryFromAtom<'a, T>: Sized {
|
||||
fn try_from_atom (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) ->
|
||||
Option<Self>;
|
||||
}
|
||||
|
||||
pub trait TryIntoEdn<'a, T>: Sized {
|
||||
fn try_from_edn (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) ->
|
||||
pub trait TryIntoAtom<'a, T>: Sized {
|
||||
fn try_from_atom (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) ->
|
||||
Option<Self>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue