diff options
Diffstat (limited to 'src/tree/mod.rs')
-rw-r--r-- | src/tree/mod.rs | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/tree/mod.rs b/src/tree/mod.rs index a330caf54..aaf048c73 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs | |||
@@ -7,6 +7,7 @@ use std::cmp; | |||
7 | mod file_builder; | 7 | mod file_builder; |
8 | pub use self::file_builder::{FileBuilder, Sink}; | 8 | pub use self::file_builder::{FileBuilder, Sink}; |
9 | 9 | ||
10 | /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. | ||
10 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 11 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
11 | pub struct SyntaxKind(pub(crate) u32); | 12 | pub struct SyntaxKind(pub(crate) u32); |
12 | 13 | ||
@@ -37,12 +38,17 @@ pub(crate) struct SyntaxInfo { | |||
37 | pub name: &'static str, | 38 | pub name: &'static str, |
38 | } | 39 | } |
39 | 40 | ||
41 | /// A token of Rust source. | ||
40 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 42 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
41 | pub struct Token { | 43 | pub struct Token { |
44 | /// The kind of token. | ||
42 | pub kind: SyntaxKind, | 45 | pub kind: SyntaxKind, |
46 | /// The length of the token. | ||
43 | pub len: TextUnit, | 47 | pub len: TextUnit, |
44 | } | 48 | } |
45 | 49 | ||
50 | /// The contents of a Rust source file. | ||
51 | #[derive(Debug)] | ||
46 | pub struct File { | 52 | pub struct File { |
47 | text: String, | 53 | text: String, |
48 | nodes: Vec<NodeData>, | 54 | nodes: Vec<NodeData>, |
@@ -50,6 +56,7 @@ pub struct File { | |||
50 | } | 56 | } |
51 | 57 | ||
52 | impl File { | 58 | impl File { |
59 | /// The root node of this source file. | ||
53 | pub fn root<'f>(&'f self) -> Node<'f> { | 60 | pub fn root<'f>(&'f self) -> Node<'f> { |
54 | assert!(!self.nodes.is_empty()); | 61 | assert!(!self.nodes.is_empty()); |
55 | Node { | 62 | Node { |
@@ -59,6 +66,7 @@ impl File { | |||
59 | } | 66 | } |
60 | } | 67 | } |
61 | 68 | ||
69 | /// A reference to a token in a Rust source file. | ||
62 | #[derive(Clone, Copy)] | 70 | #[derive(Clone, Copy)] |
63 | pub struct Node<'f> { | 71 | pub struct Node<'f> { |
64 | file: &'f File, | 72 | file: &'f File, |
@@ -66,28 +74,34 @@ pub struct Node<'f> { | |||
66 | } | 74 | } |
67 | 75 | ||
68 | impl<'f> Node<'f> { | 76 | impl<'f> Node<'f> { |
77 | /// The kind of the token at this node. | ||
69 | pub fn kind(&self) -> SyntaxKind { | 78 | pub fn kind(&self) -> SyntaxKind { |
70 | self.data().kind | 79 | self.data().kind |
71 | } | 80 | } |
72 | 81 | ||
82 | /// The text range covered by the token at this node. | ||
73 | pub fn range(&self) -> TextRange { | 83 | pub fn range(&self) -> TextRange { |
74 | self.data().range | 84 | self.data().range |
75 | } | 85 | } |
76 | 86 | ||
87 | /// The text at this node. | ||
77 | pub fn text(&self) -> &'f str { | 88 | pub fn text(&self) -> &'f str { |
78 | &self.file.text.as_str()[self.range()] | 89 | &self.file.text.as_str()[self.range()] |
79 | } | 90 | } |
80 | 91 | ||
92 | /// The parent node to this node. | ||
81 | pub fn parent(&self) -> Option<Node<'f>> { | 93 | pub fn parent(&self) -> Option<Node<'f>> { |
82 | self.as_node(self.data().parent) | 94 | self.as_node(self.data().parent) |
83 | } | 95 | } |
84 | 96 | ||
97 | /// The children nodes of this node. | ||
85 | pub fn children(&self) -> Children<'f> { | 98 | pub fn children(&self) -> Children<'f> { |
86 | Children { | 99 | Children { |
87 | next: self.as_node(self.data().first_child), | 100 | next: self.as_node(self.data().first_child), |
88 | } | 101 | } |
89 | } | 102 | } |
90 | 103 | ||
104 | /// Any errors contained in this node. | ||
91 | pub fn errors(&self) -> SyntaxErrors<'f> { | 105 | pub fn errors(&self) -> SyntaxErrors<'f> { |
92 | let pos = self.file.errors.iter().position(|e| e.node == self.idx); | 106 | let pos = self.file.errors.iter().position(|e| e.node == self.idx); |
93 | let next = pos.map(|i| ErrorIdx(i as u32)).map(|idx| SyntaxError { | 107 | let next = pos.map(|i| ErrorIdx(i as u32)).map(|idx| SyntaxError { |
@@ -123,7 +137,7 @@ impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> { | |||
123 | 137 | ||
124 | impl<'f> cmp::Eq for Node<'f> {} | 138 | impl<'f> cmp::Eq for Node<'f> {} |
125 | 139 | ||
126 | #[derive(Clone, Copy)] | 140 | #[derive(Clone, Copy, Debug)] |
127 | pub struct SyntaxError<'f> { | 141 | pub struct SyntaxError<'f> { |
128 | file: &'f File, | 142 | file: &'f File, |
129 | idx: ErrorIdx, | 143 | idx: ErrorIdx, |
@@ -162,6 +176,7 @@ impl<'f> SyntaxError<'f> { | |||
162 | } | 176 | } |
163 | } | 177 | } |
164 | 178 | ||
179 | #[derive(Debug)] | ||
165 | pub struct Children<'f> { | 180 | pub struct Children<'f> { |
166 | next: Option<Node<'f>>, | 181 | next: Option<Node<'f>>, |
167 | } | 182 | } |
@@ -176,6 +191,7 @@ impl<'f> Iterator for Children<'f> { | |||
176 | } | 191 | } |
177 | } | 192 | } |
178 | 193 | ||
194 | #[derive(Debug)] | ||
179 | pub struct SyntaxErrors<'f> { | 195 | pub struct SyntaxErrors<'f> { |
180 | next: Option<SyntaxError<'f>>, | 196 | next: Option<SyntaxError<'f>>, |
181 | } | 197 | } |
@@ -190,9 +206,10 @@ impl<'f> Iterator for SyntaxErrors<'f> { | |||
190 | } | 206 | } |
191 | } | 207 | } |
192 | 208 | ||
193 | #[derive(Clone, Copy, PartialEq, Eq)] | 209 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
194 | struct NodeIdx(u32); | 210 | struct NodeIdx(u32); |
195 | 211 | ||
212 | #[derive(Debug)] | ||
196 | struct NodeData { | 213 | struct NodeData { |
197 | kind: SyntaxKind, | 214 | kind: SyntaxKind, |
198 | range: TextRange, | 215 | range: TextRange, |
@@ -215,9 +232,10 @@ impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> { | |||
215 | } | 232 | } |
216 | } | 233 | } |
217 | 234 | ||
218 | #[derive(Clone, Copy)] | 235 | #[derive(Clone, Copy, Debug)] |
219 | struct ErrorIdx(u32); | 236 | struct ErrorIdx(u32); |
220 | 237 | ||
238 | #[derive(Debug)] | ||
221 | struct SyntaxErrorData { | 239 | struct SyntaxErrorData { |
222 | node: NodeIdx, | 240 | node: NodeIdx, |
223 | message: String, | 241 | message: String, |