-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathch_usi_si_seart_treesitter_Node.cc
400 lines (370 loc) · 14.7 KB
/
ch_usi_si_seart_treesitter_Node.cc
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "ch_usi_si_seart_treesitter.h"
#include "ch_usi_si_seart_treesitter_Node.h"
#include <jni.h>
#include <string.h>
#include <tree_sitter/api.h>
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getChild(
JNIEnv* env, jobject thisObject, jint index, jboolean named) {
uint32_t (*child_counter)(TSNode) = (bool)named
? ts_node_named_child_count
: ts_node_child_count;
TSNode (*child_getter)(TSNode, uint32_t) = (bool)named
? ts_node_named_child
: ts_node_child;
TSNode node = __unmarshalNode(env, thisObject);
uint32_t childIndex = (uint32_t)index;
if ((childIndex < 0) || (childIndex >= child_counter(node))) {
__throwIOB(env, index);
return NULL;
}
TSNode child = child_getter(node, childIndex);
jobject childObject = __marshalNode(env, child);
__copyTree(env, thisObject, childObject);
return childObject;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getChildByFieldName(
JNIEnv* env, jobject thisObject, jstring name) {
if (name == NULL) {
__throwNPE(env, "Field name must not be null!");
return NULL;
}
uint32_t length = env->GetStringLength(name);
const char* childName = env->GetStringUTFChars(name, NULL);
TSNode node = __unmarshalNode(env, thisObject);
TSNode child = ts_node_child_by_field_name(node, childName, length);
env->ReleaseStringUTFChars(name, childName);
if (ts_node_is_null(child)) return NULL;
jobject childObject = __marshalNode(env, child);
__copyTree(env, thisObject, childObject);
return childObject;
}
JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_Node_getChildCount(
JNIEnv* env, jobject thisObject, jboolean named) {
uint32_t (*child_counter)(TSNode) = (bool)named
? ts_node_named_child_count
: ts_node_child_count;
TSNode node = __unmarshalNode(env, thisObject);
uint32_t count = ts_node_is_null(node) ? 0 : child_counter(node);
return (jint)count;
}
JNIEXPORT jobjectArray JNICALL Java_ch_usi_si_seart_treesitter_Node_getChildren(
JNIEnv* env, jclass thisClass, jobject nodeObject, jboolean named) {
uint32_t (*child_counter)(TSNode) = (bool)named
? ts_node_named_child_count
: ts_node_child_count;
TSNode (*child_getter)(TSNode, uint32_t) = (bool)named
? ts_node_named_child
: ts_node_child;
TSNode node = __unmarshalNode(env, nodeObject);
uint32_t count = ts_node_is_null(node) ? 0 : child_counter(node);
jobjectArray children = env->NewObjectArray(count, _nodeClass, NULL);
for (uint32_t i = 0; i < count; i++) {
TSNode child = child_getter(node, i);
jobject childObject = __marshalNode(env, child);
__copyTree(env, nodeObject, childObject);
env->SetObjectArrayElement(children, i, childObject);
}
return children;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getDescendant__IIZ(
JNIEnv* env, jobject thisObject, jint start, jint end, jboolean named) {
if (start < 0 || end < 0) {
__throwIAE(env, "The start and end bytes must not be negative!");
return NULL;
}
if (start > end) {
__throwIAE(env, "The starting byte of the range must not be greater than the ending byte!");
return NULL;
}
// Not sure why I need to multiply by two, again probably because of utf-16
TSNode node = __unmarshalNode(env, thisObject);
uint32_t nodeStart = ts_node_start_byte(node);
uint32_t rangeStart = (uint32_t)start * 2;
if (rangeStart < nodeStart) {
__throwBOB(env, start);
return NULL;
}
uint32_t nodeEnd = ts_node_end_byte(node);
uint32_t rangeEnd = (uint32_t)end * 2;
if (rangeEnd > nodeEnd) {
__throwBOB(env, end);
return NULL;
}
TSNode (*descendant_getter)(TSNode, uint32_t, uint32_t) = (bool)named
? ts_node_named_descendant_for_byte_range
: ts_node_descendant_for_byte_range;
TSNode descendant = descendant_getter(node, rangeStart, rangeEnd);
jobject descendantObject = __marshalNode(env, descendant);
__copyTree(env, thisObject, descendantObject);
return descendantObject;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getDescendant__Lch_usi_si_seart_treesitter_Point_2Lch_usi_si_seart_treesitter_Point_2Z(
JNIEnv* env, jobject thisObject, jobject startPointObject, jobject endPointObject, jboolean named) {
if (startPointObject == NULL) {
__throwNPE(env, "Start point must not be null!");
return NULL;
}
if (endPointObject == NULL) {
__throwNPE(env, "End point must not be null!");
return NULL;
}
TSNode node = __unmarshalNode(env, thisObject);
TSPoint startPoint = __unmarshalPoint(env, startPointObject);
TSPoint endPoint = __unmarshalPoint(env, endPointObject);
if (endPoint.row < 0 || endPoint.column < 0) {
__throwIAE(env, "End point can not have negative coordinates!");
return NULL;
}
if (startPoint.row < 0 || startPoint.column < 0) {
__throwIAE(env, "Start point can not have negative coordinates!");
return NULL;
}
if (__comparePoints(startPoint, endPoint) == GT) {
__throwIAE(env, "Start point can not be greater than end point!");
return NULL;
}
TSPoint lowerBound = ts_node_start_point(node);
if (__comparePoints(lowerBound, startPoint) == GT) {
__throwPOB(env, startPointObject);
return NULL;
}
TSPoint upperBound = ts_node_end_point(node);
if (__comparePoints(endPoint, upperBound) == GT) {
__throwPOB(env, endPointObject);
return NULL;
}
TSNode (*descendant_getter)(TSNode, TSPoint, TSPoint) = (bool)named
? ts_node_named_descendant_for_point_range
: ts_node_descendant_for_point_range;
TSNode descendant = descendant_getter(node, startPoint, endPoint);
jobject descendantObject = __marshalNode(env, descendant);
__copyTree(env, thisObject, descendantObject);
return descendantObject;
}
JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_Node_getDescendantCount(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_null(node) ? (jint)0 : (jint)ts_node_descendant_count(node);
}
JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_Node_getEndByte(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_null(node) ? (jint)0 : (jint)ts_node_end_byte(node) / 2;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getEndPoint(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
if (ts_node_is_null(node)) return env->CallStaticObjectMethod(_pointClass, _pointOriginStaticMethod);
TSPoint point = ts_node_end_point(node);
return __marshalPoint(env, point);
}
JNIEXPORT jstring JNICALL Java_ch_usi_si_seart_treesitter_Node_getFieldNameForChild(
JNIEnv* env, jobject thisObject, jint index) {
TSNode node = __unmarshalNode(env, thisObject);
uint32_t childIndex = (uint32_t)index;
if ((childIndex < 0) || (childIndex >= ts_node_child_count(node))) {
__throwIOB(env, index);
return NULL;
}
const char* childName = ts_node_field_name_for_child(node, childIndex);
jstring result = env->NewStringUTF(childName);
return result;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getFirstChildForByte(
JNIEnv* env, jobject thisObject, jint offset, jboolean named) {
TSNode node = __unmarshalNode(env, thisObject);
uint32_t position = (uint32_t)offset * 2;
uint32_t nodeStart = ts_node_start_byte(node);
if (position < nodeStart) {
__throwBOB(env, offset);
return NULL;
}
uint32_t nodeEnd = ts_node_end_byte(node);
if (position > nodeEnd) {
__throwBOB(env, offset);
return NULL;
}
TSNode (*child_getter)(TSNode, uint32_t) = (bool)named
? ts_node_first_named_child_for_byte
: ts_node_first_child_for_byte;
TSNode child = child_getter(node, position);
jobject childObject = __marshalNode(env, child);
__copyTree(env, thisObject, childObject);
return childObject;
}
JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_Node_getNextParseState(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_null(node) ? (jint)-1 : (jint)ts_node_next_parse_state(node);
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getNextSibling(
JNIEnv* env, jobject thisObject, jboolean named) {
TSNode (*next_sibling_getter)(TSNode) = (bool)named
? ts_node_next_named_sibling
: ts_node_next_sibling;
TSNode node = __unmarshalNode(env, thisObject);
TSNode sibling = next_sibling_getter(node);
if (ts_node_is_null(sibling)) return NULL;
jobject siblingObject = __marshalNode(env, sibling);
__copyTree(env, thisObject, siblingObject);
return siblingObject;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getPrevSibling(
JNIEnv* env, jobject thisObject, jboolean named) {
TSNode (*prev_sibling_getter)(TSNode) = (bool)named
? ts_node_prev_named_sibling
: ts_node_prev_sibling;
TSNode node = __unmarshalNode(env, thisObject);
TSNode sibling = prev_sibling_getter(node);
if (ts_node_is_null(sibling)) return NULL;
jobject siblingObject = __marshalNode(env, sibling);
__copyTree(env, thisObject, siblingObject);
return siblingObject;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getParent(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
TSNode parent = ts_node_parent(node);
if (ts_node_is_null(parent)) return NULL;
jobject parentObject = __marshalNode(env, parent);
__copyTree(env, thisObject, parentObject);
return parentObject;
}
JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_Node_getParseState(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_null(node) ? (jint)-1 : (jint)ts_node_parse_state(node);
}
JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_Node_getStartByte(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_null(node) ? (jint)0 : (jint)ts_node_start_byte(node) / 2;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getStartPoint(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
if (ts_node_is_null(node)) return env->CallStaticObjectMethod(_pointClass, _pointOriginStaticMethod);
TSPoint point = ts_node_start_point(node);
return __marshalPoint(env, point);
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_getSymbol(
JNIEnv* env, jobject thisObject, jboolean grammar) {
jobject treeObject = env->GetObjectField(thisObject, _nodeTreeField);
if (treeObject == NULL) return NULL;
jobject languageObject = env->GetObjectField(treeObject, _treeLanguageField);
if (languageObject == NULL) return NULL;
const TSLanguage* language = __unmarshalLanguage(env, languageObject);
TSNode node = __unmarshalNode(env, thisObject);
if (ts_node_is_null(node)) return NULL;
TSSymbol (*symbol_getter)(TSNode) = (bool)grammar
? ts_node_grammar_symbol
: ts_node_symbol;
TSSymbol symbol = symbol_getter(node);
const char* name = ts_language_symbol_name(language, symbol);
TSSymbolType type = ts_language_symbol_type(language, symbol);
return _newObject(
_symbolClass,
_symbolConstructor,
(jint)symbol,
(jint)type,
env->NewStringUTF(name)
);
}
JNIEXPORT jstring JNICALL Java_ch_usi_si_seart_treesitter_Node_getType(
JNIEnv* env, jobject thisObject, jboolean grammar) {
TSNode node = __unmarshalNode(env, thisObject);
if (ts_node_is_null(node)) return NULL;
const char* (*type_getter)(TSNode) = (bool)grammar
? ts_node_grammar_type
: ts_node_type;
const char* type = type_getter(node);
return env->NewStringUTF(type);
}
JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_Node_hasChanges(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_has_changes(node) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_Node_hasError(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_has_error(node) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_Node_isError(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_error(node) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_Node_isExtra(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_extra(node) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_Node_isMissing(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_missing(node) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_Node_isNamed(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_named(node) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_Node_isNull(
JNIEnv* env, jobject thisObject) {
TSNode node = __unmarshalNode(env, thisObject);
return ts_node_is_null(node) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL Java_ch_usi_si_seart_treesitter_Node_equals(
JNIEnv* env, jclass self, jobject node, jobject other) {
TSNode node_1 = __unmarshalNode(env, node);
TSNode node_2 = __unmarshalNode(env, other);
return ts_node_eq(node_1, node_2) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_walk__(
JNIEnv* env, jobject thisObject) {
jobject treeObject = env->GetObjectField(thisObject, _nodeTreeField);
if (treeObject == NULL) {
__throwISE(env, "Cannot construct a TreeCursor instance without a Tree!");
return NULL;
}
TSNode node = __unmarshalNode(env, thisObject);
if (ts_node_is_null(node)) {
__throwISE(env, "Cannot construct a TreeCursor instance from a `null` Node!");
return NULL;
}
TSTreeCursor cursor = ts_tree_cursor_new(node);
return _newObject(
_treeCursorClass,
_treeCursorConstructor,
new TSTreeCursor(cursor),
cursor.context[0],
cursor.context[1],
cursor.id,
treeObject
);
}
JNIEXPORT jobject JNICALL Java_ch_usi_si_seart_treesitter_Node_walk__Lch_usi_si_seart_treesitter_Query_2(
JNIEnv* env, jobject thisObject, jobject queryObject) {
if (queryObject == NULL) {
__throwNPE(env, "Query must not be null!");
return NULL;
}
jobject treeObject = env->GetObjectField(thisObject, _nodeTreeField);
if (treeObject == NULL) {
__throwISE(env, "Cannot construct a QueryCursor instance without a Tree!");
return NULL;
}
TSNode node = __unmarshalNode(env, thisObject);
if (ts_node_is_null(node)) {
__throwISE(env, "Cannot construct a QueryCursor instance from a `null` Node!");
return NULL;
}
return _newObject(
_queryCursorClass,
_queryCursorConstructor,
ts_query_cursor_new(),
thisObject,
queryObject
);
}