mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
wip: EdnItem -> Atom, rewrite tokenizer
This commit is contained in:
parent
143cd24e09
commit
ff31957fed
39 changed files with 477 additions and 376 deletions
11
edn/src/describe.rs
Normal file
11
edn/src/describe.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use crate::*;
|
||||
|
||||
pub trait TryFromEdn<'a, T>: Sized {
|
||||
fn try_from_edn (state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]) ->
|
||||
Option<Self>;
|
||||
}
|
||||
|
||||
pub trait TryIntoEdn<'a, T>: Sized {
|
||||
fn try_from_edn (state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]) ->
|
||||
Option<Self>;
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
use crate::*;
|
||||
#[derive(Debug)] pub enum ParseError {
|
||||
Empty,
|
||||
Incomplete,
|
||||
Unexpected(char),
|
||||
Code(u8),
|
||||
}
|
||||
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}'"),
|
||||
Self::Code(i) => write!(f, "error #{i}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::error::Error for ParseError {}
|
||||
|
|
@ -1,89 +0,0 @@
|
|||
use crate::*;
|
||||
use std::sync::Arc;
|
||||
#[derive(Default, Clone, PartialEq)] pub enum EdnItem<T> {
|
||||
#[default] Nil,
|
||||
Num(usize),
|
||||
Sym(T),
|
||||
Key(T),
|
||||
Exp(Vec<EdnItem<T>>),
|
||||
}
|
||||
impl<'a, T: 'a> EdnItem<T> {
|
||||
pub fn transform <U: 'a, F: Fn(&'a T)->U + Clone> (&'a self, f: F) -> EdnItem<U> {
|
||||
use EdnItem::*;
|
||||
match self {
|
||||
Nil => Nil,
|
||||
Num(n) => Num(*n),
|
||||
Sym(t) => Sym(f(t)),
|
||||
Key(t) => Key(f(t)),
|
||||
Exp(e) => Exp(e.iter().map(|i|i.transform(f.clone())).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, T: AsRef<str>> EdnItem<T> {
|
||||
pub fn to_ref (&'a self) -> EdnItem<&'a str> {
|
||||
self.transform(|t|t.as_ref())
|
||||
}
|
||||
pub fn to_arc (&'a self) -> EdnItem<Arc<str>> {
|
||||
self.transform(|t|t.as_ref().into())
|
||||
}
|
||||
}
|
||||
impl<'a> EdnItem<Arc<str>> {
|
||||
pub fn read_all (mut source: &'a str) -> Result<Vec<Self>, ParseError> {
|
||||
let mut items = vec![];
|
||||
loop {
|
||||
if source.len() == 0 {
|
||||
break
|
||||
}
|
||||
let (remaining, token) = Token::chomp(source)?;
|
||||
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::Code(5))
|
||||
}
|
||||
let (remaining, token) = Token::chomp(source)?;
|
||||
(Self::read(token)?, remaining)
|
||||
})
|
||||
}
|
||||
pub fn read (token: Token<'a>) -> Result<Self, ParseError> {
|
||||
use EdnItem::*;
|
||||
Ok(match token {
|
||||
Token::Nil => Nil,
|
||||
Token::Num(_, _, _) => Num(Token::number(token.string())),
|
||||
Token::Sym(_, _, _) => Sym(token.string().into()),
|
||||
Token::Key(_, _, _) => Key(token.string().into()),
|
||||
Token::Exp(_, _, _, 0) => Exp(EdnItem::read_all(token.string())?),
|
||||
_ => panic!("unclosed delimiter")
|
||||
})
|
||||
}
|
||||
}
|
||||
impl<T: Debug> Debug for EdnItem<T> {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
use EdnItem::*;
|
||||
match self {
|
||||
Nil => write!(f, "(nil)"),
|
||||
Num(u) => write!(f, "(num {u})"),
|
||||
Sym(u) => write!(f, "(sym {u:?})"),
|
||||
Key(u) => write!(f, "(key {u:?})"),
|
||||
Exp(e) => write!(f, "(exp {})",
|
||||
itertools::join(e.iter().map(|i|format!("{:?}", i)), ","))
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Display> Display for EdnItem<T> {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
use EdnItem::*;
|
||||
use itertools::join;
|
||||
match self {
|
||||
Nil => write!(f, ""),
|
||||
Num(u) => write!(f, "{u}"),
|
||||
Sym(u) => write!(f, "{u}"),
|
||||
Key(u) => write!(f, "{u}"),
|
||||
Exp(e) => write!(f, "({})", join(e.iter().map(|i|format!("{}", i)), " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,99 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub enum Token<'a> {
|
||||
#[default] Nil,
|
||||
Num(&'a str, usize, usize),
|
||||
Sym(&'a str, usize, usize),
|
||||
Key(&'a str, usize, usize),
|
||||
Exp(&'a str, usize, usize, usize),
|
||||
}
|
||||
|
||||
pub type ChompResult<'a> = Result<(&'a str, Token<'a>), ParseError>;
|
||||
|
||||
impl<'a> Token<'a> {
|
||||
pub const fn chomp (source: &'a str) -> ChompResult<'a> {
|
||||
use Token::*;
|
||||
use konst::string::{split_at, char_indices};
|
||||
let mut state = Self::Nil;
|
||||
let mut chars = char_indices(source);
|
||||
while let Some(((index, c), next)) = chars.next() {
|
||||
state = match state {
|
||||
// must begin expression
|
||||
Nil => match c {
|
||||
' '|'\n'|'\r'|'\t' => Nil,
|
||||
'(' => Exp(source, index, 1, 1),
|
||||
':'|'@' => Sym(source, index, 1),
|
||||
'0'..='9' => Num(source, index, 1),
|
||||
'a'..='z' => Key(source, index, 1),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Num(_, _, 0) => unreachable!(),
|
||||
Sym(_, _, 0) => unreachable!(),
|
||||
Key(_, _, 0) => unreachable!(),
|
||||
Num(source, index, length) => match c {
|
||||
'0'..='9' => Num(source, index, length + 1),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((
|
||||
split_at(source, index+length).1,
|
||||
Num(source, index, length)
|
||||
)),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Sym(source, index, length) => match c {
|
||||
'a'..='z'|'0'..='9'|'-' => Sym(source, index, length + 1),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((
|
||||
split_at(source, index+length).1,
|
||||
Sym(source, index, length)
|
||||
)),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Key(source, index, length) => match c {
|
||||
'a'..='z'|'0'..='9'|'-'|'/' => Key(source, index, length + 1),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((
|
||||
split_at(source, index+length).1,
|
||||
Key(source, index, length)
|
||||
)),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Exp(source, index, length, 0) => match c {
|
||||
' '|'\n'|'\r'|'\t' => return Ok((
|
||||
split_at(source, index+length).1,
|
||||
Exp(source, index, length, 0)
|
||||
)),
|
||||
_ => return Err(ParseError::Unexpected(c))
|
||||
},
|
||||
Exp(source, index, length, balance) => match c {
|
||||
')' => Exp(source, index, length + 1, balance - 1),
|
||||
'(' => Exp(source, index, length + 1, balance + 1),
|
||||
_ => Exp(source, index, length + 1, balance)
|
||||
},
|
||||
};
|
||||
chars = next
|
||||
}
|
||||
Ok(("", state))
|
||||
}
|
||||
pub fn string (&self) -> &str {
|
||||
use Token::*;
|
||||
match self {
|
||||
Nil => "",
|
||||
Num(src, start, len) => &src[*start..start+len],
|
||||
Sym(src, start, len) => &src[*start..start+len],
|
||||
Key(src, start, len) => &src[*start..start+len],
|
||||
Exp(src, start, len, 0) => &src[*start..(start+len).saturating_sub(1)],
|
||||
_ => panic!("unclosed delimiter")
|
||||
}
|
||||
}
|
||||
pub const fn number (digits: &str) -> usize {
|
||||
let mut value = 0;
|
||||
let mut chars = konst::string::char_indices(digits);
|
||||
while let Some(((_, c), next)) = chars.next() {
|
||||
value = 10 * value + Self::digit(c);
|
||||
chars = next
|
||||
}
|
||||
value
|
||||
}
|
||||
pub const fn digit (c: char) -> usize {
|
||||
match c { '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
|
||||
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, _ => panic!("not a digit") }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +1,39 @@
|
|||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_fn_trait_return)]
|
||||
mod edn_error; pub use self::edn_error::*;
|
||||
mod edn_item; pub use self::edn_item::*;
|
||||
//mod edn_iter; pub use self::edn_iter::*;
|
||||
mod edn_token; pub use self::edn_token::*;
|
||||
mod edn_provide; pub use self::edn_provide::*;
|
||||
mod try_from_edn; pub use self::try_from_edn::*;
|
||||
pub(crate) use std::{fmt::{Debug, Display, Formatter, Error as FormatError}};
|
||||
#[cfg(test)] #[test] fn test_edn () -> Result<(), ParseError> {
|
||||
use EdnItem::*;
|
||||
assert_eq!(EdnItem::<String>::read_all("")?,
|
||||
mod token; pub use self::token::*;
|
||||
mod provide; pub use self::provide::*;
|
||||
mod describe; pub use self::describe::*;
|
||||
pub(crate) use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
||||
pub(crate) use std::sync::Arc;
|
||||
#[cfg(test)] #[test] fn test_lang () -> Result<(), ParseError> {
|
||||
use Atom::*;
|
||||
assert_eq!(Atom::read_all("")?,
|
||||
vec![]);
|
||||
assert_eq!(EdnItem::<String>::read_all(" ")?,
|
||||
assert_eq!(Atom::read_all(" ")?,
|
||||
vec![]);
|
||||
assert_eq!(EdnItem::<String>::read_all("1234")?,
|
||||
assert_eq!(Atom::read_all("1234")?,
|
||||
vec![Num(1234)]);
|
||||
assert_eq!(EdnItem::<String>::read_all("1234 5 67")?,
|
||||
assert_eq!(Atom::read_all("1234 5 67")?,
|
||||
vec![Num(1234), Num(5), Num(67)]);
|
||||
assert_eq!(EdnItem::<String>::read_all("foo/bar")?,
|
||||
assert_eq!(Atom::read_all("foo/bar")?,
|
||||
vec![Key("foo/bar".into())]);
|
||||
assert_eq!(EdnItem::<String>::read_all(":symbol")?,
|
||||
assert_eq!(Atom::read_all(":symbol")?,
|
||||
vec![Sym(":symbol".into())]);
|
||||
assert_eq!(EdnItem::<String>::read_all(" foo/bar :baz 456")?,
|
||||
assert_eq!(Atom::read_all(" foo/bar :baz 456")?,
|
||||
vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)]);
|
||||
assert_eq!(EdnItem::<String>::read_all(" (foo/bar :baz 456) ")?,
|
||||
assert_eq!(Atom::read_all(" (foo/bar :baz 456) ")?,
|
||||
vec![Exp(vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)])]);
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(test)] #[test] fn test_edn_layout () -> Result<(), ParseError> {
|
||||
EdnItem::<String>::read_all(include_str!("../../output/examples/edn01.edn"))?;
|
||||
EdnItem::<String>::read_all(include_str!("../../output/examples/edn02.edn"))?;
|
||||
//panic!("{layout:?}");
|
||||
//let content = <dyn EdnViewData<::tek_engine::tui::Tui>>::from(&layout);
|
||||
#[cfg(test)] #[test] fn test_lang_examples () -> Result<(), ParseError> {
|
||||
for example in [
|
||||
include_str!("../../tui/examples/edn01.edn"),
|
||||
include_str!("../../tui/examples/edn02.edn"),
|
||||
] {
|
||||
let items = Atom::read_all(example)?;
|
||||
//panic!("{layout:?}");
|
||||
//let content = <dyn EdnViewData<::tek_engine::tui::Tui>>::from(&layout);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[macro_export] macro_rules! from_edn {
|
||||
|
|
|
|||
|
|
@ -1,26 +1,26 @@
|
|||
use crate::*;
|
||||
/// Map EDN tokens to parameters of a given type for a given context
|
||||
pub trait EdnProvide<'a, U>: Sized {
|
||||
fn get (&'a self, _edn: &'a EdnItem<impl AsRef<str>>) -> Option<U> {
|
||||
fn get (&'a self, _edn: &'a Atom<impl AsRef<str>>) -> Option<U> {
|
||||
None
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a EdnItem<impl AsRef<str>>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a Atom<impl AsRef<str>>) -> U {
|
||||
self.get(edn).expect("no value")
|
||||
}
|
||||
}
|
||||
impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for &T {
|
||||
fn get (&'a self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<U> {
|
||||
fn get (&'a self, edn: &'a Atom<impl AsRef<str>>) -> Option<U> {
|
||||
(*self).get(edn)
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a EdnItem<impl AsRef<str>>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a Atom<impl AsRef<str>>) -> U {
|
||||
(*self).get_or_fail(edn)
|
||||
}
|
||||
}
|
||||
impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
|
||||
fn get (&'a self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<U> {
|
||||
fn get (&'a self, edn: &'a Atom<impl AsRef<str>>) -> Option<U> {
|
||||
self.as_ref().map(|s|s.get(edn)).flatten()
|
||||
}
|
||||
fn get_or_fail (&'a self, edn: &'a EdnItem<impl AsRef<str>>) -> U {
|
||||
fn get_or_fail (&'a self, edn: &'a Atom<impl AsRef<str>>) -> U {
|
||||
self.as_ref().map(|s|s.get_or_fail(edn)).expect("no provider")
|
||||
}
|
||||
}
|
||||
|
|
@ -29,8 +29,8 @@ impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
|
|||
// Provide a value to the EDN template
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> EdnProvide<'a, $type> for $State {
|
||||
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<$type> {
|
||||
use EdnItem::*;
|
||||
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<$type> {
|
||||
use Atom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* _ => return None })
|
||||
}
|
||||
}
|
||||
|
|
@ -38,8 +38,8 @@ impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
|
|||
// Provide a value more generically
|
||||
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<$lt> EdnProvide<$lt, $type> for $State {
|
||||
fn get (&$lt $self, edn: &$lt EdnItem<impl AsRef<str>>) -> Option<$type> {
|
||||
use EdnItem::*;
|
||||
fn get (&$lt $self, edn: &$lt Atom<impl AsRef<str>>) -> Option<$type> {
|
||||
use Atom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* _ => return None })
|
||||
}
|
||||
}
|
||||
|
|
@ -52,8 +52,8 @@ impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
|
|||
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
|
||||
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, $T: $Trait> EdnProvide<'a, $type> for $T {
|
||||
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<$type> {
|
||||
use EdnItem::*;
|
||||
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<$type> {
|
||||
use Atom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* Num(n) => n as $type, _ => return None })
|
||||
}
|
||||
}
|
||||
|
|
@ -61,8 +61,8 @@ impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
|
|||
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
|
||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> EdnProvide<'a, $type> for $State {
|
||||
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<$type> {
|
||||
use EdnItem::*;
|
||||
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<$type> {
|
||||
use Atom::*;
|
||||
Some(match edn.to_ref() { $(Sym($pat) => $expr,)* Num(n) => n as $type, _ => return None })
|
||||
}
|
||||
}
|
||||
|
|
@ -74,9 +74,9 @@ impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
|
|||
#[macro_export] macro_rules! edn_provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, E: Output> EdnProvide<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr),*,
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
|
|
@ -84,9 +84,9 @@ impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
|
|||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> EdnProvide<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a EdnItem<impl AsRef<str>>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr),*,
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
299
edn/src/token.rs
Normal file
299
edn/src/token.rs
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
use crate::*;
|
||||
use konst::string::{split_at, str_range, char_indices};
|
||||
use self::ParseError::*;
|
||||
use self::TokenKind::*;
|
||||
macro_rules! iterate {
|
||||
($expr:expr => $arg: pat => $body:expr) => {
|
||||
let mut iter = $expr;
|
||||
while let Some(($arg, next)) = iter.next() {
|
||||
$body;
|
||||
iter = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug)] pub enum ParseError { Unimplemented, Empty, Incomplete, Unexpected(char), Code(u8), }
|
||||
impl std::fmt::Display for ParseError {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
Unimplemented => write!(f, "unimplemented"),
|
||||
Empty => write!(f, "empty"),
|
||||
Incomplete => write!(f, "incomplete"),
|
||||
Unexpected(c) => write!(f, "unexpected '{c}'"),
|
||||
Code(i) => write!(f, "error #{i}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::error::Error for ParseError {}
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub enum TokenKind { #[default] Nil, Num, Sym, Key, Exp }
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct Token<'a> {
|
||||
source: &'a str,
|
||||
kind: TokenKind,
|
||||
start: usize,
|
||||
length: usize,
|
||||
depth: usize,
|
||||
}
|
||||
impl<'a> Token<'a> {
|
||||
pub const fn new (
|
||||
source: &'a str, kind: TokenKind, start: usize, length: usize, depth: usize
|
||||
) -> Self {
|
||||
Self { source, kind, start, length, depth }
|
||||
}
|
||||
pub const fn end (&self) -> usize { self.start + self.length }
|
||||
pub const fn slice (&self) -> &str { str_range(self.source, self.start, self.end()) }
|
||||
pub const fn kind (&self) -> TokenKind { Nil }
|
||||
pub const fn grow (self) -> Self {
|
||||
Self { length: self.length + 1, ..self }
|
||||
}
|
||||
pub const fn grow_in (self) -> Self {
|
||||
Self { length: self.length + 1, depth: self.depth + 1, ..self }
|
||||
}
|
||||
pub const fn grow_out (self) -> Result<Self, ParseError> {
|
||||
match self.depth {
|
||||
0 => Err(Unexpected(')')),
|
||||
d => Ok(Self { length: self.length + 1, depth: d - 1, ..self })
|
||||
}
|
||||
}
|
||||
pub const fn chomp_first (source: &'a str) -> Result<Self, ParseError> {
|
||||
match Self::chomp(source) {
|
||||
Ok((token, _)) => match token.kind() { Nil => Err(Empty), _ => Ok(token) },
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
pub const fn chomp (src: &'a str) -> Result<(Self, &'a str), ParseError> {
|
||||
let mut token: Token<'a> = Token::new(src, Nil, 0, 0, 0);
|
||||
iterate!(char_indices(src) => (index, c) => token = match token.kind() {
|
||||
Nil => match c {
|
||||
'(' => Self::new(src, Exp, index, 1, 1),
|
||||
':'|'@' => Self::new(src, Sym, index, 1, 0),
|
||||
'0'..='9' => Self::new(src, Num, index, 1, 0),
|
||||
'/'|'a'..='z' => Self::new(src, Key, index, 1, 0),
|
||||
' '|'\n'|'\r'|'\t' => token.grow(),
|
||||
_ => return Err(Unexpected(c))
|
||||
},
|
||||
Num => match c {
|
||||
'0'..='9' => token.grow(),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((token, split_at(src, token.end()).1)),
|
||||
_ => return Err(Unexpected(c))
|
||||
},
|
||||
Sym => match c {
|
||||
'a'..='z'|'0'..='9'|'-' => token.grow(),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((token, split_at(src, token.end()).1)),
|
||||
_ => return Err(Unexpected(c)),
|
||||
},
|
||||
Key => match c {
|
||||
'a'..='z'|'0'..='9'|'-'|'/' => token.grow(),
|
||||
' '|'\n'|'\r'|'\t' => return Ok((token, split_at(src, token.end()).1)),
|
||||
_ => return Err(Unexpected(c))
|
||||
},
|
||||
Exp => match token.depth {
|
||||
0 => match c {
|
||||
' '|'\n'|'\r'|'\t' => return Ok((token, split_at(src, token.end()).1)),
|
||||
_ => return Err(Unexpected(c))
|
||||
},
|
||||
_ => match c {
|
||||
')' => match token.grow_out() {
|
||||
Ok(token) => token,
|
||||
Err(e) => return Err(e)
|
||||
},
|
||||
'(' => token.grow_in(),
|
||||
_ => token.grow(),
|
||||
}
|
||||
},
|
||||
});
|
||||
Err(Empty)
|
||||
}
|
||||
pub const fn number (digits: &str) -> Result<usize, ParseError> {
|
||||
let mut value = 0;
|
||||
iterate!(char_indices(digits) => (_, c) => match Self::digit(c) {
|
||||
Ok(digit) => value = 10 * value + digit,
|
||||
Err(e) => return Err(e)
|
||||
});
|
||||
Ok(value)
|
||||
}
|
||||
pub const fn 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 Err(Unexpected(c))
|
||||
})
|
||||
}
|
||||
pub const fn to_ref_atom (&'a self) -> Result<Atom<&'a str>, ParseError> {
|
||||
Ok(match self.kind {
|
||||
Nil => return Err(ParseError::Empty),
|
||||
Num => match Self::number(self.slice()) {
|
||||
Ok(n) => Atom::Num(n),
|
||||
Err(e) => return Err(e)
|
||||
},
|
||||
Sym => Atom::Sym(self.slice()),
|
||||
Key => Atom::Key(self.slice()),
|
||||
Exp => todo!()
|
||||
})
|
||||
}
|
||||
pub fn to_arc_atom (&self) -> Result<Atom<Arc<str>>, ParseError> {
|
||||
Ok(match self.kind {
|
||||
Nil => return Err(ParseError::Empty),
|
||||
Num => match Self::number(self.slice()) {
|
||||
Ok(n) => Atom::Num(n),
|
||||
Err(e) => return Err(e)
|
||||
},
|
||||
Sym => Atom::Sym(self.slice().into()),
|
||||
Key => Atom::Key(self.slice().into()),
|
||||
Exp => todo!()
|
||||
})
|
||||
}
|
||||
}
|
||||
#[derive(Clone, PartialEq)] pub enum Atom<T> { Num(usize), Sym(T), Key(T), Exp(Vec<Atom<T>>) }
|
||||
impl<'a, T: 'a> Atom<T> {
|
||||
pub fn transform <U: 'a, F: Fn(&'a T)->U + Clone> (&'a self, f: F) -> Atom<U> {
|
||||
use Atom::*;
|
||||
match self {
|
||||
Num(n) => Num(*n),
|
||||
Sym(t) => Sym(f(t)),
|
||||
Key(t) => Key(f(t)),
|
||||
Exp(e) => Exp(e.iter().map(|i|i.transform(f.clone())).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, T: AsRef<str>> Atom<T> {
|
||||
pub fn to_ref (&'a self) -> Atom<&'a str> {
|
||||
self.transform(|t|t.as_ref())
|
||||
}
|
||||
pub fn to_arc (&'a self) -> Atom<Arc<str>> {
|
||||
self.transform(|t|t.as_ref().into())
|
||||
}
|
||||
}
|
||||
impl<'a> Atom<&'a str> {
|
||||
pub const fn read_all_ref (_: &'a str) -> Result<Vec<Self>, ParseError> {
|
||||
Err(Unimplemented)
|
||||
}
|
||||
}
|
||||
impl<T: Debug> Debug for Atom<T> {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
use Atom::*;
|
||||
match self {
|
||||
Num(u) => write!(f, "(num {u})"),
|
||||
Sym(u) => write!(f, "(sym {u:?})"),
|
||||
Key(u) => write!(f, "(key {u:?})"),
|
||||
Exp(e) => write!(f, "(exp {})",
|
||||
itertools::join(e.iter().map(|i|format!("{:?}", i)), ","))
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Display> Display for Atom<T> {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
use Atom::*;
|
||||
use itertools::join;
|
||||
match self {
|
||||
Num(u) => write!(f, "{u}"),
|
||||
Sym(u) => write!(f, "{u}"),
|
||||
Key(u) => write!(f, "{u}"),
|
||||
Exp(e) => write!(f, "({})", join(e.iter().map(|i|format!("{}", i)), " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
//impl<'a> Token<'a> {
|
||||
//pub const fn chomp_one (source: &'a str) -> Result<Token<'a>, ParseError> {
|
||||
//match Self::chomp(source) {
|
||||
//Ok((_, token)) => Ok(token),
|
||||
//Err(e) => Err(e)
|
||||
//}
|
||||
//}
|
||||
//pub const fn from_nil (c: char) -> Result<(&'a str, Token<'a>), ParseError> {
|
||||
//match c {
|
||||
//' '|'\n'|'\r'|'\t' => Nil,
|
||||
//'(' => Exp(source, index, 1, 1),
|
||||
//':'|'@' => Sym(source, index, 1),
|
||||
//'0'..='9' => Num(source, index, 1),
|
||||
//'a'..='z' => Key(source, index, 1),
|
||||
//_ => return Err(Unexpected(c))
|
||||
//}
|
||||
//}
|
||||
//pub const fn chomp (source: &'a str) -> Result<(&'a str, Token<'a>), ParseError> {
|
||||
//let mut state = Self::Nil;
|
||||
//let mut chars = char_indices(source);
|
||||
//while let Some(((index, c), next)) = chars.next() {
|
||||
//state = match state {
|
||||
//// must begin expression
|
||||
//Nil => Self::from_nil(c),
|
||||
//Num(_, _, 0) => unreachable!(),
|
||||
//Sym(_, _, 0) => unreachable!(),
|
||||
//Key(_, _, 0) => unreachable!(),
|
||||
//Num(src, idx, len) => match c {
|
||||
//'0'..='9' => Num(src, idx, len + 1),
|
||||
//' '|'\n'|'\r'|'\t' => return Ok((split(src, idx+len).1, Num(src, idx, len))),
|
||||
//_ => return Err(Unexpected(c))
|
||||
//},
|
||||
//Sym(src, idx, len) => match c {
|
||||
//'a'..='z'|'0'..='9'|'-' => Sym(src, idx, len + 1),
|
||||
//' '|'\n'|'\r'|'\t' => return Ok((split(src, idx+len).1, Sym(src, idx, len))),
|
||||
//_ => return Err(Unexpected(c))
|
||||
//},
|
||||
//Key(src, idx, len) => match c {
|
||||
//'a'..='z'|'0'..='9'|'-'|'/' => Key(src, idx, len + 1),
|
||||
//' '|'\n'|'\r'|'\t' => return Ok((split(src, idx+len).1, Key(src, idx, len))),
|
||||
//_ => return Err(Unexpected(c))
|
||||
//},
|
||||
//Exp(src, idx, len, 0) => match c {
|
||||
//' '|'\n'|'\r'|'\t' => return Ok((split(src, idx+len).1, Exp(src, idx, len, 0))),
|
||||
//_ => return Err(Unexpected(c))
|
||||
//},
|
||||
//Exp(src, idx, len, balance) => match c {
|
||||
//')' => Exp(src, idx, len + 1, balance - 1),
|
||||
//'(' => Exp(src, idx, len + 1, balance + 1),
|
||||
//_ => Exp(src, idx, len + 1, balance)
|
||||
//},
|
||||
//};
|
||||
//chars = next
|
||||
//}
|
||||
//Ok(("", state))
|
||||
//}
|
||||
//pub fn src (&self) -> &str {
|
||||
//match self {
|
||||
//Self::Nil => "",
|
||||
//Self::Num(src, _, _) => src,
|
||||
//Self::Sym(src, _, _) => src,
|
||||
//Self::Key(src, _, _) => src,
|
||||
//Self::Exp(src, _, _, _) => src,
|
||||
//}
|
||||
//}
|
||||
//pub fn str (&self) -> &str {
|
||||
//match self {
|
||||
//Self::Nil => "",
|
||||
//Self::Num(src, start, len) => &src[*start..start+len],
|
||||
//Self::Sym(src, start, len) => &src[*start..start+len],
|
||||
//Self::Key(src, start, len) => &src[*start..start+len],
|
||||
//Self::Exp(src, start, len, 0) => &src[*start..(start+len)],
|
||||
//Self::Exp(src, start, len, d) => panic!(
|
||||
//"unclosed delimiter with depth {d} in:\n{}",
|
||||
//&src[*start..(start+len)]
|
||||
//)
|
||||
//}
|
||||
//}
|
||||
//pub fn to_atom_ref (&'a self) -> Result<Atom<&'a str>, ParseError> {
|
||||
//use Atom::*;
|
||||
//Ok(match self {
|
||||
//Token::Nil => Nil,
|
||||
//Token::Num(_, _, _) => Num(Token::number(self.str())),
|
||||
//Token::Sym(_, _, _) => Sym(self.str().into()),
|
||||
//Token::Key(_, _, _) => Key(self.str().into()),
|
||||
//Token::Exp(_, _, _, _) => Exp(match Atom::read_all_ref(self.str()) {
|
||||
//Ok(exp) => exp,
|
||||
//Err(e) => return Err(e)
|
||||
//}),
|
||||
//})
|
||||
//}
|
||||
//}
|
||||
|
||||
#[cfg(test)] #[test] fn test_edn_token () -> Result<(), Box<dyn std::error::Error>> {
|
||||
use Token::*;
|
||||
assert_eq!(Nil, Token::chomp_one("")?);
|
||||
assert_eq!(Nil, Token::chomp_one(" \n \r \t ")?);
|
||||
assert_eq!(Num("8", 0, 1), Token::chomp_one("8")?);
|
||||
assert_eq!(Num(" 8 ", 3, 1), Token::chomp_one(" 8 ")?);
|
||||
assert_eq!(Sym(":foo", 0, 4), Token::chomp_one(":foo")?);
|
||||
assert_eq!(Sym("@bar", 0, 4), Token::chomp_one("@bar")?);
|
||||
assert_eq!(Key("foo/bar", 0, 7), Token::chomp_one("foo/bar")?);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
use crate::*;
|
||||
|
||||
pub trait TryFromEdn<'a, T>: Sized {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem<impl AsRef<str>>, tail: &'a [EdnItem<impl AsRef<str>>]) ->
|
||||
Option<Self>;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue