mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-07 20:26:44 +01:00
This commit is contained in:
parent
91dc77cfea
commit
a46d0d2258
16 changed files with 506 additions and 513 deletions
|
|
@ -1,14 +1,20 @@
|
|||
use crate::*;
|
||||
|
||||
/// The abstract syntax tree (AST) is produced from the CST
|
||||
/// by cloning source slices into owned ([Arc]) string slices.
|
||||
#[derive(Debug, Clone, Default, PartialEq)]
|
||||
pub struct Ast(pub AstValue);
|
||||
pub struct Ast(pub DslVal<Arc<str>, VecDeque<Ast>>);
|
||||
|
||||
/// The abstract syntax tree (AST) can be produced from the CST
|
||||
/// by cloning source slices into owned [Arc] values.
|
||||
pub type AstValue = DslValue<Arc<str>, VecDeque<Ast>>;
|
||||
|
||||
//#[derive(Debug, Clone, Default, PartialEq)]
|
||||
//pub struct AstIter();
|
||||
impl Dsl for Ast {
|
||||
type Str = Arc<str>;
|
||||
type Exp = VecDeque<Ast>;
|
||||
fn val (&self) -> &DslVal<Self::Str, Self::Exp> {
|
||||
self.0.val()
|
||||
}
|
||||
fn nth (&self, index: usize) -> Option<&DslVal<Self::Str, Self::Exp>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> From<Cst<'src>> for Ast {
|
||||
fn from (token: Cst<'src>) -> Self {
|
||||
|
|
@ -18,14 +24,15 @@ impl<'src> From<Cst<'src>> for Ast {
|
|||
|
||||
impl<'src> From<CstValue<'src>> for Ast {
|
||||
fn from (value: CstValue<'src>) -> Self {
|
||||
use DslVal::*;
|
||||
Self(match value {
|
||||
DslValue::Nil => DslValue::Nil,
|
||||
DslValue::Err(e) => DslValue::Err(e),
|
||||
DslValue::Num(u) => DslValue::Num(u),
|
||||
DslValue::Sym(s) => DslValue::Sym(s.into()),
|
||||
DslValue::Key(s) => DslValue::Key(s.into()),
|
||||
DslValue::Str(s) => DslValue::Str(s.into()),
|
||||
DslValue::Exp(d, x) => DslValue::Exp(d, x.map(|x|x.into()).collect()),
|
||||
Nil => Nil,
|
||||
Err(e) => Err(e),
|
||||
Num(u) => Num(u),
|
||||
Sym(s) => Sym(s.into()),
|
||||
Key(s) => Key(s.into()),
|
||||
Str(s) => Str(s.into()),
|
||||
Exp(d, x) => Exp(d, x.map(|x|x.into()).collect()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ macro_rules! iterate {
|
|||
}
|
||||
|
||||
/// CST stores strings as source references and expressions as [SourceIter] instances.
|
||||
pub type CstValue<'source> = DslValue<&'source str, SourceIter<'source>>;
|
||||
pub type CstValue<'src> = DslVal<&'src str, SourceIter<'src>>;
|
||||
|
||||
/// Token sharing memory with source reference.
|
||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||
|
|
@ -46,6 +46,17 @@ pub struct Cst<'src> {
|
|||
pub value: CstValue<'src>,
|
||||
}
|
||||
|
||||
impl<'src> Dsl for Cst<'src> {
|
||||
type Str = &'src str;
|
||||
type Exp = SourceIter<'src>;
|
||||
fn val (&self) -> &DslVal<&'src str, SourceIter<'src>> {
|
||||
&self.value
|
||||
}
|
||||
fn nth (&self, index: usize) -> Option<&DslVal<&'src str, SourceIter<'src>>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> Cst<'src> {
|
||||
pub const fn new (
|
||||
source: &'src str,
|
||||
|
|
@ -73,51 +84,51 @@ impl<'src> Cst<'src> {
|
|||
pub const fn value (&self) -> CstValue<'src> {
|
||||
self.value
|
||||
}
|
||||
pub const fn error (self, error: DslError) -> Self {
|
||||
Self { value: DslValue::Err(error), ..self }
|
||||
pub const fn error (self, error: DslErr) -> Self {
|
||||
Self { value: DslVal::Err(error), ..self }
|
||||
}
|
||||
pub const fn grow (self) -> Self {
|
||||
Self { length: self.length.saturating_add(1), ..self }
|
||||
}
|
||||
pub const fn grow_num (self, m: usize, c: char) -> Self {
|
||||
match to_digit(c) {
|
||||
Result::Ok(n) => Self { value: DslValue::Num(10*m+n), ..self.grow() },
|
||||
Result::Err(e) => Self { value: DslValue::Err(e), ..self.grow() },
|
||||
Result::Ok(n) => Self { value: DslVal::Num(10*m+n), ..self.grow() },
|
||||
Result::Err(e) => Self { value: DslVal::Err(e), ..self.grow() },
|
||||
}
|
||||
}
|
||||
pub const fn grow_key (self) -> Self {
|
||||
let token = self.grow();
|
||||
token.with_value(DslValue::Key(token.slice_source(self.source)))
|
||||
token.with_value(DslVal::Key(token.slice_source(self.source)))
|
||||
}
|
||||
pub const fn grow_sym (self) -> Self {
|
||||
let token = self.grow();
|
||||
token.with_value(DslValue::Sym(token.slice_source(self.source)))
|
||||
token.with_value(DslVal::Sym(token.slice_source(self.source)))
|
||||
}
|
||||
pub const fn grow_str (self) -> Self {
|
||||
let token = self.grow();
|
||||
token.with_value(DslValue::Str(token.slice_source(self.source)))
|
||||
token.with_value(DslVal::Str(token.slice_source(self.source)))
|
||||
}
|
||||
pub const fn grow_exp (self) -> Self {
|
||||
let token = self.grow();
|
||||
if let DslValue::Exp(depth, _) = token.value() {
|
||||
token.with_value(DslValue::Exp(depth, SourceIter::new(token.slice_source_exp(self.source))))
|
||||
if let DslVal::Exp(depth, _) = token.value() {
|
||||
token.with_value(DslVal::Exp(depth, SourceIter::new(token.slice_source_exp(self.source))))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_in (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let DslValue::Exp(depth, source) = token.value() {
|
||||
token.with_value(DslValue::Exp(depth.saturating_add(1), source))
|
||||
if let DslVal::Exp(depth, source) = token.value() {
|
||||
token.with_value(DslVal::Exp(depth.saturating_add(1), source))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
pub const fn grow_out (self) -> Self {
|
||||
let token = self.grow_exp();
|
||||
if let DslValue::Exp(depth, source) = token.value() {
|
||||
if let DslVal::Exp(depth, source) = token.value() {
|
||||
if depth > 0 {
|
||||
token.with_value(DslValue::Exp(depth - 1, source))
|
||||
token.with_value(DslVal::Exp(depth - 1, source))
|
||||
} else {
|
||||
return self.error(Unexpected(')'))
|
||||
}
|
||||
|
|
@ -145,7 +156,7 @@ pub const fn to_digit (c: char) -> DslResult<usize> {
|
|||
}
|
||||
|
||||
pub const fn peek_src <'src> (source: &'src str) -> Option<Cst<'src>> {
|
||||
use DslValue::*;
|
||||
use DslVal::*;
|
||||
let mut token: Cst<'src> = Cst::new(source, 0, 0, Nil);
|
||||
iterate!(char_indices(source) => (start, c) => token = match token.value() {
|
||||
Err(_) => return Some(token),
|
||||
|
|
@ -162,8 +173,8 @@ pub const fn peek_src <'src> (source: &'src str) -> Option<Cst<'src>> {
|
|||
Cst::new(source, start, 1, Key(str_range(source, start, start + 1))),
|
||||
'0'..='9' =>
|
||||
Cst::new(source, start, 1, match to_digit(c) {
|
||||
Ok(c) => DslValue::Num(c),
|
||||
Result::Err(e) => DslValue::Err(e)
|
||||
Ok(c) => DslVal::Num(c),
|
||||
Result::Err(e) => DslVal::Err(e)
|
||||
}),
|
||||
_ => token.error(Unexpected(c))
|
||||
},
|
||||
|
|
|
|||
137
dsl/src/dsl.rs
137
dsl/src/dsl.rs
|
|
@ -1,6 +1,10 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Error, Debug, Copy, Clone, PartialEq)] pub enum DslError {
|
||||
/// Standard result type for DSL-specific operations.
|
||||
pub type DslResult<T> = Result<T, DslErr>;
|
||||
|
||||
/// DSL-specific error codes.
|
||||
#[derive(Error, Debug, Copy, Clone, PartialEq)] pub enum DslErr {
|
||||
#[error("parse failed: not implemented")]
|
||||
Unimplemented,
|
||||
#[error("parse failed: empty")]
|
||||
|
|
@ -13,41 +17,13 @@ use crate::*;
|
|||
Code(u8),
|
||||
}
|
||||
|
||||
/// Thing that may construct itself from `State` and [DslValue].
|
||||
pub trait FromDsl<State>: Sized {
|
||||
fn try_provide (
|
||||
state: &State,
|
||||
value: DslValue<impl DslStr, impl DslExp>
|
||||
) -> Perhaps<Self>;
|
||||
fn provide (
|
||||
state: &State,
|
||||
value: DslValue<impl DslStr, impl DslExp>,
|
||||
error: impl Fn()->Box<dyn std::error::Error>
|
||||
) -> Usually<Self> {
|
||||
match Self::try_provide(state, value)? {
|
||||
Some(value) => Ok(value),
|
||||
_ => Err(error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type DslResult<T> = Result<T, DslError>;
|
||||
|
||||
/// Marker trait for supported string types.
|
||||
pub trait DslStr: PartialEq + Clone + Default + Debug + AsRef<str> {}
|
||||
impl<T: PartialEq + Clone + Default + Debug + AsRef<str>> DslStr for T {}
|
||||
|
||||
/// Marker trait for supported expression types.
|
||||
pub trait DslExp: PartialEq + Clone + Default + Debug {}
|
||||
impl<T: PartialEq + Clone + Default + Debug> DslExp for T {}
|
||||
|
||||
/// A DSL value generic over string and expression types.
|
||||
/// See [CstValue] and [AstValue].
|
||||
/// Enumeration of possible DSL tokens.
|
||||
/// Generic over string and expression storage.
|
||||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
pub enum DslValue<Str: DslStr, Exp: DslExp> {
|
||||
pub enum DslVal<Str, Exp> {
|
||||
#[default]
|
||||
Nil,
|
||||
Err(DslError),
|
||||
Err(DslErr),
|
||||
Num(usize),
|
||||
Sym(Str),
|
||||
Key(Str),
|
||||
|
|
@ -55,38 +31,67 @@ pub enum DslValue<Str: DslStr, Exp: DslExp> {
|
|||
Exp(usize, Exp),
|
||||
}
|
||||
|
||||
impl<Str: DslStr, Exp: DslExp> DslValue<Str, Exp> {
|
||||
pub trait Dsl {
|
||||
type Str: PartialEq + Clone + Default + Debug + AsRef<str>;
|
||||
type Exp: PartialEq + Clone + Default + Debug;
|
||||
fn val (&self) -> &DslVal<Self::Str, Self::Exp>;
|
||||
fn nth (&self, index: usize) -> Option<&DslVal<Self::Str, Self::Exp>>;
|
||||
// exp-only nth here?
|
||||
}
|
||||
|
||||
impl<
|
||||
Str: PartialEq + Clone + Default + Debug + AsRef<str>,
|
||||
Exp: PartialEq + Clone + Default + Debug,
|
||||
> Dsl for DslVal<Str, Exp> {
|
||||
type Str = Str;
|
||||
type Exp = Exp;
|
||||
fn val (&self) -> &DslVal<Str, Exp> {
|
||||
self
|
||||
}
|
||||
fn nth (&self, index: usize) -> Option<&DslVal<Str, Exp>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// May construct self from state and DSL.
|
||||
pub trait DslFrom<State>: Sized {
|
||||
fn try_dsl_from (state: &State, value: &impl Dsl) -> Perhaps<Self>;
|
||||
fn dsl_from (
|
||||
state: &State, value: &impl Dsl, error: impl Fn()->Box<dyn std::error::Error>
|
||||
) -> Usually<Self> {
|
||||
match Self::try_dsl_from(state, value)? {
|
||||
Some(value) => Ok(value),
|
||||
_ => Err(error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// May construct another from self and DSL.
|
||||
pub trait DslInto<Item> {
|
||||
fn try_dsl_into (&self, dsl: &impl Dsl) -> Perhaps<Item>;
|
||||
fn dsl_into (
|
||||
&self, value: &impl Dsl, error: impl Fn()->Box<dyn std::error::Error>
|
||||
) -> Usually<Item> {
|
||||
match Self::try_dsl_into(self, value)? {
|
||||
Some(value) => Ok(value),
|
||||
_ => Err(error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Str, Exp> DslVal<Str, Exp> {
|
||||
pub fn is_nil (&self) -> bool {
|
||||
matches!(self, Self::Nil)
|
||||
}
|
||||
pub fn as_err (&self) -> Option<&DslError> {
|
||||
pub fn as_err (&self) -> Option<&DslErr> {
|
||||
if let Self::Err(e) = self { Some(e) } else { None }
|
||||
}
|
||||
pub fn as_num (&self) -> Option<usize> {
|
||||
if let Self::Num(n) = self { Some(*n) } else { None }
|
||||
}
|
||||
pub fn as_sym (&self) -> Option<&str> {
|
||||
if let Self::Sym(s) = self { Some(s.as_ref()) } else { None }
|
||||
}
|
||||
pub fn as_key (&self) -> Option<&str> {
|
||||
if let Self::Key(k) = self { Some(k.as_ref()) } else { None }
|
||||
}
|
||||
pub fn as_str (&self) -> Option<&str> {
|
||||
if let Self::Str(s) = self { Some(s.as_ref()) } else { None }
|
||||
}
|
||||
pub fn as_exp (&self) -> Option<&Exp> {
|
||||
if let Self::Exp(_, x) = self { Some(x) } else { None }
|
||||
}
|
||||
pub fn exp_match <T, F> (&self, namespace: &str, cb: F) -> Perhaps<T>
|
||||
where F: Fn(&str, &Exp)-> Perhaps<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)
|
||||
}
|
||||
}
|
||||
pub fn exp_depth (&self) -> Option<usize> {
|
||||
todo!()
|
||||
}
|
||||
|
|
@ -107,4 +112,26 @@ impl<Str: DslStr, Exp: DslExp> DslValue<Str, Exp> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<Str: DslStr + Copy, Exp: DslExp + Copy> Copy for DslValue<Str, Exp> {}
|
||||
impl<Str: AsRef<str>, Exp> DslVal<Str, Exp> {
|
||||
pub fn as_sym (&self) -> Option<&str> {
|
||||
if let Self::Sym(s) = self { Some(s.as_ref()) } else { None }
|
||||
}
|
||||
pub fn as_key (&self) -> Option<&str> {
|
||||
if let Self::Key(k) = self { Some(k.as_ref()) } else { None }
|
||||
}
|
||||
pub fn as_str (&self) -> Option<&str> {
|
||||
if let Self::Str(s) = self { Some(s.as_ref()) } else { None }
|
||||
}
|
||||
pub fn exp_match <T, F> (&self, namespace: &str, cb: F) -> Perhaps<T>
|
||||
where F: Fn(&str, &Exp)-> Perhaps<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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Str: Copy, Exp: Copy> Copy for DslVal<Str, Exp> {}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ 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::DslError::*;
|
||||
pub(crate) use self::DslErr::*;
|
||||
|
||||
mod dsl; pub use self::dsl::*;
|
||||
mod ast; pub use self::ast::*;
|
||||
|
|
@ -96,7 +96,7 @@ mod cst; pub use self::cst::*;
|
|||
//}
|
||||
|
||||
#[cfg(test)] #[test] fn test_token () -> Result<(), Box<dyn std::error::Error>> {
|
||||
use crate::DslValue::*;
|
||||
use crate::DslVal::*;
|
||||
let source = ":f00";
|
||||
let mut token = Cst::new(source, 0, 1, Sym(":"));
|
||||
token = token.grow_sym();
|
||||
|
|
@ -126,7 +126,7 @@ mod cst; pub use self::cst::*;
|
|||
Ok(())
|
||||
}
|
||||
|
||||
//#[cfg(test)] #[test] fn test_examples () -> Result<(), DslError> {
|
||||
//#[cfg(test)] #[test] fn test_examples () -> Result<(), DslErr> {
|
||||
//// Let's pretend to render some view.
|
||||
//let source = include_str!("../../tek/src/view_arranger.edn");
|
||||
//// The token iterator allows you to get the tokens represented by the source text.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue