mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-08 12:46:44 +01:00
parent
ca4c558eab
commit
e72225f83c
11 changed files with 421 additions and 574 deletions
371
dsl/src/lib.rs
371
dsl/src/lib.rs
|
|
@ -1,17 +1,376 @@
|
|||
#![feature(adt_const_params)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_fn_trait_return)]
|
||||
#![feature(const_precise_live_drops)]
|
||||
extern crate const_panic;
|
||||
use const_panic::{concat_panic, PanicFmt};
|
||||
pub(crate) use ::tengri_core::*;
|
||||
pub(crate) use std::error::Error;
|
||||
pub(crate) use std::fmt::Debug;
|
||||
pub(crate) use std::sync::Arc;
|
||||
pub(crate) use std::collections::VecDeque;
|
||||
pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
|
||||
pub(crate) use konst::string::{split_at, str_range, char_indices};
|
||||
pub(crate) use thiserror::Error;
|
||||
pub(crate) use self::DslErr::*;
|
||||
|
||||
mod dsl; pub use self::dsl::*;
|
||||
mod ast; pub use self::ast::*;
|
||||
mod cst; pub use self::cst::*;
|
||||
|
||||
pub(crate) use self::DslError::*;
|
||||
#[cfg(test)] mod test;
|
||||
|
||||
pub type DslUsually<T> = Result<T, DslError>;
|
||||
pub type DslPerhaps<T> = Result<Option<T>, DslError>;
|
||||
|
||||
/// Pronounced dizzle.
|
||||
pub trait Dsl: Clone + Debug {
|
||||
/// The string representation for a dizzle.
|
||||
type Str: DslStr;
|
||||
/// The expression representation for a dizzle.
|
||||
type Exp: DslExp;
|
||||
/// Return a token iterator for this dizzle.
|
||||
fn dsl (&self) -> DslUsually<&Val<Self>>;
|
||||
}
|
||||
|
||||
/// Enumeration of values representable by a DSL [Token]s.
|
||||
/// Generic over string and expression storage.
|
||||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
pub enum Val<D: Dsl> {
|
||||
#[default]
|
||||
Nil,
|
||||
/// Unsigned integer literal
|
||||
Num(usize),
|
||||
/// Tokens that start with `:`
|
||||
Sym(D::Str),
|
||||
/// Tokens that don't start with `:`
|
||||
Key(D::Str),
|
||||
/// Quoted string literals
|
||||
Str(D::Str),
|
||||
/// Expressions.
|
||||
Exp(
|
||||
/// Expression depth checksum. Must be 0, otherwise you have an unclosed delimiter.
|
||||
usize,
|
||||
/// Expression content.
|
||||
D::Exp
|
||||
),
|
||||
Error(DslError),
|
||||
}
|
||||
|
||||
impl<Str: Copy, Exp: Copy, D: Dsl<Str=Str,Exp=Exp>> Copy for Val<D> {}
|
||||
|
||||
impl<D: Dsl> Val<D> {
|
||||
pub fn convert <T: Dsl> (&self) -> Val<T> where
|
||||
B::Str: for<'a> From<&'a D::Str>,
|
||||
B::Exp: for<'a> From<&'a D::Exp>
|
||||
{
|
||||
match self { Val::Nil => Val::Nil,
|
||||
Val::Num(u) => Val::Num(*u),
|
||||
Val::Sym(s) => Val::Sym(s.into()),
|
||||
Val::Key(s) => Val::Key(s.into()),
|
||||
Val::Str(s) => Val::Str(s.into()),
|
||||
Val::Exp(d, x) => Val::Exp(*d, x.into()),
|
||||
Val::Error(e) => Val::Error(*e) } }
|
||||
pub fn is_nil (&self) -> bool { matches!(self, Self::Nil) }
|
||||
pub fn as_error (&self) -> Option<&DslError> { if let Self::Error(e) = self { Some(e) } else { None } }
|
||||
pub fn as_num (&self) -> Option<usize> {match self{Self::Num(n)=>Some(*n),_=>None}}
|
||||
pub fn as_sym (&self) -> Option<&str> {match self{Self::Sym(s )=>Some(s.as_ref()),_=>None}}
|
||||
pub fn as_key (&self) -> Option<&str> {match self{Self::Key(k )=>Some(k.as_ref()),_=>None}}
|
||||
pub fn as_str (&self) -> Option<&str> {match self{Self::Str(s )=>Some(s.as_ref()),_=>None}}
|
||||
pub fn as_exp (&self) -> Option<&D::Exp> {match self{Self::Exp(_, x)=>Some(x),_=>None}}
|
||||
pub fn exp_depth (&self) -> Option<usize> { todo!() }
|
||||
pub fn exp_head_tail (&self) -> (Option<&Self>, Option<&D::Exp>) { (self.exp_head(), self.exp_tail()) }
|
||||
pub fn exp_head (&self) -> Option<&Self> { todo!() } // TODO
|
||||
pub fn exp_tail (&self) -> Option<&D::Exp> { todo!() }
|
||||
pub fn peek (&self) -> Option<Self> { todo!() }
|
||||
pub fn next (&mut self) -> Option<Self> { todo!() }
|
||||
pub fn rest (self) -> Vec<Self> { todo!() }
|
||||
//pub fn exp_match <T, F> (&self, namespace: &str, cb: F) -> DslPerhaps<T>
|
||||
//where F: Fn(&str, &Exp)-> DslPerhaps<T> {
|
||||
//if let Some(Self::Key(key)) = self.exp_head()
|
||||
//&& key.as_ref().starts_with(namespace)
|
||||
//&& let Some(tail) = self.exp_tail() {
|
||||
//cb(key.as_ref().split_at(namespace.len()).1, tail)
|
||||
//} else {
|
||||
//Ok(None)
|
||||
//}
|
||||
//}
|
||||
}
|
||||
|
||||
/// The string representation for a [Dsl] implementation.
|
||||
/// [Cst] uses `&'s str`. [Ast] uses `Arc<str>`.
|
||||
pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef<str> + std::ops::Deref<Target = str> {}
|
||||
impl<T: PartialEq + Clone + Default + Debug + AsRef<str> + std::ops::Deref<Target = str>> DslStr for T {}
|
||||
|
||||
/// The expression representation for a [Dsl] implementation.
|
||||
/// [Cst] uses [CstIter]. [Ast] uses [VecDeque].
|
||||
pub trait DslExp: PartialEq + Clone + Default + Debug {}
|
||||
impl <T: PartialEq + Clone + Default + Debug> DslExp for T {}
|
||||
|
||||
/// The abstract syntax tree (AST) can be produced from the CST
|
||||
/// by cloning source slices into owned ([Arc]) string slices.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct Ast(Token<Ast>);
|
||||
impl Dsl for Ast {
|
||||
type Str = Arc<str>;
|
||||
type Exp = VecDeque<Arc<Token<Ast>>>;
|
||||
fn dsl (&self) -> DslUsually<&Val<Self>> {
|
||||
Ok(self.0.value())
|
||||
}
|
||||
}
|
||||
|
||||
/// The concrete syntax tree (CST) implements zero-copy
|
||||
/// parsing of the DSL from a string reference. CST items
|
||||
/// preserve info about their location in the source.
|
||||
/// CST stores strings as source references and expressions as [CstIter] instances.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct Cst<'s>(Token<Cst<'s>>);
|
||||
impl<'s> Dsl for Cst<'s> {
|
||||
type Str = &'s str;
|
||||
type Exp = CstConstIter<'s>;
|
||||
fn dsl (&self) -> DslUsually<&Val<Self>> {
|
||||
Ok(self.0.value())
|
||||
}
|
||||
}
|
||||
|
||||
/// `State` + [Dsl] -> `Self`.
|
||||
pub trait FromDsl<State>: Sized {
|
||||
fn try_from_dsl (state: &State, dsl: &impl Dsl) -> Perhaps<Self>;
|
||||
fn from_dsl (state: &State, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Self> {
|
||||
match Self::try_from_dsl(state, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } } }
|
||||
|
||||
/// `self` + `Options` -> [Dsl]
|
||||
pub trait IntoDsl { /*TODO*/ }
|
||||
|
||||
/// `self` + [Dsl] -> `Item`
|
||||
pub trait DslInto<Item> {
|
||||
fn try_dsl_into (&self, dsl: &impl Dsl) -> Perhaps<Item>;
|
||||
fn dsl_into (&self, dsl: &impl Dsl, err: impl Fn()->Box<dyn Error>) -> Usually<Item> {
|
||||
match Self::try_dsl_into(self, dsl)? { Some(dsl) => Ok(dsl), _ => Err(err()) } } }
|
||||
|
||||
/// `self` + `Item` -> [Dsl]
|
||||
pub trait DslFrom { /*TODO*/ }
|
||||
/// Standard result type for DSL-specific operations.
|
||||
pub type DslResult<T> = Result<T, 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),
|
||||
#[error("parse failed: error #{0}")]
|
||||
Code(u8),
|
||||
#[error("end reached")]
|
||||
End
|
||||
}
|
||||
/// Provides native [Iterator] API over [CstConstIter], emitting [Cst] items.
|
||||
///
|
||||
/// [Cst::next] returns just the [Cst] and mutates `self`,
|
||||
/// instead of returning an updated version of the struct as [CstConstIter::next] does.
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct CstIter<'s>(CstConstIter<'s>);
|
||||
impl<'s> CstIter<'s> {
|
||||
pub const fn new (source: &'s str) -> Self { Self(CstConstIter::new(source)) }
|
||||
}
|
||||
impl<'s> Iterator for CstIter<'s> {
|
||||
type Item = Token<Cst<'s>>;
|
||||
fn next (&mut self) -> Option<Self::Item> {
|
||||
match self.0.advance() {
|
||||
Ok(Some((item, rest))) => { self.0 = rest; item.into() },
|
||||
Ok(None) => None,
|
||||
Err(e) => panic!("{e:?}")
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Holds a reference to the source text.
|
||||
/// [CstConstIter::next] emits subsequent pairs of:
|
||||
/// * a [Cst] and
|
||||
/// * the source text remaining
|
||||
/// * [ ] TODO: maybe [CstConstIter::next] should wrap the remaining source in `Self` ?
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct CstConstIter<'s>(pub &'s str);
|
||||
impl<'s> From <&'s str> for CstConstIter<'s> {
|
||||
fn from (src: &'s str) -> Self { Self(src) }
|
||||
}
|
||||
impl<'s> Iterator for CstConstIter<'s> {
|
||||
type Item = Token<Cst<'s>>;
|
||||
fn next (&mut self) -> Option<Token<Cst<'s>>> { self.advance().unwrap().map(|x|x.0) }
|
||||
}
|
||||
impl<'s> ConstIntoIter for CstConstIter<'s> {
|
||||
type Kind = IsIteratorKind;
|
||||
type Item = Cst<'s>;
|
||||
type IntoIter = Self;
|
||||
}
|
||||
impl<'s> CstConstIter<'s> {
|
||||
pub const fn new (source: &'s str) -> Self { Self(source) }
|
||||
pub const fn chomp (&self, index: usize) -> Self { Self(split_at(self.0, index).1) }
|
||||
pub const fn peek (&self) -> DslPerhaps<Token<Cst<'s>>> { Token::peek(self.0) }
|
||||
//pub const fn next (mut self) -> Option<(Token<Cst<'s>>, Self)> {
|
||||
//Self::advance(&mut self).unwrap() }
|
||||
pub const fn advance (&mut self) -> DslPerhaps<(Token<Cst<'s>>, Self)> {
|
||||
match self.peek() {
|
||||
Ok(Some(token)) => {
|
||||
let end = self.chomp(token.span.end());
|
||||
Ok(Some((token.copy(), end)))
|
||||
},
|
||||
Ok(None) => Ok(None),
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
pub struct Span<D: Dsl> {
|
||||
/// Reference to source text.
|
||||
pub source: D::Str,
|
||||
/// Index of 1st character of span.
|
||||
pub start: usize,
|
||||
/// Length of span.
|
||||
pub length: usize,
|
||||
}
|
||||
impl<'s, D: Dsl> Span<D> {
|
||||
pub const fn end (&self) -> usize { self.start.saturating_add(self.length) }
|
||||
}
|
||||
impl<'s, D: Dsl<Str=&'s str>> Span<D> {
|
||||
pub const fn slice (&self) -> &'s str {
|
||||
str_range(self.source, self.start, self.end()) }
|
||||
pub const fn slice_exp (&self) -> &'s str {
|
||||
str_range(self.source, self.start.saturating_add(1), self.end()) }
|
||||
pub const fn grow (&mut self) -> DslUsually<&mut Self> {
|
||||
if self.length + self.start >= self.source.len() { return Err(End) }
|
||||
self.length = self.length.saturating_add(1);
|
||||
Ok(self) }
|
||||
}
|
||||
|
||||
/// Parsed substring with range and value.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct Token<D: Dsl> {
|
||||
/// Source span of token.
|
||||
span: Span<D>,
|
||||
/// Meaning of token.
|
||||
value: Val<D>,
|
||||
}
|
||||
impl<D: Dsl> Token<D> {
|
||||
pub const fn value (&self) -> &Val<D> { &self.value }
|
||||
pub const fn span (&self) -> &Span<D> { &self.span }
|
||||
pub const fn err (&self) -> Option<DslError> {
|
||||
if let Val::Error(e) = self.value { Some(e) } else { None } }
|
||||
pub const fn new (source: D::Str, start: usize, length: usize, value: Val<D>) -> Self {
|
||||
Self { value, span: Span { source, start, length } } }
|
||||
pub const fn copy (&self) -> Self where D::Str: Copy, D::Exp: Copy {
|
||||
Self { span: Span { ..self.span }, value: self.value } }
|
||||
}
|
||||
const fn or_panic <T> (result: DslUsually<T>) -> T {
|
||||
match result { Ok(t) => t, Err(e) => const_panic::concat_panic!(e) }
|
||||
}
|
||||
impl<'s, D: Dsl<Str = &'s str>> Token<D> {
|
||||
pub const fn peek (src: D::Str) -> DslPerhaps<Self> where D::Exp: From<&'s str> {
|
||||
use Val::*;
|
||||
let mut t = Self::new(src, 0, 0, Nil);
|
||||
let mut iter = char_indices(src);
|
||||
while let Some(((i, c), next)) = iter.next() {
|
||||
t = match (t.value(), c) {
|
||||
(Error(_), _) =>
|
||||
return Ok(Some(t)),
|
||||
(Nil, ' '|'\n'|'\r'|'\t') =>
|
||||
*or_panic(t.grow()),
|
||||
(Nil, '(') =>
|
||||
Self::new(src, i, 1, Exp(1, D::Exp::from(str_range(src, i, i + 1)))),
|
||||
(Nil, '"') =>
|
||||
Self::new(src, i, 1, Str(str_range(src, i, i + 1))),
|
||||
(Nil, ':'|'@') =>
|
||||
Self::new(src, i, 1, Sym(str_range(src, i, i + 1))),
|
||||
(Nil, '/'|'a'..='z') =>
|
||||
Self::new(src, i, 1, Key(str_range(src, i, i + 1))),
|
||||
(Nil, '0'..='9') =>
|
||||
Self::new(src, i, 1, match to_digit(c) { Ok(c) => Num(c), Err(e) => Error(e) }),
|
||||
(Nil, _) =>
|
||||
{ t.value = Val::Error(Unexpected(c)); t },
|
||||
(Str(_), '"') =>
|
||||
return Ok(Some(t)),
|
||||
(Str(_), _) =>
|
||||
{ or_panic(t.grow()); t.value = Str(t.span.slice()); t },
|
||||
(Num(m), ' '|'\n'|'\r'|'\t'|')') =>
|
||||
return Ok(Some(t)),
|
||||
(Num(m), _) => match to_digit(c) {
|
||||
Ok(n) => { t.grow()?; t.value = Num(10*m+n); t },
|
||||
Err(e) => { t.grow()?; t.value = Error(e); t } },
|
||||
(Sym(_), ' '|'\n'|'\r'|'\t'|')') =>
|
||||
return Ok(Some(t)),
|
||||
(Sym(_), 'a'..='z'|'A'..='Z'|'0'..='9'|'-') => {
|
||||
t.grow()?; t.value = Sym(t.span.slice()); t },
|
||||
(Sym(_), _) =>
|
||||
{ t.value = Error(Unexpected(c)); t },
|
||||
(Key(_), ' '|'\n'|'\r'|'\t'|')') =>
|
||||
return Ok(Some(t)),
|
||||
(Key(_), 'a'..='z'|'0'..='9'|'-'|'/') =>
|
||||
{ t.grow()?; t.value = Key(t.span.slice()); t },
|
||||
(Key(_), _ ) =>
|
||||
{ t.value = Error(Unexpected(c)); t },
|
||||
(Exp(0, _), _) =>
|
||||
{ t.grow()?; t.value = Exp(0, D::Exp::from(t.span.slice_exp())); return Ok(Some(t)) },
|
||||
(Exp(d, _), ')') =>
|
||||
{ t.grow()?; t.value = Exp(d-1, D::Exp::from(t.span.slice_exp())); t },
|
||||
(Exp(d, _), '(') =>
|
||||
{ t.grow()?; t.value = Exp(d+1, D::Exp::from(t.span.slice_exp())); t },
|
||||
(Exp(d, _), _ ) =>
|
||||
{ t.grow()?; t.value = Exp(*d, D::Exp::from(t.span.slice_exp())); t },
|
||||
};
|
||||
iter = next;
|
||||
}
|
||||
Ok(match t.value() {
|
||||
Nil => None,
|
||||
_ => Some(t)
|
||||
})
|
||||
}
|
||||
pub const fn grow (&mut self) -> DslUsually<&mut Self> { self.span.grow()?; Ok(self) }
|
||||
pub const fn grow_exp (&mut self, d: isize) -> &mut Self where D::Exp: From<&'s str> {
|
||||
if let Val::Exp(depth, _) = self.value() {
|
||||
self.value = Val::Exp((*depth as isize + d) as usize, D::Exp::from(self.span.slice_exp()));
|
||||
self
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn to_digit (c: char) -> DslResult<usize> {
|
||||
Ok(match c {
|
||||
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
|
||||
'5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
|
||||
_ => return Result::Err(Unexpected(c)) }) }
|
||||
|
||||
macro_rules! iterate(($expr:expr => $arg: pat => $body:expr) => {
|
||||
let mut iter = $expr; while let Some(($arg, next)) = iter.next() { $body; iter = next; } });
|
||||
pub const fn to_number (digits: &str) -> DslResult<usize> {
|
||||
let mut value = 0;
|
||||
iterate!(char_indices(digits) => (_, c) => match to_digit(c) {
|
||||
Ok(digit) => value = 10 * value + digit,
|
||||
Result::Err(e) => return Result::Err(e) });
|
||||
Ok(value) }
|
||||
|
||||
/// Implement type conversions.
|
||||
macro_rules! from(($($Struct:ty { $(
|
||||
$(<$($l:lifetime),* $($T:ident$(:$U:ident)?),*>)? ($source:ident: $From:ty) $expr:expr
|
||||
);+ $(;)? })*) => { $(
|
||||
$(impl $(<$($l),* $($T$(:$U)?),*>)? From<$From> for $Struct {
|
||||
fn from ($source: $From) -> Self { $expr }
|
||||
})+
|
||||
)* });
|
||||
|
||||
//from! {
|
||||
////Vec<Token<'s>> { <'s> (val: CstIter<'s>) val.collect(); }
|
||||
//CstConstIter<'s> {
|
||||
//<'s> (src: &'s str) Self(src);
|
||||
//<'s> (iter: CstIter<'s>) iter.0; }
|
||||
//CstIter<'s> {
|
||||
//<'s> (src: &'s str) Self(CstConstIter(src));
|
||||
//<'s> (iter: CstConstIter<'s>) Self(iter); }
|
||||
//Cst<'s> { <'s> (src: &'s str) Self(CstIter(CstConstIter(src))); }
|
||||
//Vec<Ast> { <'s> (val: CstIter<'s>) val.map(Into::into).collect(); }
|
||||
//Token<Cst<'s>> { <'s> (token: Token<Cst<'s>>) Self { value: token.value.into(), span: token.span.into() } }
|
||||
//Ast {
|
||||
//<'s> (src: &'s str) Ast::from(CstIter(CstConstIter(src)));
|
||||
//<'s> (cst: Cst<'s>) Ast(VecDeque::from([dsl_val(cst.val())]).into());
|
||||
//<'s> (iter: CstIter<'s>) Ast(iter.map(|x|x.value.into()).collect::<VecDeque<_>>().into());
|
||||
//<D: Dsl> (token: Token<D>) Ast(VecDeque::from([dsl_val(token.val())]).into()); }
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue