diff --git a/dsl/src/dsl.rs b/dsl/src/dsl.rs index c6c4747..c43e2f1 100644 --- a/dsl/src/dsl.rs +++ b/dsl/src/dsl.rs @@ -13,11 +13,6 @@ pub(crate) use ::tengri_core::*; pub(crate) use self::DslError::*; mod dsl_conv; pub use self::dsl_conv::*; #[cfg(test)] mod dsl_test; -/// DSL-specific result type. -pub type DslResult = Result; -/// DSL-specific optional result type. -pub type DslPerhaps = Result, DslError>; -// Designates a string as parsable DSL. flex_trait!(Dsl: Debug + Send + Sync + Sized { fn src (&self) -> DslPerhaps<&str> { unreachable!("Dsl::src default impl") } }); @@ -29,6 +24,11 @@ impl Dsl for Option { impl Dsl for Result { fn src (&self) -> DslPerhaps<&str> {match self {Ok(dsl) => Ok(dsl.src()?), Err(e) => Err(*e)}} } +impl DslExp for D {} pub trait DslExp: Dsl { + fn exp (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(exp_peek_inner_only))} + fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))} + fn tail (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek_tail(self.head())))} +} impl DslText for D {} pub trait DslText: Dsl { fn text (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(text_peek_only))} } @@ -41,72 +41,44 @@ impl DslKey for D {} pub trait DslKey: Dsl { impl DslNum for D {} pub trait DslNum: Dsl { fn num (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(num_peek_only))} } -impl DslExp for D {} pub trait DslExp: Dsl { - fn exp (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(exp_peek_inner_only))} - fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))} - fn tail (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek_tail))} - fn each (&self, mut cb: impl FnMut(&str)->Usually<()>) -> Usually<()> { - Ok(if let Some(head) = self.head()? { - println!("| HEAD ->\n{head}"); - cb(head)?; - if let Some(tail) = self.tail()? { - println!("| TAIL ->\n{tail}"); - tail.each(cb)?; - } - }) - } -} -pub const fn peek (src: &str) -> DslPerhaps<&str> { - Ok(Some(if let Ok(Some(exp)) = exp_peek(src) { exp } else - if let Ok(Some(sym)) = sym_peek(src) { sym } else - if let Ok(Some(key)) = key_peek(src) { key } else - if let Ok(Some(num)) = num_peek(src) { num } else - if let Ok(Some(text)) = text_peek(src) { text } else - if let Err(e) = no_trailing_non_space(src, 0, Some("peek")) { return Err(e) } - else { return Ok(None) })) -} -pub const fn seek (src: &str) -> DslPerhaps<(usize, usize)> { - Ok(Some(if let Ok(Some(exp)) = exp_seek(src) { exp } else - if let Ok(Some(sym)) = sym_seek(src) { sym } else - if let Ok(Some(key)) = key_seek(src) { key } else - if let Ok(Some(num)) = num_seek(src) { num } else - if let Ok(Some(text)) = text_seek(src) { text } else - if let Err(e) = no_trailing_non_space(src, 0, Some("seek")) { return Err(e) } - else { return Ok(None) })) -} -pub const fn peek_tail (src: &str) -> DslPerhaps<&str> { - match seek(src) { - Err(e) => Err(e), - Ok(None) => Ok(None), - Ok(Some((start, length))) => { - let tail = str_range(src, start + length, src.len()); - for_each!((i, c) in char_indices(tail) => if !is_space(c) { return Ok(Some(tail)) }); - Ok(None) - }, - } -} +/// DSL-specific result type. +pub type DslResult = Result; +/// DSL-specific optional result type. +pub type DslPerhaps = Result, DslError>; /// DSL-specific error codes. #[derive(Error, Debug, Copy, Clone, PartialEq, PanicFmt)] pub enum DslError { #[error("parse failed: not implemented")] Unimplemented, #[error("parse failed: empty")] Empty, #[error("parse failed: incomplete")] Incomplete, - #[error("parse failed: unexpected character '{0}'")] Unexpected(char, Option, Option<&'static str>), + #[error("parse failed: unexpected character '{0}'")] Unexpected(char), #[error("parse failed: error #{0}")] Code(u8), #[error("end reached")] End } -fn ok_flat (x: Option>) -> DslPerhaps { Ok(x.transpose()?.flatten()) } -pub const fn is_space (c: char) -> bool { - matches!(c, ' '|'\n'|'\r'|'\t') -} -pub const fn no_trailing_non_space (source: &str, offset: usize, context: Option<&'static str>) -> DslResult<()> { - Ok(for_each!((i, c) in char_indices(str_range(source, offset, source.len())) => if !is_space(c) { - return Err(Unexpected(c, Some(offset + i), if let Some(context) = context { - Some(context) +#[macro_export] macro_rules! dsl_for_each (($dsl:expr => |$next:ident|$body:expr)=>{ + let mut dsl: Arc = $dsl.src().into(); + let mut $next: Option> = dsl.next()?.map(Into::into); + let mut rest: Option> = dsl.rest()?.map(Into::into); + loop { + if let Some($next) = $next { $body } else { break }; + if let Some(next) = rest { + $next = next.next()?.map(Into::into); + rest = next.rest()?.map(Into::into); } else { - Some("trailing non-space") - })) - })) + break + } + } +}); +fn ok_flat (x: Option>) -> DslPerhaps { Ok(x.transpose()?.flatten()) } +fn peek_tail <'a> (head: DslPerhaps<&'a str>) -> impl Fn(&'a str)->DslPerhaps<&'a str> { + move|src|match head { + Ok(Some(next)) => { + let src = &src[src.len().min(1 + next.len())..]; + for c in src.chars() { if !is_whitespace(c) { return Ok(Some(src)) } } + Ok(None) + }, + e => e + } } macro_rules! def_peek_seek(($peek:ident, $peek_only:ident, $seek:ident, $seek_start:ident, $seek_length:ident)=>{ /// Find a slice corrensponding to a syntax token. @@ -125,7 +97,7 @@ macro_rules! def_peek_seek(($peek:ident, $peek_only:ident, $seek:ident, $seek_st Err(e) => Err(e), Ok(None) => Ok(None), Ok(Some((start, length))) => { - if let Err(e) = no_trailing_non_space(source, start + length, Some("peek_only")) { return Err(e) } + if let Err(e) = no_trailing_non_whitespace(source, start + length) { return Err(e) } Ok(Some(str_range(source, start, start + length))) } } @@ -159,7 +131,7 @@ pub const fn exp_peek_inner_only (source: &str) -> DslPerhaps<&str> { Err(e) => Err(e), Ok(None) => Ok(None), Ok(Some((start, length))) => { - if let Err(e) = no_trailing_non_space(source, start + length, Some("exp_peek_inner_only")) { return Err(e) } + if let Err(e) = no_trailing_non_whitespace(source, start + length) { return Err(e) } let peeked = str_range(source, start, start + length); let len = peeked.len(); let start = if len > 0 { 1 } else { 0 }; @@ -172,8 +144,8 @@ pub const fn is_exp_end (c: char) -> bool { c == ')' } pub const fn exp_seek_start (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_exp_start(c) { return Ok(Some(i)) - } else if !is_space(c) { - return Err(Unexpected(c, Some(i), Some("expected expression start"))) + } else if !is_whitespace(c) { + return Err(Unexpected(c)) }); Ok(None) } @@ -183,7 +155,7 @@ pub const fn exp_seek_length (source: &str) -> DslPerhaps { depth += 1; } else if is_exp_end(c) { if depth == 0 { - return Err(Unexpected(c, Some(i), Some("expected expression end"))) + return Err(Unexpected(c)) } else if depth == 1 { return Ok(Some(i + 1)) } else { @@ -194,13 +166,13 @@ pub const fn exp_seek_length (source: &str) -> DslPerhaps { } def_peek_seek!(sym_peek, sym_peek_only, sym_seek, sym_seek_start, sym_seek_length); pub const fn is_sym_start (c: char) -> bool { matches!(c, ':'|'@') } -pub const fn is_sym_char (c: char) -> bool { matches!(c, ':'|'a'..='z'|'A'..='Z'|'0'..='9'|'-'|'/') } -pub const fn is_sym_end (c: char) -> bool { is_space(c) || matches!(c, ')') } +pub const fn is_sym_char (c: char) -> bool { matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-') } +pub const fn is_sym_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') } pub const fn sym_seek_start (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_sym_start(c) { return Ok(Some(i)) - } else if !is_space(c) { - return Err(Unexpected(c, Some(i), Some("sym_seek_start"))) + } else if !is_whitespace(c) { + return Err(Unexpected(c)) }); Ok(None) } @@ -208,19 +180,19 @@ pub const fn sym_seek_length (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_sym_end(c) { return Ok(Some(i)) } else if !is_sym_char(c) { - return Err(Unexpected(c, Some(i), Some("sym_seek_length"))) + return Err(Unexpected(c)) }); Ok(Some(source.len())) } def_peek_seek!(key_peek, key_peek_only, key_seek, key_seek_start, key_seek_length); pub const fn is_key_start (c: char) -> bool { matches!(c, '/'|('a'..='z')) } pub const fn is_key_char (c: char) -> bool { matches!(c, 'a'..='z'|'0'..='9'|'-'|'/') } -pub const fn is_key_end (c: char) -> bool { is_space(c) || matches!(c, ')') } +pub const fn is_key_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') } pub const fn key_seek_start (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_key_start(c) { return Ok(Some(i)) - } else if !is_space(c) { - return Err(Unexpected(c, Some(i), None)) + } else if !is_whitespace(c) { + return Err(Unexpected(c)) }); Ok(None) } @@ -228,7 +200,7 @@ pub const fn key_seek_length (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_key_end(c) { return Ok(Some(i)) } else if !is_key_char(c) { - return Err(Unexpected(c, Some(i), None)) + return Err(Unexpected(c)) }); Ok(Some(source.len())) } @@ -238,8 +210,8 @@ pub const fn is_text_end (c: char) -> bool { matches!(c, '"') } pub const fn text_seek_start (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_text_start(c) { return Ok(Some(i)) - } else if !is_space(c) { - return Err(Unexpected(c, Some(i), None)) + } else if !is_whitespace(c) { + return Err(Unexpected(c)) }); Ok(None) } @@ -251,8 +223,8 @@ def_peek_seek!(num_peek, num_peek_only, num_seek, num_seek_start, num_seek_lengt pub const fn num_seek_start (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_digit(c) { return Ok(Some(i)); - } else if !is_space(c) { - return Err(Unexpected(c, Some(i), None)) + } else if !is_whitespace(c) { + return Err(Unexpected(c)) }); Ok(None) } @@ -260,7 +232,7 @@ pub const fn num_seek_length (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_num_end(c) { return Ok(Some(i)) } else if !is_digit(c) { - return Err(Unexpected(c, Some(i), None)) + return Err(Unexpected(c)) }); Ok(None) } @@ -282,6 +254,46 @@ pub const fn to_digit (c: char) -> Result { 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, None, Some("parse digit"))) + _ => return Err(Unexpected(c)) }) } +pub const fn peek (src: &str) -> DslPerhaps<&str> { + Ok(Some(match () { + _ if let Ok(Some(exp)) = exp_peek(src) => exp, + _ if let Ok(Some(sym)) = sym_peek(src) => sym, + _ if let Ok(Some(key)) = key_peek(src) => key, + _ if let Ok(Some(num)) = num_peek(src) => num, + _ if let Ok(Some(text)) = text_peek(src) => text, + _ => { + for_each!((_, c) in char_indices(src) => if !is_whitespace(c) { + return Err(Unexpected(c)) + }); + return Ok(None) + } + })) +} +pub const fn seek (src: &str) -> DslPerhaps<(usize, usize)> { + Ok(Some(match () { + _ if let Ok(Some(exp)) = exp_seek(src) => exp, + _ if let Ok(Some(sym)) = sym_seek(src) => sym, + _ if let Ok(Some(key)) = key_seek(src) => key, + _ if let Ok(Some(num)) = num_seek(src) => num, + _ if let Ok(Some(text)) = text_seek(src) => text, + _ => { + for_each!((_, c) in char_indices(src) => if !is_whitespace(c) { + return Err(Unexpected(c)) + }); + return Ok(None) + } + })) +} +pub const fn is_whitespace (c: char) -> bool { + matches!(c, ' '|'\n'|'\r'|'\t') +} +pub const fn no_trailing_non_whitespace (source: &str, offset: usize) -> DslResult<()> { + let tail = str_range(source, offset, source.len()); + for_each!((_, c) in char_indices(tail) => if !is_whitespace(c) { + return Err(Unexpected(c)) + }); + Ok(()) +} diff --git a/dsl/src/dsl_test.rs b/dsl/src/dsl_test.rs index 253b4c9..05f0308 100644 --- a/dsl/src/dsl_test.rs +++ b/dsl/src/dsl_test.rs @@ -21,8 +21,6 @@ macro_rules!is_err(($exp:expr)=>{assert!($exp.is_err())}; check("(a b c) d e f", Err(e1), Err(e3), Ok(Some("(a b c)")), Ok(Some("d e f"))); check("a (b c d) e f", Err(e1), Err(e0), Ok(Some("a")), Ok(Some("(b c d) e f"))); - is_some!(sym_peek("\n :view/transport"), ":view/transport"); - assert!(is_whitespace(' ')); assert!(!is_key_start(' ')); assert!(is_key_start('f')); diff --git a/input/src/input.rs b/input/src/input.rs index 48e692f..7b451ee 100644 --- a/input/src/input.rs +++ b/input/src/input.rs @@ -1,4 +1,5 @@ use crate::*; + /// Event source pub trait Input: Sized { /// Type of input event @@ -12,11 +13,13 @@ pub trait Input: Sized { /// Mark component as done fn done (&self); } + flex_trait_mut!(Handle { fn handle (&mut self, _input: &E) -> Perhaps { Ok(None) } }); + pub trait Command: Send + Sync + Sized { fn execute (self, state: &mut S) -> Perhaps; fn delegate (self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps @@ -25,6 +28,7 @@ pub trait Command: Send + Sync + Sized { Ok(self.execute(state)?.map(wrap)) } } + impl> Command for Option { fn execute (self, _: &mut S) -> Perhaps { Ok(None)