From 5a2177cc77fd8827b565a2bf08c18d6bc25ea0dd Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 24 May 2025 00:29:50 +0300 Subject: [PATCH] dsl: give! and take! macros --- dsl/src/dsl_provide.rs | 69 ++++++++++++++++++++++++++++---- output/src/ops/align.rs | 57 ++++++++++++--------------- proc/src/proc_expose.rs | 12 +----- proc/src/proc_view.rs | 87 ++++++++++++++++++++--------------------- 4 files changed, 131 insertions(+), 94 deletions(-) diff --git a/dsl/src/dsl_provide.rs b/dsl/src/dsl_provide.rs index a0130f7..85e622d 100644 --- a/dsl/src/dsl_provide.rs +++ b/dsl/src/dsl_provide.rs @@ -17,13 +17,6 @@ pub trait Give { } } } - -impl<'n, T: Take<'n, U>, U> Give for U { - fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps { - T::take(self, words) - } -} - /// [Give]s instances of [Self] given [TokenIter]. pub trait Take<'n, State>: Sized + 'n { fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps; @@ -39,6 +32,68 @@ pub trait Take<'n, State>: Sized + 'n { } } +#[macro_export] macro_rules! take { + () => { + impl<'n, Type: 'n, State: Give + 'n> Take<'n, State> for Type { + fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { + state.give(words) + } + } + }; + (box) => { + impl<'n, T, State: Give> + 'n> Take<'n, State> for Box { + fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { + state.give(words) + } + } + }; + ($Type:ty:$State:ty) => { + impl<'n> Take<'n, $State> for $Type { + fn take <'source> (state: &$State, words: &mut TokenIter<'source>) -> Perhaps { + state.give(words) + } + } + }; + ($Type:path$(,$Arg:ident)*|$state:ident:$State:path,$words:ident|$expr:expr) => { + impl<'n, State: $State + 'n $(, $Arg: 'n)*> Take<'n, State> for $Type { + fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps { + $expr + } + } + }; +} +#[macro_export] macro_rules! give { + () => { + impl<'n, Type: Take<'n, State>, State> Give for State { + fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps { + Type::take(self, words) + } + } + }; + (box) => { + //impl<'n, T, Type: Take<'n, Box>> Give> for Box { + //fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps> { + //Type::take(self, words) + //} + //} + }; + ($Type:ty: $State:ty) => { + impl<'n, $Type: Take<'n, $State>> Give<$Type> for $State { + fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<$Type> { + $Type::take(self, words) + } + } + }; + ($Type:ty|$state:ident:$State:ident,$words:ident|$expr:expr) => { + impl Give<$Type> for $State { + fn give <'source> (&self, $words: &mut TokenIter<'source>) -> Perhaps<$Type> { + let $state = self; + $expr + } + } + }; +} + /// Implement the [Give] trait, which boils down to /// specifying two types and providing an expression. #[macro_export] macro_rules! from_dsl { diff --git a/output/src/ops/align.rs b/output/src/ops/align.rs index 8031e0e..1626da5 100644 --- a/output/src/ops/align.rs +++ b/output/src/ops/align.rs @@ -27,45 +27,36 @@ //! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]); //! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]); //! ``` - 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(Alignment, A); - -#[cfg(feature = "dsl")] -impl<'n, State: Give, A: 'n> Take<'n, State> for Align { - fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { - if let Some(Token { value: Value::Key(key), .. }) = words.peek() { +#[cfg(feature = "dsl")]take!(Align, A|state: Give, words|Ok(Some(match words.peek() { + Some(Token { value: Value::Key(key), .. }) => match key { + "align/c"|"align/x"|"align/y"| + "align/n"|"align/s"|"align/e"|"al;qign/w"| + "align/nw"|"align/sw"|"align/ne"|"align/se" => { + let _ = words.next().unwrap(); + let content = state.give_or_fail(&mut words.clone(), ||"expected content")?; match key { - "align/c"|"align/x"|"align/y"| - "align/n"|"align/s"|"align/e"|"align/w"| - "align/nw"|"align/sw"|"align/ne"|"align/se" => { - let _ = words.next().unwrap(); - let content = state.give_or_fail(&mut words.clone(), ||"expected content")?; - return Ok(Some(match key { - "align/c" => Self::c(content), - "align/x" => Self::x(content), - "align/y" => Self::y(content), - "align/n" => Self::n(content), - "align/s" => Self::s(content), - "align/e" => Self::e(content), - "align/w" => Self::w(content), - "align/nw" => Self::nw(content), - "align/ne" => Self::ne(content), - "align/sw" => Self::sw(content), - "align/se" => Self::se(content), - _ => unreachable!() - })) - }, - _ => {} + "align/c" => Self::c(content), + "align/x" => Self::x(content), + "align/y" => Self::y(content), + "align/n" => Self::n(content), + "align/s" => Self::s(content), + "align/e" => Self::e(content), + "align/w" => Self::w(content), + "align/nw" => Self::nw(content), + "align/ne" => Self::ne(content), + "align/sw" => Self::sw(content), + "align/se" => Self::se(content), + _ => unreachable!() } - } - Ok(None) - } -} + }, + _ => return Ok(None) + }, + _ => return Ok(None) +}))); impl Align { #[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) } diff --git a/proc/src/proc_expose.rs b/proc/src/proc_expose.rs index b0fae39..11f4c03 100644 --- a/proc/src/proc_expose.rs +++ b/proc/src/proc_expose.rs @@ -121,11 +121,7 @@ impl ToTokens for ExposeArm { } } -impl From for ExposeSym { - fn from (this: LitStr) -> Self { - Self(this) - } -} +impl From for ExposeSym { fn from (this: LitStr) -> Self { Self(this) } } impl PartialOrd for ExposeSym { fn partial_cmp (&self, other: &Self) -> Option { @@ -153,11 +149,7 @@ impl PartialEq for ExposeSym { impl Eq for ExposeSym {} -impl From for ExposeType { - fn from (this: Type) -> Self { - Self(Box::new(this)) - } -} +impl From for ExposeType { fn from (this: Type) -> Self { Self(Box::new(this)) } } impl PartialOrd for ExposeType { fn partial_cmp (&self, other: &Self) -> Option { diff --git a/proc/src/proc_view.rs b/proc/src/proc_view.rs index f68adc0..08d1684 100644 --- a/proc/src/proc_view.rs +++ b/proc/src/proc_view.rs @@ -4,9 +4,7 @@ use crate::*; pub(crate) struct ViewDef(pub(crate) ViewMeta, pub(crate) ViewImpl); #[derive(Debug, Clone)] -pub(crate) struct ViewMeta { - pub(crate) output: Ident, -} +pub(crate) struct ViewMeta { pub(crate) output: Ident } #[derive(Debug, Clone)] pub(crate) struct ViewImpl { @@ -43,53 +41,54 @@ impl ToTokens for ViewDef { fn to_tokens (&self, out: &mut TokenStream2) { let Self(ViewMeta { output }, ViewImpl { block, exposed }) = self; let self_ty = &block.self_ty; - let builtins: Vec<_> = builtins_with_types() - .iter() - .map(|ty|write_quote(quote! { - let value: Option<#ty> = Give::<#ty>::give(self, &mut exp.clone())?; - if let Some(value) = value { - return Ok(Some(value.boxed())) - } - })) - .collect(); - let exposed: Vec<_> = exposed - .iter() - .map(|(key, value)|write_quote(quote! { - #key => { - Some(Box::new(Thunk::new(move||#self_ty::#value(self))))//Box::new("todo")) - }, - })).collect(); + // Expressions are handled by built-in functions + // that operate over constants and symbols. + let builtins: Vec<_> = builtins_with_types().iter().map(|ty|write_quote(quote! { + ::tengri::dsl::Value::Exp(_, expr) => { + Give::<#ty>::give(&mut expr.clone()).map(|value|value.boxed()) + }, + })).collect(); + // Symbols are handled by user-taked functions + // that take no parameters but `&self`. + let exposed: Vec<_> = exposed.iter().map(|(key, value)|write_quote(quote! { + ::tengri::dsl::Value::Sym(#key) => { + Some(Box::new(Thunk::new(move||#self_ty::#value(self)))) + }, + })).collect(); write_quote_to(out, quote! { /// Generated by [tengri_proc]. /// /// Gives [#self_ty] the ability to construct the [Render]able /// which might corresponds to a given [TokenStream], /// while taking [#self_ty]'s state into consideration. - impl ::tengri::dsl::Give>> for #self_ty { - fn give <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>) - -> Perhaps>> - { - Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() { - match value { - // Expressions are handled by built-in functions - // that operate over constants and symbols. - ::tengri::dsl::Value::Exp(_, exp) => { - #(#builtins)* - None - }, - // Symbols are handled by user-taked functions - // that take no parameters but `&self`. - ::tengri::dsl::Value::Sym(sym) => match sym { - #(#exposed)* - _ => None - }, - _ => None - } - } else { - None - }) - } - } + give!(Box>|state:#self_ty, words|Ok(None)); + //impl <'n, State: ::tengri::dsl::Give>>> + //Take<'n, Box> for #self_ty + //{ + //fn give <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>) + //-> Perhaps>> + //{ + //Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() { + //match value { + //// Expressions are handled by built-in functions + //// that operate over constants and symbols. + //::tengri::dsl::Value::Exp(_, exp) => { + //#(#builtins)* + //None + //}, + //// Symbols are handled by user-taked functions + //// that take no parameters but `&self`. + //::tengri::dsl::Value::Sym(sym) => match sym { + //#(#exposed)* + //_ => None + //}, + //_ => None + //} + //} else { + //None + //}) + //} + //} /// Generated by [tengri_proc]. /// /// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,