do_not_use_antlr_rust/
trees.rs

1/*!
2A set of utility routines useful for all kinds of ANTLR trees.
3*/
4
5use std::ops::Deref;
6
7use crate::tree::Tree;
8use crate::utils;
9
10/// Print out a whole tree, not just a node, in LISP format
11/// {@code (root child1 .. childN)}. Print just a node if this is a leaf.
12pub fn string_tree<'a, T: Tree<'a> + ?Sized>(tree: &T, rule_names: &[&str]) -> String {
13    let s = utils::escape_whitespaces(get_node_text(tree, rule_names), false);
14    if tree.get_child_count() == 0 {
15        return s;
16    }
17    let mut result = String::new();
18    result.push('(');
19    result.extend(s.chars());
20    result = tree
21        .get_children()
22        // .iter()
23        .map(|child| string_tree(child.deref(), rule_names))
24        .fold(result, |mut acc, text| {
25            acc.push(' ');
26            acc.extend(text.chars());
27            acc
28        });
29    result.push(')');
30    result
31}
32
33/// Print out tree node text representation (rule name or token text)
34pub fn get_node_text<'a>(t: &(impl Tree<'a> + ?Sized), rule_names: &[&str]) -> String {
35    t.get_node_text(rule_names)
36}
37
38//pub fn get_children(t: impl Tree) -> Vec<Rc<dyn Tree>> { unimplemented!() }
39//
40//pub fn get_ancestors(t: impl Tree) -> Vec<Rc<dyn Tree>> { unimplemented!() }
41//
42//pub fn find_all_token_nodes(t: impl ParseTree, ttype: isize) -> Vec<Rc<dyn ParseTree>> { unimplemented!() }
43//
44//pub fn find_all_rule_nodes(t: impl ParseTree, rule_index: isize) -> Vec<Rc<dyn ParseTree>> { unimplemented!() }
45//
46//pub fn find_all_nodes(t: impl ParseTree, index: isize, find_tokens: bool) -> Vec<Rc<dyn ParseTree>> { unimplemented!() }
47//
48////fn trees_find_all_nodes(t: ParseTree, index: isize, findTokens: bool, nodes: * Vec<ParseTree>) { unimplemented!() }
49//
50//pub fn descendants(t: impl ParseTree) -> Vec<dyn ParseTree> { unimplemented!() }