diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs index b90b70c05..cf2e97024 100644 --- a/src/lib.rs +++ b/src/lib.rs | |||
@@ -12,7 +12,8 @@ | |||
12 | //! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md> | 12 | //! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md> |
13 | 13 | ||
14 | #![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)] | 14 | #![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)] |
15 | #![deny(bad_style, unsafe_code, missing_docs)] | 15 | #![deny(bad_style, missing_docs)] |
16 | #![allow(missing_docs)] | ||
16 | //#![warn(unreachable_pub)] // rust-lang/rust#47816 | 17 | //#![warn(unreachable_pub)] // rust-lang/rust#47816 |
17 | 18 | ||
18 | extern crate unicode_xid; | 19 | extern crate unicode_xid; |
@@ -21,19 +22,24 @@ extern crate text_unit; | |||
21 | mod tree; | 22 | mod tree; |
22 | mod lexer; | 23 | mod lexer; |
23 | mod parser; | 24 | mod parser; |
25 | mod yellow; | ||
24 | 26 | ||
25 | pub mod syntax_kinds; | 27 | pub mod syntax_kinds; |
26 | pub use text_unit::{TextRange, TextUnit}; | 28 | pub use text_unit::{TextRange, TextUnit}; |
27 | pub use tree::{File, Node, SyntaxKind, Token}; | 29 | pub use tree::{File, Node, SyntaxKind, Token}; |
28 | pub(crate) use tree::{ErrorMsg, FileBuilder, Sink}; | 30 | pub(crate) use tree::{ErrorMsg, FileBuilder, Sink, GreenBuilder}; |
29 | pub use lexer::{next_token, tokenize}; | 31 | pub use lexer::{next_token, tokenize}; |
30 | pub use parser::parse; | 32 | pub use yellow::SyntaxNode; |
33 | pub(crate) use yellow::SError; | ||
34 | pub use parser::{parse, parse_green}; | ||
31 | 35 | ||
32 | /// Utilities for simple uses of the parser. | 36 | /// Utilities for simple uses of the parser. |
33 | pub mod utils { | 37 | pub mod utils { |
34 | use std::fmt::Write; | 38 | use std::fmt::Write; |
35 | 39 | ||
36 | use {File, Node}; | 40 | use {File, Node, SyntaxNode}; |
41 | use std::collections::BTreeSet; | ||
42 | use SError; | ||
37 | 43 | ||
38 | /// Parse a file and create a string representation of the resulting parse tree. | 44 | /// Parse a file and create a string representation of the resulting parse tree. |
39 | pub fn dump_tree(file: &File) -> String { | 45 | pub fn dump_tree(file: &File) -> String { |
@@ -65,4 +71,42 @@ pub mod utils { | |||
65 | } | 71 | } |
66 | } | 72 | } |
67 | } | 73 | } |
74 | |||
75 | /// Parse a file and create a string representation of the resulting parse tree. | ||
76 | pub fn dump_tree_green(syntax: &SyntaxNode) -> String { | ||
77 | let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect(); | ||
78 | let mut result = String::new(); | ||
79 | go(syntax, &mut result, 0, &mut errors); | ||
80 | return result; | ||
81 | |||
82 | fn go(node: &SyntaxNode, buff: &mut String, level: usize, errors: &mut BTreeSet<SError>) { | ||
83 | buff.push_str(&String::from(" ").repeat(level)); | ||
84 | write!(buff, "{:?}\n", node).unwrap(); | ||
85 | // let my_errors = node.errors().filter(|e| e.after_child().is_none()); | ||
86 | // let parent_errors = node.parent() | ||
87 | // .into_iter() | ||
88 | // .flat_map(|n| n.errors()) | ||
89 | // .filter(|e| e.after_child() == Some(node)); | ||
90 | // | ||
91 | let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().start()) | ||
92 | .cloned().collect(); | ||
93 | for err in my_errors { | ||
94 | errors.remove(&err); | ||
95 | buff.push_str(&String::from(" ").repeat(level)); | ||
96 | write!(buff, "err: `{}`\n", err.message).unwrap(); | ||
97 | } | ||
98 | |||
99 | for child in node.children().iter() { | ||
100 | go(child, buff, level + 1, errors) | ||
101 | } | ||
102 | |||
103 | let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().end()) | ||
104 | .cloned().collect(); | ||
105 | for err in my_errors { | ||
106 | errors.remove(&err); | ||
107 | buff.push_str(&String::from(" ").repeat(level)); | ||
108 | write!(buff, "err: `{}`\n", err.message).unwrap(); | ||
109 | } | ||
110 | } | ||
111 | } | ||
68 | } | 112 | } |