fix keymap macros. rendering issue

This commit is contained in:
🪞👃🪞 2025-01-05 01:15:53 +01:00
parent 6f51872856
commit f3fd88a199
15 changed files with 303 additions and 180 deletions

View file

@ -15,6 +15,7 @@ const fn digit (c: char) -> usize {
#[derive(Debug)]
pub enum ParseError {
Unknown(u8),
Empty,
Incomplete,
Unexpected(char),
@ -25,27 +26,90 @@ impl std::fmt::Display for ParseError {
Self::Empty => write!(f, "empty"),
Self::Incomplete => write!(f, "incomplete"),
Self::Unexpected(c) => write!(f, "unexpected '{c}'"),
Self::Unknown(i) => write!(f, "unknown #{i}"),
}
}
}
impl std::error::Error for ParseError {}
#[derive(Debug, Clone, Default, PartialEq)]
pub enum Item<T: std::fmt::Debug + Clone + Default + PartialEq> {
#[default] Nil,
pub enum EdnItem<T> {
Nil,
Num(usize),
Sym(T),
Key(T),
Exp(Vec<Item<T>>),
Exp(Vec<EdnItem<T>>),
}
impl<T> Default for EdnItem<T> {
fn default () -> Self {
Self::Nil
}
}
impl Item<String> {
impl<T: AsRef<str> + PartialEq + Default + Clone + std::fmt::Debug> EdnItem<T> {
pub fn to_ref (&self) -> EdnItem<&str> {
match self {
Self::Key(x) => EdnItem::Key(x.as_ref()),
_ => todo!()
}
}
pub fn to_str (&self) -> &str {
match self {
Self::Key(x) => x.as_ref(),
_ => todo!()
}
}
}
impl<'a> Item<&'a str> {
impl EdnItem<String> {
fn from (other: EdnItem<impl AsRef<str>>) -> EdnItem<String> {
use EdnItem::*;
match other {
Nil => Nil,
Key(t) => Key(t.as_ref().to_string()),
_ => todo!()
}
}
}
impl<'a, T: std::fmt::Debug + Clone + Default + PartialEq + From<&'a str>> Item<T> {
impl<'a> TryFrom<Token<'a>> for EdnItem<String> {
type Error = ParseError;
fn try_from (token: Token<'a>) -> Result<Self, Self::Error> {
use Token::*;
Ok(match token {
Nil => Self::Nil,
Num(chars, index, length) =>
Self::Num(number(&chars[index..index+length])),
Sym(chars, index, length) =>
Self::Sym(String::from(&chars[index..index+length])),
Key(chars, index, length) =>
Self::Key(String::from(&chars[index..index+length])),
Exp(chars, index, length, 0) =>
Self::Exp(Self::read_all(&chars[index+1..(index+length).saturating_sub(1)])?),
_ => panic!("unclosed delimiter")
})
}
}
impl<'a> TryFrom<Token<'a>> for EdnItem<&'a str> {
type Error = ParseError;
fn try_from (token: Token<'a>) -> Result<Self, Self::Error> {
use Token::*;
Ok(match token {
Nil => EdnItem::Nil,
Num(chars, index, length) =>
Self::Num(number(&chars[index..index+length])),
Sym(chars, index, length) =>
Self::Sym(&chars[index..index+length]),
Key(chars, index, length) =>
Self::Key(&chars[index..index+length]),
Exp(chars, index, length, 0) =>
Self::Exp(Self::read_all(&chars[index+1..(index+length).saturating_sub(1)])?),
_ => panic!("unclosed delimiter")
})
}
}
impl<'a, T: std::fmt::Debug + Clone + Default + PartialEq + From<&'a str>> EdnItem<T> {
pub fn read_all (mut source: &'a str) -> Result<Vec<Self>, ParseError> {
let mut items = vec![];
loop {
@ -53,15 +117,24 @@ impl<'a, T: std::fmt::Debug + Clone + Default + PartialEq + From<&'a str>> Item<
break
}
let (remaining, token) = Token::chomp(source)?;
match Item::read(token)? { Item::Nil => {}, item => items.push(item) };
match Self::read(token)? { Self::Nil => {}, item => items.push(item) };
source = remaining
}
Ok(items)
}
pub fn read_one (source: &'a str) -> Result<(Self, &'a str), ParseError> {
Ok({
if source.len() == 0 {
return Err(ParseError::Unknown(5))
}
let (remaining, token) = Token::chomp(source)?;
(Self::read(token)?, remaining)
})
}
pub fn read (token: Token<'a>) -> Result<Self, ParseError> {
use Token::*;
Ok(match token {
Nil => Item::Nil,
Nil => EdnItem::Nil,
Num(chars, index, length) =>
Self::Num(number(&chars[index..index+length])),
Sym(chars, index, length) =>
@ -73,8 +146,8 @@ impl<'a, T: std::fmt::Debug + Clone + Default + PartialEq + From<&'a str>> Item<
_ => panic!("unclosed delimiter")
})
}
//pub fn to_str <'a> (&'a self) -> Item<&'a str> {
//use Item::*;
//pub fn to_str <'a> (&'a self) -> EdnItem<&'a str> {
//use EdnItem::*;
//match self {
//Nil => Nil,
//Num(n) => Num(*n),