remove Atom. almost there

This commit is contained in:
🪞👃🪞 2025-01-18 15:37:53 +01:00
parent dc7b713108
commit cf1fd5b45a
20 changed files with 539 additions and 739 deletions

87
edn/src/iter.rs Normal file
View file

@ -0,0 +1,87 @@
use crate::*;
#[derive(Copy, Clone, Debug, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
const_iter!(<'a>|self: TokenIter<'a>| => Token<'a> => self.next_mut().map(|(result, _)|result));
impl<'a> From<&'a str> for TokenIter<'a> {fn from (source: &'a str) -> Self{Self::new(source)}}
impl<'a> TokenIter<'a> {
pub const fn new (source: &'a str) -> Self { Self(source) }
pub const fn chomp (&self, index: usize) -> Self { Self(split_at(self.0, index).1) }
pub const fn next (mut self) -> Option<(Token<'a>, Self)> { Self::next_mut(&mut self) }
pub const fn peek (&self) -> Option<Token<'a>> { peek_src(self.0) }
pub const fn next_mut (&mut self) -> Option<(Token<'a>, Self)> {
match self.peek() {
Some(token) => Some((token, self.chomp(token.end()))),
None => None
}
}
}
pub const fn peek_src <'a> (source: &'a str) -> Option<Token<'a>> {
let mut token: Token<'a> = Token::new(source, Nil, 0, 0);
iterate!(char_indices(source) => (start, c) => token = match token.value() {
Err(_) => return Some(token),
Nil => match c {
' '|'\n'|'\r'|'\t' => token.grow(),
'(' => Token {
source, start, length: 1,
value: Value::Exp(1, TokenIter(str_range(source, start, start + 1))),
},
'0'..='9' => Token {
source, start, length: 1,
value: match to_digit(c) {
Ok(c) => Value::Num(c),
Result::Err(e) => Value::Err(e)
}
},
':'|'@' => Token {
source, start, length: 1,
value: Value::Sym(str_range(source, start, start + 1)),
},
'/'|'a'..='z' => Token {
source, start, length: 1,
value: Value::Key(str_range(source, start, start + 1)),
},
_ => token.error(Unexpected(c))
},
Num(_) => match c {
'0'..='9' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Sym(_) => match c {
'a'..='z'|'0'..='9'|'-' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Key(_) => match c {
'a'..='z'|'0'..='9'|'-'|'/' => token.grow(),
' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c))
},
Exp(depth, _) => match depth {
0 => return Some(token),
_ => match c {
')' => token.grow_out(),
'(' => token.grow_in(),
_ => token.grow(),
}
},
});
match token.value() {
Nil => None,
_ => Some(token),
}
}
pub const fn to_number (digits: &str) -> Result<usize, ParseError> {
let mut value = 0;
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
Ok(digit) => value = 10 * value + digit,
Result::Err(e) => return Result::Err(e)
});
Ok(value)
}
pub const fn to_digit (c: char) -> Result<usize, ParseError> {
Ok(match c {
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
_ => return Result::Err(Unexpected(c))
})
}