mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
core, input, output, dsl: factor away 'flexi' traits
This commit is contained in:
parent
8cbd7dd8e8
commit
9f7d0efda5
11 changed files with 355 additions and 339 deletions
|
|
@ -6,48 +6,78 @@
|
|||
extern crate const_panic;
|
||||
use const_panic::PanicFmt;
|
||||
use std::fmt::Debug;
|
||||
pub(crate) use ::tengri_core::*;
|
||||
|
||||
pub(crate) use std::error::Error;
|
||||
pub(crate) use std::sync::Arc;
|
||||
|
||||
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_types; pub use self::dsl_types::*;
|
||||
#[cfg(test)] mod dsl_test;
|
||||
mod dsl_conv;
|
||||
pub use self::dsl_conv::*;
|
||||
|
||||
pub trait Dsl: Debug + Send + Sync + Sized {
|
||||
fn src (&self) -> &str;
|
||||
}
|
||||
mod dsl_parse;
|
||||
pub(crate) use self::dsl_parse::*;
|
||||
pub mod parse { pub use crate::dsl_parse::*; }
|
||||
|
||||
impl<'s> Dsl for &'s str {
|
||||
fn src (&self) -> &str { self }
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod dsl_test;
|
||||
|
||||
impl Dsl for String {
|
||||
fn src (&self) -> &str { self.as_str() }
|
||||
}
|
||||
|
||||
impl Dsl for std::sync::Arc<str> {
|
||||
flex_trait!(Dsl: Debug + Send + Sync + Sized {
|
||||
fn src (&self) -> &str {
|
||||
unreachable!("Dsl::src default impl")
|
||||
}
|
||||
});
|
||||
impl Dsl for Arc<str> {
|
||||
fn src (&self) -> &str { self.as_ref() }
|
||||
}
|
||||
|
||||
impl<D: Dsl> Dsl for &D {
|
||||
fn src (&self) -> &str { (*self).src() }
|
||||
impl<'s> Dsl for &'s str {
|
||||
fn src (&self) -> &str { self.as_ref() }
|
||||
}
|
||||
|
||||
impl<D: Dsl> Dsl for &mut D {
|
||||
fn src (&self) -> &str { (**self).src() }
|
||||
}
|
||||
|
||||
impl<D: Dsl> Dsl for std::sync::Arc<D> {
|
||||
fn src (&self) -> &str { (*self).src() }
|
||||
}
|
||||
|
||||
impl<D: Dsl> Dsl for Option<D> {
|
||||
fn src (&self) -> &str { if let Some(dsl) = self { dsl.src() } else { "" } }
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslExp for D {}
|
||||
pub trait DslExp: Dsl {
|
||||
fn exp (&self) -> DslPerhaps<&str> {
|
||||
Ok(exp_peek(self.src())?)
|
||||
}
|
||||
fn head (&self) -> DslPerhaps<&str> {
|
||||
Ok(peek(&self.src()[1..])?)
|
||||
}
|
||||
fn tail (&self) -> DslPerhaps<&str> {
|
||||
Ok(if let Some((head_start, head_len)) = seek(&self.src()[1..])? {
|
||||
peek(&self.src()[(1+head_start+head_len)..])?
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslSym for D {}
|
||||
pub trait DslSym: Dsl {
|
||||
fn sym (&self) -> DslPerhaps<&str> { crate::parse::sym_peek(self.src()) }
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslKey for D {}
|
||||
pub trait DslKey: Dsl {
|
||||
fn key (&self) -> DslPerhaps<&str> { crate::parse::key_peek(self.src()) }
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslText for D {}
|
||||
pub trait DslText: Dsl {
|
||||
fn text (&self) -> DslPerhaps<&str> { crate::parse::text_peek(self.src()) }
|
||||
}
|
||||
|
||||
impl<D: Dsl> DslNum for D {}
|
||||
pub trait DslNum: Dsl {
|
||||
fn num (&self) -> DslPerhaps<&str> { crate::parse::num_peek(self.src()) }
|
||||
}
|
||||
|
||||
/// DSL-specific result type.
|
||||
pub type DslResult<T> = Result<T, DslError>;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,18 @@
|
|||
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.
|
||||
const fn $peek (source: &str) -> DslPerhaps<&str> {
|
||||
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) } }
|
||||
/// Find a start and length corresponding to a syntax token.
|
||||
const fn $seek (source: &str) -> DslPerhaps<(usize, usize)> {
|
||||
pub const fn $seek (source: &str) -> DslPerhaps<(usize, usize)> {
|
||||
match $seek_start(source) {
|
||||
Ok(Some(start)) => match $seek_length(str_range(source, start, source.len() - start)) {
|
||||
Ok(Some(length)) => Ok(Some((start, length))),
|
||||
|
|
@ -20,6 +22,130 @@ macro_rules! def_peek_seek(($peek:ident, $seek:ident, $seek_start:ident, $seek_l
|
|||
Ok(None) => Ok(None),
|
||||
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 { matches!(c, '(') }
|
||||
pub const fn is_exp_end (c: char) -> bool { matches!(c, ')') }
|
||||
pub const fn exp_seek_start (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_exp_start(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn exp_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
let mut depth = 0;
|
||||
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))
|
||||
} else {
|
||||
depth -= 1;
|
||||
}
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
def_peek_seek!(sym_peek, 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 { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
|
||||
pub const fn sym_seek_start (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_sym_start(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn sym_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
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(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_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<usize> {
|
||||
iter_chars!(source => |i, c| if is_key_start(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn key_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
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(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<usize> {
|
||||
iter_chars!(source => |i, c| if is_text_start(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn text_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
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<usize> {
|
||||
iter_chars!(source => |i, c| if is_digit(c) {
|
||||
return Ok(Some(i));
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
pub const fn num_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_num_end(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_digit(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
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 <D: Dsl> (digits: &str) -> Result<usize, DslError> {
|
||||
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<usize, DslError> {
|
||||
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 peek (mut src: &str) -> DslPerhaps<&str> {
|
||||
Ok(Some(match () {
|
||||
_ if let Ok(Some(exp)) = exp_peek(src) => exp,
|
||||
|
|
@ -52,152 +178,6 @@ pub const fn seek (mut src: &str) -> DslPerhaps<(usize, usize)> {
|
|||
}))
|
||||
}
|
||||
|
||||
const fn is_whitespace (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t') }
|
||||
pub trait DslExp<'s>: Dsl + From<&'s str> {
|
||||
fn exp (&'s self) -> DslPerhaps<Self> {
|
||||
Ok(exp_peek(self.src())?.map(Into::into))
|
||||
}
|
||||
fn head (&'s self) -> DslPerhaps<Self> {
|
||||
Ok(peek(&self.src()[1..])?.map(Into::into))
|
||||
}
|
||||
fn tail (&'s self) -> DslPerhaps<Self> {
|
||||
Ok(if let Some((head_start, head_len)) = seek(&self.src()[1..])? {
|
||||
peek(&self.src()[(1+head_start+head_len)..])?.map(Into::into)
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
}
|
||||
//impl<'s, D: DslExp<'s>> DslExp<'s> for Option<D> {}
|
||||
impl<'s, D: Dsl + From<&'s str>> DslExp<'s> for D {}
|
||||
def_peek_seek!(exp_peek, exp_seek, exp_seek_start, exp_seek_length);
|
||||
const fn is_exp_start (c: char) -> bool { matches!(c, '(') }
|
||||
const fn is_exp_end (c: char) -> bool { matches!(c, ')') }
|
||||
const fn exp_seek_start (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_exp_start(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
const fn exp_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
let mut depth = 0;
|
||||
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))
|
||||
} else {
|
||||
depth -= 1;
|
||||
}
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub trait DslSym: Dsl { fn sym (&self) -> DslPerhaps<&str> { sym_peek(self.src()) } }
|
||||
impl<D: Dsl> DslSym for D {}
|
||||
def_peek_seek!(sym_peek, sym_seek, sym_seek_start, sym_seek_length);
|
||||
const fn is_sym_start (c: char) -> bool { matches!(c, ':'|'@') }
|
||||
const fn is_sym_char (c: char) -> bool { matches!(c, 'a'..='z'|'A'..='Z'|'0'..='9'|'-') }
|
||||
const fn is_sym_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
|
||||
const fn sym_seek_start (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_sym_start(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
const fn sym_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
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(None)
|
||||
}
|
||||
|
||||
pub trait DslKey: Dsl { fn key (&self) -> DslPerhaps<&str> { key_peek(self.src()) } }
|
||||
impl<D: Dsl> DslKey for D {}
|
||||
def_peek_seek!(key_peek, key_seek, key_seek_start, key_seek_length);
|
||||
const fn is_key_start (c: char) -> bool { matches!(c, '/'|'a'..='z') }
|
||||
const fn is_key_char (c: char) -> bool { matches!(c, 'a'..='z'|'0'..='9'|'-'|'/') }
|
||||
const fn is_key_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
|
||||
const fn key_seek_start (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_key_start(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
const fn key_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
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(None)
|
||||
}
|
||||
|
||||
pub trait DslText: Dsl { fn text (&self) -> DslPerhaps<&str> { text_peek(self.src()) } }
|
||||
impl<D: Dsl> DslText for D {}
|
||||
def_peek_seek!(text_peek, text_seek, text_seek_start, text_seek_length);
|
||||
const fn is_text_start (c: char) -> bool { matches!(c, '"') }
|
||||
const fn is_text_end (c: char) -> bool { matches!(c, '"') }
|
||||
const fn text_seek_start (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_text_start(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
const fn text_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_text_end(c) { return Ok(Some(i)) });
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub trait DslNum: Dsl { fn num (&self) -> DslPerhaps<&str> { num_peek(self.src()) } }
|
||||
impl<D: Dsl> DslNum for D {}
|
||||
def_peek_seek!(num_peek, num_seek, num_seek_start, num_seek_length);
|
||||
const fn num_seek_start (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_digit(c) {
|
||||
return Ok(Some(i));
|
||||
} else if !is_whitespace(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
const fn num_seek_length (mut source: &str) -> DslPerhaps<usize> {
|
||||
iter_chars!(source => |i, c| if is_num_end(c) {
|
||||
return Ok(Some(i))
|
||||
} else if !is_digit(c) {
|
||||
return Err(Unexpected(c))
|
||||
});
|
||||
Ok(None)
|
||||
}
|
||||
const fn is_digit (c: char) -> bool { matches!(c, '0'..='9') }
|
||||
const fn is_num_end (c: char) -> bool { matches!(c, ' '|'\n'|'\r'|'\t'|')') }
|
||||
pub const fn to_number <D: Dsl> (digits: &str) -> Result<usize, DslError> {
|
||||
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<usize, DslError> {
|
||||
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 is_whitespace (c: char) -> bool {
|
||||
matches!(c, ' '|'\n'|'\r'|'\t')
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue