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

View file

@ -1,291 +1,17 @@
use crate::*;
/// Provides a native [Iterator] API over [CstConstIter],
/// emitting [CstToken] items.
///
/// [Cst::next] returns just the [CstToken] and mutates `self`,
/// instead of returning an updated version of the struct as [CstConstIter::next] does.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Cst<'a>(pub CstConstIter<'a>);
/// Keeps the reference to the source slice.
pub type CstToken<'source> = Token<CstValue<'source>, CstMeta<'source>>;
/// Owns a reference to the source text.
/// [CstConstIter::next] emits subsequent pairs of:
/// * a [CstToken] 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<'a>(pub &'a str);
/// A CST token, with reference to the source slice.
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstToken<'source> {
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
pub source: &'source str,
pub start: usize,
pub length: usize,
pub value: CstValue<'source>,
}
/// The meaning of a CST token. Strip the source from this to get an [AstValue].
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub enum CstValue<'source> {
#[default] Nil,
Err(DslError),
Num(usize),
Sym(&'source str),
Key(&'source str),
Str(&'source str),
Exp(usize, Cst<'source>),
}
pub type CstValue<'source> = Value<&'source str, CstExp<'source>>;
impl<'a> Cst<'a> {
pub const fn new (source: &'a str) -> Self {
Self(CstConstIter::new(source))
}
pub const fn peek (&self) -> Option<CstToken<'a>> {
self.0.peek()
}
}
impl<'a> Iterator for Cst<'a> {
type Item = CstToken<'a>;
fn next (&mut self) -> Option<CstToken<'a>> {
self.0.next().map(|(item, rest)|{self.0 = rest; item})
}
}
impl<'a> From<&'a str> for Cst<'a> {
fn from (source: &'a str) -> Self{
Self(CstConstIter(source))
}
}
impl<'a> From<CstConstIter<'a>> for Cst<'a> {
fn from (source: CstConstIter<'a>) -> Self{
Self(source)
}
}
/// Implement the const iterator pattern.
#[macro_export] macro_rules! const_iter {
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
impl$(<$l>)? Iterator for $Struct {
type Item = $Item;
fn next (&mut $self) -> Option<$Item> { $expr }
}
impl$(<$l>)? ConstIntoIter for $Struct {
type Kind = IsIteratorKind;
type Item = $Item;
type IntoIter = Self;
}
}
}
const_iter!(<'a>|self: CstConstIter<'a>| => CstToken<'a> => self.next_mut().map(|(result, _)|result));
impl<'a> From<&'a str> for CstConstIter<'a> {
fn from (source: &'a str) -> Self{
Self::new(source)
}
}
impl<'a> CstConstIter<'a> {
pub const fn new (source: &'a str) -> Self {
Self(source)
}
pub const fn chomp (&self, index: usize) -> Self {
Self(split_at(self.0, index).1)
}
pub const fn next (mut self) -> Option<(CstToken<'a>, Self)> {
Self::next_mut(&mut self)
}
pub const fn peek (&self) -> Option<CstToken<'a>> {
peek_src(self.0)
}
pub const fn next_mut (&mut self) -> Option<(CstToken<'a>, Self)> {
match self.peek() {
Some(token) => Some((token, self.chomp(token.end()))),
None => None
}
}
}
/// 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 CstValue::*;
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) => CstValue::Num(c),
Result::Err(e) => CstValue::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),
}
}
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))
})
}
impl<'source> CstToken<'source> {
pub const fn new (
source: &'source str, start: usize, length: usize, value: CstValue<'source>
) -> Self {
Self { source, start, length, value }
}
pub const fn end (&self) -> usize {
self.start.saturating_add(self.length)
}
pub const fn slice (&'source self) -> &'source str {
self.slice_source(self.source)
}
pub const fn slice_source <'range> (&'source self, source: &'range str) -> &'range str {
str_range(source, self.start, self.end())
}
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: CstValue<'source>) -> Self {
Self { value, ..self }
}
pub const fn value (&self) -> CstValue {
self.value
}
pub const fn error (self, error: DslError) -> Self {
Self { value: CstValue::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 {
use CstValue::*;
match to_digit(c) {
Ok(n) => Self { value: Num(10*m+n), ..self.grow() },
Result::Err(e) => Self { value: Err(e), ..self.grow() },
}
}
pub const fn grow_key (self) -> Self {
use CstValue::*;
let token = self.grow();
token.with_value(Key(token.slice_source(self.source)))
}
pub const fn grow_sym (self) -> Self {
use CstValue::*;
let token = self.grow();
token.with_value(Sym(token.slice_source(self.source)))
}
pub const fn grow_str (self) -> Self {
use CstValue::*;
let token = self.grow();
token.with_value(Str(token.slice_source(self.source)))
}
pub const fn grow_exp (self) -> Self {
use CstValue::*;
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 CstValue::Exp(depth, source) = token.value {
token.with_value(CstValue::Exp(depth.saturating_add(1), source))
} else {
unreachable!()
}
}
pub const fn grow_out (self) -> Self {
let token = self.grow_exp();
if let CstValue::Exp(depth, source) = token.value {
if depth > 0 {
token.with_value(CstValue::Exp(depth - 1, source))
} else {
return self.error(Unexpected(')'))
}
} else {
unreachable!()
}
}
}
impl<'source> std::fmt::Display for CstValue<'source> {
fn fmt (&self, out: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
use CstValue::*;
write!(out, "{}", match self {
Nil => String::new(),
Err(e) => format!("[error: {e}]"),
Num(n) => format!("{n}"),
Sym(s) => format!("{s}"),
Key(s) => format!("{s}"),
Str(s) => format!("{s}"),
Exp(_, e) => format!("{e:?}"),
})
}
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstExp<'source> {
pub depth: usize,
pub words: SourceIter<'source>
}