tree-sitter-java-orchard 0.5.8 → 0.5.9

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.
@@ -50,69 +50,104 @@ extern "C" {
50
50
  /// memory allocated for the array's contents.
51
51
  #define array_clear(self) ((self)->size = 0)
52
52
 
53
+ #ifdef __cplusplus
54
+ #define _array__cast(self, expr) (decltype((self)->contents))(expr)
55
+ #else
56
+ #define _array__cast(self, expr) (expr)
57
+ #endif
58
+
53
59
  /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
54
60
  /// 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)
61
+ #define array_reserve(self, new_capacity) \
62
+ ((self)->contents = _array__cast(self, _array__reserve( \
63
+ (void *)(self)->contents, &(self)->capacity, \
64
+ array_elem_size(self), new_capacity)) \
65
+ )
57
66
 
58
67
  /// Free any memory allocated for this array. Note that this does not free any
59
68
  /// memory allocated for the array's contents.
60
- #define array_delete(self) _array__delete((Array *)(self))
69
+ #define array_delete(self) \
70
+ do { \
71
+ if ((self)->contents) ts_free((self)->contents); \
72
+ (self)->contents = NULL; \
73
+ (self)->size = 0; \
74
+ (self)->capacity = 0; \
75
+ } while (0)
61
76
 
62
77
  /// 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))
78
+ #define array_push(self, element) \
79
+ do { \
80
+ (self)->contents = _array__cast(self, _array__grow( \
81
+ (void *)(self)->contents, (self)->size, &(self)->capacity, \
82
+ 1, array_elem_size(self) \
83
+ )); \
84
+ (self)->contents[(self)->size++] = (element); \
85
+ } while(0)
66
86
 
67
87
  /// Increase the array's size by `count` elements.
68
88
  /// 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)); \
89
+ #define array_grow_by(self, count) \
90
+ do { \
91
+ if ((count) == 0) break; \
92
+ (self)->contents = _array__cast(self, _array__grow( \
93
+ (self)->contents, (self)->size, &(self)->capacity, \
94
+ count, array_elem_size(self) \
95
+ )); \
73
96
  memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
74
- (self)->size += (count); \
97
+ (self)->size += (count); \
75
98
  } while (0)
76
99
 
77
100
  /// Append all elements from one array to the end of another.
78
- #define array_push_all(self, other) \
101
+ #define array_push_all(self, other) \
79
102
  array_extend((self), (other)->size, (other)->contents)
80
103
 
81
104
  /// Append `count` elements to the end of the array, reading their values from the
82
105
  /// `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
- )
106
+ #define array_extend(self, count, other_contents) \
107
+ ((self)->contents = _array__cast(self, _array__splice( \
108
+ (void*)(self)->contents, &(self)->size, &(self)->capacity, \
109
+ array_elem_size(self), (self)->size, 0, count, other_contents \
110
+ )))
88
111
 
89
112
  /// Remove `old_count` elements from the array starting at the given `index`. At
90
113
  /// the same index, insert `new_count` new elements, reading their values from the
91
114
  /// `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
- )
115
+ #define array_splice(self, _index, old_count, new_count, new_contents) \
116
+ ((self)->contents = _array__cast(self, _array__splice( \
117
+ (void *)(self)->contents, &(self)->size, &(self)->capacity, \
118
+ array_elem_size(self), _index, old_count, new_count, new_contents \
119
+ )))
97
120
 
98
121
  /// 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))
122
+ #define array_insert(self, _index, element) \
123
+ ((self)->contents = _array__cast(self, _array__splice( \
124
+ (void *)(self)->contents, &(self)->size, &(self)->capacity, \
125
+ array_elem_size(self), _index, 0, 1, &(element) \
126
+ )))
101
127
 
102
128
  /// Remove one element from the array at the given `index`.
103
129
  #define array_erase(self, _index) \
104
- _array__erase((Array *)(self), array_elem_size(self), _index)
130
+ _array__erase((void *)(self)->contents, &(self)->size, array_elem_size(self), _index)
105
131
 
106
132
  /// Pop the last element off the array, returning the element by value.
107
133
  #define array_pop(self) ((self)->contents[--(self)->size])
108
134
 
109
135
  /// 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))
136
+ #define array_assign(self, other) \
137
+ ((self)->contents = _array__cast(self, _array__assign( \
138
+ (void *)(self)->contents, &(self)->size, &(self)->capacity, \
139
+ (const void *)(other)->contents, (other)->size, array_elem_size(self) \
140
+ )))
112
141
 
113
142
  /// Swap one array with another
114
- #define array_swap(self, other) \
115
- _array__swap((Array *)(self), (Array *)(other))
143
+ #define array_swap(self, other) \
144
+ do { \
145
+ void *_array_swap_tmp = (void *)(self)->contents; \
146
+ (self)->contents = (other)->contents; \
147
+ (other)->contents = _array__cast(other, _array_swap_tmp); \
148
+ _array__swap(&(self)->size, &(self)->capacity, \
149
+ &(other)->size, &(other)->capacity); \
150
+ } while (0)
116
151
 
117
152
  /// Get the size of the array contents
118
153
  #define array_elem_size(self) (sizeof *(self)->contents)
@@ -157,82 +192,90 @@ extern "C" {
157
192
 
158
193
  // Private
159
194
 
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
- }
195
+ // Pointers to individual `Array` fields (rather than the entire `Array` itself)
196
+ // are passed to the various `_array__*` functions below to address strict aliasing
197
+ // violations that arises when the _entire_ `Array` struct is passed as `Array(void)*`.
198
+ //
199
+ // The `Array` type itself was not altered as a solution in order to avoid breakage
200
+ // with existing consumers (in particular, parsers with external scanners).
171
201
 
172
202
  /// 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;
203
+ static inline void _array__erase(void* self_contents, uint32_t *size,
204
+ size_t element_size, uint32_t index) {
205
+ assert(index < *size);
206
+ char *contents = (char *)self_contents;
177
207
  memmove(contents + index * element_size, contents + (index + 1) * element_size,
178
- (self->size - index - 1) * element_size);
179
- self->size--;
208
+ (*size - index - 1) * element_size);
209
+ (*size)--;
180
210
  }
181
211
 
182
212
  /// 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);
213
+ static inline void *_array__reserve(void *contents, uint32_t *capacity,
214
+ size_t element_size, uint32_t new_capacity) {
215
+ void *new_contents = contents;
216
+ if (new_capacity > *capacity) {
217
+ if (contents) {
218
+ new_contents = ts_realloc(contents, new_capacity * element_size);
187
219
  } else {
188
- self->contents = ts_malloc(new_capacity * element_size);
220
+ new_contents = ts_malloc(new_capacity * element_size);
189
221
  }
190
- self->capacity = new_capacity;
222
+ *capacity = new_capacity;
191
223
  }
224
+ return new_contents;
192
225
  }
193
226
 
194
227
  /// 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);
228
+ static inline void *_array__assign(void* self_contents, uint32_t *self_size, uint32_t *self_capacity,
229
+ const void *other_contents, uint32_t other_size, size_t element_size) {
230
+ void *new_contents = _array__reserve(self_contents, self_capacity, element_size, other_size);
231
+ *self_size = other_size;
232
+ memcpy(new_contents, other_contents, *self_size * element_size);
233
+ return new_contents;
199
234
  }
200
235
 
201
236
  /// 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;
237
+ static inline void _array__swap(uint32_t *self_size, uint32_t *self_capacity,
238
+ uint32_t *other_size, uint32_t *other_capacity) {
239
+ uint32_t tmp_size = *self_size;
240
+ uint32_t tmp_capacity = *self_capacity;
241
+ *self_size = *other_size;
242
+ *self_capacity = *other_capacity;
243
+ *other_size = tmp_size;
244
+ *other_capacity = tmp_capacity;
206
245
  }
207
246
 
208
247
  /// 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;
248
+ static inline void *_array__grow(void *contents, uint32_t size, uint32_t *capacity,
249
+ uint32_t count, size_t element_size) {
250
+ void *new_contents = contents;
251
+ uint32_t new_size = size + count;
252
+ if (new_size > *capacity) {
253
+ uint32_t new_capacity = *capacity * 2;
213
254
  if (new_capacity < 8) new_capacity = 8;
214
255
  if (new_capacity < new_size) new_capacity = new_size;
215
- _array__reserve(self, element_size, new_capacity);
256
+ new_contents = _array__reserve(contents, capacity, element_size, new_capacity);
216
257
  }
258
+ return new_contents;
217
259
  }
218
260
 
219
261
  /// This is not what you're looking for, see `array_splice`.
220
- static inline void _array__splice(Array *self, size_t element_size,
262
+ static inline void *_array__splice(void *self_contents, uint32_t *size, uint32_t *capacity,
263
+ size_t element_size,
221
264
  uint32_t index, uint32_t old_count,
222
265
  uint32_t new_count, const void *elements) {
223
- uint32_t new_size = self->size + new_count - old_count;
266
+ uint32_t new_size = *size + new_count - old_count;
224
267
  uint32_t old_end = index + old_count;
225
268
  uint32_t new_end = index + new_count;
226
- assert(old_end <= self->size);
269
+ assert(old_end <= *size);
227
270
 
228
- _array__reserve(self, element_size, new_size);
271
+ void *new_contents = _array__reserve(self_contents, capacity, element_size, new_size);
229
272
 
230
- char *contents = (char *)self->contents;
231
- if (self->size > old_end) {
273
+ char *contents = (char *)new_contents;
274
+ if (*size > old_end) {
232
275
  memmove(
233
276
  contents + new_end * element_size,
234
277
  contents + old_end * element_size,
235
- (self->size - old_end) * element_size
278
+ (*size - old_end) * element_size
236
279
  );
237
280
  }
238
281
  if (new_count > 0) {
@@ -250,7 +293,9 @@ static inline void _array__splice(Array *self, size_t element_size,
250
293
  );
251
294
  }
252
295
  }
253
- self->size += new_count - old_count;
296
+ *size += new_count - old_count;
297
+
298
+ return new_contents;
254
299
  }
255
300
 
256
301
  /// A binary search routine, based on Rust's `std::slice::binary_search_by`.
Binary file
package/tree-sitter.json CHANGED
@@ -14,7 +14,7 @@
14
14
  }
15
15
  ],
16
16
  "metadata": {
17
- "version": "0.5.8",
17
+ "version": "0.5.9",
18
18
  "license": "MIT",
19
19
  "description": "Java grammar for tree-sitter",
20
20
  "authors": [
@@ -32,7 +32,8 @@
32
32
  }
33
33
  ],
34
34
  "links": {
35
- "repository": "https://codeberg.org/grammar-orchard/tree-sitter-java-orchard"
35
+ "repository": "https://codeberg.org/grammar-orchard/tree-sitter-java-orchard",
36
+ "upstream": "https://github.com/tree-sitter/tree-sitter-java"
36
37
  }
37
38
  },
38
39
  "bindings": {