aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-01-21 23:12:26 +0000
committerAleksey Kladov <[email protected]>2018-01-21 23:12:26 +0000
commit05ad469ac33965f76ccc0f5b8a9959695b8979a0 (patch)
tree1212adb51b8970a458c322cf687f625c5ef612e7 /src/lib.rs
parentc8cf1d8cdac48f48caf9505bd5dc20dd2b962317 (diff)
Command-line utilty to print the parse tree
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index d95e26662..7fd9e547a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -10,3 +10,38 @@ pub use text::{TextUnit, TextRange};
10pub use tree::{SyntaxKind, Token, FileBuilder, Sink, File, Node}; 10pub use tree::{SyntaxKind, Token, FileBuilder, Sink, File, Node};
11pub use lexer::{next_token, tokenize}; 11pub use lexer::{next_token, tokenize};
12pub use parser::parse; 12pub use parser::parse;
13
14pub mod utils {
15 use std::fmt::Write;
16
17 use {File, Node};
18
19 pub fn dump_tree(file: &File) -> String {
20 let mut result = String::new();
21 go(file.root(), &mut result, 0);
22 return result;
23
24 fn go(node: Node, buff: &mut String, level: usize) {
25 buff.push_str(&String::from(" ").repeat(level));
26 write!(buff, "{:?}\n", node).unwrap();
27 let my_errors = node.errors().filter(|e| e.after_child().is_none());
28 let parent_errors = node.parent().into_iter()
29 .flat_map(|n| n.errors())
30 .filter(|e| e.after_child() == Some(node));
31
32 for err in my_errors {
33 buff.push_str(&String::from(" ").repeat(level));
34 write!(buff, "err: `{}`\n", err.message()).unwrap();
35 }
36
37 for child in node.children() {
38 go(child, buff, level + 1)
39 }
40
41 for err in parent_errors {
42 buff.push_str(&String::from(" ").repeat(level));
43 write!(buff, "err: `{}`\n", err.message()).unwrap();
44 }
45 }
46 }
47}