1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use std::ffi::{c_char, c_void, CStr};
use std::fmt;
use std::marker::PhantomData;
use std::ops::Range;
use std::ptr::NonNull;
use crate::tree::Tree;
use crate::tree_cursor::TreeCursor;
use crate::Grammar;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub(super) struct NodeRaw {
context: [u32; 4],
id: *const c_void,
tree: *const c_void,
}
impl From<Node<'_>> for NodeRaw {
fn from(node: Node) -> NodeRaw {
NodeRaw {
context: node.context,
id: node.id.as_ptr(),
tree: node.tree.as_ptr(),
}
}
}
#[derive(Clone)]
#[repr(C)]
pub struct Node<'tree> {
context: [u32; 4],
id: NonNull<c_void>,
tree: NonNull<c_void>,
_phantom: PhantomData<&'tree Tree>,
}
impl fmt::Debug for Node<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let range = self.byte_range();
write!(f, "{{Node {} {range:?}}}", self.kind())
}
}
impl<'tree> Node<'tree> {
#[inline]
pub(super) unsafe fn from_raw(raw: NodeRaw) -> Option<Self> {
Some(Node {
context: raw.context,
id: NonNull::new(raw.id as *mut _)?,
tree: unsafe { NonNull::new_unchecked(raw.tree as *mut _) },
_phantom: PhantomData,
})
}
#[inline]
pub(crate) fn as_raw(&self) -> NodeRaw {
NodeRaw {
context: self.context,
id: self.id.as_ptr(),
tree: self.tree.as_ptr(),
}
}
pub fn id(&self) -> usize {
self.id.as_ptr() as usize
}
/// Get this node's type as a string
#[inline]
pub fn kind(&self) -> &'tree str {
unsafe { CStr::from_ptr(ts_node_type(self.as_raw())) }
.to_str()
.unwrap()
}
/// Get this node's type as a numerical id.
#[inline]
pub fn kind_id(&self) -> u16 {
unsafe { ts_node_symbol(self.as_raw()) }
}
/// Get the [`Grammar`] that was used to parse this node's syntax tree.
#[inline]
pub fn grammar(&self) -> Grammar {
unsafe { ts_node_language(self.as_raw()) }
}
/// Check if this node is *named*.
///
/// Named nodes correspond to named rules in the grammar, whereas
/// *anonymous* nodes correspond to string literals in the grammar.
#[inline]
pub fn is_named(&self) -> bool {
unsafe { ts_node_is_named(self.as_raw()) }
}
/// Returns true if and only if this node is contained "inside" the given
/// input range, i.e. either start_new > start_old and end_new <= end_old OR
/// start_new >= start_old and end_new < end_old
pub fn is_contained_within(&self, range: Range<u32>) -> bool {
(self.start_byte() > range.start && self.end_byte() <= range.end)
|| (self.start_byte() >= range.start && self.end_byte() < range.end)
}
/// Check if this node is *missing*.
///
/// Missing nodes are inserted by the parser in order to recover from
/// certain kinds of syntax errors.
#[inline]
pub fn is_missing(&self) -> bool {
unsafe { ts_node_is_missing(self.as_raw()) }
}
/// Check if this node is *extra*.
///
/// Extra nodes represent things like comments, which are not required by the
/// grammar, but can appear anywhere.
#[inline]
pub fn is_extra(&self) -> bool {
unsafe { ts_node_is_extra(self.as_raw()) }
}
/// Get the byte offsets where this node starts.
#[inline(always)]
pub fn start_byte(&self) -> u32 {
// Normally we would implement this method like so:
//
// extern "C" {
// /// Get the node's start byte.
// fn ts_node_start_byte(self_: NodeRaw) -> u32;
// }
// unsafe { ts_node_start_byte(self.as_raw()) }
//
// However this method has a trivial implementation which is unlikely to change (though
// there is no guarantee) and this method can be called often, in tight loops, on a hot
// code path (for example the highlighter's `next_event_offset` method). So we inline the
// implementation directly from `node.c` in the C library to minimize overhead:
self.context[0]
}
/// Get the byte offsets where this node end.
#[inline]
pub fn end_byte(&self) -> u32 {
unsafe { ts_node_end_byte(self.as_raw()) }
}
/// Get the byte range of source code that this node represents.
#[inline]
pub fn byte_range(&self) -> Range<u32> {
self.start_byte()..self.end_byte()
}
/// Get the node's child at the given index, where zero represents the first
/// child.
///
/// This method is fairly fast, but its cost is technically log(i), so if
/// you might be iterating over a long list of children, you should use
/// [`Node::children`] instead.
#[inline]
pub fn child(&self, i: u32) -> Option<Node<'tree>> {
unsafe { Node::from_raw(ts_node_child(self.as_raw(), i)) }
}
/// Get this node's number of children.
#[inline]
pub fn child_count(&self) -> u32 {
unsafe { ts_node_child_count(self.as_raw()) }
}
/// Get this node's *named* child at the given index.
///
/// See also [`Node::is_named`].
/// This method is fairly fast, but its cost is technically log(i), so if
/// you might be iterating over a long list of children, you should use
/// `Node::named_children` instead.
#[inline]
pub fn named_child(&self, i: u32) -> Option<Node<'tree>> {
unsafe { Node::from_raw(ts_node_named_child(self.as_raw(), i)) }
}
/// Get this node's number of *named* children.
///
/// See also [`Node::is_named`].
#[inline]
pub fn named_child_count(&self) -> u32 {
unsafe { ts_node_named_child_count(self.as_raw()) }
}
#[inline]
unsafe fn map(&self, f: unsafe extern "C" fn(NodeRaw) -> NodeRaw) -> Option<Node<'tree>> {
Node::from_raw(f(self.as_raw()))
}
/// Get this node's immediate parent.
#[inline]
pub fn parent(&self) -> Option<Self> {
unsafe { self.map(ts_node_parent) }
}
/// Get this node's next sibling.
#[inline]
pub fn next_sibling(&self) -> Option<Self> {
unsafe { self.map(ts_node_next_sibling) }
}
/// Get this node's previous sibling.
#[inline]
pub fn prev_sibling(&self) -> Option<Self> {
unsafe { self.map(ts_node_prev_sibling) }
}
/// Get this node's next named sibling.
#[inline]
pub fn next_named_sibling(&self) -> Option<Self> {
unsafe { self.map(ts_node_next_named_sibling) }
}
/// Get this node's previous named sibling.
#[inline]
pub fn prev_named_sibling(&self) -> Option<Self> {
unsafe { self.map(ts_node_prev_named_sibling) }
}
/// Get the smallest node within this node that spans the given range.
#[inline]
pub fn descendant_for_byte_range(&self, start: u32, end: u32) -> Option<Self> {
unsafe { Self::from_raw(ts_node_descendant_for_byte_range(self.as_raw(), start, end)) }
}
/// Get the smallest named node within this node that spans the given range.
#[inline]
pub fn named_descendant_for_byte_range(&self, start: u32, end: u32) -> Option<Self> {
unsafe {
Self::from_raw(ts_node_named_descendant_for_byte_range(
self.as_raw(),
start,
end,
))
}
}
/// Iterate over this node's children.
///
/// A [`TreeCursor`] is used to retrieve the children efficiently. Obtain
/// a [`TreeCursor`] by calling [`Tree::walk`] or [`Node::walk`]. To avoid
/// unnecessary allocations, you should reuse the same cursor for
/// subsequent calls to this method.
///
/// If you're walking the tree recursively, you may want to use the
/// [`TreeCursor`] APIs directly instead.
pub fn children(&self) -> impl ExactSizeIterator<Item = Node<'tree>> {
let mut cursor = TreeCursor::new(self);
cursor.goto_first_child();
(0..self.child_count()).map(move |_| {
let result = cursor.node();
cursor.goto_next_sibling();
result
})
}
pub fn walk(&self) -> TreeCursor<'tree> {
TreeCursor::new(self)
}
}
impl PartialEq for Node<'_> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for Node<'_> {}
unsafe impl Send for Node<'_> {}
unsafe impl Sync for Node<'_> {}
extern "C" {
/// Get the node's type as a null-terminated string.
fn ts_node_type(node: NodeRaw) -> *const c_char;
/// Get the node's type as a numerical id.
fn ts_node_symbol(node: NodeRaw) -> u16;
/// Get the node's language.
fn ts_node_language(node: NodeRaw) -> Grammar;
/// Check if the node is *named*. Named nodes correspond to named rules in
/// the grammar, whereas *anonymous* nodes correspond to string literals in
/// the grammar
fn ts_node_is_named(node: NodeRaw) -> bool;
/// Check if the node is *missing*. Missing nodes are inserted by the parser
/// in order to recover from certain kinds of syntax errors
fn ts_node_is_missing(node: NodeRaw) -> bool;
/// Check if this node is *extra*.
///
/// Extra nodes represent things like comments, which are not required by the
/// grammar, but can appear anywhere.
fn ts_node_is_extra(node: NodeRaw) -> bool;
/// Get the node's immediate parent
fn ts_node_parent(node: NodeRaw) -> NodeRaw;
/// Get the node's child at the given index, where zero represents the first
/// child
fn ts_node_child(node: NodeRaw, child_index: u32) -> NodeRaw;
/// Get the node's number of children
fn ts_node_child_count(node: NodeRaw) -> u32;
/// Get the node's *named* child at the given index. See also
/// [`ts_node_is_named`]
fn ts_node_named_child(node: NodeRaw, child_index: u32) -> NodeRaw;
/// Get the node's number of *named* children. See also [`ts_node_is_named`]
fn ts_node_named_child_count(node: NodeRaw) -> u32;
/// Get the node's next sibling
fn ts_node_next_sibling(node: NodeRaw) -> NodeRaw;
fn ts_node_prev_sibling(node: NodeRaw) -> NodeRaw;
/// Get the node's next *named* sibling
fn ts_node_next_named_sibling(node: NodeRaw) -> NodeRaw;
fn ts_node_prev_named_sibling(node: NodeRaw) -> NodeRaw;
/// Get the smallest node within this node that spans the given range of
/// bytes or (row, column) positions
fn ts_node_descendant_for_byte_range(node: NodeRaw, start: u32, end: u32) -> NodeRaw;
/// Get the smallest named node within this node that spans the given range
/// of bytes or (row, column) positions
fn ts_node_named_descendant_for_byte_range(node: NodeRaw, start: u32, end: u32) -> NodeRaw;
/// Get the node's end byte.
fn ts_node_end_byte(node: NodeRaw) -> u32;
}