fixed up some parsing and removed some edn mentions

This commit is contained in:
🪞👃🪞 2025-01-18 01:54:06 +01:00
parent 5e7b867aba
commit 452bdf9598
15 changed files with 290 additions and 292 deletions

View file

@ -1,58 +1,58 @@
use crate::*;
pub trait KeyMap {
/// Try to find a command that matches the currently pressed key
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C>;
}
pub struct SourceKeyMap<'a>(&'a str);
impl<'a> KeyMap for SourceKeyMap<'a> {
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C> {
todo!();
None
}
}
pub struct ParsedKeyMap<'a>(TokenIterator<'a>);
impl<'a> KeyMap for ParsedKeyMap<'a> {
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C> {
todo!();
None
}
}
pub struct RefKeyMap<'a>(TokenIterator<'a>);
impl<'a> KeyMap for RefKeyMap<'a> {
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C> {
todo!();
//for token in self.0 {
//match token?.kind() {
//TokenKind::Exp => match atoms.as_slice() {
//[key, command, args @ ..] => match (key.kind(), key.text()) {
//(TokenKind::Sym, key) => {
//if input.matches_edn(key) {
//let command = C::from_edn(state, command, args);
//if command.is_some() {
//return command
//}
//}
//},
//_ => panic!("invalid config: {item}")
//},
//_ => panic!("invalid config: {item}")
//}
//_ => panic!("invalid config: {item}")
//}
//}
None
}
fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C>;
}
//pub struct SourceKeyMap<'a>(&'a str);
//impl<'a> KeyMap for SourceKeyMap<'a> {
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C> {
//todo!();
//None
//}
//}
//pub struct ParsedKeyMap<'a>(TokensIterator<'a>);
//impl<'a> KeyMap for ParsedKeyMap<'a> {
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C> {
//todo!();
//None
//}
//}
//pub struct RefKeyMap<'a>(TokensIterator<'a>);
//impl<'a> KeyMap for RefKeyMap<'a> {
//fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C> {
//todo!();
////for token in self.0 {
////match token?.kind() {
////TokenKind::Exp => match atoms.as_slice() {
////[key, command, args @ ..] => match (key.kind(), key.text()) {
////(TokenKind::Sym, key) => {
////if input.matches_atom(key) {
////let command = C::from_atom(state, command, args);
////if command.is_some() {
////return command
////}
////}
////},
////_ => panic!("invalid config: {item}")
////},
////_ => panic!("invalid config: {item}")
////}
////_ => panic!("invalid config: {item}")
////}
////}
//None
//}
//}
pub struct ArcKeyMap(Vec<ArcAtom>);
impl<'a> KeyMap for ArcKeyMap {
fn command <S, C: EdnCommand<S>> (&self, state: &S, input: &impl EdnInput) -> Option<C> {
fn command <S, C: AtomCommand<S>> (&self, state: &S, input: &impl AtomInput) -> Option<C> {
for atom in self.0.iter() {
match atom {
ArcAtom::Exp(atoms) => match atoms.as_slice() {
[key, command, args @ ..] => match (key.kind(), key.text()) {
(TokenKind::Sym, key) => {
if input.matches_edn(key) {
let command = C::from_edn(state, command, args);
if input.matches_atom(key) {
let command = C::from_atom(state, command, args);
if command.is_some() {
return command
}
@ -69,22 +69,22 @@ impl<'a> KeyMap for ArcKeyMap {
}
}
/// [Input] state that can be matched against an [Atom].
pub trait EdnInput: Input {
fn matches_edn (&self, token: &str) -> bool;
pub trait AtomInput: Input {
fn matches_atom (&self, token: &str) -> bool;
fn get_event <'a> (_: &impl Atom) -> Option<Self::Event> {
None
}
}
/// Turns an EDN item sequence into a command enum variant.
pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (
pub trait AtomCommand<C>: Command<C> {
fn from_atom <'a> (
state: &C,
head: &impl Atom,
tail: &'a [impl Atom]
) -> Option<Self>;
}
/** Implement `EdnCommand` for given `State` and `Command` */
#[macro_export] macro_rules! edn_command {
/** Implement `AtomCommand` for given `State` and `Command` */
#[macro_export] macro_rules! atom_command {
($Command:ty : |$state:ident:<$T:ident: $Trait:path>| { $((
// identifier
$key:literal [
@ -106,22 +106,20 @@ pub trait EdnCommand<C>: Command<C> {
// bound command:
$command:expr
))* }) => {
impl<$T: $Trait> EdnCommand<$T> for $Command {
fn from_edn <'a> (
$state: &$T,
head: &impl Atom,
tail: &'a [impl Atom]
impl<$T: $Trait> AtomCommand<$T> for $Command {
fn from_atom <'a> (
$state: &$T, head: &impl Atom, tail: &'a [impl Atom]
) -> Option<Self> {
$(if let (Atom::Key($key), [ // if the identifier matches
$(if let (TokenType::Key, $key, [ // if the identifier matches
// bind argument ids
$($arg),*
// bind rest parameters
$(, $rest @ ..)?
]) = (head.to_ref(), tail) {
]) = (head.kind(), head.text(), tail) {
$(
$(let $arg: Option<$type> = Context::<$type>::get($state, $arg);)?
)*
//$(edn_command!(@bind $state => $arg $(?)? : $type);)*
//$(atom_command!(@bind $state => $arg $(?)? : $type);)*
return Some($command)
})*
None
@ -149,22 +147,22 @@ pub trait EdnCommand<C>: Command<C> {
// bound command:
$command:expr
))* }) => {
impl EdnCommand<$State> for $Command {
fn from_edn <'a> (
impl AtomCommand<$State> for $Command {
fn from_atom <'a> (
$state: &$State,
head: &impl Atom,
tail: &'a [impl Atom],
) -> Option<Self> {
$(if let (Atom::Key($key), [ // if the identifier matches
$(if let (TokenType::Key, $key, [ // if the identifier matches
// bind argument ids
$($arg),*
// bind rest parameters
$(, $rest @ ..)?
]) = (head.to_ref(), tail) {
]) = (head.kind(), head.text(), tail) {
$(
$(let $arg: Option<$type> = Context::<$type>::get($state, $arg);)?
)*
//$(edn_command!(@bind $state => $arg $(?)? : $type);)*
//$(atom_command!(@bind $state => $arg $(?)? : $type);)*
return Some($command)
})*
None
@ -178,7 +176,7 @@ pub trait EdnCommand<C>: Command<C> {
let $arg: $type = Context::<$type>::get_or_fail($state, $arg);
};
}
#[cfg(test)] #[test] fn test_edn_keymap () -> Usually<()> {
#[cfg(test)] #[test] fn test_atom_keymap () -> Usually<()> {
let keymap = KeyMap::new("")?;
Ok(())
}