AstToken -> Ast <- AstValue

This commit is contained in:
🪞👃🪞 2025-05-26 23:32:59 +03:00
parent f77139c8fd
commit e9d4c7e0bc
5 changed files with 30 additions and 40 deletions

View file

@ -2,15 +2,21 @@ use crate::*;
use std::sync::Arc;
use std::borrow::Cow;
/// Owns its values, and has no metadata.
#[derive(PartialEq, Clone, Default, Debug)]
pub struct AstToken(pub AstValue, pub AstMeta);
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct AstMeta;
pub type AstValue = Value<Arc<str>, Vec<AstToken>>;
impl<'source> From<CstValue<'source>> for AstValue {
pub struct AstIter;
impl<'source> From<SourceIter<'source>> for AstIter {
fn from (source: SourceIter<'source>) -> Self {
Self // TODO
}
}
/// Owns its values, and has no metadata.
pub type Ast = Value<Arc<str>, AstIter>;
impl<'source> From<CstToken<'source>> for Ast {
fn from (token: CstToken<'source>) -> Self {
token.value().into()
}
}
impl<'source> From<CstValue<'source>> for Ast {
fn from (value: CstValue<'source>) -> Self {
use Value::*;
match value {
@ -24,11 +30,3 @@ impl<'source> From<CstValue<'source>> for AstValue {
}
}
}
pub trait AstIter: DslIter<Token = AstToken> {}
//impl<'source> From<CstExp<'source>> for AstExp {
//fn from (exp: CstExp<'source>) -> AstExp {
//AstExp(exp.words.map(|token|Token(AstValue::from(token.value()), AstMeta,)).collect())
//}
//}

View file

@ -1,6 +1,6 @@
use crate::*;
use std::fmt::{Debug, Display, Formatter, Error as FormatError};
impl Display for AstValue {
impl Display for Ast {
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
use Value::*;
write!(out, "{}", match self {

View file

@ -43,18 +43,12 @@ impl<'source> Into<Vec<CstToken<'source>>> for SourceIter<'source> {
}
}
impl<'source> Into<Vec<AstToken>> for SourceIter<'source> {
fn into (self) -> Vec<AstToken> {
impl<'source> Into<Vec<Ast>> for SourceIter<'source> {
fn into (self) -> Vec<Ast> {
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.
#[macro_export] macro_rules! const_iter {
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {