diff --git a/src/bin/parse-rust.rs b/src/bin/parse-rust.rs new file mode 100644 index 0000000000..3c13e732ed --- /dev/null +++ b/src/bin/parse-rust.rs @@ -0,0 +1,20 @@ +extern crate libsyntax2; + +use std::io::Read; + +use libsyntax2::{tokenize, parse}; +use libsyntax2::utils::dump_tree; + +fn main() { + let text = read_input(); + let tokens = tokenize(&text); + let file = parse(text, &tokens); + let tree = dump_tree(&file); + println!("{}", tree); +} + +fn read_input() -> String { + let mut buff = String::new(); + ::std::io::stdin().read_to_string(&mut buff).unwrap(); + buff +} diff --git a/src/lib.rs b/src/lib.rs index d95e266624..7fd9e547a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,3 +10,38 @@ pub use text::{TextUnit, TextRange}; pub use tree::{SyntaxKind, Token, FileBuilder, Sink, File, Node}; pub use lexer::{next_token, tokenize}; pub use parser::parse; + +pub mod utils { + use std::fmt::Write; + + use {File, Node}; + + pub fn dump_tree(file: &File) -> String { + let mut result = String::new(); + go(file.root(), &mut result, 0); + return result; + + fn go(node: Node, buff: &mut String, level: usize) { + buff.push_str(&String::from(" ").repeat(level)); + write!(buff, "{:?}\n", node).unwrap(); + let my_errors = node.errors().filter(|e| e.after_child().is_none()); + let parent_errors = node.parent().into_iter() + .flat_map(|n| n.errors()) + .filter(|e| e.after_child() == Some(node)); + + for err in my_errors { + buff.push_str(&String::from(" ").repeat(level)); + write!(buff, "err: `{}`\n", err.message()).unwrap(); + } + + for child in node.children() { + go(child, buff, level + 1) + } + + for err in parent_errors { + buff.push_str(&String::from(" ").repeat(level)); + write!(buff, "err: `{}`\n", err.message()).unwrap(); + } + } + } +} diff --git a/tests/parser.rs b/tests/parser.rs index 02cef938d3..370b02c741 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -2,9 +2,8 @@ extern crate file; extern crate libsyntax2; extern crate testutils; -use std::fmt::Write; - -use libsyntax2::{tokenize, parse, Node, File}; +use libsyntax2::{tokenize, parse}; +use libsyntax2::utils::dump_tree; use testutils::dir_tests; #[test] @@ -18,32 +17,3 @@ fn parser_tests() { } ) } - -fn dump_tree(file: &File) -> String { - let mut result = String::new(); - go(file.root(), &mut result, 0); - return result; - - fn go(node: Node, buff: &mut String, level: usize) { - buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "{:?}\n", node).unwrap(); - let my_errors = node.errors().filter(|e| e.after_child().is_none()); - let parent_errors = node.parent().into_iter() - .flat_map(|n| n.errors()) - .filter(|e| e.after_child() == Some(node)); - - for err in my_errors { - buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "err: `{}`\n", err.message()).unwrap(); - } - - for child in node.children() { - go(child, buff, level + 1) - } - - for err in parent_errors { - buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "err: `{}`\n", err.message()).unwrap(); - } - } -}