1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*!
A set of utility routines useful for all kinds of ANTLR trees.
*/

use std::ops::Deref;

use crate::tree::Tree;
use crate::utils;

/// Print out a whole tree, not just a node, in LISP format
/// {@code (root child1 .. childN)}. Print just a node if this is a leaf.
pub fn string_tree<'a, T: Tree<'a> + ?Sized>(tree: &T, rule_names: &[&str]) -> String {
    let s = utils::escape_whitespaces(get_node_text(tree, rule_names), false);
    if tree.get_child_count() == 0 {
        return s;
    }
    let mut result = String::new();
    result.push('(');
    result.extend(s.chars());
    result = tree
        .get_children()
        // .iter()
        .map(|child| string_tree(child.deref(), rule_names))
        .fold(result, |mut acc, text| {
            acc.push(' ');
            acc.extend(text.chars());
            acc
        });
    result.push(')');
    result
}

/// Print out tree node text representation (rule name or token text)
pub fn get_node_text<'a>(t: &(impl Tree<'a> + ?Sized), rule_names: &[&str]) -> String {
    t.get_node_text(rule_names)
}

//pub fn get_children(t: impl Tree) -> Vec<Rc<dyn Tree>> { unimplemented!() }
//
//pub fn get_ancestors(t: impl Tree) -> Vec<Rc<dyn Tree>> { unimplemented!() }
//
//pub fn find_all_token_nodes(t: impl ParseTree, ttype: isize) -> Vec<Rc<dyn ParseTree>> { unimplemented!() }
//
//pub fn find_all_rule_nodes(t: impl ParseTree, rule_index: isize) -> Vec<Rc<dyn ParseTree>> { unimplemented!() }
//
//pub fn find_all_nodes(t: impl ParseTree, index: isize, find_tokens: bool) -> Vec<Rc<dyn ParseTree>> { unimplemented!() }
//
////fn trees_find_all_nodes(t: ParseTree, index: isize, findTokens: bool, nodes: * Vec<ParseTree>) { unimplemented!() }
//
//pub fn descendants(t: impl ParseTree) -> Vec<dyn ParseTree> { unimplemented!() }