aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-29 11:51:55 +0100
committerAleksey Kladov <[email protected]>2018-07-29 11:51:55 +0100
commitc12450fb4e30c3418555e47d045bb9fd4318a10a (patch)
treee2dc508e1e415388392657cda3dfb00175cdabf2 /src/lib.rs
parent8d9961b75377a7bd2656b5aa1451710de8c86f60 (diff)
Introduce red-green syntax tree
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs52
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
18extern crate unicode_xid; 19extern crate unicode_xid;
@@ -21,19 +22,24 @@ extern crate text_unit;
21mod tree; 22mod tree;
22mod lexer; 23mod lexer;
23mod parser; 24mod parser;
25mod yellow;
24 26
25pub mod syntax_kinds; 27pub mod syntax_kinds;
26pub use text_unit::{TextRange, TextUnit}; 28pub use text_unit::{TextRange, TextUnit};
27pub use tree::{File, Node, SyntaxKind, Token}; 29pub use tree::{File, Node, SyntaxKind, Token};
28pub(crate) use tree::{ErrorMsg, FileBuilder, Sink}; 30pub(crate) use tree::{ErrorMsg, FileBuilder, Sink, GreenBuilder};
29pub use lexer::{next_token, tokenize}; 31pub use lexer::{next_token, tokenize};
30pub use parser::parse; 32pub use yellow::SyntaxNode;
33pub(crate) use yellow::SError;
34pub use parser::{parse, parse_green};
31 35
32/// Utilities for simple uses of the parser. 36/// Utilities for simple uses of the parser.
33pub mod utils { 37pub 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}