//#![feature(adt_const_params)] //#![feature(type_alias_impl_trait)] #![feature(if_let_guard)] #![feature(impl_trait_in_fn_trait_return)] #![feature(const_precise_live_drops)] extern crate const_panic; use const_panic::PanicFmt; use std::fmt::Debug; pub(crate) use std::{error::Error, sync::Arc}; pub(crate) use konst::{iter::for_each, string::{str_from, 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::*; #[cfg(test)] mod dsl_test; /// DSL-specific result type. pub type DslResult = Result; /// DSL-specific optional result type. pub type DslPerhaps = Result, DslError>; // Some things that can be DSL source: impl Dsl for String { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } } impl Dsl for Arc { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } } impl<'s> Dsl for &'s str { fn src (&self) -> DslPerhaps<&str> { Ok(Some(self.as_ref())) } } // Designates a string as parsable DSL. flex_trait!(Dsl: Debug + Send + Sync + Sized { fn src (&self) -> DslPerhaps<&str> { unreachable!("Dsl::src default impl") } }); 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)}} } impl DslText for D {} pub trait DslText: Dsl { fn text (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(text_peek_only))} } impl DslSym for D {} pub trait DslSym: Dsl { fn sym (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(sym_peek_only))} } impl DslKey for D {} pub trait DslKey: Dsl { fn key (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(key_peek_only))} } 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))} /// my other car is a cdr :< fn each (&self, mut cb: impl FnMut(&str)->Usually<()>) -> Usually<()> { Ok(if let Some(head) = self.head()? { cb(head)?; if let Some(tail) = self.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 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: 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) } else { Some("trailing non-space") })) })) } 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. pub const fn $peek (source: &str) -> DslPerhaps<&str> { match $seek(source) { Err(e) => Err(e), Ok(None) => Ok(None), Ok(Some((start, length))) => Ok(Some(str_range(source, start, start + length))), } } /// Find a slice corrensponding to a syntax token /// but return an error if it isn't the only thing /// in the source. pub const fn $peek_only (source: &str) -> DslPerhaps<&str> { match $seek(source) { 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) } Ok(Some(str_range(source, start, start + length))) } } } /// Find a start and length corresponding to a syntax token. pub const fn $seek (source: &str) -> DslPerhaps<(usize, usize)> { match $seek_start(source) { Err(e) => Err(e), Ok(None) => Ok(None), Ok(Some(start)) => match $seek_length(str_from(source, start)) { Ok(Some(length)) => Ok(Some((start, length))), Ok(None) => Ok(None), Err(e) => Err(e), }, } } }); def_peek_seek!(exp_peek, exp_peek_only, exp_seek, exp_seek_start, exp_seek_length); pub const fn exp_peek_inner (source: &str) -> DslPerhaps<&str> { match exp_peek(source) { Ok(Some(peeked)) => { let len = peeked.len(); let start = if len > 0 { 1 } else { 0 }; Ok(Some(str_range(source, start, start + len.saturating_sub(2)))) }, e => e } } pub const fn exp_peek_inner_only (source: &str) -> DslPerhaps<&str> { match exp_seek(source) { 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) } let peeked = str_range(source, start, start + length); let len = peeked.len(); let start = if len > 0 { 1 } else { 0 }; Ok(Some(str_range(peeked, start, start + len.saturating_sub(2)))) }, } } pub const fn is_exp_start (c: char) -> bool { c == '(' } 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"))) }); Ok(None) } pub const fn exp_seek_length (source: &str) -> DslPerhaps { let mut depth = 0; for_each!((i, c) in char_indices(source) => if is_exp_start(c) { depth += 1; } else if is_exp_end(c) { if depth == 0 { return Err(Unexpected(c, Some(i), Some("expected expression end"))) } else if depth == 1 { return Ok(Some(i + 1)) } else { depth -= 1; } }); Err(Incomplete) } 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 { is_sym_start(c) || 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 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"))) }); Ok(None) } 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"))) }); 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 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)) }); Ok(None) } 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)) }); Ok(Some(source.len())) } def_peek_seek!(text_peek, text_peek_only, 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 (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)) }); Ok(None) } pub const fn text_seek_length (source: &str) -> DslPerhaps { for_each!((i, c) in char_indices(source) => if is_text_end(c) { return Ok(Some(i)) }); Ok(None) } def_peek_seek!(num_peek, num_peek_only, num_seek, num_seek_start, num_seek_length); 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)) }); Ok(None) } 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)) }); Ok(None) } pub const fn is_digit (c: char) -> bool { matches!(c, '0'..='9') } pub const fn is_num_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') } pub const fn to_number (digits: &str) -> Result { let mut iter = char_indices(digits); let mut value = 0; while let Some(((_, c), next)) = iter.next() { match to_digit(c) { Ok(digit) => value = 10 * value + digit, Err(e) => return Err(e), } iter = next; } Ok(value) } 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"))) }) }