remove old declarative macros

This commit is contained in:
🪞👃🪞 2025-05-08 22:07:10 +03:00
parent bcbcc387a2
commit b7bb6119aa
9 changed files with 33 additions and 408 deletions

View file

@ -24,127 +24,6 @@
}
}
/// Implement `Context` for one or more base structs, types, and keys. */
#[macro_export] macro_rules! expose {
($([$self:ident:$State:ty] $(([$($Type:tt)*] $(($pat:literal $expr:expr))*))*)*) => {
$(expose!(@impl [$self: $State] { $([$($Type)*] => { $($pat => $expr),* })* });)*
};
($([$self:ident:$State:ty] { $([$($Type:tt)*] => { $($pat:pat => $expr:expr),* $(,)? })* })*) => {
$(expose!(@impl [$self: $State] { $([$($Type)*] => { $($pat => $expr),* })* });)*
};
(@impl [$self:ident:$State:ty] { $([$($Type:tt)*] => { $($pat:pat => $expr:expr),* $(,)? })* }) => {
$(expose!(@type [$($Type)*] [$self: $State] => { $($pat => $expr),* });)*
};
(@type [bool] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
provide_bool!(bool: |$self: $State| { $($pat => $expr),* });
};
(@type [u16] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
provide_num!(u16: |$self: $State| { $($pat => $expr),* });
};
(@type [usize] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
provide_num!(usize: |$self: $State| { $($pat => $expr),* });
};
(@type [isize] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
provide_num!(isize: |$self: $State| { $($pat => $expr),* });
};
(@type [$Type:ty] [$self:ident: $State:ty] => { $($pat:pat => $expr:expr),* $(,)? }) => {
provide!($Type: |$self: $State| { $($pat => $expr),* });
};
}
/// Implement `Context` for a context and type.
#[macro_export] macro_rules! provide {
// Provide a value to the EDN template
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
#[allow(unreachable_code)]
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(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 {
#[allow(unreachable_code)]
fn get (&$lt $self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
}
/// Implement `Context` for a context and numeric type.
///
/// This enables support for numeric literals.
#[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<$T: $Trait> Context<$type> for $T {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* Num(n) => *n 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 Context<$type> for $State {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
}
}
};
}
/// Implement `Context` for a context and the boolean type.
///
/// This enables support for boolean literals.
#[macro_export] macro_rules! provide_bool {
// 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<$T: $Trait> Context<$type> for $T {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl {
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
$(Sym($pat) => $expr,)*
_ => return Context::get(self, dsl)
})
}
}
};
// 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 Context<$type> for $State {
fn get (&$self, dsl: &Value) -> Option<$type> {
use Value::*;
Some(match dsl {
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
$(Sym($pat) => $expr,)*
_ => return None
})
}
}
};
}
#[macro_export] macro_rules! impose {
([$self:ident:$Struct:ty] $(($Command:ty : $(($cmd:literal $args:tt $result:expr))*))*) => {
$(atom_command!($Command: |$self: $Struct| { $(($cmd $args $result))* });)*
};
([$self:ident:$Struct:ty] { $($Command:ty => $variants:tt)* }) => {
$(atom_command!($Command: |$self: $Struct| $variants);)*
};
}
#[macro_export] macro_rules! get_value {
($state:expr => $token:expr) => {
if let Some(value) = $state.get(&$token.value) {