@dekobon/tree-sitter-groovy 0.2.0
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-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +187 -0
- package/binding.gyp +21 -0
- package/bindings/node/binding.cc +19 -0
- package/bindings/node/binding_test.js +24 -0
- package/bindings/node/index.d.ts +56 -0
- package/bindings/node/index.js +27 -0
- package/grammar.js +1607 -0
- package/package.json +88 -0
- package/prebuilds/darwin-arm64/@dekobon+tree-sitter-groovy.node +0 -0
- package/prebuilds/linux-x64/@dekobon+tree-sitter-groovy.node +0 -0
- package/prebuilds/win32-x64/@dekobon+tree-sitter-groovy.node +0 -0
- package/queries/groovy/folds.scm +18 -0
- package/queries/groovy/highlights.scm +235 -0
- package/queries/groovy/indents.scm +35 -0
- package/queries/groovy/injections.scm +38 -0
- package/queries/groovy/locals.scm +32 -0
- package/queries/groovy/tags.scm +67 -0
- package/src/grammar.json +6527 -0
- package/src/node-types.json +16643 -0
- package/src/parser.c +147303 -0
- package/src/scanner.c +213 -0
- package/src/tree_sitter/alloc.h +54 -0
- package/src/tree_sitter/array.h +291 -0
- package/src/tree_sitter/parser.h +286 -0
- package/tree-sitter.json +38 -0
package/src/scanner.c
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Hand-written external scanner for tree-sitter-groovy.
|
|
2
|
+
//
|
|
3
|
+
// Handles §6.2 slashy-string-start and §6.6 line/block/groovydoc
|
|
4
|
+
// comments. Both start with `/`; combining the dispatch keeps the
|
|
5
|
+
// in-grammar lexer from partially consuming the leading `/`.
|
|
6
|
+
//
|
|
7
|
+
// Stateless — `create` returns NULL and the runtime calls
|
|
8
|
+
// `destroy` / `serialize` / `deserialize` as no-ops. When a future
|
|
9
|
+
// branch needs persistent state, introduce a struct then.
|
|
10
|
+
|
|
11
|
+
#include "tree_sitter/parser.h"
|
|
12
|
+
|
|
13
|
+
enum TokenType {
|
|
14
|
+
SLASHY_STRING_START,
|
|
15
|
+
LINE_COMMENT,
|
|
16
|
+
BLOCK_COMMENT,
|
|
17
|
+
GROOVYDOC_COMMENT,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
static inline void advance(TSLexer *lexer) {
|
|
21
|
+
lexer->advance(lexer, false);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static inline bool is_whitespace(int32_t c) {
|
|
25
|
+
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
void *tree_sitter_groovy_external_scanner_create(void) {
|
|
29
|
+
return NULL;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
void tree_sitter_groovy_external_scanner_destroy(void *payload) {
|
|
33
|
+
(void)payload;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
unsigned tree_sitter_groovy_external_scanner_serialize(
|
|
37
|
+
void *payload,
|
|
38
|
+
char *buffer
|
|
39
|
+
) {
|
|
40
|
+
(void)payload;
|
|
41
|
+
(void)buffer;
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
void tree_sitter_groovy_external_scanner_deserialize(
|
|
46
|
+
void *payload,
|
|
47
|
+
const char *buffer,
|
|
48
|
+
unsigned length
|
|
49
|
+
) {
|
|
50
|
+
(void)payload;
|
|
51
|
+
(void)buffer;
|
|
52
|
+
(void)length;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
bool tree_sitter_groovy_external_scanner_scan(
|
|
56
|
+
void *payload,
|
|
57
|
+
TSLexer *lexer,
|
|
58
|
+
const bool *valid_symbols
|
|
59
|
+
) {
|
|
60
|
+
(void)payload;
|
|
61
|
+
|
|
62
|
+
// SPECIFICATION.md §6.6 — line / block / groovydoc comments
|
|
63
|
+
// and §6.2 slashy strings. Both start with `/`; combining their
|
|
64
|
+
// dispatch keeps the in-grammar lexer from partially consuming
|
|
65
|
+
// the leading `/`. Leading whitespace is skipped via
|
|
66
|
+
// advance(true) so the scanner gets a chance to fire after the
|
|
67
|
+
// in-grammar `\s` SKIP rule — without this, tree-sitter's lexer
|
|
68
|
+
// eats `\n`, does NOT re-invoke the external scanner, then
|
|
69
|
+
// fails on the following `/` and consumes it through error
|
|
70
|
+
// recovery.
|
|
71
|
+
bool wants_comment = valid_symbols[LINE_COMMENT]
|
|
72
|
+
|| valid_symbols[BLOCK_COMMENT]
|
|
73
|
+
|| valid_symbols[GROOVYDOC_COMMENT];
|
|
74
|
+
bool wants_slashy = valid_symbols[SLASHY_STRING_START];
|
|
75
|
+
if (!wants_comment && !wants_slashy) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
while (is_whitespace(lexer->lookahead)) {
|
|
80
|
+
lexer->advance(lexer, true);
|
|
81
|
+
}
|
|
82
|
+
if (lexer->lookahead != '/') {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
advance(lexer);
|
|
87
|
+
// Dispatch on the char after `/`:
|
|
88
|
+
// `/` → line comment
|
|
89
|
+
// `*` → block / groovydoc comment
|
|
90
|
+
// `=` → augmented assign, in-grammar handles it
|
|
91
|
+
// else → slashy string (when valid_symbols allow)
|
|
92
|
+
int32_t next = lexer->lookahead;
|
|
93
|
+
if (next == '/') {
|
|
94
|
+
if (!valid_symbols[LINE_COMMENT]) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
advance(lexer);
|
|
98
|
+
// EOF-bounded loop: lexer->eof() guarantees termination on
|
|
99
|
+
// unterminated input. Do not tighten without preserving the
|
|
100
|
+
// EOF guard.
|
|
101
|
+
while (!lexer->eof(lexer) && lexer->lookahead != '\n') {
|
|
102
|
+
advance(lexer);
|
|
103
|
+
}
|
|
104
|
+
lexer->result_symbol = LINE_COMMENT;
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
if (next == '*') {
|
|
108
|
+
if (!wants_comment) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
advance(lexer);
|
|
112
|
+
bool is_groovydoc = false;
|
|
113
|
+
if (lexer->lookahead == '*') {
|
|
114
|
+
advance(lexer);
|
|
115
|
+
is_groovydoc = true;
|
|
116
|
+
if (lexer->lookahead == '/') {
|
|
117
|
+
advance(lexer);
|
|
118
|
+
if (!valid_symbols[GROOVYDOC_COMMENT]) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
lexer->result_symbol = GROOVYDOC_COMMENT;
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// EOF-bounded loop: a `/* …` without a closer terminates
|
|
126
|
+
// here via lexer->eof(), not by going past end-of-input.
|
|
127
|
+
while (!lexer->eof(lexer)) {
|
|
128
|
+
if (lexer->lookahead == '*') {
|
|
129
|
+
advance(lexer);
|
|
130
|
+
if (lexer->lookahead == '/') {
|
|
131
|
+
advance(lexer);
|
|
132
|
+
if (is_groovydoc) {
|
|
133
|
+
if (!valid_symbols[GROOVYDOC_COMMENT]) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
lexer->result_symbol = GROOVYDOC_COMMENT;
|
|
137
|
+
} else {
|
|
138
|
+
if (!valid_symbols[BLOCK_COMMENT]) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
lexer->result_symbol = BLOCK_COMMENT;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
advance(lexer);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
if (next == '=') {
|
|
152
|
+
// `/=` is the augmented-assign operator. The in-grammar
|
|
153
|
+
// lexer owns it. Returning false here causes tree-sitter
|
|
154
|
+
// to rewind the cursor to before the leading `/`, so the
|
|
155
|
+
// in-grammar lexer can match `/=` cleanly.
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
// Slashy string opening `/` — we've consumed it; the body and
|
|
159
|
+
// closing `/` are matched by the grammar's `slashy_string` rule
|
|
160
|
+
// via regex tokens, so `$identifier` and `${expr}` segments can
|
|
161
|
+
// be exposed as structured children. SPECIFICATION.md §5.4 /
|
|
162
|
+
// §6.2 require a context-sensitive emit. Three pragmatic guards
|
|
163
|
+
// keep `a / b` and similar division contexts from being
|
|
164
|
+
// mis-tokenised as the opening of a slashy:
|
|
165
|
+
//
|
|
166
|
+
// (1) Reject an empty body (the next char after the opening
|
|
167
|
+
// `/` is a closing `/`) — `//` is already a line comment
|
|
168
|
+
// and `/=` is already trapped above, so an empty slashy
|
|
169
|
+
// is excluded.
|
|
170
|
+
// (2) Reject a body that starts with whitespace — `a / b`
|
|
171
|
+
// has a space immediately after the operator `/`, but
|
|
172
|
+
// every legal slashy regex starts with a non-whitespace
|
|
173
|
+
// char.
|
|
174
|
+
// (3) Look ahead through the body to confirm there is a
|
|
175
|
+
// closing `/` on the same line. Without this, `a / b\nc`
|
|
176
|
+
// at end of file would emit a slashy opener and leave
|
|
177
|
+
// the body unterminated.
|
|
178
|
+
if (!wants_slashy) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
if (lexer->lookahead == '/' || is_whitespace(lexer->lookahead)) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
// Look ahead (without consuming) to confirm a closing `/` exists
|
|
185
|
+
// before a newline or EOF. `lexer->mark_end` records the position
|
|
186
|
+
// where the slashy opener token ends; advancing past it during
|
|
187
|
+
// look-ahead does not extend the token range as long as we do
|
|
188
|
+
// not call `mark_end` again.
|
|
189
|
+
lexer->mark_end(lexer);
|
|
190
|
+
bool found_close = false;
|
|
191
|
+
while (!lexer->eof(lexer)) {
|
|
192
|
+
if (lexer->lookahead == '\\') {
|
|
193
|
+
advance(lexer);
|
|
194
|
+
if (!lexer->eof(lexer)) {
|
|
195
|
+
advance(lexer);
|
|
196
|
+
}
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (lexer->lookahead == '/') {
|
|
200
|
+
found_close = true;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
if (lexer->lookahead == '\n' || lexer->lookahead == '\r') {
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
advance(lexer);
|
|
207
|
+
}
|
|
208
|
+
if (!found_close) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
lexer->result_symbol = SLASHY_STRING_START;
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
@@ -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,291 @@
|
|
|
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
|
+
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
|
|
57
|
+
|
|
58
|
+
/// Free any memory allocated for this array. Note that this does not free any
|
|
59
|
+
/// memory allocated for the array's contents.
|
|
60
|
+
#define array_delete(self) _array__delete((Array *)(self))
|
|
61
|
+
|
|
62
|
+
/// Push a new `element` onto the end of the array.
|
|
63
|
+
#define array_push(self, element) \
|
|
64
|
+
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
|
|
65
|
+
(self)->contents[(self)->size++] = (element))
|
|
66
|
+
|
|
67
|
+
/// Increase the array's size by `count` elements.
|
|
68
|
+
/// New elements are zero-initialized.
|
|
69
|
+
#define array_grow_by(self, count) \
|
|
70
|
+
do { \
|
|
71
|
+
if ((count) == 0) break; \
|
|
72
|
+
_array__grow((Array *)(self), count, array_elem_size(self)); \
|
|
73
|
+
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
|
|
74
|
+
(self)->size += (count); \
|
|
75
|
+
} while (0)
|
|
76
|
+
|
|
77
|
+
/// Append all elements from one array to the end of another.
|
|
78
|
+
#define array_push_all(self, other) \
|
|
79
|
+
array_extend((self), (other)->size, (other)->contents)
|
|
80
|
+
|
|
81
|
+
/// Append `count` elements to the end of the array, reading their values from the
|
|
82
|
+
/// `contents` pointer.
|
|
83
|
+
#define array_extend(self, count, contents) \
|
|
84
|
+
_array__splice( \
|
|
85
|
+
(Array *)(self), array_elem_size(self), (self)->size, \
|
|
86
|
+
0, count, contents \
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
/// Remove `old_count` elements from the array starting at the given `index`. At
|
|
90
|
+
/// the same index, insert `new_count` new elements, reading their values from the
|
|
91
|
+
/// `new_contents` pointer.
|
|
92
|
+
#define array_splice(self, _index, old_count, new_count, new_contents) \
|
|
93
|
+
_array__splice( \
|
|
94
|
+
(Array *)(self), array_elem_size(self), _index, \
|
|
95
|
+
old_count, new_count, new_contents \
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
/// Insert one `element` into the array at the given `index`.
|
|
99
|
+
#define array_insert(self, _index, element) \
|
|
100
|
+
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
|
|
101
|
+
|
|
102
|
+
/// Remove one element from the array at the given `index`.
|
|
103
|
+
#define array_erase(self, _index) \
|
|
104
|
+
_array__erase((Array *)(self), array_elem_size(self), _index)
|
|
105
|
+
|
|
106
|
+
/// Pop the last element off the array, returning the element by value.
|
|
107
|
+
#define array_pop(self) ((self)->contents[--(self)->size])
|
|
108
|
+
|
|
109
|
+
/// Assign the contents of one array to another, reallocating if necessary.
|
|
110
|
+
#define array_assign(self, other) \
|
|
111
|
+
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
|
|
112
|
+
|
|
113
|
+
/// Swap one array with another
|
|
114
|
+
#define array_swap(self, other) \
|
|
115
|
+
_array__swap((Array *)(self), (Array *)(other))
|
|
116
|
+
|
|
117
|
+
/// Get the size of the array contents
|
|
118
|
+
#define array_elem_size(self) (sizeof *(self)->contents)
|
|
119
|
+
|
|
120
|
+
/// Search a sorted array for a given `needle` value, using the given `compare`
|
|
121
|
+
/// callback to determine the order.
|
|
122
|
+
///
|
|
123
|
+
/// If an existing element is found to be equal to `needle`, then the `index`
|
|
124
|
+
/// out-parameter is set to the existing value's index, and the `exists`
|
|
125
|
+
/// out-parameter is set to true. Otherwise, `index` is set to an index where
|
|
126
|
+
/// `needle` should be inserted in order to preserve the sorting, and `exists`
|
|
127
|
+
/// is set to false.
|
|
128
|
+
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
|
|
129
|
+
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
|
|
130
|
+
|
|
131
|
+
/// Search a sorted array for a given `needle` value, using integer comparisons
|
|
132
|
+
/// of a given struct field (specified with a leading dot) to determine the order.
|
|
133
|
+
///
|
|
134
|
+
/// See also `array_search_sorted_with`.
|
|
135
|
+
#define array_search_sorted_by(self, field, needle, _index, _exists) \
|
|
136
|
+
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
|
|
137
|
+
|
|
138
|
+
/// Insert a given `value` into a sorted array, using the given `compare`
|
|
139
|
+
/// callback to determine the order.
|
|
140
|
+
#define array_insert_sorted_with(self, compare, value) \
|
|
141
|
+
do { \
|
|
142
|
+
unsigned _index, _exists; \
|
|
143
|
+
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
|
|
144
|
+
if (!_exists) array_insert(self, _index, value); \
|
|
145
|
+
} while (0)
|
|
146
|
+
|
|
147
|
+
/// Insert a given `value` into a sorted array, using integer comparisons of
|
|
148
|
+
/// a given struct field (specified with a leading dot) to determine the order.
|
|
149
|
+
///
|
|
150
|
+
/// See also `array_search_sorted_by`.
|
|
151
|
+
#define array_insert_sorted_by(self, field, value) \
|
|
152
|
+
do { \
|
|
153
|
+
unsigned _index, _exists; \
|
|
154
|
+
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
|
|
155
|
+
if (!_exists) array_insert(self, _index, value); \
|
|
156
|
+
} while (0)
|
|
157
|
+
|
|
158
|
+
// Private
|
|
159
|
+
|
|
160
|
+
typedef Array(void) Array;
|
|
161
|
+
|
|
162
|
+
/// This is not what you're looking for, see `array_delete`.
|
|
163
|
+
static inline void _array__delete(Array *self) {
|
|
164
|
+
if (self->contents) {
|
|
165
|
+
ts_free(self->contents);
|
|
166
|
+
self->contents = NULL;
|
|
167
|
+
self->size = 0;
|
|
168
|
+
self->capacity = 0;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/// This is not what you're looking for, see `array_erase`.
|
|
173
|
+
static inline void _array__erase(Array *self, size_t element_size,
|
|
174
|
+
uint32_t index) {
|
|
175
|
+
assert(index < self->size);
|
|
176
|
+
char *contents = (char *)self->contents;
|
|
177
|
+
memmove(contents + index * element_size, contents + (index + 1) * element_size,
|
|
178
|
+
(self->size - index - 1) * element_size);
|
|
179
|
+
self->size--;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// This is not what you're looking for, see `array_reserve`.
|
|
183
|
+
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
|
|
184
|
+
if (new_capacity > self->capacity) {
|
|
185
|
+
if (self->contents) {
|
|
186
|
+
self->contents = ts_realloc(self->contents, new_capacity * element_size);
|
|
187
|
+
} else {
|
|
188
|
+
self->contents = ts_malloc(new_capacity * element_size);
|
|
189
|
+
}
|
|
190
|
+
self->capacity = new_capacity;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/// This is not what you're looking for, see `array_assign`.
|
|
195
|
+
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
|
|
196
|
+
_array__reserve(self, element_size, other->size);
|
|
197
|
+
self->size = other->size;
|
|
198
|
+
memcpy(self->contents, other->contents, self->size * element_size);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// This is not what you're looking for, see `array_swap`.
|
|
202
|
+
static inline void _array__swap(Array *self, Array *other) {
|
|
203
|
+
Array swap = *other;
|
|
204
|
+
*other = *self;
|
|
205
|
+
*self = swap;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
|
|
209
|
+
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
|
|
210
|
+
uint32_t new_size = self->size + count;
|
|
211
|
+
if (new_size > self->capacity) {
|
|
212
|
+
uint32_t new_capacity = self->capacity * 2;
|
|
213
|
+
if (new_capacity < 8) new_capacity = 8;
|
|
214
|
+
if (new_capacity < new_size) new_capacity = new_size;
|
|
215
|
+
_array__reserve(self, element_size, new_capacity);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/// This is not what you're looking for, see `array_splice`.
|
|
220
|
+
static inline void _array__splice(Array *self, size_t element_size,
|
|
221
|
+
uint32_t index, uint32_t old_count,
|
|
222
|
+
uint32_t new_count, const void *elements) {
|
|
223
|
+
uint32_t new_size = self->size + new_count - old_count;
|
|
224
|
+
uint32_t old_end = index + old_count;
|
|
225
|
+
uint32_t new_end = index + new_count;
|
|
226
|
+
assert(old_end <= self->size);
|
|
227
|
+
|
|
228
|
+
_array__reserve(self, element_size, new_size);
|
|
229
|
+
|
|
230
|
+
char *contents = (char *)self->contents;
|
|
231
|
+
if (self->size > old_end) {
|
|
232
|
+
memmove(
|
|
233
|
+
contents + new_end * element_size,
|
|
234
|
+
contents + old_end * element_size,
|
|
235
|
+
(self->size - old_end) * element_size
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
if (new_count > 0) {
|
|
239
|
+
if (elements) {
|
|
240
|
+
memcpy(
|
|
241
|
+
(contents + index * element_size),
|
|
242
|
+
elements,
|
|
243
|
+
new_count * element_size
|
|
244
|
+
);
|
|
245
|
+
} else {
|
|
246
|
+
memset(
|
|
247
|
+
(contents + index * element_size),
|
|
248
|
+
0,
|
|
249
|
+
new_count * element_size
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
self->size += new_count - old_count;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
|
|
257
|
+
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
|
|
258
|
+
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
|
|
259
|
+
do { \
|
|
260
|
+
*(_index) = start; \
|
|
261
|
+
*(_exists) = false; \
|
|
262
|
+
uint32_t size = (self)->size - *(_index); \
|
|
263
|
+
if (size == 0) break; \
|
|
264
|
+
int comparison; \
|
|
265
|
+
while (size > 1) { \
|
|
266
|
+
uint32_t half_size = size / 2; \
|
|
267
|
+
uint32_t mid_index = *(_index) + half_size; \
|
|
268
|
+
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
|
|
269
|
+
if (comparison <= 0) *(_index) = mid_index; \
|
|
270
|
+
size -= half_size; \
|
|
271
|
+
} \
|
|
272
|
+
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
|
|
273
|
+
if (comparison == 0) *(_exists) = true; \
|
|
274
|
+
else if (comparison < 0) *(_index) += 1; \
|
|
275
|
+
} while (0)
|
|
276
|
+
|
|
277
|
+
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
|
|
278
|
+
/// parameter by reference in order to work with the generic sorting function above.
|
|
279
|
+
#define _compare_int(a, b) ((int)*(a) - (int)(b))
|
|
280
|
+
|
|
281
|
+
#ifdef _MSC_VER
|
|
282
|
+
#pragma warning(pop)
|
|
283
|
+
#elif defined(__GNUC__) || defined(__clang__)
|
|
284
|
+
#pragma GCC diagnostic pop
|
|
285
|
+
#endif
|
|
286
|
+
|
|
287
|
+
#ifdef __cplusplus
|
|
288
|
+
}
|
|
289
|
+
#endif
|
|
290
|
+
|
|
291
|
+
#endif // TREE_SITTER_ARRAY_H_
|