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

151
input/src/keymap.rs Normal file
View file

@ -0,0 +1,151 @@
use crate::*;
use std::sync::Arc;
pub trait EdnInput: Input {
fn matches_edn (&self, token: &str) -> bool;
fn get_event (_: &Atom<impl AsRef<str>>) -> Option<Self::Event> {
None
}
}
/// Turns an EDN item sequence into a command enum variant.
pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (
state: &C,
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>]
) -> Option<Self>;
}
/** Implement `EdnCommand` for given `State` and `Command` */
#[macro_export] macro_rules! edn_command {
($Command:ty : |$state:ident:<$T:ident: $Trait:path>| { $((
// identifier
$key:literal [
// named parameters
$(
// argument name
$arg:ident
// if type is not provided defaults to Atom
$(
// type:name separator
:
// argument type
$type:ty
)?
),*
// rest of parameters
$(, ..$rest:ident)?
]
// bound command:
$command:expr
))* }) => {
impl<$T: $Trait> EdnCommand<$T> for $Command {
fn from_edn <'a> (
$state: &$T,
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>]
) -> Option<Self> {
$(if let (Atom::Key($key), [ // if the identifier matches
// bind argument ids
$($arg),*
// bind rest parameters
$(, $rest @ ..)?
]) = (head.to_ref(), tail) {
$(
$(let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);)?
)*
//$(edn_command!(@bind $state => $arg $(?)? : $type);)*
return Some($command)
})*
None
}
}
};
($Command:ty : |$state:ident:$State:ty| { $((
// identifier
$key:literal [
// named parameters
$(
// argument name
$arg:ident
// if type is not provided defaults to Atom
$(
// type:name separator
:
// argument type
$type:ty
)?
),*
// rest of parameters
$(, ..$rest:ident)?
]
// bound command:
$command:expr
))* }) => {
impl EdnCommand<$State> for $Command {
fn from_edn <'a> (
$state: &$State,
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>],
) -> Option<Self> {
$(if let (Atom::Key($key), [ // if the identifier matches
// bind argument ids
$($arg),*
// bind rest parameters
$(, $rest @ ..)?
]) = (head.to_ref(), tail) {
$(
$(let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);)?
)*
//$(edn_command!(@bind $state => $arg $(?)? : $type);)*
return Some($command)
})*
None
}
}
};
(@bind $state:ident =>$arg:ident ? : $type:ty) => {
let $arg: Option<$type> = EdnProvide::<$type>::get($state, $arg);
};
(@bind $state:ident => $arg:ident : $type:ty) => {
let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg);
};
}
pub struct KeyMap(Vec<Atom<Arc<str>>>);
impl KeyMap {
/// Construct keymap from source text or fail
pub fn new (keymap: &str) -> Usually<Self> {
Ok(Self(Atom::read_all(keymap)?))
}
/// Try to find a binding matching the currently pressed key
pub fn command <T, C> (&self, state: &T, input: &impl EdnInput) -> Option<C>
where
C: Command<T> + EdnCommand<T>
{
use Atom::*;
let mut command: Option<C> = None;
for item in self.0.iter() {
if let Exp(e) = item {
match e.as_slice() {
[Sym(key), c, args @ ..] if input.matches_edn(key) => {
command = C::from_edn(state, c, args);
if command.is_some() {
break
}
},
_ => {}
}
} else {
panic!("invalid config")
}
}
command
}
}
#[cfg(test)] #[test] fn test_edn_keymap () -> Usually<()> {
let keymap = KeyMap::new("")?;
Ok(())
}