test top level expr parsing

This commit is contained in:
🪞👃🪞 2025-01-18 22:17:10 +01:00
parent d14d67172c
commit 9756862091
7 changed files with 129 additions and 82 deletions

View file

@ -20,48 +20,44 @@ pub const fn peek_src <'a> (source: &'a str) -> Option<Token<'a>> {
Err(_) => return Some(token), Err(_) => return Some(token),
Nil => match c { Nil => match c {
' '|'\n'|'\r'|'\t' => token.grow(), ' '|'\n'|'\r'|'\t' => token.grow(),
'(' => Token { '(' => Token { source, start, length: 1,
source, start, length: 1,
value: Value::Exp(1, TokenIter(str_range(source, start, start + 1))), value: Value::Exp(1, TokenIter(str_range(source, start, start + 1))),
}, },
'0'..='9' => Token { ':'|'@' => Token { source, start, length: 1,
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)),
},
'0'..='9' => Token { source, start, length: 1,
value: match to_digit(c) { value: match to_digit(c) {
Ok(c) => Value::Num(c), Ok(c) => Value::Num(c),
Result::Err(e) => Value::Err(e) 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)) _ => token.error(Unexpected(c))
}, },
Num(_) => match c { Num(n) => match c {
'0'..='9' => token.grow(), '0'..='9' => token.grow_num(n, c),
' '|'\n'|'\r'|'\t' => return Some(token), ' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c)) _ => token.error(Unexpected(c))
}, },
Sym(_) => match c { Sym(_) => match c {
'a'..='z'|'0'..='9'|'-' => token.grow(), 'a'..='z'|'0'..='9'|'-' => token.grow_sym(),
' '|'\n'|'\r'|'\t' => return Some(token), ' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c)) _ => token.error(Unexpected(c))
}, },
Key(_) => match c { Key(_) => match c {
'a'..='z'|'0'..='9'|'-'|'/' => token.grow(), 'a'..='z'|'0'..='9'|'-'|'/' => token.grow_key(),
' '|'\n'|'\r'|'\t' => return Some(token), ' '|'\n'|'\r'|'\t' => return Some(token),
_ => token.error(Unexpected(c)) _ => token.error(Unexpected(c))
}, },
Exp(depth, _) => match depth { Exp(depth, _) => match depth {
0 => return Some(token), 0 => return Some(token.grow_exp()),
_ => match c { _ => match c {
')' => token.grow_out(), ')' => token.grow_out(),
'(' => token.grow_in(), '(' => token.grow_in(),
_ => token.grow(), _ => token.grow_exp(),
} }
}, },
}); });

View file

@ -43,44 +43,65 @@ pub(crate) use std::fmt::{Debug, Display, Formatter, Result as FormatResult, Err
} }
} }
#[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> { #[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
use Token::*; let source = ":f00";
assert_eq!(Nil, Token::chomp_one("")?); let mut token = Token { source, start: 0, length: 1, value: Sym(":") };
assert_eq!(Nil, Token::chomp_one(" \n \r \t ")?); token = token.grow_sym();
assert_eq!(Num("8", 0, 1), Token::chomp_one("8")?); assert_eq!(token, Token { source, start: 0, length: 2, value: Sym(":f") });
assert_eq!(Num(" 8 ", 3, 1), Token::chomp_one(" 8 ")?); token = token.grow_sym();
assert_eq!(Sym(":foo", 0, 4), Token::chomp_one(":foo")?); assert_eq!(token, Token { source, start: 0, length: 3, value: Sym(":f0") });
assert_eq!(Sym("@bar", 0, 4), Token::chomp_one("@bar")?); token = token.grow_sym();
assert_eq!(Key("foo/bar", 0, 7), Token::chomp_one("foo/bar")?); assert_eq!(token, Token { source, start: 0, length: 4, value: Sym(":f00") });
let src = "";
assert_eq!(None, TokenIter(src).next());
let src = " \n \r \t ";
assert_eq!(None, TokenIter(src).next());
let src = "7";
assert_eq!(Num(7), TokenIter(src).next().unwrap().0.value);
let src = " 100 ";
assert_eq!(Num(100), TokenIter(src).next().unwrap().0.value);
let src = " 9a ";
assert_eq!(Err(Unexpected('a')), TokenIter(src).next().unwrap().0.value);
let src = " :123foo ";
assert_eq!(Sym(":123foo"), TokenIter(src).next().unwrap().0.value);
let src = " \r\r\r\n\n\n@bar456\t\t\t\t\t\t";
assert_eq!(Sym("@bar456"), TokenIter(src).next().unwrap().0.value);
let src = "foo123";
assert_eq!(Key("foo123"), TokenIter(src).next().unwrap().0.value);
let src = "foo/bar";
assert_eq!(Key("foo/bar"), TokenIter(src).next().unwrap().0.value);
Ok(()) Ok(())
} }
#[cfg(test)] #[test] fn test_lang () -> Result<(), ParseError> { #[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> {
use Atom::*; let src = include_str!("../../tek/src/view_arranger.edn");
assert_eq!(Atom::read_all("")?, let mut view = TokenIter(src);
vec![]); assert_eq!(view.0, src);
assert_eq!(Atom::read_all(" ")?, let mut expr = view.peek();
vec![]); assert_eq!(view.0, src);
assert_eq!(Atom::read_all("1234")?, assert_eq!(expr, Some(Token {
vec![Num(1234)]); source: src,
assert_eq!(Atom::read_all("1234 5 67")?, start: 0,
vec![Num(1234), Num(5), Num(67)]); length: src.len(),
assert_eq!(Atom::read_all("foo/bar")?, value: Exp(0, TokenIter(src))
vec![Key("foo/bar".into())]); }));
assert_eq!(Atom::read_all(":symbol")?, //panic!("{view:?}");
vec![Sym(":symbol".into())]); //panic!("{:#?}", expr);
assert_eq!(Atom::read_all(" foo/bar :baz 456")?, //for example in [
vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)]); //include_str!("../../tui/examples/edn01.edn"),
assert_eq!(Atom::read_all(" (foo/bar :baz 456) ")?, //include_str!("../../tui/examples/edn02.edn"),
vec![Exp(vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)])]); //] {
Ok(()) ////let items = Atom::read_all(example)?;
} ////panic!("{layout:?}");
#[cfg(test)] #[test] fn test_lang_examples () -> Result<(), ParseError> { ////let content = <dyn ViewContext<::tek_engine::tui::Tui>>::from(&layout);
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 ViewContext<::tek_engine::tui::Tui>>::from(&layout);
}
Ok(()) Ok(())
} }

View file

@ -4,7 +4,7 @@ use self::Value::*;
pub source: &'a str, pub source: &'a str,
pub start: usize, pub start: usize,
pub length: usize, pub length: usize,
pub value: Value<'a>, pub value: Value<'a>,
} }
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum Value<'a> { #[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum Value<'a> {
#[default] Nil, #[default] Nil,
@ -37,25 +37,51 @@ impl<'a> Token<'a> {
pub const fn grow (self) -> Self { pub const fn grow (self) -> Self {
Self { length: self.length + 1, ..self } Self { length: self.length + 1, ..self }
} }
pub const fn grow_in (self) -> Self { pub const fn grow_num (self, m: usize, c: char) -> Self {
match self.value { match to_digit(c) {
Value::Exp(depth, source) => Self { Ok(n) => Self { value: Num(10*m+n), ..self.grow() },
value: Value::Exp(depth + 1, TokenIter(self.grow().slice_source(source.0))), Result::Err(e) => Self { value: Err(e), ..self.grow() },
..self.grow()
},
_ => self.grow()
} }
} }
pub const fn grow_key (self) -> Self {
let mut token = self.grow();
token.value = Key(token.slice_source(self.source));
token
}
pub const fn grow_sym (self) -> Self {
let mut token = self.grow();
token.value = Sym(token.slice_source(self.source));
token
}
pub const fn grow_exp (self) -> Self {
let mut token = self.grow();
if let Exp(depth, _) = token.value {
token.value = Exp(depth, TokenIter(token.slice_source(self.source)));
} else {
unreachable!()
}
token
}
pub const fn grow_in (self) -> Self {
let mut token = self.grow_exp();
if let Value::Exp(depth, source) = token.value {
token.value = Value::Exp(depth + 1, source)
} else {
unreachable!()
}
token
}
pub const fn grow_out (self) -> Self { pub const fn grow_out (self) -> Self {
match self.value { let mut token = self.grow_exp();
Value::Exp(depth, source) => match depth { if let Value::Exp(depth, source) = token.value {
0 => self.error(Unexpected(')')), if depth > 0 {
d => Self { token.value = Value::Exp(depth - 1, source)
value: Value::Exp(d - 1, TokenIter(self.grow().slice_source(source.0))), } else {
..self.grow() return self.error(Unexpected(')'))
} }
}, } else {
_ => self.grow() unreachable!()
} }
token
} }
} }

View file

@ -32,7 +32,9 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
} }
} }
try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| { try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key(key), .. }, _)) = iter.next() { panic!("bsp:\n{iter:#?}\n{:#?}", iter.peek());
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
let iter = iter.clone().next().unwrap().1;
match key { match key {
"bsp/n" => return Some(Self::n( "bsp/n" => return Some(Self::n(
state.get_content(&iter.next()?.0.value).expect("no south"), state.get_content(&iter.next()?.0.value).expect("no south"),

View file

@ -6,7 +6,7 @@ impl<A> When<A> { pub fn new (c: bool, a: A) -> Self { Self(c, a) } }
pub struct Either<A, B>(pub bool, pub A, pub B); pub struct Either<A, B>(pub bool, pub A, pub B);
impl<A, B> Either<A, B> { pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } } impl<A, B> Either<A, B> { pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } }
try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| { try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key("when"), .. }, _)) = iter.next() { if let Some(Token { value: Value::Key("when"), .. }) = iter.peek() {
let iter = iter.clone(); let iter = iter.clone();
let condition = iter.next(); let condition = iter.next();
if let Some((ref condition, _)) = condition { if let Some((ref condition, _)) = condition {

View file

@ -9,9 +9,12 @@ use crate::*;
impl<'a> ViewContext<'a, $Output> for $State { impl<'a> ViewContext<'a, $Output> for $State {
fn get_content_custom (&'a $self, value: &Value<'a>) -> Option<RenderBox<'a, $Output>> { fn get_content_custom (&'a $self, value: &Value<'a>) -> Option<RenderBox<'a, $Output>> {
if let Value::Sym(s) = value { if let Value::Sym(s) = value {
match *s { $($sym => Some($body),)* _ => None } match *s {
$($sym => Some($body),)*
_ => None
}
} else { } else {
panic!("expected symbol") panic!("expected content, got: {value:?}")
} }
} }
} }
@ -72,12 +75,10 @@ pub trait ViewContext<'a, E: Output + 'a>: Send + Sync
} }
} }
#[macro_export] macro_rules! try_from_expr { #[macro_export] macro_rules! try_from_expr {
(<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $atoms:ident|$body:expr) => { (<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $iter:ident|$body:expr) => {
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<$l, T> for $Struct { impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<$l, T> for $Struct {
fn try_from_atom ($state: &$l T, atom: Value<$l>) -> Option<Self> { fn try_from_expr ($state: &$l T, $iter: TokenIter<'a>) -> Option<Self> {
if let Value::Exp(0, $atoms) = atom { $body;
$body;
}
None None
} }
} }

View file

@ -203,7 +203,8 @@ provide_num!(u16: |self: Tek| {
":samples-y" => if self.is_editing() { 1 } else { 0 }, ":samples-y" => if self.is_editing() { 1 } else { 0 },
":pool-w" => if self.is_editing() { 5 } else { ":pool-w" => if self.is_editing() { 5 } else {
let w = self.size.w(); let w = self.size.w();
if w > 60 { 20 } else if w > 40 { 15 } else { 10 } } if w > 60 { 20 } else if w > 40 { 15 } else { 10 }
}
}); });
impl Tek { impl Tek {
fn new_clock ( fn new_clock (