rapydscript-ng 0.7.24 → 0.8.1
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/CHANGELOG.md +28 -0
- package/README.md +80 -26
- package/bin/build.ts +117 -0
- package/bin/package.json +1 -0
- package/bin/rapydscript +65 -62
- package/build_wheels.py +133 -0
- package/editor-plugins/README.md +80 -0
- package/editor-plugins/nvim.lua +321 -0
- package/local-agent.md +28 -0
- package/package.json +4 -5
- package/publish.py +27 -17
- package/release/baselib-plain-pretty.js +448 -326
- package/release/baselib-plain-ugly.js +3 -3
- package/release/compiler.js +9027 -8139
- package/release/signatures.json +27 -27
- package/release/stdlib_modules.json +1 -0
- package/src/ast.pyj +428 -280
- package/src/baselib-builtins.pyj +49 -28
- package/src/baselib-containers.pyj +241 -226
- package/src/baselib-errors.pyj +8 -1
- package/src/baselib-internal.pyj +38 -23
- package/src/baselib-itertools.pyj +13 -7
- package/src/baselib-str.pyj +59 -84
- package/src/compiler.pyj +4 -4
- package/src/errors.pyj +3 -4
- package/src/lib/aes.pyj +46 -32
- package/src/lib/elementmaker.pyj +11 -9
- package/src/lib/encodings.pyj +15 -5
- package/src/lib/gettext.pyj +9 -4
- package/src/lib/math.pyj +106 -45
- package/src/lib/operator.pyj +10 -10
- package/src/lib/pythonize.pyj +2 -1
- package/src/lib/random.pyj +28 -21
- package/src/lib/re.pyj +492 -19
- package/src/lib/traceback.pyj +171 -29
- package/src/lib/uuid.pyj +2 -2
- package/src/output/classes.pyj +28 -27
- package/src/output/codegen.pyj +87 -83
- package/src/output/comments.pyj +8 -8
- package/src/output/exceptions.pyj +20 -21
- package/src/output/functions.pyj +66 -64
- package/src/output/literals.pyj +24 -23
- package/src/output/loops.pyj +84 -142
- package/src/output/modules.pyj +55 -68
- package/src/output/operators.pyj +40 -29
- package/src/output/statements.pyj +21 -15
- package/src/output/stream.pyj +82 -56
- package/src/output/utils.pyj +12 -8
- package/src/parse.pyj +971 -620
- package/src/string_interpolation.pyj +5 -3
- package/src/tokenizer.pyj +188 -148
- package/src/utils.pyj +32 -15
- package/test/_treeshake_mod.pyj +30 -0
- package/test/_treeshake_side_effect.pyj +9 -0
- package/test/ast_serialization.pyj +392 -0
- package/test/async.pyj +95 -0
- package/test/baselib.pyj +1 -2
- package/test/embedded_compiler.pyj +57 -0
- package/test/fmt.pyj +291 -0
- package/test/generic.pyj +16 -3
- package/test/imports.pyj +8 -6
- package/test/internationalization.pyj +38 -37
- package/test/lint.pyj +120 -117
- package/test/lsp.pyj +257 -0
- package/test/repl.pyj +70 -65
- package/test/sourcemaps.pyj +315 -0
- package/test/starargs.pyj +46 -39
- package/test/str.pyj +8 -0
- package/test/traceback.pyj +263 -0
- package/test/treeshaking.pyj +181 -0
- package/test/web_repl_export.pyj +88 -0
- package/tools/ast_serialize.mjs +557 -0
- package/tools/cli.mjs +531 -0
- package/tools/compile.mjs +205 -0
- package/tools/compiler.mjs +159 -0
- package/tools/{completer.js → completer.mjs} +6 -6
- package/tools/{embedded_compiler.js → embedded_compiler.mjs} +17 -13
- package/tools/export.js +109 -56
- package/tools/fmt.mjs +975 -0
- package/tools/{gettext.js → gettext.mjs} +46 -53
- package/tools/ini.mjs +175 -0
- package/tools/{lint.js → lint.mjs} +78 -55
- package/tools/lsp.mjs +964 -0
- package/tools/lsp_protocol.mjs +259 -0
- package/tools/lsp_symbols.mjs +418 -0
- package/tools/{msgfmt.js → msgfmt.mjs} +18 -18
- package/tools/{repl.js → repl.mjs} +56 -42
- package/tools/{self.js → self.mjs} +71 -46
- package/tools/sourcemap.mjs +123 -0
- package/tools/test.mjs +252 -0
- package/tools/treeshake.mjs +400 -0
- package/tools/{utils.js → utils.mjs} +26 -23
- package/tools/{web_repl.js → web_repl.mjs} +15 -13
- package/tools/web_repl_export.mjs +97 -0
- package/tree-sitter/README.md +101 -0
- package/tree-sitter/grammar.js +992 -0
- package/tree-sitter/package.json +43 -0
- package/tree-sitter/queries/highlights.scm +232 -0
- package/tree-sitter/queries/indents.scm +64 -0
- package/tree-sitter/queries/injections.scm +14 -0
- package/tree-sitter/queries/locals.scm +108 -0
- package/tree-sitter/src/grammar.json +4976 -0
- package/tree-sitter/src/node-types.json +2901 -0
- package/tree-sitter/src/parser.c +196465 -0
- package/tree-sitter/src/scanner.c +294 -0
- package/tree-sitter/src/tree_sitter/alloc.h +54 -0
- package/tree-sitter/src/tree_sitter/array.h +330 -0
- package/tree-sitter/src/tree_sitter/parser.h +286 -0
- package/tree-sitter/test/corpus/chaining.txt +99 -0
- package/tree-sitter/test/corpus/classes.txt +147 -0
- package/tree-sitter/test/corpus/comprehensions.txt +155 -0
- package/tree-sitter/test/corpus/containers.txt +242 -0
- package/tree-sitter/test/corpus/control_flow.txt +361 -0
- package/tree-sitter/test/corpus/decorators.txt +64 -0
- package/tree-sitter/test/corpus/existential.txt +102 -0
- package/tree-sitter/test/corpus/functions.txt +293 -0
- package/tree-sitter/test/corpus/imports.txt +117 -0
- package/tree-sitter/test/corpus/literals.txt +135 -0
- package/tree-sitter/test/corpus/operators.txt +296 -0
- package/tree-sitter/test/corpus/statements.txt +189 -0
- package/tree-sitter/test/corpus/strings.txt +90 -0
- package/tree-sitter/test/corpus/subscripts.txt +227 -0
- package/tree-sitter/tree-sitter.json +36 -0
- package/web-repl/env.js +35 -23
- package/web-repl/main.js +8 -8
- package/bin/export +0 -75
- package/bin/web-repl-export +0 -102
- package/tools/cli.js +0 -523
- package/tools/compile.js +0 -184
- package/tools/compiler.js +0 -84
- package/tools/ini.js +0 -65
- package/tools/test.js +0 -110
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
#include "tree_sitter/parser.h"
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <string.h>
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
|
|
6
|
+
// External tokens produced by this scanner.
|
|
7
|
+
enum TokenType {
|
|
8
|
+
NEWLINE,
|
|
9
|
+
INDENT,
|
|
10
|
+
DEDENT,
|
|
11
|
+
REGEX,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// A simple growable stack of indentation widths. The bottom of the stack is
|
|
15
|
+
// always 0 (the module level indentation).
|
|
16
|
+
typedef struct {
|
|
17
|
+
uint32_t size;
|
|
18
|
+
uint32_t capacity;
|
|
19
|
+
uint16_t *data;
|
|
20
|
+
// When `has_pending` is set, `pending_indent` records the indentation of a
|
|
21
|
+
// line whose NEWLINE has already been emitted but whose INDENT/DEDENT
|
|
22
|
+
// tokens still need to be reconciled against the stack. This lets us emit
|
|
23
|
+
// several DEDENT tokens for a single line without needing to re-read the
|
|
24
|
+
// (already consumed) newline.
|
|
25
|
+
uint16_t pending_indent;
|
|
26
|
+
uint8_t has_pending;
|
|
27
|
+
} Scanner;
|
|
28
|
+
|
|
29
|
+
static void stack_push(Scanner *s, uint16_t value) {
|
|
30
|
+
if (s->size >= s->capacity) {
|
|
31
|
+
uint32_t new_cap = s->capacity ? s->capacity * 2 : 8;
|
|
32
|
+
s->data = realloc(s->data, new_cap * sizeof(uint16_t));
|
|
33
|
+
s->capacity = new_cap;
|
|
34
|
+
}
|
|
35
|
+
s->data[s->size++] = value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static uint16_t stack_top(Scanner *s) {
|
|
39
|
+
return s->size ? s->data[s->size - 1] : 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static void stack_pop(Scanner *s) {
|
|
43
|
+
// Never pop the base (module) level.
|
|
44
|
+
if (s->size > 1) {
|
|
45
|
+
s->size--;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
void *tree_sitter_rapydscript_external_scanner_create(void) {
|
|
50
|
+
Scanner *s = calloc(1, sizeof(Scanner));
|
|
51
|
+
stack_push(s, 0);
|
|
52
|
+
return s;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void tree_sitter_rapydscript_external_scanner_destroy(void *payload) {
|
|
56
|
+
Scanner *s = (Scanner *)payload;
|
|
57
|
+
if (s->data) {
|
|
58
|
+
free(s->data);
|
|
59
|
+
}
|
|
60
|
+
free(s);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
unsigned tree_sitter_rapydscript_external_scanner_serialize(void *payload, char *buffer) {
|
|
64
|
+
Scanner *s = (Scanner *)payload;
|
|
65
|
+
unsigned i = 0;
|
|
66
|
+
buffer[i++] = (char)(s->has_pending ? 1 : 0);
|
|
67
|
+
buffer[i++] = (char)(s->pending_indent & 0xFF);
|
|
68
|
+
buffer[i++] = (char)((s->pending_indent >> 8) & 0xFF);
|
|
69
|
+
for (uint32_t k = 0; k < s->size; k++) {
|
|
70
|
+
if (i + 2 > TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
uint16_t v = s->data[k];
|
|
74
|
+
buffer[i++] = (char)(v & 0xFF);
|
|
75
|
+
buffer[i++] = (char)((v >> 8) & 0xFF);
|
|
76
|
+
}
|
|
77
|
+
return i;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
void tree_sitter_rapydscript_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
|
|
81
|
+
Scanner *s = (Scanner *)payload;
|
|
82
|
+
s->size = 0;
|
|
83
|
+
s->has_pending = 0;
|
|
84
|
+
s->pending_indent = 0;
|
|
85
|
+
unsigned i = 0;
|
|
86
|
+
if (length >= 3) {
|
|
87
|
+
s->has_pending = (uint8_t)buffer[i++];
|
|
88
|
+
s->pending_indent = (uint8_t)buffer[i] | ((uint16_t)(uint8_t)buffer[i + 1] << 8);
|
|
89
|
+
i += 2;
|
|
90
|
+
}
|
|
91
|
+
while (i + 2 <= length) {
|
|
92
|
+
uint16_t v = (uint8_t)buffer[i] | ((uint16_t)(uint8_t)buffer[i + 1] << 8);
|
|
93
|
+
i += 2;
|
|
94
|
+
stack_push(s, v);
|
|
95
|
+
}
|
|
96
|
+
if (s->size == 0) {
|
|
97
|
+
stack_push(s, 0);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
|
|
102
|
+
static void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
|
|
103
|
+
|
|
104
|
+
static bool is_letter(int32_t c) {
|
|
105
|
+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Reads a JavaScript style regular expression literal. Assumes the current
|
|
109
|
+
// lookahead is the opening '/'. Handles both plain literals (/ab/gi) and the
|
|
110
|
+
// RapydScript "verbose" form (/// ab ///).
|
|
111
|
+
static bool scan_regex(TSLexer *lexer) {
|
|
112
|
+
advance(lexer); // consume opening '/'
|
|
113
|
+
|
|
114
|
+
if (lexer->lookahead == '/') {
|
|
115
|
+
advance(lexer);
|
|
116
|
+
if (lexer->lookahead == '/') {
|
|
117
|
+
// verbose regex: read until the closing '///'
|
|
118
|
+
advance(lexer);
|
|
119
|
+
int slashes = 0;
|
|
120
|
+
while (lexer->lookahead != 0) {
|
|
121
|
+
if (lexer->lookahead == '\\') {
|
|
122
|
+
advance(lexer);
|
|
123
|
+
if (lexer->lookahead != 0) {
|
|
124
|
+
advance(lexer);
|
|
125
|
+
}
|
|
126
|
+
slashes = 0;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (lexer->lookahead == '/') {
|
|
130
|
+
slashes++;
|
|
131
|
+
advance(lexer);
|
|
132
|
+
if (slashes == 3) {
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
slashes = 0;
|
|
137
|
+
advance(lexer);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// else: empty regex '//' -> nothing more to read in the body
|
|
142
|
+
} else {
|
|
143
|
+
// plain regex literal: read until an unescaped '/', respecting classes
|
|
144
|
+
bool in_class = false;
|
|
145
|
+
while (lexer->lookahead != 0 && lexer->lookahead != '\n') {
|
|
146
|
+
if (lexer->lookahead == '\\') {
|
|
147
|
+
advance(lexer);
|
|
148
|
+
if (lexer->lookahead != 0) {
|
|
149
|
+
advance(lexer);
|
|
150
|
+
}
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (lexer->lookahead == '[') {
|
|
154
|
+
in_class = true;
|
|
155
|
+
advance(lexer);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (lexer->lookahead == ']') {
|
|
159
|
+
in_class = false;
|
|
160
|
+
advance(lexer);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (lexer->lookahead == '/' && !in_class) {
|
|
164
|
+
advance(lexer);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
advance(lexer);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// trailing flags (g, i, m, s, u, y, ...)
|
|
172
|
+
while (is_letter(lexer->lookahead)) {
|
|
173
|
+
advance(lexer);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
lexer->mark_end(lexer);
|
|
177
|
+
lexer->result_symbol = REGEX;
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
bool tree_sitter_rapydscript_external_scanner_scan(void *payload, TSLexer *lexer,
|
|
182
|
+
const bool *valid_symbols) {
|
|
183
|
+
Scanner *scanner = (Scanner *)payload;
|
|
184
|
+
|
|
185
|
+
// 1. Reconcile any pending indentation left over from a previously emitted
|
|
186
|
+
// NEWLINE/DEDENT. This is how we emit several DEDENT tokens for a single
|
|
187
|
+
// line without re-reading the (already consumed) newline. Only DEDENT is
|
|
188
|
+
// reconciled here: a pending INDENT is never correct (a new block always
|
|
189
|
+
// begins with a freshly scanned indent), and acting on a stale pending
|
|
190
|
+
// value would corrupt the stack when the scanner is next consulted for
|
|
191
|
+
// an unrelated block.
|
|
192
|
+
if (scanner->has_pending) {
|
|
193
|
+
uint16_t current = stack_top(scanner);
|
|
194
|
+
if (valid_symbols[DEDENT] && scanner->pending_indent < current) {
|
|
195
|
+
stack_pop(scanner);
|
|
196
|
+
lexer->result_symbol = DEDENT;
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
// Nothing left to reconcile (or the grammar does not want it here).
|
|
200
|
+
scanner->has_pending = 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 2. Regular expression literals. Only valid where a primary expression may
|
|
204
|
+
// begin, so '/' cannot be a division operator in that position.
|
|
205
|
+
if (valid_symbols[REGEX] && !valid_symbols[NEWLINE] && !valid_symbols[INDENT] &&
|
|
206
|
+
!valid_symbols[DEDENT]) {
|
|
207
|
+
// Skip surrounding whitespace, including newlines: a regex literal may
|
|
208
|
+
// appear on its own line, e.g. as an argument following a preceding
|
|
209
|
+
// multi-line one (`foo(\n def(): ...\n ,\n /re/\n)`).
|
|
210
|
+
while (lexer->lookahead == ' ' || lexer->lookahead == '\t' ||
|
|
211
|
+
lexer->lookahead == '\n' || lexer->lookahead == '\r' ||
|
|
212
|
+
lexer->lookahead == '\f') {
|
|
213
|
+
skip(lexer);
|
|
214
|
+
}
|
|
215
|
+
if (lexer->lookahead == '/') {
|
|
216
|
+
return scan_regex(lexer);
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// 3. Indentation / newline handling.
|
|
222
|
+
bool found_end_of_line = false;
|
|
223
|
+
uint32_t indent_length = 0;
|
|
224
|
+
|
|
225
|
+
for (;;) {
|
|
226
|
+
if (lexer->lookahead == '\n') {
|
|
227
|
+
found_end_of_line = true;
|
|
228
|
+
indent_length = 0;
|
|
229
|
+
skip(lexer);
|
|
230
|
+
} else if (lexer->lookahead == '\r' || lexer->lookahead == '\f' ||
|
|
231
|
+
lexer->lookahead == 0x0b) {
|
|
232
|
+
indent_length = 0;
|
|
233
|
+
skip(lexer);
|
|
234
|
+
} else if (lexer->lookahead == ' ') {
|
|
235
|
+
indent_length++;
|
|
236
|
+
skip(lexer);
|
|
237
|
+
} else if (lexer->lookahead == '\t') {
|
|
238
|
+
indent_length += 8;
|
|
239
|
+
skip(lexer);
|
|
240
|
+
} else if (lexer->eof(lexer)) {
|
|
241
|
+
found_end_of_line = true;
|
|
242
|
+
indent_length = 0;
|
|
243
|
+
break;
|
|
244
|
+
} else {
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (found_end_of_line) {
|
|
250
|
+
uint16_t current = stack_top(scanner);
|
|
251
|
+
|
|
252
|
+
// Leading-dot chaining: a line at the same indentation that begins with
|
|
253
|
+
// '.' binds to the previous logical line (e.g. jQuery-style method
|
|
254
|
+
// chains and `do: ... .while`). Suppress the newline so the '.' simply
|
|
255
|
+
// continues the preceding expression. When the indentation differs a
|
|
256
|
+
// real INDENT/DEDENT is still required (that is how `.while` closes a
|
|
257
|
+
// `do` block), so this only applies at matching indentation.
|
|
258
|
+
if (lexer->lookahead == '.' && indent_length == current &&
|
|
259
|
+
!valid_symbols[INDENT]) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (valid_symbols[INDENT] && indent_length > current) {
|
|
264
|
+
stack_push(scanner, (uint16_t)indent_length);
|
|
265
|
+
lexer->result_symbol = INDENT;
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (valid_symbols[DEDENT] && indent_length < current) {
|
|
270
|
+
stack_pop(scanner);
|
|
271
|
+
// There may be more levels to pop; remember for subsequent calls.
|
|
272
|
+
if (indent_length < stack_top(scanner)) {
|
|
273
|
+
scanner->pending_indent = (uint16_t)indent_length;
|
|
274
|
+
scanner->has_pending = 1;
|
|
275
|
+
}
|
|
276
|
+
lexer->result_symbol = DEDENT;
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (valid_symbols[NEWLINE]) {
|
|
281
|
+
// The newline (and the next line's indentation) has been consumed.
|
|
282
|
+
// Record that indentation so we can emit the DEDENT/INDENT tokens
|
|
283
|
+
// that follow, once the grammar reaches a state that accepts them.
|
|
284
|
+
if (indent_length != current) {
|
|
285
|
+
scanner->pending_indent = (uint16_t)indent_length;
|
|
286
|
+
scanner->has_pending = 1;
|
|
287
|
+
}
|
|
288
|
+
lexer->result_symbol = NEWLINE;
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
@@ -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,330 @@
|
|
|
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(push)
|
|
18
|
+
#pragma warning(disable : 4101)
|
|
19
|
+
#elif defined(__GNUC__) || defined(__clang__)
|
|
20
|
+
#pragma GCC diagnostic push
|
|
21
|
+
#pragma GCC diagnostic ignored "-Wunused-variable"
|
|
22
|
+
#endif
|
|
23
|
+
|
|
24
|
+
#define Array(T) \
|
|
25
|
+
struct { \
|
|
26
|
+
T *contents; \
|
|
27
|
+
uint32_t size; \
|
|
28
|
+
uint32_t capacity; \
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// Initialize an array.
|
|
32
|
+
#define array_init(self) \
|
|
33
|
+
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
|
|
34
|
+
|
|
35
|
+
/// Create an empty array.
|
|
36
|
+
#define array_new() \
|
|
37
|
+
{ NULL, 0, 0 }
|
|
38
|
+
|
|
39
|
+
/// Get a pointer to the element at a given `index` in the array.
|
|
40
|
+
#define array_get(self, _index) \
|
|
41
|
+
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
|
|
42
|
+
|
|
43
|
+
/// Get a pointer to the first element in the array.
|
|
44
|
+
#define array_front(self) array_get(self, 0)
|
|
45
|
+
|
|
46
|
+
/// Get a pointer to the last element in the array.
|
|
47
|
+
#define array_back(self) array_get(self, (self)->size - 1)
|
|
48
|
+
|
|
49
|
+
/// Clear the array, setting its size to zero. Note that this does not free any
|
|
50
|
+
/// memory allocated for the array's contents.
|
|
51
|
+
#define array_clear(self) ((self)->size = 0)
|
|
52
|
+
|
|
53
|
+
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
|
|
54
|
+
/// less than the array's current capacity, this function has no effect.
|
|
55
|
+
#define array_reserve(self, new_capacity) \
|
|
56
|
+
((self)->contents = _array__reserve( \
|
|
57
|
+
(void *)(self)->contents, &(self)->capacity, \
|
|
58
|
+
array_elem_size(self), new_capacity) \
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
/// Free any memory allocated for this array. Note that this does not free any
|
|
62
|
+
/// memory allocated for the array's contents.
|
|
63
|
+
#define array_delete(self) \
|
|
64
|
+
do { \
|
|
65
|
+
if ((self)->contents) ts_free((self)->contents); \
|
|
66
|
+
(self)->contents = NULL; \
|
|
67
|
+
(self)->size = 0; \
|
|
68
|
+
(self)->capacity = 0; \
|
|
69
|
+
} while (0)
|
|
70
|
+
|
|
71
|
+
/// Push a new `element` onto the end of the array.
|
|
72
|
+
#define array_push(self, element) \
|
|
73
|
+
do { \
|
|
74
|
+
(self)->contents = _array__grow( \
|
|
75
|
+
(void *)(self)->contents, (self)->size, &(self)->capacity, \
|
|
76
|
+
1, array_elem_size(self) \
|
|
77
|
+
); \
|
|
78
|
+
(self)->contents[(self)->size++] = (element); \
|
|
79
|
+
} while(0)
|
|
80
|
+
|
|
81
|
+
/// Increase the array's size by `count` elements.
|
|
82
|
+
/// New elements are zero-initialized.
|
|
83
|
+
#define array_grow_by(self, count) \
|
|
84
|
+
do { \
|
|
85
|
+
if ((count) == 0) break; \
|
|
86
|
+
(self)->contents = _array__grow( \
|
|
87
|
+
(self)->contents, (self)->size, &(self)->capacity, \
|
|
88
|
+
count, array_elem_size(self) \
|
|
89
|
+
); \
|
|
90
|
+
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
|
|
91
|
+
(self)->size += (count); \
|
|
92
|
+
} while (0)
|
|
93
|
+
|
|
94
|
+
/// Append all elements from one array to the end of another.
|
|
95
|
+
#define array_push_all(self, other) \
|
|
96
|
+
array_extend((self), (other)->size, (other)->contents)
|
|
97
|
+
|
|
98
|
+
/// Append `count` elements to the end of the array, reading their values from the
|
|
99
|
+
/// `contents` pointer.
|
|
100
|
+
#define array_extend(self, count, other_contents) \
|
|
101
|
+
(self)->contents = _array__splice( \
|
|
102
|
+
(void*)(self)->contents, &(self)->size, &(self)->capacity, \
|
|
103
|
+
array_elem_size(self), (self)->size, 0, count, other_contents \
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
/// Remove `old_count` elements from the array starting at the given `index`. At
|
|
107
|
+
/// the same index, insert `new_count` new elements, reading their values from the
|
|
108
|
+
/// `new_contents` pointer.
|
|
109
|
+
#define array_splice(self, _index, old_count, new_count, new_contents) \
|
|
110
|
+
(self)->contents = _array__splice( \
|
|
111
|
+
(void *)(self)->contents, &(self)->size, &(self)->capacity, \
|
|
112
|
+
array_elem_size(self), _index, old_count, new_count, new_contents \
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
/// Insert one `element` into the array at the given `index`.
|
|
116
|
+
#define array_insert(self, _index, element) \
|
|
117
|
+
(self)->contents = _array__splice( \
|
|
118
|
+
(void *)(self)->contents, &(self)->size, &(self)->capacity, \
|
|
119
|
+
array_elem_size(self), _index, 0, 1, &(element) \
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
/// Remove one element from the array at the given `index`.
|
|
123
|
+
#define array_erase(self, _index) \
|
|
124
|
+
_array__erase((void *)(self)->contents, &(self)->size, array_elem_size(self), _index)
|
|
125
|
+
|
|
126
|
+
/// Pop the last element off the array, returning the element by value.
|
|
127
|
+
#define array_pop(self) ((self)->contents[--(self)->size])
|
|
128
|
+
|
|
129
|
+
/// Assign the contents of one array to another, reallocating if necessary.
|
|
130
|
+
#define array_assign(self, other) \
|
|
131
|
+
(self)->contents = _array__assign( \
|
|
132
|
+
(void *)(self)->contents, &(self)->size, &(self)->capacity, \
|
|
133
|
+
(const void *)(other)->contents, (other)->size, array_elem_size(self) \
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
/// Swap one array with another
|
|
137
|
+
#define array_swap(self, other) \
|
|
138
|
+
do { \
|
|
139
|
+
void *_array_swap_tmp = (void *)(self)->contents; \
|
|
140
|
+
(self)->contents = (other)->contents; \
|
|
141
|
+
(other)->contents = _array_swap_tmp; \
|
|
142
|
+
_array__swap(&(self)->size, &(self)->capacity, \
|
|
143
|
+
&(other)->size, &(other)->capacity); \
|
|
144
|
+
} while (0)
|
|
145
|
+
|
|
146
|
+
/// Get the size of the array contents
|
|
147
|
+
#define array_elem_size(self) (sizeof *(self)->contents)
|
|
148
|
+
|
|
149
|
+
/// Search a sorted array for a given `needle` value, using the given `compare`
|
|
150
|
+
/// callback to determine the order.
|
|
151
|
+
///
|
|
152
|
+
/// If an existing element is found to be equal to `needle`, then the `index`
|
|
153
|
+
/// out-parameter is set to the existing value's index, and the `exists`
|
|
154
|
+
/// out-parameter is set to true. Otherwise, `index` is set to an index where
|
|
155
|
+
/// `needle` should be inserted in order to preserve the sorting, and `exists`
|
|
156
|
+
/// is set to false.
|
|
157
|
+
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
|
|
158
|
+
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
|
|
159
|
+
|
|
160
|
+
/// Search a sorted array for a given `needle` value, using integer comparisons
|
|
161
|
+
/// of a given struct field (specified with a leading dot) to determine the order.
|
|
162
|
+
///
|
|
163
|
+
/// See also `array_search_sorted_with`.
|
|
164
|
+
#define array_search_sorted_by(self, field, needle, _index, _exists) \
|
|
165
|
+
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
|
|
166
|
+
|
|
167
|
+
/// Insert a given `value` into a sorted array, using the given `compare`
|
|
168
|
+
/// callback to determine the order.
|
|
169
|
+
#define array_insert_sorted_with(self, compare, value) \
|
|
170
|
+
do { \
|
|
171
|
+
unsigned _index, _exists; \
|
|
172
|
+
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
|
|
173
|
+
if (!_exists) array_insert(self, _index, value); \
|
|
174
|
+
} while (0)
|
|
175
|
+
|
|
176
|
+
/// Insert a given `value` into a sorted array, using integer comparisons of
|
|
177
|
+
/// a given struct field (specified with a leading dot) to determine the order.
|
|
178
|
+
///
|
|
179
|
+
/// See also `array_search_sorted_by`.
|
|
180
|
+
#define array_insert_sorted_by(self, field, value) \
|
|
181
|
+
do { \
|
|
182
|
+
unsigned _index, _exists; \
|
|
183
|
+
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
|
|
184
|
+
if (!_exists) array_insert(self, _index, value); \
|
|
185
|
+
} while (0)
|
|
186
|
+
|
|
187
|
+
// Private
|
|
188
|
+
|
|
189
|
+
// Pointers to individual `Array` fields (rather than the entire `Array` itself)
|
|
190
|
+
// are passed to the various `_array__*` functions below to address strict aliasing
|
|
191
|
+
// violations that arises when the _entire_ `Array` struct is passed as `Array(void)*`.
|
|
192
|
+
//
|
|
193
|
+
// The `Array` type itself was not altered as a solution in order to avoid breakage
|
|
194
|
+
// with existing consumers (in particular, parsers with external scanners).
|
|
195
|
+
|
|
196
|
+
/// This is not what you're looking for, see `array_erase`.
|
|
197
|
+
static inline void _array__erase(void* self_contents, uint32_t *size,
|
|
198
|
+
size_t element_size, uint32_t index) {
|
|
199
|
+
assert(index < *size);
|
|
200
|
+
char *contents = (char *)self_contents;
|
|
201
|
+
memmove(contents + index * element_size, contents + (index + 1) * element_size,
|
|
202
|
+
(*size - index - 1) * element_size);
|
|
203
|
+
(*size)--;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/// This is not what you're looking for, see `array_reserve`.
|
|
207
|
+
static inline void *_array__reserve(void *contents, uint32_t *capacity,
|
|
208
|
+
size_t element_size, uint32_t new_capacity) {
|
|
209
|
+
void *new_contents = contents;
|
|
210
|
+
if (new_capacity > *capacity) {
|
|
211
|
+
if (contents) {
|
|
212
|
+
new_contents = ts_realloc(contents, new_capacity * element_size);
|
|
213
|
+
} else {
|
|
214
|
+
new_contents = ts_malloc(new_capacity * element_size);
|
|
215
|
+
}
|
|
216
|
+
*capacity = new_capacity;
|
|
217
|
+
}
|
|
218
|
+
return new_contents;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/// This is not what you're looking for, see `array_assign`.
|
|
222
|
+
static inline void *_array__assign(void* self_contents, uint32_t *self_size, uint32_t *self_capacity,
|
|
223
|
+
const void *other_contents, uint32_t other_size, size_t element_size) {
|
|
224
|
+
void *new_contents = _array__reserve(self_contents, self_capacity, element_size, other_size);
|
|
225
|
+
*self_size = other_size;
|
|
226
|
+
memcpy(new_contents, other_contents, *self_size * element_size);
|
|
227
|
+
return new_contents;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/// This is not what you're looking for, see `array_swap`.
|
|
231
|
+
static inline void _array__swap(uint32_t *self_size, uint32_t *self_capacity,
|
|
232
|
+
uint32_t *other_size, uint32_t *other_capacity) {
|
|
233
|
+
uint32_t tmp_size = *self_size;
|
|
234
|
+
uint32_t tmp_capacity = *self_capacity;
|
|
235
|
+
*self_size = *other_size;
|
|
236
|
+
*self_capacity = *other_capacity;
|
|
237
|
+
*other_size = tmp_size;
|
|
238
|
+
*other_capacity = tmp_capacity;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
|
|
242
|
+
static inline void *_array__grow(void *contents, uint32_t size, uint32_t *capacity,
|
|
243
|
+
uint32_t count, size_t element_size) {
|
|
244
|
+
void *new_contents = contents;
|
|
245
|
+
uint32_t new_size = size + count;
|
|
246
|
+
if (new_size > *capacity) {
|
|
247
|
+
uint32_t new_capacity = *capacity * 2;
|
|
248
|
+
if (new_capacity < 8) new_capacity = 8;
|
|
249
|
+
if (new_capacity < new_size) new_capacity = new_size;
|
|
250
|
+
new_contents = _array__reserve(contents, capacity, element_size, new_capacity);
|
|
251
|
+
}
|
|
252
|
+
return new_contents;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/// This is not what you're looking for, see `array_splice`.
|
|
256
|
+
static inline void *_array__splice(void *self_contents, uint32_t *size, uint32_t *capacity,
|
|
257
|
+
size_t element_size,
|
|
258
|
+
uint32_t index, uint32_t old_count,
|
|
259
|
+
uint32_t new_count, const void *elements) {
|
|
260
|
+
uint32_t new_size = *size + new_count - old_count;
|
|
261
|
+
uint32_t old_end = index + old_count;
|
|
262
|
+
uint32_t new_end = index + new_count;
|
|
263
|
+
assert(old_end <= *size);
|
|
264
|
+
|
|
265
|
+
void *new_contents = _array__reserve(self_contents, capacity, element_size, new_size);
|
|
266
|
+
|
|
267
|
+
char *contents = (char *)new_contents;
|
|
268
|
+
if (*size > old_end) {
|
|
269
|
+
memmove(
|
|
270
|
+
contents + new_end * element_size,
|
|
271
|
+
contents + old_end * element_size,
|
|
272
|
+
(*size - old_end) * element_size
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
if (new_count > 0) {
|
|
276
|
+
if (elements) {
|
|
277
|
+
memcpy(
|
|
278
|
+
(contents + index * element_size),
|
|
279
|
+
elements,
|
|
280
|
+
new_count * element_size
|
|
281
|
+
);
|
|
282
|
+
} else {
|
|
283
|
+
memset(
|
|
284
|
+
(contents + index * element_size),
|
|
285
|
+
0,
|
|
286
|
+
new_count * element_size
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
*size += new_count - old_count;
|
|
291
|
+
|
|
292
|
+
return new_contents;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
|
|
296
|
+
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
|
|
297
|
+
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
|
|
298
|
+
do { \
|
|
299
|
+
*(_index) = start; \
|
|
300
|
+
*(_exists) = false; \
|
|
301
|
+
uint32_t size = (self)->size - *(_index); \
|
|
302
|
+
if (size == 0) break; \
|
|
303
|
+
int comparison; \
|
|
304
|
+
while (size > 1) { \
|
|
305
|
+
uint32_t half_size = size / 2; \
|
|
306
|
+
uint32_t mid_index = *(_index) + half_size; \
|
|
307
|
+
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
|
|
308
|
+
if (comparison <= 0) *(_index) = mid_index; \
|
|
309
|
+
size -= half_size; \
|
|
310
|
+
} \
|
|
311
|
+
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
|
|
312
|
+
if (comparison == 0) *(_exists) = true; \
|
|
313
|
+
else if (comparison < 0) *(_index) += 1; \
|
|
314
|
+
} while (0)
|
|
315
|
+
|
|
316
|
+
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
|
|
317
|
+
/// parameter by reference in order to work with the generic sorting function above.
|
|
318
|
+
#define _compare_int(a, b) ((int)*(a) - (int)(b))
|
|
319
|
+
|
|
320
|
+
#ifdef _MSC_VER
|
|
321
|
+
#pragma warning(pop)
|
|
322
|
+
#elif defined(__GNUC__) || defined(__clang__)
|
|
323
|
+
#pragma GCC diagnostic pop
|
|
324
|
+
#endif
|
|
325
|
+
|
|
326
|
+
#ifdef __cplusplus
|
|
327
|
+
}
|
|
328
|
+
#endif
|
|
329
|
+
|
|
330
|
+
#endif // TREE_SITTER_ARRAY_H_
|