@athrio/loom 0.0.12 → 0.0.13

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 (31) hide show
  1. package/dist/LanguageLoader-DJGDopcP.js +3 -0
  2. package/dist/{LanguageLoader-BzlRnQhO.js → LanguageLoader-kZ-5Qbwr.js} +19 -11
  3. package/dist/{LanguageLoader-BzlRnQhO.js.map → LanguageLoader-kZ-5Qbwr.js.map} +1 -1
  4. package/dist/{LanguageService-BTCvgdWm.js → LanguageService-CcElIYBZ.js} +2 -2
  5. package/dist/{LanguageService-BTCvgdWm.js.map → LanguageService-CcElIYBZ.js.map} +1 -1
  6. package/dist/{LoomFormatter-Bu8L8cdR.js → LoomFormatter-D6wh-SLN.js} +11 -3
  7. package/dist/{LoomFormatter-Bu8L8cdR.js.map → LoomFormatter-D6wh-SLN.js.map} +1 -1
  8. package/dist/{LoomServer-DXIree_1.js → LoomServer-C54yIIo1.js} +28 -12
  9. package/dist/{LoomServer-DXIree_1.js.map → LoomServer-C54yIIo1.js.map} +1 -1
  10. package/dist/{LoomStore-pA-ohGNX.js → LoomStore-axDgE8RF.js} +103 -21
  11. package/dist/LoomStore-axDgE8RF.js.map +1 -0
  12. package/dist/{Runtime-XT-nb-2w.js → Runtime-Cj1Rsxs1.js} +1 -1
  13. package/dist/{Runtime-Byw6clui.js → Runtime-D4WijrpR.js} +3 -3
  14. package/dist/{Runtime-Byw6clui.js.map → Runtime-D4WijrpR.js.map} +1 -1
  15. package/dist/_virtual_tree-sitter-loom-CFLf84Ng.js +15 -0
  16. package/dist/{dist-D3YSXNOO.js → dist-LBqwqmSY.js} +2 -2
  17. package/dist/{dist-D3YSXNOO.js.map → dist-LBqwqmSY.js.map} +1 -1
  18. package/dist/main.js +224 -49
  19. package/dist/main.js.map +1 -1
  20. package/package.json +3 -1
  21. package/dist/LanguageLoader-DJZBFaqF.js +0 -3
  22. package/dist/LoomStore-pA-ohGNX.js.map +0 -1
  23. package/dist/tree-sitter-loom/editors/helix/queries/loom/highlights.scm +0 -120
  24. package/dist/tree-sitter-loom/editors/helix/queries/loom/injections.scm +0 -413
  25. package/dist/tree-sitter-loom/queries/highlights.scm +0 -120
  26. package/dist/tree-sitter-loom/queries/injections.scm +0 -85
  27. package/dist/tree-sitter-loom/src/parser.c +0 -4689
  28. package/dist/tree-sitter-loom/src/scanner.c +0 -293
  29. package/dist/tree-sitter-loom/src/tree_sitter/alloc.h +0 -54
  30. package/dist/tree-sitter-loom/src/tree_sitter/array.h +0 -330
  31. package/dist/tree-sitter-loom/src/tree_sitter/parser.h +0 -286
@@ -1,293 +0,0 @@
1
- #include "tree_sitter/alloc.h"
2
- #include "tree_sitter/parser.h"
3
-
4
- #include <stdbool.h>
5
- #include <stdint.h>
6
- #include <string.h>
7
-
8
- enum TokenType {
9
- FENCE_LINE,
10
- FIELD_LINE,
11
- HEADING_LINE,
12
- ARROW_LINE,
13
- TILDE_LINE,
14
- PROSE_LINE,
15
- CODE_LINE,
16
- TOC_LINE,
17
- VERBATIM_LINE,
18
- NOTE_LINE,
19
- NOTE_BODY_LINE,
20
- NEWLINE,
21
- ERROR_SENTINEL,
22
- };
23
-
24
- enum Mode { PREAMBLE, CODE, PROSE };
25
-
26
- typedef struct {
27
- bool at_line_start;
28
- bool started;
29
- bool seen_heading;
30
- bool in_frontmatter;
31
- bool in_toc;
32
- bool in_fence;
33
- bool in_note;
34
- bool in_explicit;
35
- uint8_t mode;
36
- } Scanner;
37
-
38
- #define HEAD_SIZE 48
39
-
40
- typedef struct {
41
- char head[HEAD_SIZE];
42
- uint32_t head_length;
43
- uint32_t length;
44
- int32_t last_mark;
45
- bool toc;
46
- } Line;
47
-
48
- static bool is_space(int32_t c) {
49
- return c == ' ' || c == '\t' || c == '\v' || c == '\f' || c == '\r';
50
- }
51
-
52
- static char lower(int32_t c) {
53
- if (c >= 'A' && c <= 'Z') return (char)(c - 'A' + 'a');
54
- return c < 128 ? (char)c : '\x01';
55
- }
56
-
57
- static void peek_line(TSLexer *lexer, Line *line) {
58
- char window[5] = {0, 0, 0, 0, 0};
59
- memset(line, 0, sizeof(Line));
60
- line->last_mark = -1;
61
- while (!lexer->eof(lexer) && lexer->lookahead != '\n' &&
62
- lexer->lookahead != '\r') {
63
- int32_t c = lexer->lookahead;
64
- if (line->head_length < HEAD_SIZE)
65
- line->head[line->head_length++] = c < 128 ? (char)c : '\x01';
66
- if (!is_space(c)) line->last_mark = (int32_t)line->length;
67
- memmove(window, window + 1, 4);
68
- window[4] = lower(c);
69
- if (memcmp(window, "{toc}", 5) == 0) line->toc = true;
70
- line->length++;
71
- lexer->advance(lexer, false);
72
- }
73
- }
74
-
75
- static bool head_has(const Line *line, uint32_t at, const char *text) {
76
- uint32_t n = (uint32_t)strlen(text);
77
- return at + n <= line->head_length && memcmp(line->head + at, text, n) == 0;
78
- }
79
-
80
- static uint32_t indent_of(const Line *line) {
81
- uint32_t i = 0;
82
- while (i < line->head_length && is_space(line->head[i])) i++;
83
- return i;
84
- }
85
-
86
- static uint32_t hashes_of(const Line *line) {
87
- uint32_t i = 0;
88
- while (i < line->head_length && line->head[i] == '#') i++;
89
- return i;
90
- }
91
-
92
- static bool is_heading(const Line *line) {
93
- uint32_t hashes = hashes_of(line);
94
- return hashes >= 1 && hashes <= 6 && head_has(line, hashes, " ");
95
- }
96
-
97
- static bool is_frontmatter_fence(const Line *line) {
98
- return head_has(line, 0, "---") && line->last_mark == 2;
99
- }
100
-
101
- static bool is_arrow(const Line *line) {
102
- uint32_t at = indent_of(line);
103
- return head_has(line, at, "=>") || head_has(line, at, "~>");
104
- }
105
-
106
- static bool is_explicit_arrow(const Line *line) {
107
- return head_has(line, indent_of(line), "~>");
108
- }
109
-
110
- static bool is_tilde(const Line *line) {
111
- return head_has(line, 0, "~") &&
112
- (line->length == 1 || is_space(line->head[1]));
113
- }
114
-
115
- static bool is_code_fence(const Line *line) {
116
- return head_has(line, indent_of(line), "```");
117
- }
118
-
119
- static bool is_note_open(const Line *line) {
120
- return head_has(line, indent_of(line), ":::" "[Note]");
121
- }
122
-
123
- static bool is_note_close(const Line *line) {
124
- uint32_t at = indent_of(line);
125
- return head_has(line, at, ":::") && line->last_mark == (int32_t)at + 2;
126
- }
127
-
128
- static enum TokenType prose_kind(Scanner *s, const Line *line,
129
- enum TokenType kind, enum Mode mode) {
130
- s->mode = mode;
131
- s->in_fence = is_code_fence(line);
132
- s->in_note = !s->in_fence && is_note_open(line);
133
- if (s->in_fence) return VERBATIM_LINE;
134
- if (s->in_note) return NOTE_LINE;
135
- return kind;
136
- }
137
-
138
- static enum TokenType classify_line(Scanner *s, const Line *line) {
139
- bool first = !s->started;
140
- s->started = true;
141
-
142
- if (first && is_frontmatter_fence(line)) {
143
- s->in_frontmatter = true;
144
- s->mode = PREAMBLE;
145
- return FENCE_LINE;
146
- }
147
-
148
- if (s->in_frontmatter) {
149
- s->mode = PREAMBLE;
150
- if (is_frontmatter_fence(line)) {
151
- s->in_frontmatter = false;
152
- return FENCE_LINE;
153
- }
154
- return FIELD_LINE;
155
- }
156
-
157
- if (s->in_fence) {
158
- s->in_fence = !is_code_fence(line);
159
- s->mode = s->seen_heading ? PROSE : PREAMBLE;
160
- return VERBATIM_LINE;
161
- }
162
-
163
- if (s->in_note) {
164
- s->in_note = !is_note_close(line);
165
- s->mode = s->seen_heading ? PROSE : PREAMBLE;
166
- return NOTE_BODY_LINE;
167
- }
168
-
169
- if (s->in_explicit) {
170
- if (is_tilde(line)) {
171
- s->in_explicit = false;
172
- return prose_kind(s, line, TILDE_LINE, PROSE);
173
- }
174
- s->mode = CODE;
175
- return CODE_LINE;
176
- }
177
-
178
- if (is_heading(line)) {
179
- s->mode = PREAMBLE;
180
- if (s->in_toc && hashes_of(line) > 1) return TOC_LINE;
181
- s->seen_heading = true;
182
- s->in_toc = line->toc;
183
- return HEADING_LINE;
184
- }
185
-
186
- if (s->in_toc) {
187
- s->mode = PREAMBLE;
188
- return TOC_LINE;
189
- }
190
-
191
- if (!s->seen_heading) return prose_kind(s, line, PROSE_LINE, PREAMBLE);
192
-
193
- switch (s->mode) {
194
- case PREAMBLE:
195
- if (is_arrow(line)) {
196
- s->in_explicit = is_explicit_arrow(line);
197
- s->mode = CODE;
198
- return ARROW_LINE;
199
- }
200
- if (is_tilde(line)) return prose_kind(s, line, TILDE_LINE, PROSE);
201
- return prose_kind(s, line, PROSE_LINE, PREAMBLE);
202
- case CODE:
203
- if (is_tilde(line)) return prose_kind(s, line, TILDE_LINE, PROSE);
204
- return CODE_LINE;
205
- default:
206
- if (is_arrow(line)) {
207
- s->in_explicit = is_explicit_arrow(line);
208
- s->mode = CODE;
209
- return ARROW_LINE;
210
- }
211
- return prose_kind(s, line, PROSE_LINE, PROSE);
212
- }
213
- }
214
-
215
- static bool scan_newline(Scanner *s, TSLexer *lexer) {
216
- if (lexer->lookahead == '\r') lexer->advance(lexer, false);
217
- if (lexer->lookahead == '\n') lexer->advance(lexer, false);
218
- lexer->mark_end(lexer);
219
- s->at_line_start = true;
220
- lexer->result_symbol = NEWLINE;
221
- return true;
222
- }
223
-
224
- static bool scan(Scanner *s, TSLexer *lexer, const bool *valid) {
225
- bool recovering = valid[ERROR_SENTINEL];
226
-
227
- if (!s->at_line_start) {
228
- if (!valid[NEWLINE] && !recovering) return false;
229
- if (lexer->eof(lexer) || lexer->lookahead == '\n' ||
230
- lexer->lookahead == '\r')
231
- return scan_newline(s, lexer);
232
- return false;
233
- }
234
-
235
- if (lexer->eof(lexer)) return false;
236
-
237
- lexer->mark_end(lexer);
238
- Line line;
239
- peek_line(lexer, &line);
240
- enum TokenType kind = classify_line(s, &line);
241
- if (!valid[kind] && !recovering) return false;
242
- s->at_line_start = false;
243
- lexer->result_symbol = kind;
244
- return true;
245
- }
246
-
247
- void *tree_sitter_loom_external_scanner_create(void) {
248
- Scanner *s = ts_calloc(1, sizeof(Scanner));
249
- s->at_line_start = true;
250
- return s;
251
- }
252
-
253
- void tree_sitter_loom_external_scanner_destroy(void *payload) {
254
- ts_free(payload);
255
- }
256
-
257
- unsigned tree_sitter_loom_external_scanner_serialize(void *payload,
258
- char *buffer) {
259
- Scanner *s = payload;
260
- buffer[0] = (char)s->at_line_start;
261
- buffer[1] = (char)s->started;
262
- buffer[2] = (char)s->seen_heading;
263
- buffer[3] = (char)s->in_frontmatter;
264
- buffer[4] = (char)s->in_toc;
265
- buffer[5] = (char)s->in_fence;
266
- buffer[6] = (char)s->in_note;
267
- buffer[7] = (char)s->in_explicit;
268
- buffer[8] = (char)s->mode;
269
- return 9;
270
- }
271
-
272
- void tree_sitter_loom_external_scanner_deserialize(void *payload,
273
- const char *buffer,
274
- unsigned length) {
275
- Scanner *s = payload;
276
- memset(s, 0, sizeof(Scanner));
277
- s->at_line_start = true;
278
- if (length < 9) return;
279
- s->at_line_start = buffer[0];
280
- s->started = buffer[1];
281
- s->seen_heading = buffer[2];
282
- s->in_frontmatter = buffer[3];
283
- s->in_toc = buffer[4];
284
- s->in_fence = buffer[5];
285
- s->in_note = buffer[6];
286
- s->in_explicit = buffer[7];
287
- s->mode = (uint8_t)buffer[8];
288
- }
289
-
290
- bool tree_sitter_loom_external_scanner_scan(void *payload, TSLexer *lexer,
291
- const bool *valid_symbols) {
292
- return scan(payload, lexer, valid_symbols);
293
- }
@@ -1,54 +0,0 @@
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_
@@ -1,330 +0,0 @@
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_