From 05ad469ac33965f76ccc0f5b8a9959695b8979a0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Jan 2018 02:12:26 +0300 Subject: Command-line utilty to print the parse tree --- src/bin/parse-rust.rs | 20 ++++++++++++++++++++ src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ tests/parser.rs | 34 ++-------------------------------- 3 files changed, 57 insertions(+), 32 deletions(-) create mode 100644 src/bin/parse-rust.rs 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 @@ +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 d95e26662..7fd9e547a 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 02cef938d..370b02c74 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(); - } - } -} -- cgit v1.2.3