|
|
|
@ -103,7 +103,7 @@ fn next_id() -> usize {
|
|
|
|
|
/// within a parse tree. The cursor helps with the ownership structure: so long as there is a
|
|
|
|
|
/// cursor to any node on the tree, the tree remains in memory. Once there are no cursors pointed
|
|
|
|
|
/// at the tree, it is dropped.
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Cursor {
|
|
|
|
|
pub target: Rc<Node>,
|
|
|
|
|
root: Rc<Node>,
|
|
|
|
@ -148,6 +148,7 @@ impl Cursor {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// moves the cursor up to the root of the tree in place
|
|
|
|
|
pub fn up_to_root(&mut self) {
|
|
|
|
|
self.goto(Rc::clone(&self.root));
|
|
|
|
|
}
|
|
|
|
@ -174,6 +175,22 @@ impl Cursor {
|
|
|
|
|
Rc::clone(&self.root)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn text_tree(&self) -> String {
|
|
|
|
|
let mut p = self.clone();
|
|
|
|
|
p.up_to_root();
|
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
self.text_tree_helper(0, &mut buf);
|
|
|
|
|
buf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn text_tree_helper(&self, depth: u32, mut buf: &mut String) {
|
|
|
|
|
let line = format!("{:?}\n", self.value());
|
|
|
|
|
*buf = buf.to_owned() + &line;
|
|
|
|
|
for child in self.iter_children() {
|
|
|
|
|
child.text_tree_helper(depth + 1, &mut buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn goto(&mut self, next: Rc<Node>) {
|
|
|
|
|
self.prev = self.target.id;
|
|
|
|
|
self.target = next;
|
|
|
|
|