wip: edn minefield

This commit is contained in:
🪞👃🪞 2025-01-04 12:23:33 +01:00
parent 98d2107e4e
commit 6f51872856
7 changed files with 150 additions and 210 deletions

View file

@ -16,12 +16,22 @@ const fn digit (c: char) -> usize {
#[derive(Debug)]
pub enum ParseError {
Empty,
Incomplete,
Unexpected(char),
Incomplete
}
impl std::fmt::Display for ParseError {
fn fmt (&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Empty => write!(f, "empty"),
Self::Incomplete => write!(f, "incomplete"),
Self::Unexpected(c) => write!(f, "unexpected '{c}'"),
}
}
}
impl std::error::Error for ParseError {}
#[derive(Debug, Clone, Default, PartialEq)]
pub enum Item<T: AsRef<str>> {
pub enum Item<T: std::fmt::Debug + Clone + Default + PartialEq> {
#[default] Nil,
Num(usize),
Sym(T),
@ -30,7 +40,13 @@ pub enum Item<T: AsRef<str>> {
}
impl Item<String> {
pub fn read_all <'a> (mut source: &'a str) -> Result<Vec<Self>, ParseError> {
}
impl<'a> Item<&'a str> {
}
impl<'a, T: std::fmt::Debug + Clone + Default + PartialEq + From<&'a str>> Item<T> {
pub fn read_all (mut source: &'a str) -> Result<Vec<Self>, ParseError> {
let mut items = vec![];
loop {
if source.len() == 0 {
@ -42,19 +58,29 @@ impl Item<String> {
}
Ok(items)
}
pub fn read <'a> (token: Token<'a>) -> Result<Self, ParseError> {
pub fn read (token: Token<'a>) -> Result<Self, ParseError> {
use Token::*;
Ok(match token {
Nil => Item::Nil,
Num(chars, index, length) =>
Self::Num(number(&chars[index..index+length])),
Sym(chars, index, length) =>
Self::Sym(chars[index..index+length].to_string()),
Self::Sym(T::from(&chars[index..index+length])),
Key(chars, index, length) =>
Self::Key(chars[index..index+length].to_string()),
Self::Key(T::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")
})
}
//pub fn to_str <'a> (&'a self) -> Item<&'a str> {
//use Item::*;
//match self {
//Nil => Nil,
//Num(n) => Num(*n),
//Sym(t) => Sym(t.as_str()),
//Key(t) => Key(t.as_str()),
//Exp(t) => Exp(t.iter().map(|x|x.to_str()).collect())
//}
//}
}