From 333e140a50658151002c9287aa68855358bedd56 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 15:25:52 +0300 Subject: Mior --- src/lib.rs | 52 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 50 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 91d060169..f85ee1eef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,8 @@ mod lexer; mod parser; mod syntax_kinds; mod yellow; +/// Utilities for simple uses of the parser. +pub mod utils; pub use { lexer::{tokenize, Token}, @@ -42,53 +44,3 @@ pub fn parse(text: String) -> SyntaxNode { parser::parse::(text, &tokens) } -/// Utilities for simple uses of the parser. -pub mod utils { - use std::{collections::BTreeSet, fmt::Write}; - - use {SyntaxError, SyntaxNode, SyntaxNodeRef}; - - /// Parse a file and create a string representation of the resulting parse tree. - pub fn dump_tree_green(syntax: &SyntaxNode) -> String { - let syntax = syntax.borrow(); - let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect(); - let mut result = String::new(); - go(syntax, &mut result, 0, &mut errors); - return result; - - fn go( - node: SyntaxNodeRef, - buff: &mut String, - level: usize, - errors: &mut BTreeSet, - ) { - buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "{:?}\n", node).unwrap(); - let my_errors: Vec<_> = errors - .iter() - .filter(|e| e.offset == node.range().start()) - .cloned() - .collect(); - for err in my_errors { - errors.remove(&err); - buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "err: `{}`\n", err.message).unwrap(); - } - - for child in node.children() { - go(child, buff, level + 1, errors) - } - - let my_errors: Vec<_> = errors - .iter() - .filter(|e| e.offset == node.range().end()) - .cloned() - .collect(); - for err in my_errors { - errors.remove(&err); - buff.push_str(&String::from(" ").repeat(level)); - write!(buff, "err: `{}`\n", err.message).unwrap(); - } - } - } -} -- cgit v1.2.3