aboutsummaryrefslogtreecommitdiff
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
parentc8cf1d8cdac48f48caf9505bd5dc20dd2b962317 (diff)
Command-line utilty to print the parse tree
-rw-r--r--src/bin/parse-rust.rs20
-rw-r--r--src/lib.rs35
-rw-r--r--tests/parser.rs34
3 files changed, 57 insertions, 32 deletions
diff --git a/src/bin/parse-rust.rs b/src/bin/parse-rust.rs
new file mode 100644
index 000000000..3c13e732e
--- /dev/null
+++ b/src/bin/parse-rust.rs
@@ -0,0 +1,20 @@
1extern crate libsyntax2;
2
3use std::io::Read;
4
5use libsyntax2::{tokenize, parse};
6use libsyntax2::utils::dump_tree;
7
8fn main() {
9 let text = read_input();
10 let tokens = tokenize(&text);
11 let file = parse(text, &tokens);
12 let tree = dump_tree(&file);
13 println!("{}", tree);
14}
15
16fn read_input() -> String {
17 let mut buff = String::new();
18 ::std::io::stdin().read_to_string(&mut buff).unwrap();
19 buff
20}
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}
diff --git a/tests/parser.rs b/tests/parser.rs
index 02cef938d..370b02c74 100644
--- a/tests/parser.rs
+++ b/tests/parser.rs
@@ -2,9 +2,8 @@ extern crate file;
2extern crate libsyntax2; 2extern crate libsyntax2;
3extern crate testutils; 3extern crate testutils;
4 4
5use std::fmt::Write; 5use libsyntax2::{tokenize, parse};
6 6use libsyntax2::utils::dump_tree;
7use libsyntax2::{tokenize, parse, Node, File};
8use testutils::dir_tests; 7use testutils::dir_tests;
9 8
10#[test] 9#[test]
@@ -18,32 +17,3 @@ fn parser_tests() {
18 } 17 }
19 ) 18 )
20} 19}
21
22fn dump_tree(file: &File) -> String {
23 let mut result = String::new();
24 go(file.root(), &mut result, 0);
25 return result;
26
27 fn go(node: Node, buff: &mut String, level: usize) {
28 buff.push_str(&String::from(" ").repeat(level));
29 write!(buff, "{:?}\n", node).unwrap();
30 let my_errors = node.errors().filter(|e| e.after_child().is_none());
31 let parent_errors = node.parent().into_iter()
32 .flat_map(|n| n.errors())
33 .filter(|e| e.after_child() == Some(node));
34
35 for err in my_errors {
36 buff.push_str(&String::from(" ").repeat(level));
37 write!(buff, "err: `{}`\n", err.message()).unwrap();
38 }
39
40 for child in node.children() {
41 go(child, buff, level + 1)
42 }
43
44 for err in parent_errors {
45 buff.push_str(&String::from(" ").repeat(level));
46 write!(buff, "err: `{}`\n", err.message()).unwrap();
47 }
48 }
49}