@kerebron/tree-sitter 0.4.28 → 0.4.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/esm/deno-tree-sitter/main/extended/base_node.js +1 -0
  2. package/esm/deno-tree-sitter/main/extended/base_node.js.map +1 -0
  3. package/esm/deno-tree-sitter/main/extended/node_extended.js +1 -0
  4. package/esm/deno-tree-sitter/main/extended/node_extended.js.map +1 -0
  5. package/esm/deno-tree-sitter/main/extended/parser.js +1 -0
  6. package/esm/deno-tree-sitter/main/extended/parser.js.map +1 -0
  7. package/esm/deno-tree-sitter/main/extended/soft_node.js +1 -0
  8. package/esm/deno-tree-sitter/main/extended/soft_node.js.map +1 -0
  9. package/esm/deno-tree-sitter/main/extended/soft_text_node.js +1 -0
  10. package/esm/deno-tree-sitter/main/extended/soft_text_node.js.map +1 -0
  11. package/esm/deno-tree-sitter/main/extended/whitespace_node.js +1 -0
  12. package/esm/deno-tree-sitter/main/extended/whitespace_node.js.map +1 -0
  13. package/esm/deno-tree-sitter/main/extras/misc.js +1 -0
  14. package/esm/deno-tree-sitter/main/extras/misc.js.map +1 -0
  15. package/esm/deno-tree-sitter/main/tree_sitter/bindings.js +1 -0
  16. package/esm/deno-tree-sitter/main/tree_sitter/bindings.js.map +1 -0
  17. package/esm/deno-tree-sitter/main/tree_sitter/constants.js +1 -0
  18. package/esm/deno-tree-sitter/main/tree_sitter/constants.js.map +1 -0
  19. package/esm/deno-tree-sitter/main/tree_sitter/language.js +1 -0
  20. package/esm/deno-tree-sitter/main/tree_sitter/language.js.map +1 -0
  21. package/esm/deno-tree-sitter/main/tree_sitter/lookahead_iterator.js +1 -0
  22. package/esm/deno-tree-sitter/main/tree_sitter/lookahead_iterator.js.map +1 -0
  23. package/esm/deno-tree-sitter/main/tree_sitter/marshal.js +1 -0
  24. package/esm/deno-tree-sitter/main/tree_sitter/marshal.js.map +1 -0
  25. package/esm/deno-tree-sitter/main/tree_sitter/node.js +1 -0
  26. package/esm/deno-tree-sitter/main/tree_sitter/node.js.map +1 -0
  27. package/esm/deno-tree-sitter/main/tree_sitter/parser.js +1 -0
  28. package/esm/deno-tree-sitter/main/tree_sitter/parser.js.map +1 -0
  29. package/esm/deno-tree-sitter/main/tree_sitter/query.js +1 -0
  30. package/esm/deno-tree-sitter/main/tree_sitter/query.js.map +1 -0
  31. package/esm/deno-tree-sitter/main/tree_sitter/tree.js +1 -0
  32. package/esm/deno-tree-sitter/main/tree_sitter/tree.js.map +1 -0
  33. package/esm/deno-tree-sitter/main/tree_sitter/tree_cursor.js +1 -0
  34. package/esm/deno-tree-sitter/main/tree_sitter/tree_cursor.js.map +1 -0
  35. package/esm/deno-tree-sitter/main/tree_sitter_wasm.js +0 -0
  36. package/esm/deno-tree-sitter/main/tree_sitter_wasm.js.map +1 -0
  37. package/esm/deno-tree-sitter/main/wasm_loader.js +1 -0
  38. package/esm/deno-tree-sitter/main/wasm_loader.js.map +1 -0
  39. package/esm/deno-tree-sitter/main/wasm_loader_with_defaults.js +1 -0
  40. package/esm/deno-tree-sitter/main/wasm_loader_with_defaults.js.map +1 -0
  41. package/esm/mod.js +1 -0
  42. package/esm/mod.js.map +1 -0
  43. package/package.json +5 -1
  44. package/src/deno-tree-sitter/main/extended/base_node.js +174 -0
  45. package/src/deno-tree-sitter/main/extended/node_extended.js +588 -0
  46. package/src/deno-tree-sitter/main/extended/parser.js +87 -0
  47. package/src/deno-tree-sitter/main/extended/soft_node.js +32 -0
  48. package/src/deno-tree-sitter/main/extended/soft_text_node.js +11 -0
  49. package/src/deno-tree-sitter/main/extended/whitespace_node.js +11 -0
  50. package/src/deno-tree-sitter/main/extras/misc.js +12 -0
  51. package/src/deno-tree-sitter/main/tree_sitter/bindings.js +26 -0
  52. package/src/deno-tree-sitter/main/tree_sitter/constants.js +79 -0
  53. package/src/deno-tree-sitter/main/tree_sitter/language.js +289 -0
  54. package/src/deno-tree-sitter/main/tree_sitter/lookahead_iterator.js +74 -0
  55. package/src/deno-tree-sitter/main/tree_sitter/marshal.js +186 -0
  56. package/src/deno-tree-sitter/main/tree_sitter/node.js +616 -0
  57. package/src/deno-tree-sitter/main/tree_sitter/parser.js +273 -0
  58. package/src/deno-tree-sitter/main/tree_sitter/query.js +705 -0
  59. package/src/deno-tree-sitter/main/tree_sitter/tree.js +145 -0
  60. package/src/deno-tree-sitter/main/tree_sitter/tree_cursor.js +314 -0
  61. package/src/deno-tree-sitter/main/tree_sitter_wasm.js +0 -0
  62. package/src/deno-tree-sitter/main/wasm_loader.js +1702 -0
  63. package/src/deno-tree-sitter/main/wasm_loader_with_defaults.js +9 -0
  64. package/src/mod.ts +8 -0
@@ -0,0 +1,616 @@
1
+ import { INTERNAL, assertInternal, SIZE_OF_INT, SIZE_OF_NODE, SIZE_OF_POINT, ZERO_POINT, isPoint, C } from "./constants.js"
2
+ import { getText, Tree } from "./tree.js"
3
+ import { TreeCursor } from "./tree_cursor.js"
4
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
5
+ import { Language } from "./language.js"
6
+ import { marshalNode, marshalPoint, unmarshalNode, unmarshalPoint } from "./marshal.js"
7
+ import { TRANSFER_BUFFER } from "./parser.js"
8
+
9
+ /** A single node within a syntax {@link Tree}. */
10
+ export class Node {
11
+ /** @internal */
12
+ [0] = 0 // Internal handle for Wasm
13
+
14
+ /** @internal */
15
+ _children
16
+
17
+ /** @internal */
18
+ _namedChildren
19
+
20
+ /** @internal */
21
+ constructor(internal, { id, tree, startIndex, startPosition, other }) {
22
+ assertInternal(internal)
23
+ this[0] = other
24
+ this.id = id
25
+ this.tree = tree
26
+ this.startIndex = startIndex
27
+ this.startPosition = startPosition
28
+ }
29
+
30
+ /**
31
+ * The numeric id for this node that is unique.
32
+ *
33
+ * Within a given syntax tree, no two nodes have the same id. However:
34
+ *
35
+ * * If a new tree is created based on an older tree, and a node from the old tree is reused in
36
+ * the process, then that node will have the same id in both trees.
37
+ *
38
+ * * A node not marked as having changes does not guarantee it was reused.
39
+ *
40
+ * * If a node is marked as having changed in the old tree, it will not be reused.
41
+ */
42
+ id
43
+
44
+ /** The byte index where this node starts. */
45
+ startIndex
46
+
47
+ /** The position where this node starts. */
48
+ startPosition
49
+
50
+ /** The tree that this node belongs to. */
51
+ tree
52
+
53
+ /** Get this node's type as a numerical id. */
54
+ get typeId() {
55
+ marshalNode(this)
56
+ return C._ts_node_symbol_wasm(this.tree[0])
57
+ }
58
+
59
+ /**
60
+ * Get the node's type as a numerical id as it appears in the grammar,
61
+ * ignoring aliases.
62
+ */
63
+ get grammarId() {
64
+ marshalNode(this)
65
+ return C._ts_node_grammar_symbol_wasm(this.tree[0])
66
+ }
67
+
68
+ /** Get this node's type as a string. */
69
+ get type() {
70
+ return this.tree.language.types[this.typeId] || "ERROR"
71
+ }
72
+
73
+ /**
74
+ * Get this node's symbol name as it appears in the grammar, ignoring
75
+ * aliases as a string.
76
+ */
77
+ get grammarType() {
78
+ return this.tree.language.types[this.grammarId] || "ERROR"
79
+ }
80
+
81
+ /**
82
+ * Check if this node is *named*.
83
+ *
84
+ * Named nodes correspond to named rules in the grammar, whereas
85
+ * *anonymous* nodes correspond to string literals in the grammar.
86
+ */
87
+ get isNamed() {
88
+ marshalNode(this)
89
+ return C._ts_node_is_named_wasm(this.tree[0]) === 1
90
+ }
91
+
92
+ /**
93
+ * Check if this node is *extra*.
94
+ *
95
+ * Extra nodes represent things like comments, which are not required
96
+ * by the grammar, but can appear anywhere.
97
+ */
98
+ get isExtra() {
99
+ marshalNode(this)
100
+ return C._ts_node_is_extra_wasm(this.tree[0]) === 1
101
+ }
102
+
103
+ /**
104
+ * Check if this node represents a syntax error.
105
+ *
106
+ * Syntax errors represent parts of the code that could not be incorporated
107
+ * into a valid syntax tree.
108
+ */
109
+ get isError() {
110
+ marshalNode(this)
111
+ return C._ts_node_is_error_wasm(this.tree[0]) === 1
112
+ }
113
+
114
+ /**
115
+ * Check if this node is *missing*.
116
+ *
117
+ * Missing nodes are inserted by the parser in order to recover from
118
+ * certain kinds of syntax errors.
119
+ */
120
+ get isMissing() {
121
+ marshalNode(this)
122
+ return C._ts_node_is_missing_wasm(this.tree[0]) === 1
123
+ }
124
+
125
+ /** Check if this node has been edited. */
126
+ get hasChanges() {
127
+ marshalNode(this)
128
+ return C._ts_node_has_changes_wasm(this.tree[0]) === 1
129
+ }
130
+
131
+ /**
132
+ * Check if this node represents a syntax error or contains any syntax
133
+ * errors anywhere within it.
134
+ */
135
+ get hasError() {
136
+ marshalNode(this)
137
+ return C._ts_node_has_error_wasm(this.tree[0]) === 1
138
+ }
139
+
140
+ /** Get the byte index where this node ends. */
141
+ get endIndex() {
142
+ marshalNode(this)
143
+ return C._ts_node_end_index_wasm(this.tree[0])
144
+ }
145
+
146
+ /** Get the position where this node ends. */
147
+ get endPosition() {
148
+ marshalNode(this)
149
+ C._ts_node_end_point_wasm(this.tree[0])
150
+ return unmarshalPoint(TRANSFER_BUFFER)
151
+ }
152
+
153
+ /** Get the string content of this node. */
154
+ get text() {
155
+ return getText(this.tree, this.startIndex, this.endIndex, this.startPosition)
156
+ }
157
+
158
+ /** Get this node's parse state. */
159
+ get parseState() {
160
+ marshalNode(this)
161
+ return C._ts_node_parse_state_wasm(this.tree[0])
162
+ }
163
+
164
+ /** Get the parse state after this node. */
165
+ get nextParseState() {
166
+ marshalNode(this)
167
+ return C._ts_node_next_parse_state_wasm(this.tree[0])
168
+ }
169
+
170
+ /** Check if this node is equal to another node. */
171
+ equals(other) {
172
+ return this.tree === other.tree && this.id === other.id
173
+ }
174
+
175
+ /**
176
+ * Get the node's child at the given index, where zero represents the first child.
177
+ *
178
+ * This method is fairly fast, but its cost is technically log(n), so if
179
+ * you might be iterating over a long list of children, you should use
180
+ * {@link Node#children} instead.
181
+ */
182
+ child(index) {
183
+ marshalNode(this)
184
+ C._ts_node_child_wasm(this.tree[0], index)
185
+ return unmarshalNode(this.tree)
186
+ }
187
+
188
+ /**
189
+ * Get this node's *named* child at the given index.
190
+ *
191
+ * See also {@link Node#isNamed}.
192
+ * This method is fairly fast, but its cost is technically log(n), so if
193
+ * you might be iterating over a long list of children, you should use
194
+ * {@link Node#namedChildren} instead.
195
+ */
196
+ namedChild(index) {
197
+ marshalNode(this)
198
+ C._ts_node_named_child_wasm(this.tree[0], index)
199
+ return unmarshalNode(this.tree)
200
+ }
201
+
202
+ /**
203
+ * Get this node's child with the given numerical field id.
204
+ *
205
+ * See also {@link Node#childForFieldName}. You can
206
+ * convert a field name to an id using {@link Language#fieldIdForName}.
207
+ */
208
+ childForFieldId(fieldId) {
209
+ marshalNode(this)
210
+ C._ts_node_child_by_field_id_wasm(this.tree[0], fieldId)
211
+ return unmarshalNode(this.tree)
212
+ }
213
+
214
+ /**
215
+ * Get the first child with the given field name.
216
+ *
217
+ * If multiple children may have the same field name, access them using
218
+ * {@link Node#childrenForFieldName}.
219
+ */
220
+ childForFieldName(fieldName) {
221
+ const fieldId = this.tree.language.fields.indexOf(fieldName)
222
+ if (fieldId !== -1) return this.childForFieldId(fieldId)
223
+ return null
224
+ }
225
+
226
+ /** Get the field name of this node's child at the given index. */
227
+ fieldNameForChild(index) {
228
+ marshalNode(this)
229
+ const address = C._ts_node_field_name_for_child_wasm(this.tree[0], index)
230
+ if (!address) return null
231
+ return C.AsciiToString(address)
232
+ }
233
+
234
+ /** Get the field name of this node's named child at the given index. */
235
+ fieldNameForNamedChild(index) {
236
+ marshalNode(this)
237
+ const address = C._ts_node_field_name_for_named_child_wasm(this.tree[0], index)
238
+ if (!address) return null
239
+ return C.AsciiToString(address)
240
+ }
241
+ /**
242
+ * Get an array of this node's children with a given field name.
243
+ *
244
+ * See also {@link Node#children}.
245
+ */
246
+ childrenForFieldName(fieldName) {
247
+ const fieldId = this.tree.language.fields.indexOf(fieldName)
248
+ if (fieldId !== -1 && fieldId !== 0) return this.childrenForFieldId(fieldId)
249
+ return []
250
+ }
251
+
252
+ /**
253
+ * Get an array of this node's children with a given field id.
254
+ *
255
+ * See also {@link Node#childrenForFieldName}.
256
+ */
257
+ childrenForFieldId(fieldId) {
258
+ marshalNode(this)
259
+ C._ts_node_children_by_field_id_wasm(this.tree[0], fieldId)
260
+ const count = C.getValue(TRANSFER_BUFFER, "i32")
261
+ const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32")
262
+ const result = new Array(count)
263
+
264
+ if (count > 0) {
265
+ let address = buffer
266
+ for (let i = 0; i < count; i++) {
267
+ result[i] = unmarshalNode(this.tree, address)
268
+ address += SIZE_OF_NODE
269
+ }
270
+ C._free(buffer)
271
+ }
272
+ return result
273
+ }
274
+
275
+ /** Get the node's first child that contains or starts after the given byte offset. */
276
+ firstChildForIndex(index) {
277
+ marshalNode(this)
278
+ const address = TRANSFER_BUFFER + SIZE_OF_NODE
279
+ C.setValue(address, index, "i32")
280
+ C._ts_node_first_child_for_byte_wasm(this.tree[0])
281
+ return unmarshalNode(this.tree)
282
+ }
283
+
284
+ /** Get the node's first named child that contains or starts after the given byte offset. */
285
+ firstNamedChildForIndex(index) {
286
+ marshalNode(this)
287
+ const address = TRANSFER_BUFFER + SIZE_OF_NODE
288
+ C.setValue(address, index, "i32")
289
+ C._ts_node_first_named_child_for_byte_wasm(this.tree[0])
290
+ return unmarshalNode(this.tree)
291
+ }
292
+
293
+ /** Get this node's number of children. */
294
+ get childCount() {
295
+ marshalNode(this)
296
+ return C._ts_node_child_count_wasm(this.tree[0])
297
+ }
298
+
299
+ /**
300
+ * Get this node's number of *named* children.
301
+ *
302
+ * See also {@link Node#isNamed}.
303
+ */
304
+ get namedChildCount() {
305
+ marshalNode(this)
306
+ return C._ts_node_named_child_count_wasm(this.tree[0])
307
+ }
308
+
309
+ /** Get this node's first child. */
310
+ get firstChild() {
311
+ return this.child(0)
312
+ }
313
+
314
+ /**
315
+ * Get this node's first named child.
316
+ *
317
+ * See also {@link Node#isNamed}.
318
+ */
319
+ get firstNamedChild() {
320
+ return this.namedChild(0)
321
+ }
322
+
323
+ /** Get this node's last child. */
324
+ get lastChild() {
325
+ return this.child(this.childCount - 1)
326
+ }
327
+
328
+ /**
329
+ * Get this node's last named child.
330
+ *
331
+ * See also {@link Node#isNamed}.
332
+ */
333
+ get lastNamedChild() {
334
+ return this.namedChild(this.namedChildCount - 1)
335
+ }
336
+
337
+ /**
338
+ * Iterate over this node's children.
339
+ *
340
+ * If you're walking the tree recursively, you may want to use the
341
+ * {@link TreeCursor} APIs directly instead.
342
+ */
343
+ get children() {
344
+ if (!this._children) {
345
+ marshalNode(this)
346
+ C._ts_node_children_wasm(this.tree[0])
347
+ const count = C.getValue(TRANSFER_BUFFER, "i32")
348
+ const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32")
349
+ this._children = new Array(count)
350
+ if (count > 0) {
351
+ let address = buffer
352
+ for (let i = 0; i < count; i++) {
353
+ this._children[i] = unmarshalNode(this.tree, address)
354
+ address += SIZE_OF_NODE
355
+ }
356
+ C._free(buffer)
357
+ }
358
+ }
359
+ return this._children
360
+ }
361
+
362
+ /**
363
+ * Iterate over this node's named children.
364
+ *
365
+ * See also {@link Node#children}.
366
+ */
367
+ get namedChildren() {
368
+ if (!this._namedChildren) {
369
+ marshalNode(this)
370
+ C._ts_node_named_children_wasm(this.tree[0])
371
+ const count = C.getValue(TRANSFER_BUFFER, "i32")
372
+ const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32")
373
+ this._namedChildren = new Array(count)
374
+ if (count > 0) {
375
+ let address = buffer
376
+ for (let i = 0; i < count; i++) {
377
+ this._namedChildren[i] = unmarshalNode(this.tree, address)
378
+ address += SIZE_OF_NODE
379
+ }
380
+ C._free(buffer)
381
+ }
382
+ }
383
+ return this._namedChildren
384
+ }
385
+
386
+ /**
387
+ * Get the descendants of this node that are the given type, or in the given types array.
388
+ *
389
+ * The types array should contain node type strings, which can be retrieved from {@link Language#types}.
390
+ *
391
+ * Additionally, a `startPosition` and `endPosition` can be passed in to restrict the search to a byte range.
392
+ */
393
+ descendantsOfType(types, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
394
+ if (!Array.isArray(types)) types = [types]
395
+
396
+ // Convert the type strings to numeric type symbols
397
+ const symbols = []
398
+ const typesBySymbol = this.tree.language.types
399
+ for (const node_type of types) {
400
+ if (node_type == "ERROR") {
401
+ symbols.push(65535) // Internally, ts_builtin_sym_error is -1, which is UINT_16MAX
402
+ }
403
+ }
404
+ for (let i = 0, n = typesBySymbol.length; i < n; i++) {
405
+ if (types.includes(typesBySymbol[i])) {
406
+ symbols.push(i)
407
+ }
408
+ }
409
+
410
+ // Copy the array of symbols to the Wasm heap
411
+ const symbolsAddress = C._malloc(SIZE_OF_INT * symbols.length)
412
+ for (let i = 0, n = symbols.length; i < n; i++) {
413
+ C.setValue(symbolsAddress + i * SIZE_OF_INT, symbols[i], "i32")
414
+ }
415
+
416
+ // Call the C API to compute the descendants
417
+ marshalNode(this)
418
+ C._ts_node_descendants_of_type_wasm(this.tree[0], symbolsAddress, symbols.length, startPosition.row, startPosition.column, endPosition.row, endPosition.column)
419
+
420
+ // Instantiate the nodes based on the data returned
421
+ const descendantCount = C.getValue(TRANSFER_BUFFER, "i32")
422
+ const descendantAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32")
423
+ const result = new Array(descendantCount)
424
+ if (descendantCount > 0) {
425
+ let address = descendantAddress
426
+ for (let i = 0; i < descendantCount; i++) {
427
+ result[i] = unmarshalNode(this.tree, address)
428
+ address += SIZE_OF_NODE
429
+ }
430
+ }
431
+
432
+ // Free the intermediate buffers
433
+ C._free(descendantAddress)
434
+ C._free(symbolsAddress)
435
+ return result
436
+ }
437
+
438
+ /** Get this node's next sibling. */
439
+ get nextSibling() {
440
+ marshalNode(this)
441
+ C._ts_node_next_sibling_wasm(this.tree[0])
442
+ return unmarshalNode(this.tree)
443
+ }
444
+
445
+ /** Get this node's previous sibling. */
446
+ get previousSibling() {
447
+ marshalNode(this)
448
+ C._ts_node_prev_sibling_wasm(this.tree[0])
449
+ return unmarshalNode(this.tree)
450
+ }
451
+
452
+ /**
453
+ * Get this node's next *named* sibling.
454
+ *
455
+ * See also {@link Node#isNamed}.
456
+ */
457
+ get nextNamedSibling() {
458
+ marshalNode(this)
459
+ C._ts_node_next_named_sibling_wasm(this.tree[0])
460
+ return unmarshalNode(this.tree)
461
+ }
462
+
463
+ /**
464
+ * Get this node's previous *named* sibling.
465
+ *
466
+ * See also {@link Node#isNamed}.
467
+ */
468
+ get previousNamedSibling() {
469
+ marshalNode(this)
470
+ C._ts_node_prev_named_sibling_wasm(this.tree[0])
471
+ return unmarshalNode(this.tree)
472
+ }
473
+
474
+ /** Get the node's number of descendants, including one for the node itself. */
475
+ get descendantCount() {
476
+ marshalNode(this)
477
+ return C._ts_node_descendant_count_wasm(this.tree[0])
478
+ }
479
+
480
+ /**
481
+ * Get this node's immediate parent.
482
+ * Prefer {@link Node#childWithDescendant} for iterating over this node's ancestors.
483
+ */
484
+ get parent() {
485
+ marshalNode(this)
486
+ C._ts_node_parent_wasm(this.tree[0])
487
+ return unmarshalNode(this.tree)
488
+ }
489
+
490
+ /**
491
+ * Get the node that contains `descendant`.
492
+ *
493
+ * Note that this can return `descendant` itself.
494
+ */
495
+ childWithDescendant(descendant) {
496
+ marshalNode(this)
497
+ marshalNode(descendant, 1)
498
+ C._ts_node_child_with_descendant_wasm(this.tree[0])
499
+ return unmarshalNode(this.tree)
500
+ }
501
+
502
+ /** Get the smallest node within this node that spans the given byte range. */
503
+ descendantForIndex(start, end = start) {
504
+ if (typeof start !== "number" || typeof end !== "number") {
505
+ throw new Error("Arguments must be numbers")
506
+ }
507
+
508
+ marshalNode(this)
509
+ const address = TRANSFER_BUFFER + SIZE_OF_NODE
510
+ C.setValue(address, start, "i32")
511
+ C.setValue(address + SIZE_OF_INT, end, "i32")
512
+ C._ts_node_descendant_for_index_wasm(this.tree[0])
513
+ return unmarshalNode(this.tree)
514
+ }
515
+
516
+ /** Get the smallest named node within this node that spans the given byte range. */
517
+ namedDescendantForIndex(start, end = start) {
518
+ if (typeof start !== "number" || typeof end !== "number") {
519
+ throw new Error("Arguments must be numbers")
520
+ }
521
+
522
+ marshalNode(this)
523
+ const address = TRANSFER_BUFFER + SIZE_OF_NODE
524
+ C.setValue(address, start, "i32")
525
+ C.setValue(address + SIZE_OF_INT, end, "i32")
526
+ C._ts_node_named_descendant_for_index_wasm(this.tree[0])
527
+ return unmarshalNode(this.tree)
528
+ }
529
+
530
+ /** Get the smallest node within this node that spans the given point range. */
531
+ descendantForPosition(start, end = start) {
532
+ if (!isPoint(start) || !isPoint(end)) {
533
+ throw new Error("Arguments must be {row, column} objects")
534
+ }
535
+
536
+ marshalNode(this)
537
+ const address = TRANSFER_BUFFER + SIZE_OF_NODE
538
+ marshalPoint(address, start)
539
+ marshalPoint(address + SIZE_OF_POINT, end)
540
+ C._ts_node_descendant_for_position_wasm(this.tree[0])
541
+ return unmarshalNode(this.tree)
542
+ }
543
+
544
+ /** Get the smallest named node within this node that spans the given point range. */
545
+ namedDescendantForPosition(start, end = start) {
546
+ if (!isPoint(start) || !isPoint(end)) {
547
+ throw new Error("Arguments must be {row, column} objects")
548
+ }
549
+
550
+ marshalNode(this)
551
+ const address = TRANSFER_BUFFER + SIZE_OF_NODE
552
+ marshalPoint(address, start)
553
+ marshalPoint(address + SIZE_OF_POINT, end)
554
+ C._ts_node_named_descendant_for_position_wasm(this.tree[0])
555
+ return unmarshalNode(this.tree)
556
+ }
557
+
558
+ /**
559
+ * Create a new {@link TreeCursor} starting from this node.
560
+ *
561
+ * Note that the given node is considered the root of the cursor,
562
+ * and the cursor cannot walk outside this node.
563
+ */
564
+ walk() {
565
+ marshalNode(this)
566
+ C._ts_tree_cursor_new_wasm(this.tree[0])
567
+ return new TreeCursor(INTERNAL, this.tree)
568
+ }
569
+
570
+ /**
571
+ * Edit this node to keep it in-sync with source code that has been edited.
572
+ *
573
+ * This function is only rarely needed. When you edit a syntax tree with
574
+ * the {@link Tree#edit} method, all of the nodes that you retrieve from
575
+ * the tree afterward will already reflect the edit. You only need to
576
+ * use {@link Node#edit} when you have a specific {@link Node} instance that
577
+ * you want to keep and continue to use after an edit.
578
+ */
579
+ edit(edit) {
580
+ if (this.startIndex >= edit.oldEndIndex) {
581
+ this.startIndex = edit.newEndIndex + (this.startIndex - edit.oldEndIndex)
582
+ let subbedPointRow
583
+ let subbedPointColumn
584
+ if (this.startPosition.row > edit.oldEndPosition.row) {
585
+ subbedPointRow = this.startPosition.row - edit.oldEndPosition.row
586
+ subbedPointColumn = this.startPosition.column
587
+ } else {
588
+ subbedPointRow = 0
589
+ subbedPointColumn = this.startPosition.column
590
+ if (this.startPosition.column >= edit.oldEndPosition.column) {
591
+ subbedPointColumn = this.startPosition.column - edit.oldEndPosition.column
592
+ }
593
+ }
594
+
595
+ if (subbedPointRow > 0) {
596
+ this.startPosition.row += subbedPointRow
597
+ this.startPosition.column = subbedPointColumn
598
+ } else {
599
+ this.startPosition.column += subbedPointColumn
600
+ }
601
+ } else if (this.startIndex > edit.startIndex) {
602
+ this.startIndex = edit.newEndIndex
603
+ this.startPosition.row = edit.newEndPosition.row
604
+ this.startPosition.column = edit.newEndPosition.column
605
+ }
606
+ }
607
+
608
+ /** Get the S-expression representation of this node. */
609
+ toString() {
610
+ marshalNode(this)
611
+ const address = C._ts_node_to_string_wasm(this.tree[0])
612
+ const result = C.AsciiToString(address)
613
+ C._free(address)
614
+ return result
615
+ }
616
+ }