ruby_tree_sitter 1.6.0-x86_64-linux-musl
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +213 -0
- data/ext/tree_sitter/encoding.c +29 -0
- data/ext/tree_sitter/extconf.rb +149 -0
- data/ext/tree_sitter/input.c +127 -0
- data/ext/tree_sitter/input_edit.c +42 -0
- data/ext/tree_sitter/language.c +219 -0
- data/ext/tree_sitter/logger.c +228 -0
- data/ext/tree_sitter/macros.h +163 -0
- data/ext/tree_sitter/node.c +623 -0
- data/ext/tree_sitter/parser.c +398 -0
- data/ext/tree_sitter/point.c +26 -0
- data/ext/tree_sitter/quantifier.c +43 -0
- data/ext/tree_sitter/query.c +289 -0
- data/ext/tree_sitter/query_capture.c +28 -0
- data/ext/tree_sitter/query_cursor.c +231 -0
- data/ext/tree_sitter/query_error.c +41 -0
- data/ext/tree_sitter/query_match.c +44 -0
- data/ext/tree_sitter/query_predicate_step.c +83 -0
- data/ext/tree_sitter/range.c +35 -0
- data/ext/tree_sitter/repo.rb +128 -0
- data/ext/tree_sitter/symbol_type.c +46 -0
- data/ext/tree_sitter/tree.c +234 -0
- data/ext/tree_sitter/tree_cursor.c +269 -0
- data/ext/tree_sitter/tree_sitter.c +44 -0
- data/ext/tree_sitter/tree_sitter.h +107 -0
- data/lib/tree_sitter/3.0/tree_sitter.so +0 -0
- data/lib/tree_sitter/3.1/tree_sitter.so +0 -0
- data/lib/tree_sitter/3.2/tree_sitter.so +0 -0
- data/lib/tree_sitter/3.3/tree_sitter.so +0 -0
- data/lib/tree_sitter/helpers.rb +23 -0
- data/lib/tree_sitter/mixins/language.rb +167 -0
- data/lib/tree_sitter/node.rb +167 -0
- data/lib/tree_sitter/query.rb +191 -0
- data/lib/tree_sitter/query_captures.rb +30 -0
- data/lib/tree_sitter/query_cursor.rb +27 -0
- data/lib/tree_sitter/query_match.rb +100 -0
- data/lib/tree_sitter/query_matches.rb +39 -0
- data/lib/tree_sitter/query_predicate.rb +14 -0
- data/lib/tree_sitter/text_predicate_capture.rb +37 -0
- data/lib/tree_sitter/version.rb +8 -0
- data/lib/tree_sitter.rb +34 -0
- data/lib/tree_stand/ast_modifier.rb +30 -0
- data/lib/tree_stand/breadth_first_visitor.rb +54 -0
- data/lib/tree_stand/config.rb +19 -0
- data/lib/tree_stand/node.rb +351 -0
- data/lib/tree_stand/parser.rb +87 -0
- data/lib/tree_stand/range.rb +55 -0
- data/lib/tree_stand/tree.rb +123 -0
- data/lib/tree_stand/utils/printer.rb +73 -0
- data/lib/tree_stand/version.rb +7 -0
- data/lib/tree_stand/visitor.rb +127 -0
- data/lib/tree_stand/visitors/tree_walker.rb +37 -0
- data/lib/tree_stand.rb +48 -0
- data/tree_sitter.gemspec +34 -0
- metadata +135 -0
@@ -0,0 +1,623 @@
|
|
1
|
+
#include "tree_sitter.h"
|
2
|
+
#include "tree_sitter/api.h"
|
3
|
+
|
4
|
+
extern VALUE mTreeSitter;
|
5
|
+
|
6
|
+
VALUE cNode;
|
7
|
+
|
8
|
+
DATA_TYPE(TSNode, node)
|
9
|
+
|
10
|
+
static void node_free(void *ptr) {
|
11
|
+
node_t *type = (node_t *)ptr;
|
12
|
+
tree_rc_free(type->data.tree);
|
13
|
+
xfree(ptr);
|
14
|
+
}
|
15
|
+
|
16
|
+
DATA_MEMSIZE(node)
|
17
|
+
DATA_DECLARE_DATA_TYPE(node)
|
18
|
+
DATA_ALLOCATE(node)
|
19
|
+
DATA_UNWRAP(node)
|
20
|
+
|
21
|
+
VALUE new_node(const TSNode *ptr) {
|
22
|
+
if (ptr == NULL) {
|
23
|
+
return Qnil;
|
24
|
+
}
|
25
|
+
VALUE res = node_allocate(cNode);
|
26
|
+
node_t *type = unwrap(res);
|
27
|
+
type->data = *ptr;
|
28
|
+
tree_rc_new(type->data.tree);
|
29
|
+
return res;
|
30
|
+
}
|
31
|
+
VALUE new_node_by_val(TSNode ptr) {
|
32
|
+
VALUE res = node_allocate(cNode);
|
33
|
+
node_t *type = unwrap(res);
|
34
|
+
type->data = ptr;
|
35
|
+
tree_rc_new(type->data.tree);
|
36
|
+
return res;
|
37
|
+
}
|
38
|
+
|
39
|
+
DATA_FROM_VALUE(TSNode, node)
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Check if two nodes are identical.
|
43
|
+
*
|
44
|
+
* @param other [Node]
|
45
|
+
*
|
46
|
+
* @return [Boolean]
|
47
|
+
*/
|
48
|
+
static VALUE node_eq(VALUE self, VALUE other) {
|
49
|
+
return ts_node_eq(SELF, unwrap(other)->data) ? Qtrue : Qfalse;
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Get an S-expression representing the node as a string.
|
54
|
+
*
|
55
|
+
* @return [String]
|
56
|
+
*/
|
57
|
+
static VALUE node_string(VALUE self) {
|
58
|
+
char *str = ts_node_string(SELF);
|
59
|
+
VALUE res = safe_str(str);
|
60
|
+
if (str) {
|
61
|
+
free(str);
|
62
|
+
}
|
63
|
+
return res;
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Check if a syntax node has been edited.
|
68
|
+
*
|
69
|
+
* @return [Boolean]
|
70
|
+
*/
|
71
|
+
static VALUE node_has_changes(VALUE self) {
|
72
|
+
return ts_node_has_changes(SELF) ? Qtrue : Qfalse;
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Check if the node is a syntax error or contains any syntax errors.
|
77
|
+
*
|
78
|
+
* @return [Boolean]
|
79
|
+
*/
|
80
|
+
static VALUE node_has_error(VALUE self) {
|
81
|
+
return ts_node_has_error(SELF) ? Qtrue : Qfalse;
|
82
|
+
}
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Check if the node is a syntax error.
|
86
|
+
*
|
87
|
+
* @return [Boolean]
|
88
|
+
*/
|
89
|
+
VALUE node_is_error(VALUE self) {
|
90
|
+
return ts_node_is_error(SELF) ? Qtrue : Qfalse;
|
91
|
+
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Check if the node is *named*. Named nodes correspond to named rules in the
|
95
|
+
* grammar, whereas *anonymous* nodes correspond to string literals in the
|
96
|
+
* grammar.
|
97
|
+
*
|
98
|
+
* @return [Boolean]
|
99
|
+
*/
|
100
|
+
static VALUE node_is_named(VALUE self) {
|
101
|
+
return ts_node_is_named(SELF) ? Qtrue : Qfalse;
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* Check if the node is null. Functions like {Node#child} and
|
106
|
+
* {Node#next_sibling} will return a null node to indicate that no such node
|
107
|
+
* was found.
|
108
|
+
*
|
109
|
+
* @return [Boolean]
|
110
|
+
*/
|
111
|
+
static VALUE node_is_null(VALUE self) {
|
112
|
+
return ts_node_is_null(SELF) ? Qtrue : Qfalse;
|
113
|
+
}
|
114
|
+
|
115
|
+
/**
|
116
|
+
* Check if the node is *extra*. Extra nodes represent things like comments,
|
117
|
+
* which are not required the grammar, but can appear anywhere.
|
118
|
+
*
|
119
|
+
* @return [Boolean]
|
120
|
+
*/
|
121
|
+
static VALUE node_is_extra(VALUE self) {
|
122
|
+
return ts_node_is_extra(SELF) ? Qtrue : Qfalse;
|
123
|
+
}
|
124
|
+
/**
|
125
|
+
* Check if the node is *missing*. Missing nodes are inserted by the parser in
|
126
|
+
* order to recover from certain kinds of syntax errors.
|
127
|
+
*
|
128
|
+
* @return [Boolean]
|
129
|
+
*/
|
130
|
+
static VALUE node_is_missing(VALUE self) {
|
131
|
+
return ts_node_is_missing(SELF) ? Qtrue : Qfalse;
|
132
|
+
}
|
133
|
+
|
134
|
+
/**
|
135
|
+
* Get the node's child at the given index, where zero represents the first
|
136
|
+
* child.
|
137
|
+
*
|
138
|
+
* @raise [IndexError] if out of range.
|
139
|
+
*
|
140
|
+
* @return [Node]
|
141
|
+
*/
|
142
|
+
static VALUE node_child(VALUE self, VALUE idx) {
|
143
|
+
TSNode node = SELF;
|
144
|
+
uint32_t index = NUM2UINT(idx);
|
145
|
+
uint32_t range = ts_node_child_count(node);
|
146
|
+
|
147
|
+
if (index < range) {
|
148
|
+
return new_node_by_val(ts_node_child(node, index));
|
149
|
+
} else {
|
150
|
+
rb_raise(rb_eIndexError, "Index %d is out of range (len = %d)", index,
|
151
|
+
range);
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
/**
|
156
|
+
* Get the node's child with the given numerical field id.
|
157
|
+
*
|
158
|
+
* You can convert a field name to an id using {Language#field_id_for_name}.
|
159
|
+
*
|
160
|
+
* @return [Node]
|
161
|
+
*/
|
162
|
+
static VALUE node_child_by_field_id(VALUE self, VALUE field_id) {
|
163
|
+
return new_node_by_val(ts_node_child_by_field_id(SELF, NUM2UINT(field_id)));
|
164
|
+
}
|
165
|
+
|
166
|
+
/**
|
167
|
+
* Get the node's child with the given field name.
|
168
|
+
*
|
169
|
+
* @param field_name [String, Symbol]
|
170
|
+
*
|
171
|
+
* @return [Node]
|
172
|
+
*/
|
173
|
+
static VALUE node_child_by_field_name(VALUE self, VALUE field_name) {
|
174
|
+
if (Qtrue == rb_funcall(self, rb_intern("field?"), 1, field_name)) {
|
175
|
+
VALUE field_str = rb_funcall(field_name, rb_intern("to_s"), 0);
|
176
|
+
const char *name = StringValuePtr(field_str);
|
177
|
+
uint32_t length = (uint32_t)RSTRING_LEN(field_str);
|
178
|
+
return new_node_by_val(ts_node_child_by_field_name(SELF, name, length));
|
179
|
+
} else {
|
180
|
+
return Qnil;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
/**
|
185
|
+
* Get the node's number of children.
|
186
|
+
*
|
187
|
+
* @return [Integer]
|
188
|
+
*/
|
189
|
+
static VALUE node_child_count(VALUE self) {
|
190
|
+
TSNode node = SELF;
|
191
|
+
const char *type = ts_node_type(node);
|
192
|
+
if (strcmp(type, "end") == 0) {
|
193
|
+
return UINT2NUM(0);
|
194
|
+
} else {
|
195
|
+
return UINT2NUM(ts_node_child_count(SELF));
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
/**
|
200
|
+
* Get the node's number of descendants, including one for the node itself.
|
201
|
+
*
|
202
|
+
* @return [Integer]
|
203
|
+
*/
|
204
|
+
VALUE node_descendant_count(VALUE self) {
|
205
|
+
return UINT2NUM(ts_node_descendant_count(SELF));
|
206
|
+
}
|
207
|
+
|
208
|
+
/**
|
209
|
+
* Get the smallest node within this node that spans the given range of byte
|
210
|
+
* positions.
|
211
|
+
*
|
212
|
+
* @raise [IndexError] if out of range.
|
213
|
+
*
|
214
|
+
* @param from [Integer]
|
215
|
+
* @param to [Integer]
|
216
|
+
*
|
217
|
+
* @return [Node]
|
218
|
+
*/
|
219
|
+
static VALUE node_descendant_for_byte_range(VALUE self, VALUE from, VALUE to) {
|
220
|
+
uint32_t from_b = NUM2UINT(from);
|
221
|
+
uint32_t to_b = NUM2UINT(to);
|
222
|
+
|
223
|
+
if (from_b > to_b) {
|
224
|
+
rb_raise(rb_eIndexError, "From > To: %d > %d", from_b, to_b);
|
225
|
+
} else {
|
226
|
+
return new_node_by_val(
|
227
|
+
ts_node_descendant_for_byte_range(SELF, from_b, to_b));
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
/**
|
232
|
+
* Get the smallest node within this node that spans the given range of
|
233
|
+
* (row, column) positions.
|
234
|
+
*
|
235
|
+
* @raise [IndexError] if out of range.
|
236
|
+
*
|
237
|
+
* @param from [Point]
|
238
|
+
* @param to [Point]
|
239
|
+
*
|
240
|
+
* @return [Node]
|
241
|
+
*/
|
242
|
+
static VALUE node_descendant_for_point_range(VALUE self, VALUE from, VALUE to) {
|
243
|
+
TSNode node = SELF;
|
244
|
+
TSPoint start = ts_node_start_point(node);
|
245
|
+
TSPoint end = ts_node_end_point(node);
|
246
|
+
TSPoint f = value_to_point(from);
|
247
|
+
TSPoint t = value_to_point(to);
|
248
|
+
|
249
|
+
if ((f.row < start.row) || (t.row > end.row) ||
|
250
|
+
(f.row == start.row && (f.column < start.column)) ||
|
251
|
+
(t.row == end.row && (t.column > end.column))) {
|
252
|
+
rb_raise(rb_eIndexError,
|
253
|
+
"Invalid point range: [%+" PRIsVALUE ", %+" PRIsVALUE
|
254
|
+
"] is not in [%+" PRIsVALUE ", %+" PRIsVALUE "].",
|
255
|
+
from, to, new_point(&start), new_point(&end));
|
256
|
+
} else {
|
257
|
+
return new_node_by_val(ts_node_descendant_for_point_range(node, f, t));
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
/**
|
262
|
+
* Edit the node to keep it in-sync with source code that has been edited.
|
263
|
+
*
|
264
|
+
* This function is only rarely needed. When you edit a syntax tree with the
|
265
|
+
* {Tree#edit} function, all of the nodes that you retrieve from the tree
|
266
|
+
* afterward will already reflect the edit. You only need to use {Node#edit}
|
267
|
+
* when you have a {Node} instance that you want to keep and continue to use
|
268
|
+
* after an edit.
|
269
|
+
*
|
270
|
+
* @param input_edit [InputEdit]
|
271
|
+
*
|
272
|
+
* @return [nil]
|
273
|
+
*/
|
274
|
+
static VALUE node_edit(VALUE self, VALUE input_edit) {
|
275
|
+
TSNode node = SELF;
|
276
|
+
TSInputEdit edit = value_to_input_edit(input_edit);
|
277
|
+
ts_node_edit(&node, &edit);
|
278
|
+
|
279
|
+
return Qnil;
|
280
|
+
}
|
281
|
+
|
282
|
+
/**
|
283
|
+
* Get the node's end byte.
|
284
|
+
*
|
285
|
+
* @return [Integer]
|
286
|
+
*/
|
287
|
+
static VALUE node_end_byte(VALUE self) {
|
288
|
+
return UINT2NUM(ts_node_end_byte(SELF));
|
289
|
+
}
|
290
|
+
|
291
|
+
/**
|
292
|
+
* Get the node's end position in terms of rows and columns.
|
293
|
+
*
|
294
|
+
* @return [Point]
|
295
|
+
*/
|
296
|
+
static VALUE node_end_point(VALUE self) {
|
297
|
+
return new_point_by_val(ts_node_end_point(SELF));
|
298
|
+
}
|
299
|
+
|
300
|
+
/**
|
301
|
+
* Get the field name for node's child at the given index, where zero represents
|
302
|
+
* the first child.
|
303
|
+
*
|
304
|
+
* @raise [IndexError] if out of range.
|
305
|
+
*
|
306
|
+
* @return [String]
|
307
|
+
*/
|
308
|
+
static VALUE node_field_name_for_child(VALUE self, VALUE idx) {
|
309
|
+
// FIXME: the original API returns nil if no name was found, but I made it
|
310
|
+
// raise an exception like `node_child` for consistency. The latter was made
|
311
|
+
// this way to avoid segfault. Should we absolutely stick to the original API?
|
312
|
+
TSNode node = SELF;
|
313
|
+
uint32_t index = NUM2UINT(idx);
|
314
|
+
uint32_t range = ts_node_child_count(node);
|
315
|
+
|
316
|
+
if (index < range) {
|
317
|
+
return safe_str(ts_node_field_name_for_child(node, index));
|
318
|
+
} else {
|
319
|
+
rb_raise(rb_eIndexError, "Index %d is out of range (len = %d)", index,
|
320
|
+
range);
|
321
|
+
}
|
322
|
+
}
|
323
|
+
|
324
|
+
/**
|
325
|
+
* Get the node's first child that extends beyond the given byte offset.
|
326
|
+
*
|
327
|
+
* @param byte [Integer]
|
328
|
+
*
|
329
|
+
* @return [Node]
|
330
|
+
*/
|
331
|
+
static VALUE node_first_child_for_byte(VALUE self, VALUE byte) {
|
332
|
+
return new_node_by_val(ts_node_first_child_for_byte(SELF, NUM2UINT(byte)));
|
333
|
+
}
|
334
|
+
|
335
|
+
/**
|
336
|
+
* Get the node's first named child that extends beyond the given byte offset.
|
337
|
+
*
|
338
|
+
* @param byte [Integer]
|
339
|
+
*
|
340
|
+
* @return [Node]
|
341
|
+
*/
|
342
|
+
static VALUE node_first_named_child_for_byte(VALUE self, VALUE byte) {
|
343
|
+
return new_node_by_val(
|
344
|
+
ts_node_first_named_child_for_byte(SELF, NUM2UINT(byte)));
|
345
|
+
}
|
346
|
+
|
347
|
+
/**
|
348
|
+
* Get the node's type as a numerical id as it appears in the grammar ignoring
|
349
|
+
* aliases. This should be used in {Language#next_state} instead of
|
350
|
+
* {Node#symbol}.
|
351
|
+
*
|
352
|
+
* @return [Integer]
|
353
|
+
*/
|
354
|
+
VALUE node_grammar_symbol(VALUE self) {
|
355
|
+
return UINT2NUM(ts_node_grammar_symbol(SELF));
|
356
|
+
}
|
357
|
+
|
358
|
+
/**
|
359
|
+
* Get the node's type as it appears in the grammar ignoring aliases as a
|
360
|
+
* null-terminated string.
|
361
|
+
*
|
362
|
+
* @return String
|
363
|
+
*/
|
364
|
+
VALUE node_grammar_type(VALUE self) {
|
365
|
+
return safe_str(ts_node_grammar_type(SELF));
|
366
|
+
}
|
367
|
+
|
368
|
+
/**
|
369
|
+
* Get the node's language.
|
370
|
+
*
|
371
|
+
* @return [Language]
|
372
|
+
*/
|
373
|
+
static VALUE node_language(VALUE self) {
|
374
|
+
return new_language(ts_node_language(SELF));
|
375
|
+
}
|
376
|
+
|
377
|
+
/**
|
378
|
+
* Get the smallest *named* node within this node that spans the given range of
|
379
|
+
* byte positions.
|
380
|
+
*
|
381
|
+
* @raise [IndexError] if out of range.
|
382
|
+
*
|
383
|
+
* @param from [Integer]
|
384
|
+
* @param to [Integer]
|
385
|
+
*
|
386
|
+
* @return [Node]
|
387
|
+
*/
|
388
|
+
static VALUE node_named_descendant_for_byte_range(VALUE self, VALUE from,
|
389
|
+
VALUE to) {
|
390
|
+
uint32_t from_b = NUM2UINT(from);
|
391
|
+
uint32_t to_b = NUM2UINT(to);
|
392
|
+
|
393
|
+
if (from_b > to_b) {
|
394
|
+
rb_raise(rb_eIndexError, "From > To: %d > %d", from_b, to_b);
|
395
|
+
} else {
|
396
|
+
return new_node_by_val(
|
397
|
+
ts_node_named_descendant_for_byte_range(SELF, from_b, to_b));
|
398
|
+
}
|
399
|
+
}
|
400
|
+
|
401
|
+
/**
|
402
|
+
* Get the smallest *named* node within this node that spans the given range of
|
403
|
+
* (row, column) positions.
|
404
|
+
*
|
405
|
+
* @raise [IndexError] if out of range.
|
406
|
+
*
|
407
|
+
* @param from [Point]
|
408
|
+
* @param to [Point]
|
409
|
+
*
|
410
|
+
* @return [Node]
|
411
|
+
*/
|
412
|
+
static VALUE node_named_descendant_for_point_range(VALUE self, VALUE from,
|
413
|
+
VALUE to) {
|
414
|
+
TSNode node = SELF;
|
415
|
+
TSPoint start = ts_node_start_point(node);
|
416
|
+
TSPoint end = ts_node_end_point(node);
|
417
|
+
TSPoint f = value_to_point(from);
|
418
|
+
TSPoint t = value_to_point(to);
|
419
|
+
|
420
|
+
if ((f.row < start.row) || (t.row > end.row) ||
|
421
|
+
(f.row == start.row && (f.column < start.column)) ||
|
422
|
+
(t.row == end.row && (t.column > end.column))) {
|
423
|
+
rb_raise(rb_eIndexError,
|
424
|
+
"Invalid point range: [%+" PRIsVALUE ", %+" PRIsVALUE
|
425
|
+
"] is not in [%+" PRIsVALUE ", %+" PRIsVALUE "].",
|
426
|
+
from, to, new_point(&start), new_point(&end));
|
427
|
+
} else {
|
428
|
+
return new_node_by_val(
|
429
|
+
ts_node_named_descendant_for_point_range(node, f, t));
|
430
|
+
}
|
431
|
+
}
|
432
|
+
|
433
|
+
/**
|
434
|
+
* Get the node's *named* child at the given index.
|
435
|
+
*
|
436
|
+
* @see named?
|
437
|
+
*
|
438
|
+
* @param idx [Integer]
|
439
|
+
*
|
440
|
+
* @raise [IndexError] if out of range.
|
441
|
+
*
|
442
|
+
* @return [Node]
|
443
|
+
*/
|
444
|
+
static VALUE node_named_child(VALUE self, VALUE idx) {
|
445
|
+
// FIXME: see notes in `node_field_name_for_child`
|
446
|
+
TSNode node = SELF;
|
447
|
+
uint32_t index = NUM2UINT(idx);
|
448
|
+
uint32_t range = ts_node_named_child_count(node);
|
449
|
+
|
450
|
+
if (index < range) {
|
451
|
+
return new_node_by_val(ts_node_named_child(node, index));
|
452
|
+
} else {
|
453
|
+
rb_raise(rb_eIndexError, "Index %d is out of range (len = %d)", index,
|
454
|
+
range);
|
455
|
+
}
|
456
|
+
}
|
457
|
+
|
458
|
+
/**
|
459
|
+
* Get the node's number of *named* children.
|
460
|
+
*
|
461
|
+
* @see named?
|
462
|
+
*
|
463
|
+
* @return [Integer]
|
464
|
+
*/
|
465
|
+
static VALUE node_named_child_count(VALUE self) {
|
466
|
+
return UINT2NUM(ts_node_named_child_count(SELF));
|
467
|
+
}
|
468
|
+
|
469
|
+
/**
|
470
|
+
* Get the node's next *named* sibling.
|
471
|
+
*
|
472
|
+
* @return [Node]
|
473
|
+
*/
|
474
|
+
static VALUE node_next_named_sibling(VALUE self) {
|
475
|
+
return new_node_by_val(ts_node_next_named_sibling(SELF));
|
476
|
+
}
|
477
|
+
|
478
|
+
/**
|
479
|
+
* Get the node's next sibling.
|
480
|
+
*
|
481
|
+
* @return [Node]
|
482
|
+
*/
|
483
|
+
static VALUE node_next_sibling(VALUE self) {
|
484
|
+
return new_node_by_val(ts_node_next_sibling(SELF));
|
485
|
+
}
|
486
|
+
|
487
|
+
/**
|
488
|
+
* Get the parse state after this node.
|
489
|
+
*
|
490
|
+
* @return [Integer]
|
491
|
+
*/
|
492
|
+
VALUE node_next_parse_state(VALUE self) {
|
493
|
+
return UINT2NUM(ts_node_next_parse_state(SELF));
|
494
|
+
}
|
495
|
+
|
496
|
+
/**
|
497
|
+
* Get the node's immediate parent.
|
498
|
+
*
|
499
|
+
* @return [Node]
|
500
|
+
*/
|
501
|
+
static VALUE node_parent(VALUE self) {
|
502
|
+
return new_node_by_val(ts_node_parent(SELF));
|
503
|
+
}
|
504
|
+
|
505
|
+
/**
|
506
|
+
* Get the node's previous *named* sibling.
|
507
|
+
*
|
508
|
+
* @return [Node]
|
509
|
+
*/
|
510
|
+
static VALUE node_prev_named_sibling(VALUE self) {
|
511
|
+
return new_node_by_val(ts_node_prev_named_sibling(SELF));
|
512
|
+
}
|
513
|
+
|
514
|
+
/**
|
515
|
+
* Get the node's previous sibling.
|
516
|
+
*
|
517
|
+
* @return [Node]
|
518
|
+
*/
|
519
|
+
static VALUE node_prev_sibling(VALUE self) {
|
520
|
+
return new_node_by_val(ts_node_prev_sibling(SELF));
|
521
|
+
}
|
522
|
+
|
523
|
+
/**
|
524
|
+
* Get the node's start byte.
|
525
|
+
*
|
526
|
+
* @return [Integer]
|
527
|
+
*/
|
528
|
+
static VALUE node_start_byte(VALUE self) {
|
529
|
+
return UINT2NUM(ts_node_start_byte(SELF));
|
530
|
+
}
|
531
|
+
|
532
|
+
/**
|
533
|
+
* Get the node's start position in terms of rows and columns.
|
534
|
+
*
|
535
|
+
* @return [Point]
|
536
|
+
*/
|
537
|
+
static VALUE node_start_point(VALUE self) {
|
538
|
+
return new_point_by_val(ts_node_start_point(SELF));
|
539
|
+
}
|
540
|
+
|
541
|
+
/**
|
542
|
+
* Get this node's parse state.
|
543
|
+
*
|
544
|
+
* @return [Integer]
|
545
|
+
*/
|
546
|
+
VALUE node_parse_state(VALUE self) {
|
547
|
+
return UINT2NUM(ts_node_parse_state(SELF));
|
548
|
+
}
|
549
|
+
|
550
|
+
/**
|
551
|
+
* Get the node's type as a numerical id.
|
552
|
+
*
|
553
|
+
* @return [Integer]
|
554
|
+
*/
|
555
|
+
static VALUE node_symbol(VALUE self) { return UINT2NUM(ts_node_symbol(SELF)); }
|
556
|
+
|
557
|
+
/**
|
558
|
+
* Get the node's type as a null-terminated string.
|
559
|
+
*
|
560
|
+
* @return [Symbol]
|
561
|
+
*/
|
562
|
+
static VALUE node_type(VALUE self) { return safe_symbol(ts_node_type(SELF)); }
|
563
|
+
|
564
|
+
void init_node(void) {
|
565
|
+
cNode = rb_define_class_under(mTreeSitter, "Node", rb_cObject);
|
566
|
+
|
567
|
+
rb_undef_alloc_func(cNode);
|
568
|
+
|
569
|
+
/* Builtins */
|
570
|
+
rb_define_method(cNode, "eq?", node_eq, 1);
|
571
|
+
rb_define_method(cNode, "to_s", node_string, 0);
|
572
|
+
rb_define_method(cNode, "to_str", node_string, 0);
|
573
|
+
rb_define_method(cNode, "inspect", node_string, 0);
|
574
|
+
rb_define_method(cNode, "==", node_eq, 1);
|
575
|
+
|
576
|
+
/* Class methods */
|
577
|
+
// Predicates
|
578
|
+
rb_define_method(cNode, "changed?", node_has_changes, 0);
|
579
|
+
rb_define_method(cNode, "error?", node_is_error, 0);
|
580
|
+
rb_define_method(cNode, "has_error?", node_has_error, 0);
|
581
|
+
rb_define_method(cNode, "missing?", node_is_missing, 0);
|
582
|
+
rb_define_method(cNode, "named?", node_is_named, 0);
|
583
|
+
rb_define_method(cNode, "null?", node_is_null, 0);
|
584
|
+
rb_define_method(cNode, "extra?", node_is_extra, 0);
|
585
|
+
|
586
|
+
// Other
|
587
|
+
rb_define_method(cNode, "child", node_child, 1);
|
588
|
+
rb_define_method(cNode, "child_by_field_id", node_child_by_field_id, 1);
|
589
|
+
rb_define_method(cNode, "child_by_field_name", node_child_by_field_name, 1);
|
590
|
+
rb_define_method(cNode, "child_count", node_child_count, 0);
|
591
|
+
rb_define_method(cNode, "descendant_count", node_descendant_count, 0);
|
592
|
+
rb_define_method(cNode, "descendant_for_byte_range",
|
593
|
+
node_descendant_for_byte_range, 2);
|
594
|
+
rb_define_method(cNode, "descendant_for_point_range",
|
595
|
+
node_descendant_for_point_range, 2);
|
596
|
+
rb_define_method(cNode, "edit", node_edit, 1);
|
597
|
+
rb_define_method(cNode, "end_byte", node_end_byte, 0);
|
598
|
+
rb_define_method(cNode, "end_point", node_end_point, 0);
|
599
|
+
rb_define_method(cNode, "field_name_for_child", node_field_name_for_child, 1);
|
600
|
+
rb_define_method(cNode, "first_child_for_byte", node_first_child_for_byte, 1);
|
601
|
+
rb_define_method(cNode, "first_named_child_for_byte",
|
602
|
+
node_first_named_child_for_byte, 1);
|
603
|
+
rb_define_method(cNode, "grammar_symbol", node_grammar_symbol, 0);
|
604
|
+
rb_define_method(cNode, "grammar_type", node_grammar_type, 0);
|
605
|
+
rb_define_method(cNode, "language", node_language, 0);
|
606
|
+
rb_define_method(cNode, "named_child", node_named_child, 1);
|
607
|
+
rb_define_method(cNode, "named_child_count", node_named_child_count, 0);
|
608
|
+
rb_define_method(cNode, "named_descendant_for_byte_range",
|
609
|
+
node_named_descendant_for_byte_range, 2);
|
610
|
+
rb_define_method(cNode, "named_descendant_for_point_range",
|
611
|
+
node_named_descendant_for_point_range, 2);
|
612
|
+
rb_define_method(cNode, "next_named_sibling", node_next_named_sibling, 0);
|
613
|
+
rb_define_method(cNode, "next_parse_state", node_next_parse_state, 0);
|
614
|
+
rb_define_method(cNode, "next_sibling", node_next_sibling, 0);
|
615
|
+
rb_define_method(cNode, "parent", node_parent, 0);
|
616
|
+
rb_define_method(cNode, "parse_state", node_parse_state, 0);
|
617
|
+
rb_define_method(cNode, "prev_named_sibling", node_prev_named_sibling, 0);
|
618
|
+
rb_define_method(cNode, "prev_sibling", node_prev_sibling, 0);
|
619
|
+
rb_define_method(cNode, "start_byte", node_start_byte, 0);
|
620
|
+
rb_define_method(cNode, "start_point", node_start_point, 0);
|
621
|
+
rb_define_method(cNode, "symbol", node_symbol, 0);
|
622
|
+
rb_define_method(cNode, "type", node_type, 0);
|
623
|
+
}
|