diff options
author | Aleksey Kladov <[email protected]> | 2018-07-30 01:21:17 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-07-30 01:21:17 +0100 |
commit | ae849cf134f3ff21261e175d95ba994e86ffe81a (patch) | |
tree | be123a10589615f38518e294db56e8821c972e09 /src/lib.rs | |
parent | 83acbc06bd01cd7045566170148e2150f568f77c (diff) |
Owned&Borrowed nodes
HOLLY COW, UNLIKE C++, WE CAN BE GENERIC WRT OWNERSHIP/BORROWING, SO
WE CAN BOTH MAKE SYNTAX NODES OWNED (WHICH IS CONVENIENT) AND
BORROWED (WHICH IS CONVENIENT FOR LOCAL PROCESSING, BC YOU DON'T NEED
TO BUMP REFCOUNTS).
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs index 4260e22e7..34c71fd2c 100644 --- a/src/lib.rs +++ b/src/lib.rs | |||
@@ -27,7 +27,7 @@ mod syntax_kinds; | |||
27 | pub use { | 27 | pub use { |
28 | text_unit::{TextRange, TextUnit}, | 28 | text_unit::{TextRange, TextUnit}, |
29 | syntax_kinds::SyntaxKind, | 29 | syntax_kinds::SyntaxKind, |
30 | yellow::{SyntaxNode}, | 30 | yellow::{SyntaxNode, SyntaxNodeRef}, |
31 | lexer::{tokenize, Token}, | 31 | lexer::{tokenize, Token}, |
32 | }; | 32 | }; |
33 | 33 | ||
@@ -48,16 +48,17 @@ pub mod utils { | |||
48 | collections::BTreeSet | 48 | collections::BTreeSet |
49 | }; | 49 | }; |
50 | 50 | ||
51 | use {SyntaxNode, SyntaxError}; | 51 | use {SyntaxNode, SyntaxNodeRef, SyntaxError}; |
52 | 52 | ||
53 | /// Parse a file and create a string representation of the resulting parse tree. | 53 | /// Parse a file and create a string representation of the resulting parse tree. |
54 | pub fn dump_tree_green(syntax: &SyntaxNode) -> String { | 54 | pub fn dump_tree_green(syntax: &SyntaxNode) -> String { |
55 | let syntax = syntax.borrow(); | ||
55 | let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect(); | 56 | let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect(); |
56 | let mut result = String::new(); | 57 | let mut result = String::new(); |
57 | go(syntax, &mut result, 0, &mut errors); | 58 | go(syntax, &mut result, 0, &mut errors); |
58 | return result; | 59 | return result; |
59 | 60 | ||
60 | fn go(node: &SyntaxNode, buff: &mut String, level: usize, errors: &mut BTreeSet<SyntaxError>) { | 61 | fn go(node: SyntaxNodeRef, buff: &mut String, level: usize, errors: &mut BTreeSet<SyntaxError>) { |
61 | buff.push_str(&String::from(" ").repeat(level)); | 62 | buff.push_str(&String::from(" ").repeat(level)); |
62 | write!(buff, "{:?}\n", node).unwrap(); | 63 | write!(buff, "{:?}\n", node).unwrap(); |
63 | let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().start()) | 64 | let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().start()) |
@@ -68,7 +69,7 @@ pub mod utils { | |||
68 | write!(buff, "err: `{}`\n", err.message).unwrap(); | 69 | write!(buff, "err: `{}`\n", err.message).unwrap(); |
69 | } | 70 | } |
70 | 71 | ||
71 | for child in node.children().iter() { | 72 | for child in node.children() { |
72 | go(child, buff, level + 1, errors) | 73 | go(child, buff, level + 1, errors) |
73 | } | 74 | } |
74 | 75 | ||