dsl gets the gordian treatment

This commit is contained in:
🪞👃🪞 2025-05-26 22:49:55 +03:00
parent 93b1cf1a5c
commit d6e8be6ce5
9 changed files with 540 additions and 456 deletions

264
dsl/src/dsl_token.rs Normal file
View file

@ -0,0 +1,264 @@
use crate::*;
#[derive(PartialEq, Clone, Default, Debug)]
pub enum Value<
S: PartialEq + Clone + Default + Debug,
X: PartialEq + Clone + Default + Debug,
> {
#[default] Nil, Err(DslError), Num(usize), Sym(S), Key(S), Str(S), Exp(X),
}
pub trait DslValue: PartialEq + Clone + Default + Debug {
type Err: PartialEq + Clone + Debug;
type Num: PartialEq + Copy + Clone + Default + Debug;
type Str: PartialEq + Clone + Default + Debug;
type Exp: PartialEq + Clone + Default + Debug;
fn nil (&self) -> bool;
fn err (&self) -> Option<&Self::Err>;
fn num (&self) -> Option<Self::Num>;
fn sym (&self) -> Option<&Self::Str>;
fn key (&self) -> Option<&Self::Str>;
fn str (&self) -> Option<&Self::Str>;
fn exp (&self) -> Option<&Self::Exp>;
fn exp_head (&self) -> Option<&Self> { None } // TODO
fn exp_tail (&self) -> Option<&[Self]> { None } // TODO
}
impl<
Str: PartialEq + Clone + Default + Debug,
Exp: PartialEq + Clone + Default + Debug,
> DslValue for Value<Str, Exp> {
type Err = DslError;
type Num = usize;
type Str = Str;
type Exp = Exp;
fn nil (&self) -> bool {
matches!(self, Self::Nil)
}
fn err (&self) -> Option<&DslError> {
if let Self::Err(e) = self { Some(e) } else { None }
}
fn num (&self) -> Option<usize> {
if let Self::Num(n) = self { Some(*n) } else { None }
}
fn sym (&self) -> Option<&Str> {
if let Self::Sym(s) = self { Some(s) } else { None }
}
fn key (&self) -> Option<&Str> {
if let Self::Key(k) = self { Some(k) } else { None }
}
fn str (&self) -> Option<&Str> {
if let Self::Str(s) = self { Some(s) } else { None }
}
fn exp (&self) -> Option<&Exp> {
if let Self::Exp(x) = self { Some(x) } else { None }
}
}
#[derive(PartialEq, Clone, Default, Debug)]
pub struct Token<
V: PartialEq + Clone + Default + Debug,
M: PartialEq + Clone + Default + Debug,
>(pub V, pub M);
pub trait DslToken: PartialEq + Clone + Default + Debug {
type Value: DslValue;
type Meta: Clone + Default + Debug;
fn value (&self) -> &Self::Value;
fn meta (&self) -> &Self::Meta;
}
impl<V: DslValue, M: PartialEq + Clone + Default + Debug> DslToken for Token<V, M> {
type Value = V;
type Meta = M;
fn value (&self) -> &Self::Value {
&self.0
}
fn meta (&self) -> &Self::Meta {
&self.1
}
}
impl<'source> CstToken<'source> {
pub const fn new (
source: &'source str, start: usize, length: usize, value: CstValue<'source>
) -> Self {
Self(value, CstMeta { source, start, length })
}
pub const fn end (&self) -> usize {
self.1.start.saturating_add(self.1.length)
}
pub const fn slice (&'source self) -> &'source str {
self.slice_source(self.1.source)
}
pub const fn slice_source <'range> (&'source self, source: &'range str) -> &'range str {
str_range(source, self.1.start, self.end())
}
pub const fn slice_source_exp <'range> (&'source self, source: &'range str) -> &'range str {
str_range(source, self.1.start.saturating_add(1), self.end())
}
pub const fn with_value (self, value: CstValue<'source>) -> Self {
Self(value, self.1)
}
pub const fn value (&self) -> CstValue<'source> {
self.0
}
pub const fn error (self, error: DslError) -> Self {
Self(Value::Err(error), self.1)
}
pub const fn grow (self) -> Self {
Self(self.0, CstMeta { length: self.1.length.saturating_add(1), ..self.1 })
}
pub const fn grow_num (self, m: usize, c: char) -> Self {
use Value::*;
match to_digit(c) {
Result::Ok(n) => Self(Num(10*m+n), self.grow().1),
Result::Err(e) => Self(Err(e), self.grow().1),
}
}
pub const fn grow_key (self) -> Self {
use Value::*;
let token = self.grow();
token.with_value(Key(token.slice_source(self.1.source)))
}
pub const fn grow_sym (self) -> Self {
use Value::*;
let token = self.grow();
token.with_value(Sym(token.slice_source(self.1.source)))
}
pub const fn grow_str (self) -> Self {
use Value::*;
let token = self.grow();
token.with_value(Str(token.slice_source(self.1.source)))
}
pub const fn grow_exp (self) -> Self {
use Value::*;
let token = self.grow();
if let Exp(depth, _) = token.value() {
token.with_value(Exp(depth, Cst::new(token.slice_source_exp(self.source))))
} else {
unreachable!()
}
}
pub const fn grow_in (self) -> Self {
let token = self.grow_exp();
if let Value::Exp(depth, source) = token.value {
token.with_value(Value::Exp(depth.saturating_add(1), source))
} else {
unreachable!()
}
}
pub const fn grow_out (self) -> Self {
let token = self.grow_exp();
if let Value::Exp(depth, source) = token.value {
if depth > 0 {
token.with_value(Value::Exp(depth - 1, source))
} else {
return self.error(Unexpected(')'))
}
} else {
unreachable!()
}
}
}
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)
}
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))
})
}
/// Static iteration helper.
#[macro_export] 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 peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
use Value::*;
let mut token: CstToken<'a> = CstToken::new(source, 0, 0, Nil);
iterate!(char_indices(source) => (start, c) => token = match token.value() {
Err(_) => return Some(token),
Nil => match c {
' '|'\n'|'\r'|'\t' =>
token.grow(),
'(' =>
CstToken::new(source, start, 1, Exp(1, Cst::new(str_range(source, start, start + 1)))),
'"' =>
CstToken::new(source, start, 1, Str(str_range(source, start, start + 1))),
':'|'@' =>
CstToken::new(source, start, 1, Sym(str_range(source, start, start + 1))),
'/'|'a'..='z' =>
CstToken::new(source, start, 1, Key(str_range(source, start, start + 1))),
'0'..='9' =>
CstToken::new(source, start, 1, match to_digit(c) {
Ok(c) => Value::Num(c),
Result::Err(e) => Value::Err(e)
}),
_ => token.error(Unexpected(c))
},
Str(_) => match c {
'"' => return Some(token),
_ => token.grow_str(),
},
Num(n) => match c {
'0'..='9' => token.grow_num(n, c),
' '|'\n'|'\r'|'\t'|')' => return Some(token),
_ => token.error(Unexpected(c))
},
Sym(_) => match c {
'a'..='z'|'A'..='Z'|'0'..='9'|'-' => token.grow_sym(),
' '|'\n'|'\r'|'\t'|')' => return Some(token),
_ => token.error(Unexpected(c))
},
Key(_) => match c {
'a'..='z'|'0'..='9'|'-'|'/' => token.grow_key(),
' '|'\n'|'\r'|'\t'|')' => return Some(token),
_ => token.error(Unexpected(c))
},
Exp(depth, _) => match depth {
0 => return Some(token.grow_exp()),
_ => match c {
')' => token.grow_out(),
'(' => token.grow_in(),
_ => token.grow_exp(),
}
},
});
match token.value() {
Nil => None,
_ => Some(token),
}
}
//impl<S1, S2: From<S1>, X1, X2: From<S2>> From<Value<S1, X1>> for Value<S2, X2> {
//fn from (other: Value<S2, X2>) -> Self {
//use Value::*;
//match other {
//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(x) => Exp(x.into()),
//}
//}
//}