delete the old parser
parent
aa6bcedcf0
commit
f0dc5f2e64
@ -1,112 +0,0 @@
|
||||
use crate::error::ParseError;
|
||||
use std::{cell::RefCell, fmt, rc::Rc};
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub enum Element {
|
||||
Empty,
|
||||
Command(String),
|
||||
Literal(String),
|
||||
Glob(String),
|
||||
}
|
||||
|
||||
impl fmt::Debug for Element {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
match self {
|
||||
Element::Empty => write!(f, "()"),
|
||||
Element::Command(cmd) => write!(f, "cmd<{}>", cmd),
|
||||
Element::Literal(lit) => write!(f, "lit<{}>", lit),
|
||||
Element::Glob(glob) => write!(f, "glob<{}>", glob),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Node {
|
||||
pub elem: Element,
|
||||
pub parent: Option<Rc<RefCell<Node>>>,
|
||||
pub children: Vec<Rc<RefCell<Node>>>,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub fn new(elem: Element) -> Self {
|
||||
Self {
|
||||
elem,
|
||||
parent: None,
|
||||
children: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn empty() -> Self {
|
||||
Self::new(Element::Empty)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn child_of(parent: Rc<RefCell<Self>>, elem: Element) -> Self {
|
||||
Self {
|
||||
elem,
|
||||
parent: Some(parent),
|
||||
children: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Node {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
write!(f, "{:?}", self.elem)?;
|
||||
if self.children.len() > 0 {
|
||||
write!(f, "[")?;
|
||||
for child in self.children.iter() {
|
||||
write!(f, "{:?}", child.borrow())?;
|
||||
}
|
||||
write!(f, "]")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Tree {
|
||||
target: Rc<RefCell<Node>>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Tree {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
write!(f, "{:?}", self.target.borrow())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ChildIter {
|
||||
/// pointer to the node in the tree whose children we are looking at.
|
||||
parent: Rc<RefCell<Node>>,
|
||||
idx: usize,
|
||||
}
|
||||
|
||||
impl Iterator for ChildIter {
|
||||
type Item = Tree;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.idx >= self.parent.borrow().children.len() {
|
||||
None
|
||||
} else {
|
||||
let child = Rc::clone(&self.parent.borrow().children[self.idx]);
|
||||
self.idx += 1;
|
||||
Some(Tree { target: child })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Tree> for Node {
|
||||
fn from(value: Tree) -> Self {
|
||||
value.target.replace(Node::empty())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Node> for Tree {
|
||||
fn from(value: Node) -> Self {
|
||||
Self {
|
||||
target: Rc::new(RefCell::new(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse<S: AsRef<str>>(text: S) -> Result<Tree, ParseError> {
|
||||
todo!()
|
||||
}
|
Loading…
Reference in New Issue