just/src/tree.rs

138 lines
3.1 KiB
Rust
Raw Normal View History

2024-05-14 20:07:41 -07:00
use {super::*, std::borrow::Cow};
/// Construct a `Tree` from a symbolic expression literal. This macro, and the
/// Tree type, are only used in the Parser unit tests, providing a concise
/// notation for representing the expected results of parsing a given string.
macro_rules! tree {
{ ($($child:tt)*) } => {
$crate::tree::Tree::List(vec![$(tree!($child),)*])
};
{ $atom:ident } => {
$crate::tree::Tree::atom(stringify!($atom))
};
{ $atom:literal } => {
$crate::tree::Tree::atom(format!("\"{}\"", $atom))
};
{ # } => {
$crate::tree::Tree::atom("#")
};
2023-12-29 12:16:31 -08:00
{ ? } => {
$crate::tree::Tree::atom("?")
};
{ + } => {
$crate::tree::Tree::atom("+")
};
{ * } => {
$crate::tree::Tree::atom("*")
};
{ && } => {
$crate::tree::Tree::atom("&&")
};
{ == } => {
$crate::tree::Tree::atom("==")
};
{ != } => {
$crate::tree::Tree::atom("!=")
};
}
/// A `Tree` is either…
#[derive(Debug, PartialEq)]
pub(crate) enum Tree<'text> {
/// …an atom containing text, or…
Atom(Cow<'text, str>),
/// …a list containing zero or more `Tree`s.
2024-05-14 20:07:41 -07:00
List(Vec<Self>),
}
impl<'text> Tree<'text> {
/// Construct an Atom from a text scalar
2024-05-14 20:07:41 -07:00
pub(crate) fn atom(text: impl Into<Cow<'text, str>>) -> Self {
Self::Atom(text.into())
}
/// Construct a List from an iterable of trees
2024-05-14 20:07:41 -07:00
pub(crate) fn list(children: impl IntoIterator<Item = Self>) -> Self {
Self::List(children.into_iter().collect())
}
/// Convenience function to create an atom containing quoted text
2024-05-14 20:07:41 -07:00
pub(crate) fn string(contents: impl AsRef<str>) -> Self {
Self::atom(format!("\"{}\"", contents.as_ref()))
}
/// Push a child node into self, turning it into a List if it was an Atom
2024-05-14 20:07:41 -07:00
pub(crate) fn push(self, tree: impl Into<Self>) -> Self {
match self {
2024-05-14 20:07:41 -07:00
Self::List(mut children) => {
children.push(tree.into());
2024-05-14 20:07:41 -07:00
Self::List(children)
}
2024-05-14 20:07:41 -07:00
Self::Atom(text) => Self::List(vec![Self::Atom(text), tree.into()]),
}
}
/// Extend a self with a tail of Trees, turning self into a List if it was an
/// Atom
2024-05-14 20:07:41 -07:00
pub(crate) fn extend<I, T>(self, tail: I) -> Self
where
I: IntoIterator<Item = T>,
2024-05-14 20:07:41 -07:00
T: Into<Self>,
{
// Tree::List(children.into_iter().collect())
let mut head = match self {
2024-05-14 20:07:41 -07:00
Self::List(children) => children,
Self::Atom(text) => vec![Self::Atom(text)],
};
for child in tail {
head.push(child.into());
}
2024-05-14 20:07:41 -07:00
Self::List(head)
}
/// Like `push`, but modify self in-place
2024-05-14 20:07:41 -07:00
pub(crate) fn push_mut(&mut self, tree: impl Into<Self>) {
*self = mem::replace(self, Self::List(Vec::new())).push(tree.into());
}
}
impl Display for Tree<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
2024-05-14 20:07:41 -07:00
Self::List(children) => {
write!(f, "(")?;
for (i, child) in children.iter().enumerate() {
if i > 0 {
write!(f, " ")?;
}
2022-12-15 16:53:21 -08:00
write!(f, "{child}")?;
}
write!(f, ")")
}
2024-05-14 20:07:41 -07:00
Self::Atom(text) => write!(f, "{text}"),
}
}
}
impl<'text, T> From<T> for Tree<'text>
where
T: Into<Cow<'text, str>>,
{
2024-05-14 20:07:41 -07:00
fn from(text: T) -> Self {
Self::Atom(text.into())
}
}