wip: EdnItem -> Atom, rewrite tokenizer

This commit is contained in:
🪞👃🪞 2025-01-17 21:47:34 +01:00
parent 143cd24e09
commit ff31957fed
39 changed files with 477 additions and 376 deletions

View file

@ -12,7 +12,7 @@ mod op_iter; pub use self::op_iter::*;
mod op_align; pub use self::op_align::*;
mod op_bsp; pub use self::op_bsp::*;
mod op_transform; pub use self::op_transform::*;
mod edn_view; pub use self::edn_view::*;
mod view; pub use self::view::*;
pub(crate) use std::marker::PhantomData;
pub(crate) use std::error::Error;
pub(crate) use ::tek_edn::*;

View file

@ -47,10 +47,10 @@ impl<E: Output, A: Content<E>> Content<E> for Align<E, A> {
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Align<E, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T,
head: &EdnItem<impl AsRef<str>>,
tail: &'a [EdnItem<impl AsRef<str>>]
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>]
) -> Option<Self> {
use EdnItem::*;
use Atom::*;
Some(match (head.to_ref(), tail) {
(Key("align/c"), [a]) => Self::c(state.get_content(a).expect("no content")),
(Key("align/x"), [a]) => Self::x(state.get_content(a).expect("no content")),

View file

@ -93,8 +93,8 @@ impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<E, A, B>
fn contents (&self) -> (&A, &B) { (&self.2, &self.3) }
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Bsp<E, RenderBox<'a, E>, RenderBox<'a, E>> {
fn try_from_edn (s: &'a T, head: &EdnItem<impl AsRef<str>>, tail: &'a [EdnItem<impl AsRef<str>>]) -> Option<Self> {
use EdnItem::*;
fn try_from_edn (s: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]) -> Option<Self> {
use Atom::*;
Some(match (head.to_ref(), tail) {
(Key("bsp/n"), [a, b]) => Self::n(
s.get_content(a).expect("no south"),

View file

@ -8,9 +8,9 @@ impl<E, A> When<E, A> {
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for When<E, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T, head: &EdnItem<impl AsRef<str>>, tail: &'a [EdnItem<impl AsRef<str>>]
state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]
) -> Option<Self> {
use EdnItem::*;
use Atom::*;
if let (Key("when"), [condition, content]) = (head.to_ref(), tail) {
Some(Self(
Default::default(),
@ -48,8 +48,8 @@ impl<E, A, B> Either<E, A, B> {
}
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Either<E, RenderBox<'a, E>, RenderBox<'a, E>> {
fn try_from_edn (state: &'a T, head: &EdnItem<impl AsRef<str>>, tail: &'a [EdnItem<impl AsRef<str>>]) -> Option<Self> {
use EdnItem::*;
fn try_from_edn (state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]) -> Option<Self> {
use Atom::*;
if let (Key("either"), [condition, content, alternative]) = (head.to_ref(), tail) {
Some(Self::new(
state.get_bool(condition).expect("either: no condition"),

View file

@ -28,9 +28,9 @@ macro_rules! transform_xy {
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T, head: &EdnItem<impl AsRef<str>>, tail: &'a [EdnItem<impl AsRef<str>>]
state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]
) -> Option<Self> {
use EdnItem::*;
use Atom::*;
Some(match (head.to_ref(), tail) {
(Key($x), [a]) => Self::x(state.get_content(a).expect("no content")),
(Key($y), [a]) => Self::y(state.get_content(a).expect("no content")),
@ -84,9 +84,9 @@ macro_rules! transform_xy_unit {
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T, head: &EdnItem<impl AsRef<str>>, tail: &'a [EdnItem<impl AsRef<str>>]
state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]
) -> Option<Self> {
use EdnItem::*;
use Atom::*;
Some(match (head.to_ref(), tail) {
(Key($x), [x, a]) => Self::x(
state.get_unit(x).expect("no x"),

View file

@ -1,6 +1,6 @@
use crate::*;
use std::{sync::Arc, marker::PhantomData};
use EdnItem::*;
use Atom::*;
/// Define an EDN-backed view.
///
/// This consists of:
@ -15,8 +15,8 @@ use EdnItem::*;
}
$(
impl<'a> EdnProvide<'a, $type> for $App {
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<$type> {
Some(match edn.to_ref() { $(EdnItem::Sym($sym) => $value,)* _ => return None })
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<$type> {
Some(match edn.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
}
}
)*
@ -26,9 +26,9 @@ use EdnItem::*;
#[macro_export] macro_rules! edn_provide_content {
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a, E: Output> EdnProvide<'a, Box<dyn Render<E> + 'a>> for $State {
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> {
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> {
Some(match edn.to_ref() {
$(EdnItem::Sym($pat) => $expr),*,
$(Atom::Sym($pat) => $expr),*,
_ => return None
})
}
@ -36,9 +36,9 @@ use EdnItem::*;
};
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> EdnProvide<'a, Box<dyn Render<$Output> + 'a>> for $State {
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<Box<dyn Render<$Output> + 'a>> {
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<$Output> + 'a>> {
Some(match edn.to_ref() {
$(EdnItem::Sym($pat) => $expr),*,
$(Atom::Sym($pat) => $expr),*,
_ => return None
})
}
@ -48,7 +48,7 @@ use EdnItem::*;
/// Renders from EDN source and context.
#[derive(Default)] pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
#[default] Inert,
Ok(T, EdnItem<Arc<str>>),
Ok(T, Atom<Arc<str>>),
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
Err(String),
_Unused(PhantomData<&'a E>),
@ -74,13 +74,13 @@ impl<'a, E: Output, T> EdnViewData<'a, E> for T where T:
{}
impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
pub fn from_source (state: T, source: &'a str) -> Self {
match EdnItem::read_one(&source) {
match Atom::read_one(&source) {
Ok((layout, _)) => Self::Ok(state, layout),
Err(error) => Self::Err(format!("{error} in {source}"))
}
}
pub fn from_items (state: T, items: Vec<EdnItem<impl AsRef<str>>>) -> Self {
Self::Ok(state, EdnItem::Exp(items).to_arc())
pub fn from_items (state: T, items: Vec<Atom<impl AsRef<str>>>) -> Self {
Self::Ok(state, Atom::Exp(items).to_arc())
}
}
impl<E: Output, T: for<'a>EdnViewData<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for EdnView<'_, E, T> {
@ -113,7 +113,7 @@ pub trait EdnViewData<'a, E: Output>:
EdnProvide<'a, E::Unit> +
EdnProvide<'a, Box<dyn Render<E> + 'a>>
{
fn get_bool (&'a self, item: &'a EdnItem<impl AsRef<str>>) -> Option<bool> {
fn get_bool (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<bool> {
Some(match &item {
Sym(s) => match s.as_ref() {
":false" | ":f" => false,
@ -125,13 +125,13 @@ pub trait EdnViewData<'a, E: Output>:
_ => return EdnProvide::get(self, item)
})
}
fn get_usize (&'a self, item: &'a EdnItem<impl AsRef<str>>) -> Option<usize> {
fn get_usize (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<usize> {
Some(match &item { Num(n) => *n, _ => return EdnProvide::get(self, item) })
}
fn get_unit (&'a self, item: &'a EdnItem<impl AsRef<str>>) -> Option<E::Unit> {
fn get_unit (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<E::Unit> {
Some(match &item { Num(n) => (*n as u16).into(), _ => return EdnProvide::get(self, item) })
}
fn get_content (&'a self, item: &'a EdnItem<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
fn get_content (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
Some(match item {
Nil => Box::new(()),
Exp(ref e) => {