This commit is contained in:
🪞👃🪞 2025-07-14 22:22:45 +03:00
parent 6c3a0964ec
commit 7271081fc9
10 changed files with 119 additions and 296 deletions

View file

@ -5,10 +5,10 @@ use crate::*;
/// CST stores strings as source references and expressions as [CstIter] instances.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Cst<'src>(pub CstIter<'src>);
impl<'src> Dsl for Cst<'src> {
type Str = &'src str;
type Exp = CstIter<'src>;
pub struct Cst<'s>(pub CstIter<'s>);
impl<'s> Dsl for Cst<'s> {
type Str = &'s str;
type Exp = CstIter<'s>;
fn nth (&self, index: usize) -> Option<DslVal<Self::Str, Self::Exp>> {
self.0.nth(index)
}
@ -16,49 +16,49 @@ impl<'src> Dsl for Cst<'src> {
/// Parsed substring with range and value.
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct CstVal<'src> {
pub struct CstVal<'s> {
/// Meaning of token.
pub value: DslVal<&'src str, CstIter<'src>>,
pub value: DslVal<&'s str, CstIter<'s>>,
/// Reference to source text.
pub source: &'src str,
pub source: &'s str,
/// Index of 1st character of token.
pub start: usize,
/// Length of token.
pub length: usize,
}
impl<'src> Dsl for CstVal<'src> {
type Str = &'src str;
type Exp = CstIter<'src>;
impl<'s> Dsl for CstVal<'s> {
type Str = &'s str;
type Exp = CstIter<'s>;
fn nth (&self, index: usize) -> Option<DslVal<Self::Str, Self::Exp>> {
todo!()
}
}
impl<'src> CstVal<'src> {
impl<'s> CstVal<'s> {
pub const fn new (
source: &'src str,
source: &'s str,
start: usize,
length: usize,
value: DslVal<&'src str, CstIter<'src>>
value: DslVal<&'s str, CstIter<'s>>
) -> Self {
Self { source, start, length, value }
}
pub const fn end (&self) -> usize {
self.start.saturating_add(self.length)
}
pub const fn slice (&'src self) -> &'src str {
pub const fn slice (&'s self) -> &'s str {
self.slice_source(self.source)
}
pub const fn slice_source <'range> (&'src self, source: &'range str) -> &'range str {
pub const fn slice_source <'range> (&'s self, source: &'range str) -> &'range str {
str_range(source, self.start, self.end())
}
pub const fn slice_source_exp <'range> (&'src self, source: &'range str) -> &'range str {
pub const fn slice_source_exp <'range> (&'s self, source: &'range str) -> &'range str {
str_range(source, self.start.saturating_add(1), self.end())
}
pub const fn with_value (self, value: DslVal<&'src str, CstIter<'src>>) -> Self {
pub const fn with_value (self, value: DslVal<&'s str, CstIter<'s>>) -> Self {
Self { value, ..self }
}
pub const fn value (&self) -> DslVal<&'src str, CstIter<'src>> {
pub const fn value (&self) -> DslVal<&'s str, CstIter<'s>> {
self.value
}
pub const fn error (self, error: DslErr) -> Self {
@ -121,46 +121,37 @@ impl<'src> CstVal<'src> {
/// [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<'src>(pub CstConstIter<'src>);
impl<'src> Dsl for CstIter<'src> {
type Str = &'src str;
pub struct CstIter<'s>(pub CstConstIter<'s>);
impl<'s> Dsl for CstIter<'s> {
type Str = &'s str;
type Exp = Self;
fn nth (&self, index: usize) -> Option<DslVal<Self::Str, Self::Exp>> {
use DslVal::*;
self.0.nth(index).map(|x|match x {
Nil => Nil,
Err(e) => Err(e),
Num(u) => Num(u),
Sym(s) => Sym(s),
Key(s) => Sym(s),
Str(s) => Sym(s),
Exp(d, x) => DslVal::Exp(d, CstIter(x)),
})
self.0.nth(index).map(|x|dsl_val(x))
}
}
impl<'src> CstIter<'src> {
pub const fn new (source: &'src str) -> Self {
impl<'s> CstIter<'s> {
pub const fn new (source: &'s str) -> Self {
Self(CstConstIter::new(source))
}
pub const fn peek (&self) -> Option<CstVal<'src>> {
pub const fn peek (&self) -> Option<CstVal<'s>> {
self.0.peek()
}
}
impl<'src> Iterator for CstIter<'src> {
type Item = CstVal<'src>;
fn next (&mut self) -> Option<CstVal<'src>> {
impl<'s> Iterator for CstIter<'s> {
type Item = CstVal<'s>;
fn next (&mut self) -> Option<CstVal<'s>> {
self.0.next().map(|(item, rest)|{
self.0 = rest;
item
})
}
}
impl<'src> Into<Vec<CstVal<'src>>> for CstIter<'src> {
fn into (self) -> Vec<CstVal<'src>> {
impl<'s> Into<Vec<CstVal<'s>>> for CstIter<'s> {
fn into (self) -> Vec<CstVal<'s>> {
self.collect()
}
}
impl<'src> Into<Vec<Ast>> for CstIter<'src> {
impl<'s> Into<Vec<Ast>> for CstIter<'s> {
fn into (self) -> Vec<Ast> {
self.map(Into::into).collect()
}
@ -172,9 +163,9 @@ impl<'src> Into<Vec<Ast>> for CstIter<'src> {
/// * the source text remaining
/// * [ ] TODO: maybe [CstConstIter::next] should wrap the remaining source in `Self` ?
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct CstConstIter<'src>(pub &'src str);
impl<'src> Dsl for CstConstIter<'src> {
type Str = &'src str;
pub struct CstConstIter<'s>(pub &'s str);
impl<'s> Dsl for CstConstIter<'s> {
type Str = &'s str;
type Exp = Self;
fn nth (&self, mut index: usize) -> Option<DslVal<Self::Str, Self::Exp>> {
use DslVal::*;
@ -182,37 +173,39 @@ impl<'src> Dsl for CstConstIter<'src> {
for i in 0..index {
iter = iter.next()?.1
}
iter.next().map(|(x, _)|match x.value {
Nil => Nil,
Err(e) => Err(e),
Num(u) => Num(u),
Sym(s) => Sym(s),
Key(s) => Sym(s),
Str(s) => Sym(s),
Exp(d, x) => DslVal::Exp(d, x.0),
})
iter.next().map(|(x, _)|dsl_val(x.value))
}
}
impl<'src> CstConstIter<'src> {
pub const fn new (source: &'src str) -> 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 next (mut self) -> Option<(CstVal<'src>, Self)> {
pub const fn next (mut self) -> Option<(CstVal<'s>, Self)> {
Self::next_mut(&mut self)
}
pub const fn peek (&self) -> Option<CstVal<'src>> {
pub const fn peek (&self) -> Option<CstVal<'s>> {
peek_src(self.0)
}
pub const fn next_mut (&mut self) -> Option<(CstVal<'src>, Self)> {
pub const fn next_mut (&mut self) -> Option<(CstVal<'s>, Self)> {
match self.peek() {
Some(token) => Some((token, self.chomp(token.end()))),
None => None
}
}
}
impl<'s> From<CstConstIter<'s>> for CstIter<'s> {
fn from (iter: CstConstIter<'s>) -> Self {
Self(iter)
}
}
impl<'s> From<CstIter<'s>> for CstConstIter<'s> {
fn from (iter: CstIter<'s>) -> Self {
iter.0
}
}
/// Implement the const iterator pattern.
macro_rules! const_iter {
@ -229,8 +222,8 @@ macro_rules! const_iter {
}
}
const_iter!(<'src>|self: CstConstIter<'src>|
=> CstVal<'src>
const_iter!(<'s>|self: CstConstIter<'s>|
=> CstVal<'s>
=> self.next_mut().map(|(result, _)|result));
/// Static iteration helper used by [cst].
@ -244,9 +237,9 @@ macro_rules! iterate {
}
}
pub const fn peek_src <'src> (source: &'src str) -> Option<CstVal<'src>> {
pub const fn peek_src <'s> (source: &'s str) -> Option<CstVal<'s>> {
use DslVal::*;
let mut token: CstVal<'src> = CstVal::new(source, 0, 0, Nil);
let mut token: CstVal<'s> = CstVal::new(source, 0, 0, Nil);
iterate!(char_indices(source) => (start, c) => token = match token.value() {
Err(_) => return Some(token),
Nil => match c {