@ast-grep/lang-javascript 0.0.2
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.
- package/LICENSE +21 -0
- package/README.md +24 -0
- package/index.d.ts +10 -0
- package/index.js +9 -0
- package/package.json +46 -0
- package/postinstall.js +4 -0
- package/prebuilds/prebuild-Linux-X64/parser.so +0 -0
- package/prebuilds/prebuild-Windows-X64/parser.so +0 -0
- package/prebuilds/prebuild-macOS-ARM64/parser.so +0 -0
- package/src/grammar.json +6931 -0
- package/src/node-types.json +3562 -0
- package/src/parser.c +83523 -0
- package/src/scanner.c +364 -0
- package/src/tree_sitter/alloc.h +54 -0
- package/src/tree_sitter/array.h +290 -0
- package/src/tree_sitter/parser.h +266 -0
- package/type.d.ts +2905 -0
package/src/scanner.c
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
#include "tree_sitter/parser.h"
|
|
2
|
+
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <wctype.h>
|
|
5
|
+
|
|
6
|
+
enum TokenType {
|
|
7
|
+
AUTOMATIC_SEMICOLON,
|
|
8
|
+
TEMPLATE_CHARS,
|
|
9
|
+
TERNARY_QMARK,
|
|
10
|
+
HTML_COMMENT,
|
|
11
|
+
LOGICAL_OR,
|
|
12
|
+
ESCAPE_SEQUENCE,
|
|
13
|
+
REGEX_PATTERN,
|
|
14
|
+
JSX_TEXT,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
void *tree_sitter_javascript_external_scanner_create() { return NULL; }
|
|
18
|
+
|
|
19
|
+
void tree_sitter_javascript_external_scanner_destroy(void *p) {}
|
|
20
|
+
|
|
21
|
+
unsigned tree_sitter_javascript_external_scanner_serialize(void *payload, char *buffer) { return 0; }
|
|
22
|
+
|
|
23
|
+
void tree_sitter_javascript_external_scanner_deserialize(void *p, const char *b, unsigned n) {}
|
|
24
|
+
|
|
25
|
+
static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
|
|
26
|
+
|
|
27
|
+
static inline void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
|
|
28
|
+
|
|
29
|
+
static bool scan_template_chars(TSLexer *lexer) {
|
|
30
|
+
lexer->result_symbol = TEMPLATE_CHARS;
|
|
31
|
+
for (bool has_content = false;; has_content = true) {
|
|
32
|
+
lexer->mark_end(lexer);
|
|
33
|
+
switch (lexer->lookahead) {
|
|
34
|
+
case '`':
|
|
35
|
+
return has_content;
|
|
36
|
+
case '\0':
|
|
37
|
+
return false;
|
|
38
|
+
case '$':
|
|
39
|
+
advance(lexer);
|
|
40
|
+
if (lexer->lookahead == '{') {
|
|
41
|
+
return has_content;
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
44
|
+
case '\\':
|
|
45
|
+
return has_content;
|
|
46
|
+
default:
|
|
47
|
+
advance(lexer);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
typedef enum {
|
|
53
|
+
REJECT, // Semicolon is illegal, ie a syntax error occurred
|
|
54
|
+
NO_NEWLINE, // Unclear if semicolon will be legal, continue
|
|
55
|
+
ACCEPT, // Semicolon is legal, assuming a comment was encountered
|
|
56
|
+
} WhitespaceResult;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param consume If false, only consume enough to check if comment indicates semicolon-legality
|
|
60
|
+
*/
|
|
61
|
+
static WhitespaceResult scan_whitespace_and_comments(TSLexer *lexer, bool *scanned_comment, bool consume) {
|
|
62
|
+
bool saw_block_newline = false;
|
|
63
|
+
|
|
64
|
+
for (;;) {
|
|
65
|
+
while (iswspace(lexer->lookahead)) {
|
|
66
|
+
skip(lexer);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (lexer->lookahead == '/') {
|
|
70
|
+
skip(lexer);
|
|
71
|
+
|
|
72
|
+
if (lexer->lookahead == '/') {
|
|
73
|
+
skip(lexer);
|
|
74
|
+
while (lexer->lookahead != 0 && lexer->lookahead != '\n' && lexer->lookahead != 0x2028 &&
|
|
75
|
+
lexer->lookahead != 0x2029) {
|
|
76
|
+
skip(lexer);
|
|
77
|
+
}
|
|
78
|
+
*scanned_comment = true;
|
|
79
|
+
} else if (lexer->lookahead == '*') {
|
|
80
|
+
skip(lexer);
|
|
81
|
+
while (lexer->lookahead != 0) {
|
|
82
|
+
if (lexer->lookahead == '*') {
|
|
83
|
+
skip(lexer);
|
|
84
|
+
if (lexer->lookahead == '/') {
|
|
85
|
+
skip(lexer);
|
|
86
|
+
*scanned_comment = true;
|
|
87
|
+
|
|
88
|
+
if (lexer->lookahead != '/' && !consume) {
|
|
89
|
+
return saw_block_newline ? ACCEPT : NO_NEWLINE;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
} else if (lexer->lookahead == '\n' || lexer->lookahead == 0x2028 || lexer->lookahead == 0x2029) {
|
|
95
|
+
saw_block_newline = true;
|
|
96
|
+
skip(lexer);
|
|
97
|
+
} else {
|
|
98
|
+
skip(lexer);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
return REJECT;
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
return ACCEPT;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static bool scan_automatic_semicolon(TSLexer *lexer, bool comment_condition, bool *scanned_comment) {
|
|
111
|
+
lexer->result_symbol = AUTOMATIC_SEMICOLON;
|
|
112
|
+
lexer->mark_end(lexer);
|
|
113
|
+
|
|
114
|
+
for (;;) {
|
|
115
|
+
if (lexer->lookahead == 0) {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (lexer->lookahead == '/') {
|
|
120
|
+
WhitespaceResult result = scan_whitespace_and_comments(lexer, scanned_comment, false);
|
|
121
|
+
if (result == REJECT) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (result == ACCEPT && comment_condition && lexer->lookahead != ',' && lexer->lookahead != '=') {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (lexer->lookahead == '}') {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (lexer->is_at_included_range_start(lexer)) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (lexer->lookahead == '\n' || lexer->lookahead == 0x2028 || lexer->lookahead == 0x2029) {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (!iswspace(lexer->lookahead)) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
skip(lexer);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
skip(lexer);
|
|
150
|
+
|
|
151
|
+
if (scan_whitespace_and_comments(lexer, scanned_comment, true) == REJECT) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
switch (lexer->lookahead) {
|
|
156
|
+
case '`':
|
|
157
|
+
case ',':
|
|
158
|
+
case ':':
|
|
159
|
+
case ';':
|
|
160
|
+
case '*':
|
|
161
|
+
case '%':
|
|
162
|
+
case '>':
|
|
163
|
+
case '<':
|
|
164
|
+
case '=':
|
|
165
|
+
case '[':
|
|
166
|
+
case '(':
|
|
167
|
+
case '?':
|
|
168
|
+
case '^':
|
|
169
|
+
case '|':
|
|
170
|
+
case '&':
|
|
171
|
+
case '/':
|
|
172
|
+
return false;
|
|
173
|
+
|
|
174
|
+
// Insert a semicolon before decimals literals but not otherwise.
|
|
175
|
+
case '.':
|
|
176
|
+
skip(lexer);
|
|
177
|
+
return iswdigit(lexer->lookahead);
|
|
178
|
+
|
|
179
|
+
// Insert a semicolon before `--` and `++`, but not before binary `+` or `-`.
|
|
180
|
+
case '+':
|
|
181
|
+
skip(lexer);
|
|
182
|
+
return lexer->lookahead == '+';
|
|
183
|
+
case '-':
|
|
184
|
+
skip(lexer);
|
|
185
|
+
return lexer->lookahead == '-';
|
|
186
|
+
|
|
187
|
+
// Don't insert a semicolon before `!=`, but do insert one before a unary `!`.
|
|
188
|
+
case '!':
|
|
189
|
+
skip(lexer);
|
|
190
|
+
return lexer->lookahead != '=';
|
|
191
|
+
|
|
192
|
+
// Don't insert a semicolon before `in` or `instanceof`, but do insert one
|
|
193
|
+
// before an identifier.
|
|
194
|
+
case 'i':
|
|
195
|
+
skip(lexer);
|
|
196
|
+
|
|
197
|
+
if (lexer->lookahead != 'n') {
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
skip(lexer);
|
|
201
|
+
|
|
202
|
+
if (!iswalpha(lexer->lookahead)) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
for (unsigned i = 0; i < 8; i++) {
|
|
207
|
+
if (lexer->lookahead != "stanceof"[i]) {
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
skip(lexer);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (!iswalpha(lexer->lookahead)) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
|
|
218
|
+
default:
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
static bool scan_ternary_qmark(TSLexer *lexer) {
|
|
226
|
+
for (;;) {
|
|
227
|
+
if (!iswspace(lexer->lookahead)) {
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
skip(lexer);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (lexer->lookahead == '?') {
|
|
234
|
+
advance(lexer);
|
|
235
|
+
|
|
236
|
+
if (lexer->lookahead == '?') {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
lexer->mark_end(lexer);
|
|
241
|
+
lexer->result_symbol = TERNARY_QMARK;
|
|
242
|
+
|
|
243
|
+
if (lexer->lookahead == '.') {
|
|
244
|
+
advance(lexer);
|
|
245
|
+
if (iswdigit(lexer->lookahead)) {
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
static bool scan_html_comment(TSLexer *lexer) {
|
|
256
|
+
while (iswspace(lexer->lookahead) || lexer->lookahead == 0x2028 || lexer->lookahead == 0x2029) {
|
|
257
|
+
skip(lexer);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const char *comment_start = "<!--";
|
|
261
|
+
const char *comment_end = "-->";
|
|
262
|
+
|
|
263
|
+
if (lexer->lookahead == '<') {
|
|
264
|
+
for (unsigned i = 0; i < 4; i++) {
|
|
265
|
+
if (lexer->lookahead != comment_start[i]) {
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
advance(lexer);
|
|
269
|
+
}
|
|
270
|
+
} else if (lexer->lookahead == '-') {
|
|
271
|
+
for (unsigned i = 0; i < 3; i++) {
|
|
272
|
+
if (lexer->lookahead != comment_end[i]) {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
advance(lexer);
|
|
276
|
+
}
|
|
277
|
+
} else {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
while (lexer->lookahead != 0 && lexer->lookahead != '\n' && lexer->lookahead != 0x2028 &&
|
|
282
|
+
lexer->lookahead != 0x2029) {
|
|
283
|
+
advance(lexer);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
lexer->result_symbol = HTML_COMMENT;
|
|
287
|
+
lexer->mark_end(lexer);
|
|
288
|
+
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
static bool scan_jsx_text(TSLexer *lexer) {
|
|
293
|
+
// saw_text will be true if we see any non-whitespace content, or any whitespace content that is not a newline and
|
|
294
|
+
// does not immediately follow a newline.
|
|
295
|
+
bool saw_text = false;
|
|
296
|
+
// at_newline will be true if we are currently at a newline, or if we are at whitespace that is not a newline but
|
|
297
|
+
// immediately follows a newline.
|
|
298
|
+
bool at_newline = false;
|
|
299
|
+
|
|
300
|
+
while (lexer->lookahead != 0 && lexer->lookahead != '<' && lexer->lookahead != '>' && lexer->lookahead != '{' &&
|
|
301
|
+
lexer->lookahead != '}' && lexer->lookahead != '&') {
|
|
302
|
+
bool is_wspace = iswspace(lexer->lookahead);
|
|
303
|
+
if (lexer->lookahead == '\n') {
|
|
304
|
+
at_newline = true;
|
|
305
|
+
} else {
|
|
306
|
+
// If at_newline is already true, and we see some whitespace, then it must stay true.
|
|
307
|
+
// Otherwise, it should be false.
|
|
308
|
+
//
|
|
309
|
+
// See the table below to determine the logic for computing `saw_text`.
|
|
310
|
+
//
|
|
311
|
+
// |------------------------------------|
|
|
312
|
+
// | at_newline | is_wspace | saw_text |
|
|
313
|
+
// |------------|-----------|-----------|
|
|
314
|
+
// | false (0) | false (0) | true (1) |
|
|
315
|
+
// | false (0) | true (1) | true (1) |
|
|
316
|
+
// | true (1) | false (0) | true (1) |
|
|
317
|
+
// | true (1) | true (1) | false (0) |
|
|
318
|
+
// |------------------------------------|
|
|
319
|
+
|
|
320
|
+
at_newline &= is_wspace;
|
|
321
|
+
if (!at_newline) {
|
|
322
|
+
saw_text = true;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
advance(lexer);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
lexer->result_symbol = JSX_TEXT;
|
|
330
|
+
return saw_text;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
bool tree_sitter_javascript_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
|
|
334
|
+
if (valid_symbols[TEMPLATE_CHARS]) {
|
|
335
|
+
if (valid_symbols[AUTOMATIC_SEMICOLON]) {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
return scan_template_chars(lexer);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (valid_symbols[JSX_TEXT] && scan_jsx_text(lexer)) {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (valid_symbols[AUTOMATIC_SEMICOLON]) {
|
|
346
|
+
bool scanned_comment = false;
|
|
347
|
+
bool ret = scan_automatic_semicolon(lexer, !valid_symbols[LOGICAL_OR], &scanned_comment);
|
|
348
|
+
if (!ret && !scanned_comment && valid_symbols[TERNARY_QMARK] && lexer->lookahead == '?') {
|
|
349
|
+
return scan_ternary_qmark(lexer);
|
|
350
|
+
}
|
|
351
|
+
return ret;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (valid_symbols[TERNARY_QMARK]) {
|
|
355
|
+
return scan_ternary_qmark(lexer);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (valid_symbols[HTML_COMMENT] && !valid_symbols[LOGICAL_OR] && !valid_symbols[ESCAPE_SEQUENCE] &&
|
|
359
|
+
!valid_symbols[REGEX_PATTERN]) {
|
|
360
|
+
return scan_html_comment(lexer);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#ifndef TREE_SITTER_ALLOC_H_
|
|
2
|
+
#define TREE_SITTER_ALLOC_H_
|
|
3
|
+
|
|
4
|
+
#ifdef __cplusplus
|
|
5
|
+
extern "C" {
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
#include <stdbool.h>
|
|
9
|
+
#include <stdio.h>
|
|
10
|
+
#include <stdlib.h>
|
|
11
|
+
|
|
12
|
+
// Allow clients to override allocation functions
|
|
13
|
+
#ifdef TREE_SITTER_REUSE_ALLOCATOR
|
|
14
|
+
|
|
15
|
+
extern void *(*ts_current_malloc)(size_t size);
|
|
16
|
+
extern void *(*ts_current_calloc)(size_t count, size_t size);
|
|
17
|
+
extern void *(*ts_current_realloc)(void *ptr, size_t size);
|
|
18
|
+
extern void (*ts_current_free)(void *ptr);
|
|
19
|
+
|
|
20
|
+
#ifndef ts_malloc
|
|
21
|
+
#define ts_malloc ts_current_malloc
|
|
22
|
+
#endif
|
|
23
|
+
#ifndef ts_calloc
|
|
24
|
+
#define ts_calloc ts_current_calloc
|
|
25
|
+
#endif
|
|
26
|
+
#ifndef ts_realloc
|
|
27
|
+
#define ts_realloc ts_current_realloc
|
|
28
|
+
#endif
|
|
29
|
+
#ifndef ts_free
|
|
30
|
+
#define ts_free ts_current_free
|
|
31
|
+
#endif
|
|
32
|
+
|
|
33
|
+
#else
|
|
34
|
+
|
|
35
|
+
#ifndef ts_malloc
|
|
36
|
+
#define ts_malloc malloc
|
|
37
|
+
#endif
|
|
38
|
+
#ifndef ts_calloc
|
|
39
|
+
#define ts_calloc calloc
|
|
40
|
+
#endif
|
|
41
|
+
#ifndef ts_realloc
|
|
42
|
+
#define ts_realloc realloc
|
|
43
|
+
#endif
|
|
44
|
+
#ifndef ts_free
|
|
45
|
+
#define ts_free free
|
|
46
|
+
#endif
|
|
47
|
+
|
|
48
|
+
#endif
|
|
49
|
+
|
|
50
|
+
#ifdef __cplusplus
|
|
51
|
+
}
|
|
52
|
+
#endif
|
|
53
|
+
|
|
54
|
+
#endif // TREE_SITTER_ALLOC_H_
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
#ifndef TREE_SITTER_ARRAY_H_
|
|
2
|
+
#define TREE_SITTER_ARRAY_H_
|
|
3
|
+
|
|
4
|
+
#ifdef __cplusplus
|
|
5
|
+
extern "C" {
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
#include "./alloc.h"
|
|
9
|
+
|
|
10
|
+
#include <assert.h>
|
|
11
|
+
#include <stdbool.h>
|
|
12
|
+
#include <stdint.h>
|
|
13
|
+
#include <stdlib.h>
|
|
14
|
+
#include <string.h>
|
|
15
|
+
|
|
16
|
+
#ifdef _MSC_VER
|
|
17
|
+
#pragma warning(disable : 4101)
|
|
18
|
+
#elif defined(__GNUC__) || defined(__clang__)
|
|
19
|
+
#pragma GCC diagnostic push
|
|
20
|
+
#pragma GCC diagnostic ignored "-Wunused-variable"
|
|
21
|
+
#endif
|
|
22
|
+
|
|
23
|
+
#define Array(T) \
|
|
24
|
+
struct { \
|
|
25
|
+
T *contents; \
|
|
26
|
+
uint32_t size; \
|
|
27
|
+
uint32_t capacity; \
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// Initialize an array.
|
|
31
|
+
#define array_init(self) \
|
|
32
|
+
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
|
|
33
|
+
|
|
34
|
+
/// Create an empty array.
|
|
35
|
+
#define array_new() \
|
|
36
|
+
{ NULL, 0, 0 }
|
|
37
|
+
|
|
38
|
+
/// Get a pointer to the element at a given `index` in the array.
|
|
39
|
+
#define array_get(self, _index) \
|
|
40
|
+
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
|
|
41
|
+
|
|
42
|
+
/// Get a pointer to the first element in the array.
|
|
43
|
+
#define array_front(self) array_get(self, 0)
|
|
44
|
+
|
|
45
|
+
/// Get a pointer to the last element in the array.
|
|
46
|
+
#define array_back(self) array_get(self, (self)->size - 1)
|
|
47
|
+
|
|
48
|
+
/// Clear the array, setting its size to zero. Note that this does not free any
|
|
49
|
+
/// memory allocated for the array's contents.
|
|
50
|
+
#define array_clear(self) ((self)->size = 0)
|
|
51
|
+
|
|
52
|
+
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
|
|
53
|
+
/// less than the array's current capacity, this function has no effect.
|
|
54
|
+
#define array_reserve(self, new_capacity) \
|
|
55
|
+
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
|
|
56
|
+
|
|
57
|
+
/// Free any memory allocated for this array. Note that this does not free any
|
|
58
|
+
/// memory allocated for the array's contents.
|
|
59
|
+
#define array_delete(self) _array__delete((Array *)(self))
|
|
60
|
+
|
|
61
|
+
/// Push a new `element` onto the end of the array.
|
|
62
|
+
#define array_push(self, element) \
|
|
63
|
+
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
|
|
64
|
+
(self)->contents[(self)->size++] = (element))
|
|
65
|
+
|
|
66
|
+
/// Increase the array's size by `count` elements.
|
|
67
|
+
/// New elements are zero-initialized.
|
|
68
|
+
#define array_grow_by(self, count) \
|
|
69
|
+
do { \
|
|
70
|
+
if ((count) == 0) break; \
|
|
71
|
+
_array__grow((Array *)(self), count, array_elem_size(self)); \
|
|
72
|
+
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
|
|
73
|
+
(self)->size += (count); \
|
|
74
|
+
} while (0)
|
|
75
|
+
|
|
76
|
+
/// Append all elements from one array to the end of another.
|
|
77
|
+
#define array_push_all(self, other) \
|
|
78
|
+
array_extend((self), (other)->size, (other)->contents)
|
|
79
|
+
|
|
80
|
+
/// Append `count` elements to the end of the array, reading their values from the
|
|
81
|
+
/// `contents` pointer.
|
|
82
|
+
#define array_extend(self, count, contents) \
|
|
83
|
+
_array__splice( \
|
|
84
|
+
(Array *)(self), array_elem_size(self), (self)->size, \
|
|
85
|
+
0, count, contents \
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
/// Remove `old_count` elements from the array starting at the given `index`. At
|
|
89
|
+
/// the same index, insert `new_count` new elements, reading their values from the
|
|
90
|
+
/// `new_contents` pointer.
|
|
91
|
+
#define array_splice(self, _index, old_count, new_count, new_contents) \
|
|
92
|
+
_array__splice( \
|
|
93
|
+
(Array *)(self), array_elem_size(self), _index, \
|
|
94
|
+
old_count, new_count, new_contents \
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
/// Insert one `element` into the array at the given `index`.
|
|
98
|
+
#define array_insert(self, _index, element) \
|
|
99
|
+
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
|
|
100
|
+
|
|
101
|
+
/// Remove one element from the array at the given `index`.
|
|
102
|
+
#define array_erase(self, _index) \
|
|
103
|
+
_array__erase((Array *)(self), array_elem_size(self), _index)
|
|
104
|
+
|
|
105
|
+
/// Pop the last element off the array, returning the element by value.
|
|
106
|
+
#define array_pop(self) ((self)->contents[--(self)->size])
|
|
107
|
+
|
|
108
|
+
/// Assign the contents of one array to another, reallocating if necessary.
|
|
109
|
+
#define array_assign(self, other) \
|
|
110
|
+
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
|
|
111
|
+
|
|
112
|
+
/// Swap one array with another
|
|
113
|
+
#define array_swap(self, other) \
|
|
114
|
+
_array__swap((Array *)(self), (Array *)(other))
|
|
115
|
+
|
|
116
|
+
/// Get the size of the array contents
|
|
117
|
+
#define array_elem_size(self) (sizeof *(self)->contents)
|
|
118
|
+
|
|
119
|
+
/// Search a sorted array for a given `needle` value, using the given `compare`
|
|
120
|
+
/// callback to determine the order.
|
|
121
|
+
///
|
|
122
|
+
/// If an existing element is found to be equal to `needle`, then the `index`
|
|
123
|
+
/// out-parameter is set to the existing value's index, and the `exists`
|
|
124
|
+
/// out-parameter is set to true. Otherwise, `index` is set to an index where
|
|
125
|
+
/// `needle` should be inserted in order to preserve the sorting, and `exists`
|
|
126
|
+
/// is set to false.
|
|
127
|
+
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
|
|
128
|
+
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
|
|
129
|
+
|
|
130
|
+
/// Search a sorted array for a given `needle` value, using integer comparisons
|
|
131
|
+
/// of a given struct field (specified with a leading dot) to determine the order.
|
|
132
|
+
///
|
|
133
|
+
/// See also `array_search_sorted_with`.
|
|
134
|
+
#define array_search_sorted_by(self, field, needle, _index, _exists) \
|
|
135
|
+
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
|
|
136
|
+
|
|
137
|
+
/// Insert a given `value` into a sorted array, using the given `compare`
|
|
138
|
+
/// callback to determine the order.
|
|
139
|
+
#define array_insert_sorted_with(self, compare, value) \
|
|
140
|
+
do { \
|
|
141
|
+
unsigned _index, _exists; \
|
|
142
|
+
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
|
|
143
|
+
if (!_exists) array_insert(self, _index, value); \
|
|
144
|
+
} while (0)
|
|
145
|
+
|
|
146
|
+
/// Insert a given `value` into a sorted array, using integer comparisons of
|
|
147
|
+
/// a given struct field (specified with a leading dot) to determine the order.
|
|
148
|
+
///
|
|
149
|
+
/// See also `array_search_sorted_by`.
|
|
150
|
+
#define array_insert_sorted_by(self, field, value) \
|
|
151
|
+
do { \
|
|
152
|
+
unsigned _index, _exists; \
|
|
153
|
+
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
|
|
154
|
+
if (!_exists) array_insert(self, _index, value); \
|
|
155
|
+
} while (0)
|
|
156
|
+
|
|
157
|
+
// Private
|
|
158
|
+
|
|
159
|
+
typedef Array(void) Array;
|
|
160
|
+
|
|
161
|
+
/// This is not what you're looking for, see `array_delete`.
|
|
162
|
+
static inline void _array__delete(Array *self) {
|
|
163
|
+
if (self->contents) {
|
|
164
|
+
ts_free(self->contents);
|
|
165
|
+
self->contents = NULL;
|
|
166
|
+
self->size = 0;
|
|
167
|
+
self->capacity = 0;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/// This is not what you're looking for, see `array_erase`.
|
|
172
|
+
static inline void _array__erase(Array *self, size_t element_size,
|
|
173
|
+
uint32_t index) {
|
|
174
|
+
assert(index < self->size);
|
|
175
|
+
char *contents = (char *)self->contents;
|
|
176
|
+
memmove(contents + index * element_size, contents + (index + 1) * element_size,
|
|
177
|
+
(self->size - index - 1) * element_size);
|
|
178
|
+
self->size--;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/// This is not what you're looking for, see `array_reserve`.
|
|
182
|
+
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
|
|
183
|
+
if (new_capacity > self->capacity) {
|
|
184
|
+
if (self->contents) {
|
|
185
|
+
self->contents = ts_realloc(self->contents, new_capacity * element_size);
|
|
186
|
+
} else {
|
|
187
|
+
self->contents = ts_malloc(new_capacity * element_size);
|
|
188
|
+
}
|
|
189
|
+
self->capacity = new_capacity;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/// This is not what you're looking for, see `array_assign`.
|
|
194
|
+
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
|
|
195
|
+
_array__reserve(self, element_size, other->size);
|
|
196
|
+
self->size = other->size;
|
|
197
|
+
memcpy(self->contents, other->contents, self->size * element_size);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/// This is not what you're looking for, see `array_swap`.
|
|
201
|
+
static inline void _array__swap(Array *self, Array *other) {
|
|
202
|
+
Array swap = *other;
|
|
203
|
+
*other = *self;
|
|
204
|
+
*self = swap;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
|
|
208
|
+
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
|
|
209
|
+
uint32_t new_size = self->size + count;
|
|
210
|
+
if (new_size > self->capacity) {
|
|
211
|
+
uint32_t new_capacity = self->capacity * 2;
|
|
212
|
+
if (new_capacity < 8) new_capacity = 8;
|
|
213
|
+
if (new_capacity < new_size) new_capacity = new_size;
|
|
214
|
+
_array__reserve(self, element_size, new_capacity);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/// This is not what you're looking for, see `array_splice`.
|
|
219
|
+
static inline void _array__splice(Array *self, size_t element_size,
|
|
220
|
+
uint32_t index, uint32_t old_count,
|
|
221
|
+
uint32_t new_count, const void *elements) {
|
|
222
|
+
uint32_t new_size = self->size + new_count - old_count;
|
|
223
|
+
uint32_t old_end = index + old_count;
|
|
224
|
+
uint32_t new_end = index + new_count;
|
|
225
|
+
assert(old_end <= self->size);
|
|
226
|
+
|
|
227
|
+
_array__reserve(self, element_size, new_size);
|
|
228
|
+
|
|
229
|
+
char *contents = (char *)self->contents;
|
|
230
|
+
if (self->size > old_end) {
|
|
231
|
+
memmove(
|
|
232
|
+
contents + new_end * element_size,
|
|
233
|
+
contents + old_end * element_size,
|
|
234
|
+
(self->size - old_end) * element_size
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
if (new_count > 0) {
|
|
238
|
+
if (elements) {
|
|
239
|
+
memcpy(
|
|
240
|
+
(contents + index * element_size),
|
|
241
|
+
elements,
|
|
242
|
+
new_count * element_size
|
|
243
|
+
);
|
|
244
|
+
} else {
|
|
245
|
+
memset(
|
|
246
|
+
(contents + index * element_size),
|
|
247
|
+
0,
|
|
248
|
+
new_count * element_size
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
self->size += new_count - old_count;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
|
|
256
|
+
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
|
|
257
|
+
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
|
|
258
|
+
do { \
|
|
259
|
+
*(_index) = start; \
|
|
260
|
+
*(_exists) = false; \
|
|
261
|
+
uint32_t size = (self)->size - *(_index); \
|
|
262
|
+
if (size == 0) break; \
|
|
263
|
+
int comparison; \
|
|
264
|
+
while (size > 1) { \
|
|
265
|
+
uint32_t half_size = size / 2; \
|
|
266
|
+
uint32_t mid_index = *(_index) + half_size; \
|
|
267
|
+
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
|
|
268
|
+
if (comparison <= 0) *(_index) = mid_index; \
|
|
269
|
+
size -= half_size; \
|
|
270
|
+
} \
|
|
271
|
+
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
|
|
272
|
+
if (comparison == 0) *(_exists) = true; \
|
|
273
|
+
else if (comparison < 0) *(_index) += 1; \
|
|
274
|
+
} while (0)
|
|
275
|
+
|
|
276
|
+
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
|
|
277
|
+
/// parameter by reference in order to work with the generic sorting function above.
|
|
278
|
+
#define _compare_int(a, b) ((int)*(a) - (int)(b))
|
|
279
|
+
|
|
280
|
+
#ifdef _MSC_VER
|
|
281
|
+
#pragma warning(default : 4101)
|
|
282
|
+
#elif defined(__GNUC__) || defined(__clang__)
|
|
283
|
+
#pragma GCC diagnostic pop
|
|
284
|
+
#endif
|
|
285
|
+
|
|
286
|
+
#ifdef __cplusplus
|
|
287
|
+
}
|
|
288
|
+
#endif
|
|
289
|
+
|
|
290
|
+
#endif // TREE_SITTER_ARRAY_H_
|