diff --git a/dsl/src/dsl.rs b/dsl/src/dsl.rs index 272e135..979585d 100644 --- a/dsl/src/dsl.rs +++ b/dsl/src/dsl.rs @@ -6,105 +6,76 @@ extern crate const_panic; use const_panic::PanicFmt; use std::fmt::Debug; + pub(crate) use std::error::Error; pub(crate) use std::sync::Arc; -pub(crate) use konst::iter::for_each; -pub(crate) use konst::string::{str_from, str_range, char_indices}; + +pub(crate) use konst::string::{str_range, char_indices}; pub(crate) use thiserror::Error; pub(crate) use ::tengri_core::*; + pub(crate) use self::DslError::*; -mod dsl_conv; pub use self::dsl_conv::*; + +mod dsl_conv; +pub use self::dsl_conv::*; + mod dsl_parse; pub(crate) use self::dsl_parse::*; pub mod parse { pub use crate::dsl_parse::*; } -#[cfg(test)] mod dsl_test; + +#[cfg(test)] +mod dsl_test; flex_trait!(Dsl: Debug + Send + Sync + Sized { - fn src (&self) -> DslPerhaps<&str> { + fn src (&self) -> &str { unreachable!("Dsl::src default impl") } }); impl Dsl for Arc { - fn src (&self) -> DslPerhaps<&str> { - Ok(Some(self.as_ref())) - } + fn src (&self) -> &str { self.as_ref() } } impl<'s> Dsl for &'s str { - fn src (&self) -> DslPerhaps<&str> { - Ok(Some(self.as_ref())) - } + fn src (&self) -> &str { self.as_ref() } } impl Dsl for Option { - fn src (&self) -> DslPerhaps<&str> { - Ok(if let Some(dsl) = self { dsl.src()? } else { None }) - } -} -impl Dsl for Result { - fn src (&self) -> DslPerhaps<&str> { - match self { - Ok(dsl) => Ok(dsl.src()?), - Err(e) => Err(*e) - } - } + fn src (&self) -> &str { if let Some(dsl) = self { dsl.src() } else { "" } } } impl DslExp for D {} pub trait DslExp: Dsl { fn exp (&self) -> DslPerhaps<&str> { - Ok(self.src()? - .map(exp_peek) - .transpose()? - .flatten()) + Ok(exp_peek(self.src())?) } fn head (&self) -> DslPerhaps<&str> { - Ok(self.src()? - .map(peek) //|src|Ok(if src.len() > 0 { return peek(&src)) } else { None })) - .transpose()? - .flatten()) + Ok(peek(&self.src()[1..])?) } fn tail (&self) -> DslPerhaps<&str> { - Ok(self.src()? - .map(|src|Ok(if let Some(next) = self.head()? { - let src = &src[src.len().min(1 + next.len() + 1)..]; - if src.len() > 2 { - return peek(&src) - } else { - None - } - } else { - None - })) - .transpose()? - .flatten()) + Ok(if let Some((head_start, head_len)) = seek(&self.src()[1..])? { + peek(&self.src()[(1+head_start+head_len)..])? + } else { + None + }) } } impl DslSym for D {} pub trait DslSym: Dsl { - fn sym (&self) -> DslPerhaps<&str> { - Ok(self.src()?.map(crate::parse::sym_peek).transpose()?.flatten()) - } + fn sym (&self) -> DslPerhaps<&str> { crate::parse::sym_peek(self.src()) } } impl DslKey for D {} pub trait DslKey: Dsl { - fn key (&self) -> DslPerhaps<&str> { - Ok(self.src()?.map(crate::parse::key_peek).transpose()?.flatten()) - } + fn key (&self) -> DslPerhaps<&str> { crate::parse::key_peek(self.src()) } } impl DslText for D {} pub trait DslText: Dsl { - fn text (&self) -> DslPerhaps<&str> { - Ok(self.src()?.map(crate::parse::text_peek).transpose()?.flatten()) - } + fn text (&self) -> DslPerhaps<&str> { crate::parse::text_peek(self.src()) } } impl DslNum for D {} pub trait DslNum: Dsl { - fn num (&self) -> DslPerhaps<&str> { - Ok(self.src()?.map(crate::parse::num_peek).transpose()?.flatten()) - } + fn num (&self) -> DslPerhaps<&str> { crate::parse::num_peek(self.src()) } } /// DSL-specific result type. @@ -123,22 +94,3 @@ pub enum DslError { #[error("parse failed: error #{0}")] Code(u8), #[error("end reached")] End } - -#[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 { - break - } - } -}); diff --git a/dsl/src/dsl_parse.rs b/dsl/src/dsl_parse.rs index b12e804..25eef20 100644 --- a/dsl/src/dsl_parse.rs +++ b/dsl/src/dsl_parse.rs @@ -1,33 +1,32 @@ use crate::*; +macro_rules! iter_chars(($source:expr => |$i:ident, $c:ident|$val:expr)=>{ + while let Some((($i, $c), next)) = char_indices($source).next() { + $source = next.as_str(); $val } }); + macro_rules! def_peek_seek(($peek:ident, $seek:ident, $seek_start:ident, $seek_length:ident)=>{ /// Find a slice corrensponding to a syntax token. pub const fn $peek (source: &str) -> DslPerhaps<&str> { match $seek(source) { Ok(Some((start, length))) => Ok(Some(str_range(source, start, start + length))), Ok(None) => Ok(None), - Err(e) => Err(e) - } - } + Err(e) => Err(e) } } /// Find a start and length corresponding to a syntax token. pub const fn $seek (source: &str) -> DslPerhaps<(usize, usize)> { match $seek_start(source) { - Ok(Some(start)) => match $seek_length(str_from(source, start)) { + Ok(Some(start)) => match $seek_length(str_range(source, start, source.len() - start)) { Ok(Some(length)) => Ok(Some((start, length))), Ok(None) => Ok(None), Err(e) => Err(e), }, Ok(None) => Ok(None), - Err(e) => Err(e) - } - } -}); + Err(e) => Err(e) } } }); def_peek_seek!(exp_peek, exp_seek, exp_seek_start, exp_seek_length); -pub const fn is_exp_start (c: char) -> bool { c == '(' } -pub const fn is_exp_end (c: char) -> bool { c == ')' } +pub const fn is_exp_start (c: char) -> bool { matches!(c, '(') } +pub const fn is_exp_end (c: char) -> bool { matches!(c, ')') } pub const fn exp_seek_start (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_exp_start(c) { + iter_chars!(source => |i, c| if is_exp_start(c) { return Ok(Some(i)) } else if !is_whitespace(c) { return Err(Unexpected(c)) @@ -36,18 +35,18 @@ pub const fn exp_seek_start (mut source: &str) -> DslPerhaps { } pub const fn exp_seek_length (mut source: &str) -> DslPerhaps { let mut depth = 0; - for_each!((i, c) in char_indices(source) => if is_exp_start(c) { + iter_chars!(source => |i, c| if is_exp_start(c) { depth += 1; } else if is_exp_end(c) { if depth == 0 { return Err(Unexpected(c)) } else if depth == 1 { - return Ok(Some(i + 1)) + return Ok(Some(i)) } else { depth -= 1; } }); - Err(Incomplete) + Ok(None) } def_peek_seek!(sym_peek, sym_seek, sym_seek_start, sym_seek_length); @@ -55,7 +54,7 @@ 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 { matches!(c, ' '|'\n'|'\r'|'\t'|')') } pub const fn sym_seek_start (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_sym_start(c) { + iter_chars!(source => |i, c| if is_sym_start(c) { return Ok(Some(i)) } else if !is_whitespace(c) { return Err(Unexpected(c)) @@ -63,20 +62,20 @@ pub const fn sym_seek_start (mut source: &str) -> DslPerhaps { Ok(None) } pub const fn sym_seek_length (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_sym_end(c) { + iter_chars!(source => |i, c| if is_sym_end(c) { return Ok(Some(i)) } else if !is_sym_char(c) { return Err(Unexpected(c)) }); - Ok(Some(source.len())) + Ok(None) } def_peek_seek!(key_peek, 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_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 { matches!(c, ' '|'\n'|'\r'|'\t'|')') } pub const fn key_seek_start (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_key_start(c) { + iter_chars!(source => |i, c| if is_key_start(c) { return Ok(Some(i)) } else if !is_whitespace(c) { return Err(Unexpected(c)) @@ -84,19 +83,19 @@ pub const fn key_seek_start (mut source: &str) -> DslPerhaps { Ok(None) } pub const fn key_seek_length (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_key_end(c) { + iter_chars!(source => |i, c| if is_key_end(c) { return Ok(Some(i)) } else if !is_key_char(c) { return Err(Unexpected(c)) }); - Ok(Some(source.len())) + Ok(None) } def_peek_seek!(text_peek, text_seek, text_seek_start, text_seek_length); pub const fn is_text_start (c: char) -> bool { matches!(c, '"') } pub const fn is_text_end (c: char) -> bool { matches!(c, '"') } pub const fn text_seek_start (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_text_start(c) { + iter_chars!(source => |i, c| if is_text_start(c) { return Ok(Some(i)) } else if !is_whitespace(c) { return Err(Unexpected(c)) @@ -104,13 +103,13 @@ pub const fn text_seek_start (mut source: &str) -> DslPerhaps { Ok(None) } pub const fn text_seek_length (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_text_end(c) { return Ok(Some(i)) }); + iter_chars!(source => |i, c| if is_text_end(c) { return Ok(Some(i)) }); Ok(None) } def_peek_seek!(num_peek, num_seek, num_seek_start, num_seek_length); pub const fn num_seek_start (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_digit(c) { + iter_chars!(source => |i, c| if is_digit(c) { return Ok(Some(i)); } else if !is_whitespace(c) { return Err(Unexpected(c)) @@ -118,7 +117,7 @@ pub const fn num_seek_start (mut source: &str) -> DslPerhaps { Ok(None) } pub const fn num_seek_length (mut source: &str) -> DslPerhaps { - for_each!((i, c) in char_indices(source) => if is_num_end(c) { + iter_chars!(source => |i, c| if is_num_end(c) { return Ok(Some(i)) } else if !is_digit(c) { return Err(Unexpected(c)) @@ -155,7 +154,7 @@ pub const fn peek (mut src: &str) -> DslPerhaps<&str> { _ if let Ok(Some(num)) = num_peek(src) => num, _ if let Ok(Some(text)) = text_peek(src) => text, _ => { - for_each!((i, c) in char_indices(src) => if !is_whitespace(c) { + iter_chars!(src => |_i, c| if !is_whitespace(c) { return Err(Unexpected(c)) }); return Ok(None) @@ -171,7 +170,7 @@ pub const fn seek (mut src: &str) -> DslPerhaps<(usize, usize)> { _ if let Ok(Some(num)) = num_seek(src) => num, _ if let Ok(Some(text)) = text_seek(src) => text, _ => { - for_each!((i, c) in char_indices(src) => if !is_whitespace(c) { + iter_chars!(src => |_i, c| if !is_whitespace(c) { return Err(Unexpected(c)) }); return Ok(None) diff --git a/dsl/src/dsl_test.rs b/dsl/src/dsl_test.rs index 5698d64..e69de29 100644 --- a/dsl/src/dsl_test.rs +++ b/dsl/src/dsl_test.rs @@ -1,40 +0,0 @@ -use crate::*; -#[test] fn test_exp () -> Result<(), DslError> { - assert_eq!(is_whitespace(' '), true); - - assert_eq!(is_key_start(' '), false); - assert_eq!(is_key_start('f'), true); - assert_eq!(key_seek_start("foo"), Ok(Some(0))); - assert_eq!(key_seek_start("foo "), Ok(Some(0))); - assert_eq!(key_seek_start(" foo "), Ok(Some(1))); - assert_eq!(key_seek_length(&" foo "[1..]), Ok(Some(3))); - assert_eq!(key_seek("foo"), Ok(Some((0, 3)))); - assert_eq!(key_peek("foo"), Ok(Some("foo"))); - assert_eq!(key_seek("foo "), Ok(Some((0, 3)))); - assert_eq!(key_peek("foo "), Ok(Some("foo"))); - assert_eq!(key_seek(" foo "), Ok(Some((1, 3)))); - assert_eq!(key_peek(" foo "), Ok(Some("foo"))); - - assert_eq!("foo".key(), Ok(Some("foo"))); - assert_eq!(" foo".key(), Ok(Some("foo"))); - assert_eq!(" foo ".key(), Ok(Some("foo"))); - - assert_eq!(" foo ".head(), Ok(Some("foo"))); - assert_eq!(" foo ".head().head(), Ok(Some("foo"))); - assert_eq!(" foo ".head().tail(), Ok(None)); - assert_eq!(" foo ".tail(), Ok(None)); - assert_eq!(" foo ".tail().head(), Ok(None)); - assert_eq!(" foo ".tail().tail(), Ok(None)); - - assert_eq!(" (foo) ".head(), Ok(Some("(foo)"))); - assert_eq!(" (foo) ".head().head(), Ok(Some("foo"))); - assert_eq!(" (foo) ".head().head().head(), Ok(None)); - assert_eq!(" (foo) ".tail(), Ok(None)); - - assert_eq!(" (foo bar baz) ".head(), Ok(Some("(foo bar baz)"))); - assert_eq!(" (foo bar baz) ".head().head(), Ok(Some("foo"))); - assert_eq!(" (foo bar baz) ".head().tail(), Ok(Some("bar baz"))); - assert_eq!(" (foo bar baz) ".head().tail().head(), Ok(Some("bar"))); - assert_eq!(" (foo bar baz) ".head().tail().tail(), Ok(Some("baz"))); - Ok(()) -}