add tengri_core; fix errors and warnings; unify deps

This commit is contained in:
🪞👃🪞 2025-05-10 15:25:09 +03:00
parent cb8fd26922
commit 8dda576c9d
18 changed files with 180 additions and 152 deletions

View file

@ -1,5 +1,4 @@
use crate::*;
use thiserror::Error;
pub type ParseResult<T> = Result<T, ParseError>;
@ -16,41 +15,6 @@ pub type ParseResult<T> = Result<T, ParseError>;
Code(u8),
}
pub trait TryFromDsl<'state, T>: Sized {
fn try_from_expr <'source: 'state> (
_state: &'state T, _iter: &mut TokenIter<'source>
) -> Option<Self> {
None
}
fn try_from_atom <'source: 'state> (
state: &'state T, value: Value<'source>
) -> Option<Self> {
if let Exp(0, mut iter) = value {
return Self::try_from_expr(state, &mut iter)
}
None
}
}
/// Map EDN tokens to parameters of a given type for a given context
pub trait Context<'state, U>: Sized {
fn get <'source> (&'state self, _iter: &mut TokenIter<'source>) -> Option<U> {
None
}
}
impl<'state, T: Context<'state, U>, U> Context<'state, U> for &T {
fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option<U> {
(*self).get(iter)
}
}
impl<'state, T: Context<'state, U>, U> Context<'state, U> for Option<T> {
fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option<U> {
self.as_ref().map(|s|s.get(iter)).flatten()
}
}
/// Implement the const iterator pattern.
#[macro_export] macro_rules! const_iter {
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
@ -138,6 +102,7 @@ impl<'a> SourceIter<'a> {
}
}
}
/// Static iteration helper.
#[macro_export] macro_rules! iterate {
($expr:expr => $arg: pat => $body:expr) => {
@ -240,7 +205,9 @@ pub const fn to_digit (c: char) -> Result<usize, ParseError> {
}
impl<'source> Token<'source> {
pub const fn new (source: &'source str, start: usize, length: usize, value: Value<'source>) -> Self {
pub const fn new (
source: &'source str, start: usize, length: usize, value: Value<'source>
) -> Self {
Self { source, start, length, value }
}
pub const fn end (&self) -> usize {
@ -248,7 +215,6 @@ impl<'source> Token<'source> {
}
pub const fn slice (&'source self) -> &'source str {
self.slice_source(self.source)
//str_range(self.source, self.start, self.end())
}
pub const fn slice_source <'range> (&'source self, source: &'range str) -> &'range str {
str_range(source, self.start, self.end())
@ -256,6 +222,9 @@ impl<'source> Token<'source> {
pub const fn slice_source_exp <'range> (&'source self, source: &'range str) -> &'range str {
str_range(source, self.start.saturating_add(1), self.end())
}
pub const fn with_value (self, value: Value<'source>) -> Self {
Self { value, ..self }
}
pub const fn value (&self) -> Value {
self.value
}
@ -272,49 +241,43 @@ impl<'source> Token<'source> {
}
}
pub const fn grow_key (self) -> Self {
let mut token = self.grow();
token.value = Key(token.slice_source(self.source));
token
let token = self.grow();
token.with_value(Key(token.slice_source(self.source)))
}
pub const fn grow_sym (self) -> Self {
let mut token = self.grow();
token.value = Sym(token.slice_source(self.source));
token
let token = self.grow();
token.with_value(Sym(token.slice_source(self.source)))
}
pub const fn grow_str (self) -> Self {
let mut token = self.grow();
token.value = Str(token.slice_source(self.source));
token
let token = self.grow();
token.with_value(Str(token.slice_source(self.source)))
}
pub const fn grow_exp (self) -> Self {
let mut token = self.grow();
let token = self.grow();
if let Exp(depth, _) = token.value {
token.value = Exp(depth, TokenIter::new(token.slice_source_exp(self.source)));
token.with_value(Exp(depth, TokenIter::new(token.slice_source_exp(self.source))))
} else {
unreachable!()
}
token
}
pub const fn grow_in (self) -> Self {
let mut token = self.grow_exp();
let token = self.grow_exp();
if let Value::Exp(depth, source) = token.value {
token.value = Value::Exp(depth.saturating_add(1), source)
token.with_value(Value::Exp(depth.saturating_add(1), source))
} else {
unreachable!()
}
token
}
pub const fn grow_out (self) -> Self {
let mut token = self.grow_exp();
let token = self.grow_exp();
if let Value::Exp(depth, source) = token.value {
if depth > 0 {
token.value = Value::Exp(depth - 1, source)
token.with_value(Value::Exp(depth - 1, source))
} else {
return self.error(Unexpected(')'))
}
} else {
unreachable!()
}
token
}
}

44
dsl/src/dsl_provide.rs Normal file
View file

@ -0,0 +1,44 @@
use crate::*;
/// Map EDN tokens to parameters of a given type for a given context
pub trait Dsl<U>: Sized {
fn take <'state, 'source> (_: &'state Self, _: &mut TokenIter<'source>) -> Perhaps<U> {
Ok(None)
}
}
/// Map EDN tokens to parameters of a given type for a given context
pub trait Context<'state, U>: Sized {
fn get <'source> (&'state self, _iter: &mut TokenIter<'source>) -> Option<U> {
None
}
}
impl<'state, T: Context<'state, U>, U> Context<'state, U> for &T {
fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option<U> {
(*self).get(iter)
}
}
impl<'state, T: Context<'state, U>, U> Context<'state, U> for Option<T> {
fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option<U> {
self.as_ref().map(|s|s.get(iter)).flatten()
}
}
pub trait TryFromDsl<'state, T>: Sized {
fn try_from_expr <'source: 'state> (
_state: &'state T, _iter: &mut TokenIter<'source>
) -> Option<Self> {
None
}
fn try_from_atom <'source: 'state> (
state: &'state T, value: Value<'source>
) -> Option<Self> {
if let Exp(0, mut iter) = value {
return Self::try_from_expr(state, &mut iter)
}
None
}
}

View file

@ -1,3 +1,7 @@
#![feature(adt_const_params)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_fn_trait_return)]
//! [Token]s are parsed substrings with an associated [Value].
//!
//! * [ ] FIXME: Value may be [Err] which may shadow [Result::Err]
@ -31,17 +35,18 @@
//! assert_eq!(view.0.0, src);
//! assert_eq!(view.peek(), view.0.peek())
//! ```
#![feature(adt_const_params)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_fn_trait_return)]
pub(crate) use self::Value::*;
pub(crate) use self::ParseError::*;
pub(crate) use ::tengri_core::*;
pub(crate) use std::fmt::Debug;
pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
pub(crate) use konst::string::{split_at, str_range, char_indices};
pub(crate) use std::fmt::Debug;
pub(crate) use thiserror::Error;
pub(crate) use self::Value::*;
pub(crate) use self::ParseError::*;
mod dsl; pub use self::dsl::*;
mod dsl_parse; pub use self::dsl_parse::*;
mod dsl_provide; pub use self::dsl_provide::*;
#[cfg(test)] mod test_token_iter {
use crate::*;