diff options
author | Aleksey Kladov <[email protected]> | 2018-07-30 13:25:52 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-07-30 13:25:52 +0100 |
commit | 333e140a50658151002c9287aa68855358bedd56 (patch) | |
tree | 21cd482ab080b00d5b93ebaf8e9096644bafa4b3 /src/utils.rs | |
parent | d30a2e4fac9267066dc53d71c4695843f95e6269 (diff) |
Mior
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 000000000..20991659a --- /dev/null +++ b/src/utils.rs | |||
@@ -0,0 +1,46 @@ | |||
1 | use std::{collections::BTreeSet, fmt::Write}; | ||
2 | use {SyntaxError, SyntaxNode, SyntaxNodeRef}; | ||
3 | |||
4 | /// Parse a file and create a string representation of the resulting parse tree. | ||
5 | pub fn dump_tree(syntax: &SyntaxNode) -> String { | ||
6 | let syntax = syntax.borrow(); | ||
7 | let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect(); | ||
8 | let mut result = String::new(); | ||
9 | go(syntax, &mut result, 0, &mut errors); | ||
10 | return result; | ||
11 | |||
12 | fn go( | ||
13 | node: SyntaxNodeRef, | ||
14 | buff: &mut String, | ||
15 | level: usize, | ||
16 | errors: &mut BTreeSet<SyntaxError>, | ||
17 | ) { | ||
18 | buff.push_str(&String::from(" ").repeat(level)); | ||
19 | write!(buff, "{:?}\n", node).unwrap(); | ||
20 | let my_errors: Vec<_> = errors | ||
21 | .iter() | ||
22 | .filter(|e| e.offset == node.range().start()) | ||
23 | .cloned() | ||
24 | .collect(); | ||
25 | for err in my_errors { | ||
26 | errors.remove(&err); | ||
27 | buff.push_str(&String::from(" ").repeat(level)); | ||
28 | write!(buff, "err: `{}`\n", err.message).unwrap(); | ||
29 | } | ||
30 | |||
31 | for child in node.children() { | ||
32 | go(child, buff, level + 1, errors) | ||
33 | } | ||
34 | |||
35 | let my_errors: Vec<_> = errors | ||
36 | .iter() | ||
37 | .filter(|e| e.offset == node.range().end()) | ||
38 | .cloned() | ||
39 | .collect(); | ||
40 | for err in my_errors { | ||
41 | errors.remove(&err); | ||
42 | buff.push_str(&String::from(" ").repeat(level)); | ||
43 | write!(buff, "err: `{}`\n", err.message).unwrap(); | ||
44 | } | ||
45 | } | ||
46 | } | ||