aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/parse-rust.rs20
-rw-r--r--src/lib.rs35
2 files changed, 55 insertions, 0 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}