]> git.lizzy.rs Git - rust.git/blob - src/lib.rs
39b01a1cb38afb9a356fc292d528bc9ca9bbc15e
[rust.git] / src / lib.rs
1 extern crate unicode_xid;
2
3 mod text;
4 mod tree;
5 mod lexer;
6 mod parser;
7
8 #[cfg_attr(rustfmt, rustfmt_skip)]
9 pub mod syntax_kinds;
10 pub use text::{TextRange, TextUnit};
11 pub use tree::{File, FileBuilder, Node, Sink, SyntaxKind, Token};
12 pub use lexer::{next_token, tokenize};
13 pub use parser::parse;
14
15 pub mod utils {
16     use std::fmt::Write;
17
18     use {File, Node};
19
20     pub fn dump_tree(file: &File) -> String {
21         let mut result = String::new();
22         go(file.root(), &mut result, 0);
23         return result;
24
25         fn go(node: Node, buff: &mut String, level: usize) {
26             buff.push_str(&String::from("  ").repeat(level));
27             write!(buff, "{:?}\n", node).unwrap();
28             let my_errors = node.errors().filter(|e| e.after_child().is_none());
29             let parent_errors = node.parent()
30                 .into_iter()
31                 .flat_map(|n| n.errors())
32                 .filter(|e| e.after_child() == Some(node));
33
34             for err in my_errors {
35                 buff.push_str(&String::from("  ").repeat(level));
36                 write!(buff, "err: `{}`\n", err.message()).unwrap();
37             }
38
39             for child in node.children() {
40                 go(child, buff, level + 1)
41             }
42
43             for err in parent_errors {
44                 buff.push_str(&String::from("  ").repeat(level));
45                 write!(buff, "err: `{}`\n", err.message()).unwrap();
46             }
47         }
48     }
49 }