TokenIter -> SourceIter, reuse logic in operators, and now it renders correctly!

This commit is contained in:
🪞👃🪞 2025-01-19 01:46:06 +01:00
parent 266f59085e
commit b8726de78f
13 changed files with 203 additions and 154 deletions

View file

@ -1,24 +1,33 @@
use crate::*;
#[derive(Debug, Copy, Clone, Default)] pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A);
try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key(key), .. }, iter)) = iter.next() {
try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter|
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
match key {
"align/c" => return Some(Self::c(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/x" => return Some(Self::x(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/y" => return Some(Self::y(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/n" => return Some(Self::n(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/s" => return Some(Self::s(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/e" => return Some(Self::e(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/w" => return Some(Self::w(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/nw" => return Some(Self::nw(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/ne" => return Some(Self::ne(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/sw" => return Some(Self::sw(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/se" => return Some(Self::se(state.get_content(&iter.next()?.0.value).expect("no content"))),
"align/c"|"align/x"|"align/y"|
"align/n"|"align/s"|"align/e"|"align/w"|
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
let _ = iter.next().unwrap();
let c = iter.next().expect("no content specified");
let c = state.get_content(&c.value).expect("no content provided");
return Some(match key {
"align/c" => Self::c(c),
"align/x" => Self::x(c),
"align/y" => Self::y(c),
"align/n" => Self::n(c),
"align/s" => Self::s(c),
"align/e" => Self::e(c),
"align/w" => Self::w(c),
"align/nw" => Self::nw(c),
"align/ne" => Self::ne(c),
"align/sw" => Self::sw(c),
"align/se" => Self::se(c),
_ => unreachable!()
})
},
_ => return None
}
}
});
});
impl<A> Align<A> {
pub fn c (a: A) -> Self { Self(Alignment::Center, a) }
pub fn x (a: A) -> Self { Self(Alignment::X, a) }

View file

@ -32,33 +32,27 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
}
}
try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
Some(if let Some((Token { value: Value::Key("bsp/n"), .. }, iter)) = iter.next() {
let (s, iter) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no south provided"), iter)).expect("no south specified");
let (n, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no north provided"), iter)).expect("no north specified");
Self::n(s, n)
} else if let Some((Token { value: Value::Key("bsp/s"), .. }, iter)) = iter.next() {
let (n, iter) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no north provided"), iter)).expect("no north specified");
let (s, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no south provided"), iter)).expect("no south specified");
Self::s(n, s)
} else if let Some((Token { value: Value::Key("bsp/e"), .. }, iter)) = iter.next() {
let (w, iter) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no west provided"), iter)).expect("no west specified");
let (e, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no east provided"), iter)).expect("no east specified");
Self::e(w, e)
} else if let Some((Token { value: Value::Key("bsp/w"), .. }, iter)) = iter.next() {
let (e, iter) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no west provided"), iter)).expect("no west specified");
let (w, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no east provided"), iter)).expect("no east specified");
Self::w(e, w)
} else if let Some((Token { value: Value::Key("bsp/a"), .. }, iter)) = iter.next() {
let (a, iter) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no above provided"), iter)).expect("no above specified");
let (b, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no below provided"), iter)).expect("no below specified");
Self::a(a, b)
} else if let Some((Token { value: Value::Key("bsp/b"), .. }, iter)) = iter.next() {
let (a, iter) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no above provided"), iter)).expect("no above specified");
let (b, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no below provided"), iter)).expect("no below specified");
Self::b(a, b)
} else {
return None
})
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
match key {
"bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b" => {
let _ = iter.next().unwrap();
let c1 = iter.next().expect("no content1 specified");
let c2 = iter.next().expect("no content2 specified");
let c1 = state.get_content(&c1.value).expect("no content1 provided");
let c2 = state.get_content(&c2.value).expect("no content2 provided");
return Some(match key {
"bsp/n" => Self::n(c1, c2),
"bsp/s" => Self::s(c1, c2),
"bsp/e" => Self::e(c1, c2),
"bsp/w" => Self::w(c1, c2),
"bsp/a" => Self::a(c1, c2),
"bsp/b" => Self::b(c1, c2),
_ => unreachable!(),
})
},
_ => return None
}
}
});
impl<A, B> Bsp<A, B> {
pub fn n (a: A, b: B) -> Self { Self(North, a, b) }

View file

@ -7,31 +7,24 @@ pub struct Either<A, B>(pub bool, pub A, pub B);
impl<A, B> Either<A, B> { pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } }
try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
if let Some(Token { value: Value::Key("when"), .. }) = iter.peek() {
let iter = iter.clone().next().unwrap().1;
let condition = iter.next();
if let Some((ref condition, _)) = condition {
let condition = state.get(&condition.value).expect("no condition");
if let Some((ref content, _)) = iter.next() {
let content = state.get_content(&content.value).expect("no atom");
return Some(Self(condition, content))
}
}
let _ = iter.next().unwrap();
let condition = iter.next().expect("no condition specified");
let content = iter.next().expect("no content specified");
let condition = state.get(&condition.value).expect("no condition provided");
let content = state.get_content(&content.value).expect("no content provided");
return Some(Self(condition, content))
}
});
try_from_expr!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key("either"), .. }, _)) = iter.next() {
let iter = iter.clone().next().unwrap().1;
let condition = iter.next();
if let Some((ref condition, _)) = condition {
let condition = state.get(&condition.value).expect("no condition");
if let Some((ref content1, _)) = iter.next() {
let content1 = state.get_content(&content1.value).expect("no content1");
if let Some((ref content2, _)) = iter.next() {
let content2 = state.get_content(&content2.value).expect("no content2");
return Some(Self(condition, content1, content2))
}
}
}
if let Some(Token { value: Value::Key("either"), .. }) = iter.peek() {
let _ = iter.next().unwrap();
let condition = iter.next().expect("no condition specified");
let content = iter.next().expect("no content specified");
let alternate = iter.next().expect("no alternate specified");
let condition = state.get(&condition.value).expect("no condition provided");
let content = state.get_content(&content.value).expect("no content provided");
let alternate = state.get_content(&alternate.value).expect("no alternate provided");
return Some(Self(condition, content, alternate))
}
});
impl<E: Output, A: Render<E>> Content<E> for When<A> {

View file

@ -12,16 +12,21 @@ macro_rules! transform_xy {
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T>
for $Enum<RenderBox<'a, E>> {
fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option<Self> {
let iter = iter.clone();
Some(if let Some((Token { value: Value::Key($x), .. }, iter)) = iter.next() {
Self::x(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else if let Some((Token { value: Value::Key($y), .. }, iter)) = iter.next() {
Self::y(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else if let Some((Token { value: Value::Key($xy), .. }, iter)) = iter.next() {
Self::xy(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else {
return None
})
let mut iter = iter.clone();
if let Some(Token { value: Value::Key(k), .. }) = iter.peek() {
if k == $x || k == $y || k == $xy {
let _ = iter.next().unwrap();
let token = iter.next().expect("no content specified");
let content = state.get_content(&token.value).expect("no content provided");
return Some(match k {
$x => Self::x(content),
$y => Self::y(content),
$xy => Self::xy(content),
_ => unreachable!()
})
}
}
None
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
@ -53,22 +58,31 @@ macro_rules! transform_xy_unit {
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T>
for $Enum<E::Unit, RenderBox<'a, E>> {
fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option<Self> {
Some(if let Some((Token { value: Value::Key($x), .. }, iter)) = iter.next() {
let (x, iter) = iter.next().map(|(x, iter)|(state.get(&x.value).expect("no x provided"), iter)).expect("no x specified");
let (c, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no content provided"), iter)).expect("no content specified");
Self::x(x, c)
} else if let Some((Token { value: Value::Key($y), .. }, iter)) = iter.next() {
let (y, iter) = iter.next().map(|(y, iter)|(state.get(&y.value).expect("no y provided"), iter)).expect("no y specified");
let (c, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no content provided"), iter)).expect("no content specified");
Self::y(y, c)
} else if let Some((Token { value: Value::Key($xy), .. }, iter)) = iter.next() {
let (x, iter) = iter.next().map(|(x, iter)|(state.get(&x.value).expect("no x provided"), iter)).expect("no x specified");
let (y, iter) = iter.next().map(|(y, iter)|(state.get(&y.value).expect("no y provided"), iter)).expect("no y specified");
let (c, _) = iter.next().map(|(c, iter)|(state.get_content(&c.value).expect("no content provided"), iter)).expect("no content specified");
Self::xy(x, y, c)
} else {
return None
})
let mut iter = iter.clone();
if let Some(Token { value: Value::Key(k), .. }) = iter.peek() {
if k == $x || k == $y {
let _ = iter.next().unwrap();
let u = iter.next().expect("no unit specified");
let c = iter.next().expect("no content specified");
let u = state.get(&u.value).expect("no unit provided");
let c = state.get_content(&c.value).expect("not content provided");
return Some(match k {
$x => Self::x(u, c),
$y => Self::y(u, c),
_ => unreachable!(),
})
} else if k == $xy {
let _ = iter.next().unwrap();
let u = iter.next().expect("no unit specified");
let v = iter.next().expect("no unit specified");
let c = iter.next().expect("no content specified");
let u = state.get(&u.value).expect("no unit provided");
let v = state.get(&v.value).expect("no unit provided");
let c = state.get_content(&c.value).expect("not content provided");
return Some(Self::xy(u, v, c))
}
}
None
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {

View file

@ -7,7 +7,7 @@ use crate::*;
fn content (&$self) -> impl Render<$Output> { $expr }
}
impl<'a> ViewContext<'a, $Output> for $State {
fn get_content_custom (&'a $self, value: &Value<'a>) -> Option<RenderBox<'a, $Output>> {
fn get_content_sym (&'a $self, value: &Value<'a>) -> Option<RenderBox<'a, $Output>> {
if let Value::Sym(s) = value {
match *s {
$($sym => Some($body),)*
@ -23,7 +23,7 @@ use crate::*;
// An ephemeral wrapper around view state and view description,
// that is meant to be constructed and returned from [Content::content].
pub struct View<'a, T>(pub &'a T, pub TokenIter<'a>);
pub struct View<'a, T>(pub &'a T, pub SourceIter<'a>);
impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content<O> for View<'a, T> {
fn content (&self) -> impl Render<O> {
let iter = self.1.clone();
@ -42,14 +42,14 @@ pub trait ViewContext<'a, E: Output + 'a>: Send + Sync
+ Context<E::Unit>
{
fn get_content (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>> {
if let Some(builtin) = self.get_content_builtin(value) {
Some(builtin)
} else {
self.get_content_custom(value)
match value {
Value::Sym(_) => self.get_content_sym(value),
Value::Exp(_, _) => self.get_content_exp(value),
_ => panic!("only :symbols and (expressions) accepted here")
}
}
fn get_content_custom (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>>;
fn get_content_builtin (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>> {
fn get_content_sym (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>>;
fn get_content_exp (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>> {
try_delegate!(self, *value, When::<RenderBox<'a, E>>);
try_delegate!(self, *value, Either::<RenderBox<'a, E>, RenderBox<'a, E>>);
try_delegate!(self, *value, Align::<RenderBox<'a, E>>);
@ -78,6 +78,7 @@ pub trait ViewContext<'a, E: Output + 'a>: Send + Sync
(<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $iter:ident|$body:expr) => {
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<$l, T> for $Struct {
fn try_from_expr ($state: &$l T, $iter: TokenIter<'a>) -> Option<Self> {
let mut $iter = $iter.clone();
$body;
None
}