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

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
pub trait EdnInput: Input {
fn matches_edn (&self, token: &str) -> bool;
fn get_event (_: &EdnItem<impl AsRef<str>>) -> Option<Self::Event> {
fn get_event (_: &Atom<impl AsRef<str>>) -> Option<Self::Event> {
None
}
}
@ -12,8 +12,8 @@ pub trait EdnInput: Input {
pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (
state: &C,
head: &EdnItem<impl AsRef<str>>,
tail: &'a [EdnItem<impl AsRef<str>>]
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>]
) -> Option<Self>;
}
@ -26,7 +26,7 @@ pub trait EdnCommand<C>: Command<C> {
$(
// argument name
$arg:ident
// if type is not provided defaults to EdnItem
// if type is not provided defaults to Atom
$(
// type:name separator
:
@ -43,10 +43,10 @@ pub trait EdnCommand<C>: Command<C> {
impl<$T: $Trait> EdnCommand<$T> for $Command {
fn from_edn <'a> (
$state: &$T,
head: &EdnItem<impl AsRef<str>>,
tail: &'a [EdnItem<impl AsRef<str>>]
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>]
) -> Option<Self> {
$(if let (EdnItem::Key($key), [ // if the identifier matches
$(if let (Atom::Key($key), [ // if the identifier matches
// bind argument ids
$($arg),*
// bind rest parameters
@ -69,7 +69,7 @@ pub trait EdnCommand<C>: Command<C> {
$(
// argument name
$arg:ident
// if type is not provided defaults to EdnItem
// if type is not provided defaults to Atom
$(
// type:name separator
:
@ -86,10 +86,10 @@ pub trait EdnCommand<C>: Command<C> {
impl EdnCommand<$State> for $Command {
fn from_edn <'a> (
$state: &$State,
head: &EdnItem<impl AsRef<str>>,
tail: &'a [EdnItem<impl AsRef<str>>],
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>],
) -> Option<Self> {
$(if let (EdnItem::Key($key), [ // if the identifier matches
$(if let (Atom::Key($key), [ // if the identifier matches
// bind argument ids
$($arg),*
// bind rest parameters
@ -113,18 +113,18 @@ pub trait EdnCommand<C>: Command<C> {
};
}
pub struct EdnKeyMapToCommand(Vec<EdnItem<Arc<str>>>);
impl EdnKeyMapToCommand {
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(EdnItem::read_all(keymap)?))
Ok(Self(Atom::read_all(keymap)?))
}
/// Try to find a binding matching the currently pressed key
pub fn from <T, C> (&self, state: &T, input: &impl EdnInput) -> Option<C>
pub fn command <T, C> (&self, state: &T, input: &impl EdnInput) -> Option<C>
where
C: Command<T> + EdnCommand<T>
{
use EdnItem::*;
use Atom::*;
let mut command: Option<C> = None;
for item in self.0.iter() {
if let Exp(e) = item {
@ -145,6 +145,7 @@ impl EdnKeyMapToCommand {
}
}
#[cfg(test)] #[test] fn test_edn_keymap () {
let keymap = EdnKeymapToCommand::new("")?;
#[cfg(test)] #[test] fn test_edn_keymap () -> Usually<()> {
let keymap = KeyMap::new("")?;
Ok(())
}

View file

@ -1,19 +1,15 @@
#![feature(associated_type_defaults)]
//mod component; pub use self::component::*;
mod input; pub use self::input::*;
mod command; pub use self::command::*;
mod event_map; pub use self::event_map::*;
mod edn_input; pub use self::edn_input::*;
pub(crate) use ::tek_edn::EdnItem;
mod input; pub use self::input::*;
mod command; pub use self::command::*;
mod keymap; pub use self::keymap::*;
//mod event_map; pub use self::event_map::*;
pub(crate) use ::tek_edn::Atom;
/// Standard error trait.
pub(crate) use std::error::Error;
/// Standard result type.
pub(crate) type Usually<T> = Result<T, Box<dyn Error>>;
/// Standard optional result type.
pub(crate) type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
#[cfg(test)] #[test] fn test_stub_input () -> Usually<()> {
use crate::*;
struct TestInput(bool);