mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
wip: wee
This commit is contained in:
parent
d6e8be6ce5
commit
f77139c8fd
5 changed files with 68 additions and 59 deletions
|
|
@ -3,12 +3,13 @@ use std::sync::Arc;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// Owns its values, and has no metadata.
|
/// Owns its values, and has no metadata.
|
||||||
pub type AstToken = Token<AstValue, AstMeta>;
|
#[derive(PartialEq, Clone, Default, Debug)]
|
||||||
|
pub struct AstToken(pub AstValue, pub AstMeta);
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||||
pub struct AstMeta;
|
pub struct AstMeta;
|
||||||
|
|
||||||
pub type AstValue = Value<Arc<str>, AstExp>;
|
pub type AstValue = Value<Arc<str>, Vec<AstToken>>;
|
||||||
impl<'source> From<CstValue<'source>> for AstValue {
|
impl<'source> From<CstValue<'source>> for AstValue {
|
||||||
fn from (value: CstValue<'source>) -> Self {
|
fn from (value: CstValue<'source>) -> Self {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
|
|
@ -19,18 +20,15 @@ impl<'source> From<CstValue<'source>> for AstValue {
|
||||||
Sym(s) => Sym(s.into()),
|
Sym(s) => Sym(s.into()),
|
||||||
Key(s) => Key(s.into()),
|
Key(s) => Key(s.into()),
|
||||||
Str(s) => Str(s.into()),
|
Str(s) => Str(s.into()),
|
||||||
Exp(x) => Exp(x.into()),
|
Exp(d, x) => Exp(d, x.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default, PartialEq)]
|
pub trait AstIter: DslIter<Token = AstToken> {}
|
||||||
pub struct AstExp(pub Vec<AstToken>);
|
|
||||||
impl<'source> From<CstExp<'source>> for AstExp {
|
//impl<'source> From<CstExp<'source>> for AstExp {
|
||||||
fn from (exp: CstExp<'source>) -> AstExp {
|
//fn from (exp: CstExp<'source>) -> AstExp {
|
||||||
AstExp(exp.words.map(|token|Token(
|
//AstExp(exp.words.map(|token|Token(AstValue::from(token.value()), AstMeta,)).collect())
|
||||||
AstValue::from(token.value()),
|
//}
|
||||||
AstMeta,
|
//}
|
||||||
)).collect())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
/// Keeps the reference to the source slice.
|
/// Keeps the reference to the source slice.
|
||||||
pub type CstToken<'source> = Token<CstValue<'source>, CstMeta<'source>>;
|
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||||
|
pub struct CstToken<'source>(pub CstValue<'source>, pub CstMeta<'source>);
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
|
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
|
||||||
pub source: &'source str,
|
pub source: &'source str,
|
||||||
|
|
@ -9,9 +10,4 @@ pub type CstToken<'source> = Token<CstValue<'source>, CstMeta<'source>>;
|
||||||
pub length: usize,
|
pub length: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type CstValue<'source> = Value<&'source str, CstExp<'source>>;
|
pub type CstValue<'source> = Value<&'source str, SourceIter<'source>>;
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstExp<'source> {
|
|
||||||
pub depth: usize,
|
|
||||||
pub words: SourceIter<'source>
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
||||||
|
|
||||||
impl Display for AstValue {
|
impl Display for AstValue {
|
||||||
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
|
|
@ -12,17 +10,10 @@ impl Display for AstValue {
|
||||||
Sym(s) => format!("{s}"),
|
Sym(s) => format!("{s}"),
|
||||||
Key(s) => format!("{s}"),
|
Key(s) => format!("{s}"),
|
||||||
Str(s) => format!("{s}"),
|
Str(s) => format!("{s}"),
|
||||||
Exp(e) => format!("{e:?}"),
|
Exp(_, e) => format!("{e:?}"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for AstExp {
|
|
||||||
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'source> Display for CstValue<'source> {
|
impl<'source> Display for CstValue<'source> {
|
||||||
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
|
|
@ -33,7 +24,7 @@ impl<'source> Display for CstValue<'source> {
|
||||||
Sym(s) => format!("{s}"),
|
Sym(s) => format!("{s}"),
|
||||||
Key(s) => format!("{s}"),
|
Key(s) => format!("{s}"),
|
||||||
Str(s) => format!("{s}"),
|
Str(s) => format!("{s}"),
|
||||||
Exp(e) => format!("{e:?}"),
|
Exp(_, e) => format!("{e:?}"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,30 +13,48 @@ pub trait DslIter {
|
||||||
/// [Cst::next] returns just the [CstToken] and mutates `self`,
|
/// [Cst::next] returns just the [CstToken] and mutates `self`,
|
||||||
/// instead of returning an updated version of the struct as [SourceConstIter::next] does.
|
/// instead of returning an updated version of the struct as [SourceConstIter::next] does.
|
||||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||||
pub struct SourceIter<'a>(pub SourceConstIter<'a>);
|
pub struct SourceIter<'source>(pub SourceConstIter<'source>);
|
||||||
|
|
||||||
impl<'a> SourceIter<'a> {
|
impl<'source> SourceIter<'source> {
|
||||||
pub const fn new (source: &'a str) -> Self {
|
pub const fn new (source: &'source str) -> Self {
|
||||||
Self(SourceConstIter::new(source))
|
Self(SourceConstIter::new(source))
|
||||||
}
|
}
|
||||||
pub const fn peek (&self) -> Option<CstToken<'a>> {
|
pub const fn peek (&self) -> Option<CstToken<'source>> {
|
||||||
self.0.peek()
|
self.0.peek()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for SourceIter<'a> {
|
impl<'source> Iterator for SourceIter<'source> {
|
||||||
type Item = CstToken<'a>;
|
type Item = CstToken<'source>;
|
||||||
fn next (&mut self) -> Option<CstToken<'a>> {
|
fn next (&mut self) -> Option<CstToken<'source>> {
|
||||||
self.0.next().map(|(item, rest)|{self.0 = rest; item})
|
self.0.next().map(|(item, rest)|{self.0 = rest; item})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a str> for SourceIter<'a> {
|
impl<'source> From<&'source str> for SourceIter<'source> {
|
||||||
fn from (source: &'a str) -> Self{
|
fn from (source: &'source str) -> Self{
|
||||||
Self(SourceConstIter(source))
|
Self(SourceConstIter(source))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'source> Into<Vec<CstToken<'source>>> for SourceIter<'source> {
|
||||||
|
fn into (self) -> Vec<CstToken<'source>> {
|
||||||
|
self.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'source> Into<Vec<AstToken>> for SourceIter<'source> {
|
||||||
|
fn into (self) -> Vec<AstToken> {
|
||||||
|
self.map(Into::into).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'source> From<CstToken<'source>> for AstToken {
|
||||||
|
fn from (value: CstToken<'source>) -> Self {
|
||||||
|
Self(value.0.into(), AstMeta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Implement the const iterator pattern.
|
/// Implement the const iterator pattern.
|
||||||
#[macro_export] macro_rules! const_iter {
|
#[macro_export] macro_rules! const_iter {
|
||||||
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
||||||
|
|
@ -58,34 +76,34 @@ impl<'a> From<&'a str> for SourceIter<'a> {
|
||||||
/// * the source text remaining
|
/// * the source text remaining
|
||||||
/// * [ ] TODO: maybe [SourceConstIter::next] should wrap the remaining source in `Self` ?
|
/// * [ ] TODO: maybe [SourceConstIter::next] should wrap the remaining source in `Self` ?
|
||||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||||
pub struct SourceConstIter<'a>(pub &'a str);
|
pub struct SourceConstIter<'source>(pub &'source str);
|
||||||
|
|
||||||
impl<'a> From<SourceConstIter<'a>> for SourceIter<'a> {
|
impl<'source> From<SourceConstIter<'source>> for SourceIter<'source> {
|
||||||
fn from (source: SourceConstIter<'a>) -> Self{
|
fn from (source: SourceConstIter<'source>) -> Self{
|
||||||
Self(source)
|
Self(source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a str> for SourceConstIter<'a> {
|
impl<'source> From<&'source str> for SourceConstIter<'source> {
|
||||||
fn from (source: &'a str) -> Self{
|
fn from (source: &'source str) -> Self{
|
||||||
Self::new(source)
|
Self::new(source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SourceConstIter<'a> {
|
impl<'source> SourceConstIter<'source> {
|
||||||
pub const fn new (source: &'a str) -> Self {
|
pub const fn new (source: &'source str) -> Self {
|
||||||
Self(source)
|
Self(source)
|
||||||
}
|
}
|
||||||
pub const fn chomp (&self, index: usize) -> Self {
|
pub const fn chomp (&self, index: usize) -> Self {
|
||||||
Self(split_at(self.0, index).1)
|
Self(split_at(self.0, index).1)
|
||||||
}
|
}
|
||||||
pub const fn next (mut self) -> Option<(CstToken<'a>, Self)> {
|
pub const fn next (mut self) -> Option<(CstToken<'source>, Self)> {
|
||||||
Self::next_mut(&mut self)
|
Self::next_mut(&mut self)
|
||||||
}
|
}
|
||||||
pub const fn peek (&self) -> Option<CstToken<'a>> {
|
pub const fn peek (&self) -> Option<CstToken<'source>> {
|
||||||
peek_src(self.0)
|
peek_src(self.0)
|
||||||
}
|
}
|
||||||
pub const fn next_mut (&mut self) -> Option<(CstToken<'a>, Self)> {
|
pub const fn next_mut (&mut self) -> Option<(CstToken<'source>, Self)> {
|
||||||
match self.peek() {
|
match self.peek() {
|
||||||
Some(token) => Some((token, self.chomp(token.end()))),
|
Some(token) => Some((token, self.chomp(token.end()))),
|
||||||
None => None
|
None => None
|
||||||
|
|
@ -93,4 +111,4 @@ impl<'a> SourceConstIter<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iter!(<'a>|self: SourceConstIter<'a>| => CstToken<'a> => self.next_mut().map(|(result, _)|result));
|
const_iter!(<'source>|self: SourceConstIter<'source>| => CstToken<'source> => self.next_mut().map(|(result, _)|result));
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,14 @@ pub enum Value<
|
||||||
S: PartialEq + Clone + Default + Debug,
|
S: PartialEq + Clone + Default + Debug,
|
||||||
X: PartialEq + Clone + Default + Debug,
|
X: PartialEq + Clone + Default + Debug,
|
||||||
> {
|
> {
|
||||||
#[default] Nil, Err(DslError), Num(usize), Sym(S), Key(S), Str(S), Exp(X),
|
#[default] Nil, Err(DslError), Num(usize), Sym(S), Key(S), Str(S), Exp(usize, X),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<
|
||||||
|
S: Copy + PartialEq + Clone + Default + Debug,
|
||||||
|
X: Copy + PartialEq + Clone + Default + Debug,
|
||||||
|
> Copy for Value<S, X> {}
|
||||||
|
|
||||||
pub trait DslValue: PartialEq + Clone + Default + Debug {
|
pub trait DslValue: PartialEq + Clone + Default + Debug {
|
||||||
type Err: PartialEq + Clone + Debug;
|
type Err: PartialEq + Clone + Debug;
|
||||||
type Num: PartialEq + Copy + Clone + Default + Debug;
|
type Num: PartialEq + Copy + Clone + Default + Debug;
|
||||||
|
|
@ -20,8 +25,9 @@ pub trait DslValue: PartialEq + Clone + Default + Debug {
|
||||||
fn key (&self) -> Option<&Self::Str>;
|
fn key (&self) -> Option<&Self::Str>;
|
||||||
fn str (&self) -> Option<&Self::Str>;
|
fn str (&self) -> Option<&Self::Str>;
|
||||||
fn exp (&self) -> Option<&Self::Exp>;
|
fn exp (&self) -> Option<&Self::Exp>;
|
||||||
fn exp_head (&self) -> Option<&Self> { None } // TODO
|
fn exp_depth (&self) -> Option<usize> { None } // TODO
|
||||||
fn exp_tail (&self) -> Option<&[Self]> { None } // TODO
|
fn exp_head (&self) -> Option<&Self> { None } // TODO
|
||||||
|
fn exp_tail (&self) -> Option<&[Self]> { None } // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
|
|
@ -51,7 +57,7 @@ impl<
|
||||||
if let Self::Str(s) = self { Some(s) } else { None }
|
if let Self::Str(s) = self { Some(s) } else { None }
|
||||||
}
|
}
|
||||||
fn exp (&self) -> Option<&Exp> {
|
fn exp (&self) -> Option<&Exp> {
|
||||||
if let Self::Exp(x) = self { Some(x) } else { None }
|
if let Self::Exp(_, x) = self { Some(x) } else { None }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -136,14 +142,14 @@ impl<'source> CstToken<'source> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
let token = self.grow();
|
let token = self.grow();
|
||||||
if let Exp(depth, _) = token.value() {
|
if let Exp(depth, _) = token.value() {
|
||||||
token.with_value(Exp(depth, Cst::new(token.slice_source_exp(self.source))))
|
token.with_value(Exp(depth, SourceIter::new(token.slice_source_exp(self.1.source))))
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub const fn grow_in (self) -> Self {
|
pub const fn grow_in (self) -> Self {
|
||||||
let token = self.grow_exp();
|
let token = self.grow_exp();
|
||||||
if let Value::Exp(depth, source) = token.value {
|
if let Value::Exp(depth, source) = token.value() {
|
||||||
token.with_value(Value::Exp(depth.saturating_add(1), source))
|
token.with_value(Value::Exp(depth.saturating_add(1), source))
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
|
|
@ -151,7 +157,7 @@ impl<'source> CstToken<'source> {
|
||||||
}
|
}
|
||||||
pub const fn grow_out (self) -> Self {
|
pub const fn grow_out (self) -> Self {
|
||||||
let token = self.grow_exp();
|
let token = self.grow_exp();
|
||||||
if let Value::Exp(depth, source) = token.value {
|
if let Value::Exp(depth, source) = token.value() {
|
||||||
if depth > 0 {
|
if depth > 0 {
|
||||||
token.with_value(Value::Exp(depth - 1, source))
|
token.with_value(Value::Exp(depth - 1, source))
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -200,7 +206,7 @@ pub const fn peek_src <'a> (source: &'a str) -> Option<CstToken<'a>> {
|
||||||
' '|'\n'|'\r'|'\t' =>
|
' '|'\n'|'\r'|'\t' =>
|
||||||
token.grow(),
|
token.grow(),
|
||||||
'(' =>
|
'(' =>
|
||||||
CstToken::new(source, start, 1, Exp(1, Cst::new(str_range(source, start, start + 1)))),
|
CstToken::new(source, start, 1, Exp(1, SourceIter::new(str_range(source, start, start + 1)))),
|
||||||
'"' =>
|
'"' =>
|
||||||
CstToken::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
CstToken::new(source, start, 1, Str(str_range(source, start, start + 1))),
|
||||||
':'|'@' =>
|
':'|'@' =>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue