aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs52
1 files changed, 2 insertions, 50 deletions
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;
27mod parser; 27mod parser;
28mod syntax_kinds; 28mod syntax_kinds;
29mod yellow; 29mod yellow;
30/// Utilities for simple uses of the parser.
31pub mod utils;
30 32
31pub use { 33pub use {
32 lexer::{tokenize, Token}, 34 lexer::{tokenize, Token},
@@ -42,53 +44,3 @@ pub fn parse(text: String) -> SyntaxNode {
42 parser::parse::<yellow::GreenBuilder>(text, &tokens) 44 parser::parse::<yellow::GreenBuilder>(text, &tokens)
43} 45}
44 46
45/// Utilities for simple uses of the parser.
46pub mod utils {
47 use std::{collections::BTreeSet, fmt::Write};
48
49 use {SyntaxError, SyntaxNode, SyntaxNodeRef};
50
51 /// Parse a file and create a string representation of the resulting parse tree.
52 pub fn dump_tree_green(syntax: &SyntaxNode) -> String {
53 let syntax = syntax.borrow();
54 let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect();
55 let mut result = String::new();
56 go(syntax, &mut result, 0, &mut errors);
57 return result;
58
59 fn go(
60 node: SyntaxNodeRef,
61 buff: &mut String,
62 level: usize,
63 errors: &mut BTreeSet<SyntaxError>,
64 ) {
65 buff.push_str(&String::from(" ").repeat(level));
66 write!(buff, "{:?}\n", node).unwrap();
67 let my_errors: Vec<_> = errors
68 .iter()
69 .filter(|e| e.offset == node.range().start())
70 .cloned()
71 .collect();
72 for err in my_errors {
73 errors.remove(&err);
74 buff.push_str(&String::from(" ").repeat(level));
75 write!(buff, "err: `{}`\n", err.message).unwrap();
76 }
77
78 for child in node.children() {
79 go(child, buff, level + 1, errors)
80 }
81
82 let my_errors: Vec<_> = errors
83 .iter()
84 .filter(|e| e.offset == node.range().end())
85 .cloned()
86 .collect();
87 for err in my_errors {
88 errors.remove(&err);
89 buff.push_str(&String::from(" ").repeat(level));
90 write!(buff, "err: `{}`\n", err.message).unwrap();
91 }
92 }
93 }
94}