@gram-data/tree-sitter-gram 0.2.7 → 0.3.4

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.
@@ -52,67 +52,91 @@ extern "C" {
52
52
 
53
53
  /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
54
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)
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
+ )
57
60
 
58
61
  /// Free any memory allocated for this array. Note that this does not free any
59
62
  /// memory allocated for the array's contents.
60
- #define array_delete(self) _array__delete((Array *)(self))
63
+ #define array_delete(self) _array__delete((self), (void *)(self)->contents, sizeof(*self))
61
64
 
62
65
  /// 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
+ #define array_push(self, element) \
67
+ do { \
68
+ (self)->contents = _array__grow( \
69
+ (void *)(self)->contents, (self)->size, &(self)->capacity, \
70
+ 1, array_elem_size(self) \
71
+ ); \
72
+ (self)->contents[(self)->size++] = (element); \
73
+ } while(0)
66
74
 
67
75
  /// Increase the array's size by `count` elements.
68
76
  /// 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)); \
77
+ #define array_grow_by(self, count) \
78
+ do { \
79
+ if ((count) == 0) break; \
80
+ (self)->contents = _array__grow( \
81
+ (self)->contents, (self)->size, &(self)->capacity, \
82
+ count, array_elem_size(self) \
83
+ ); \
73
84
  memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
74
- (self)->size += (count); \
85
+ (self)->size += (count); \
75
86
  } while (0)
76
87
 
77
88
  /// Append all elements from one array to the end of another.
78
- #define array_push_all(self, other) \
89
+ #define array_push_all(self, other) \
79
90
  array_extend((self), (other)->size, (other)->contents)
80
91
 
81
92
  /// Append `count` elements to the end of the array, reading their values from the
82
93
  /// `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 \
94
+ #define array_extend(self, count, other_contents) \
95
+ (self)->contents = _array__splice( \
96
+ (void*)(self)->contents, &(self)->size, &(self)->capacity, \
97
+ array_elem_size(self), (self)->size, 0, count, other_contents \
87
98
  )
88
99
 
89
100
  /// Remove `old_count` elements from the array starting at the given `index`. At
90
101
  /// the same index, insert `new_count` new elements, reading their values from the
91
102
  /// `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 \
103
+ #define array_splice(self, _index, old_count, new_count, new_contents) \
104
+ (self)->contents = _array__splice( \
105
+ (void *)(self)->contents, &(self)->size, &(self)->capacity, \
106
+ array_elem_size(self), _index, old_count, new_count, new_contents \
96
107
  )
97
108
 
98
109
  /// 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))
110
+ #define array_insert(self, _index, element) \
111
+ (self)->contents = _array__splice( \
112
+ (void *)(self)->contents, &(self)->size, &(self)->capacity, \
113
+ array_elem_size(self), _index, 0, 1, &(element) \
114
+ )
101
115
 
102
116
  /// Remove one element from the array at the given `index`.
103
117
  #define array_erase(self, _index) \
104
- _array__erase((Array *)(self), array_elem_size(self), _index)
118
+ _array__erase((void *)(self)->contents, &(self)->size, array_elem_size(self), _index)
105
119
 
106
120
  /// Pop the last element off the array, returning the element by value.
107
121
  #define array_pop(self) ((self)->contents[--(self)->size])
108
122
 
109
123
  /// 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))
124
+ #define array_assign(self, other) \
125
+ (self)->contents = _array__assign( \
126
+ (void *)(self)->contents, &(self)->size, &(self)->capacity, \
127
+ (const void *)(other)->contents, (other)->size, array_elem_size(self) \
128
+ )
112
129
 
113
130
  /// Swap one array with another
114
- #define array_swap(self, other) \
115
- _array__swap((Array *)(self), (Array *)(other))
131
+ #define array_swap(self, other) \
132
+ do { \
133
+ struct Swap swapped_contents = _array__swap( \
134
+ (void *)(self)->contents, &(self)->size, &(self)->capacity, \
135
+ (void *)(other)->contents, &(other)->size, &(other)->capacity \
136
+ ); \
137
+ (self)->contents = swapped_contents.self_contents; \
138
+ (other)->contents = swapped_contents.other_contents; \
139
+ } while (0)
116
140
 
117
141
  /// Get the size of the array contents
118
142
  #define array_elem_size(self) (sizeof *(self)->contents)
@@ -157,82 +181,112 @@ extern "C" {
157
181
 
158
182
  // Private
159
183
 
160
- typedef Array(void) Array;
184
+ // Pointers to individual `Array` fields (rather than the entire `Array` itself)
185
+ // are passed to the various `_array__*` functions below to address strict aliasing
186
+ // violations that arises when the _entire_ `Array` struct is passed as `Array(void)*`.
187
+ //
188
+ // The `Array` type itself was not altered as a solution in order to avoid breakage
189
+ // with existing consumers (in particular, parsers with external scanners).
161
190
 
162
191
  /// 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
- }
192
+ static inline void _array__delete(void *self, void *contents, size_t self_size) {
193
+ if (contents) ts_free(contents);
194
+ if (self) memset(self, 0, self_size);
170
195
  }
171
196
 
172
197
  /// 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;
198
+ static inline void _array__erase(void* self_contents, uint32_t *size,
199
+ size_t element_size, uint32_t index) {
200
+ assert(index < *size);
201
+ char *contents = (char *)self_contents;
177
202
  memmove(contents + index * element_size, contents + (index + 1) * element_size,
178
- (self->size - index - 1) * element_size);
179
- self->size--;
203
+ (*size - index - 1) * element_size);
204
+ (*size)--;
180
205
  }
181
206
 
182
207
  /// 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);
208
+ static inline void *_array__reserve(void *contents, uint32_t *capacity,
209
+ size_t element_size, uint32_t new_capacity) {
210
+ void *new_contents = contents;
211
+ if (new_capacity > *capacity) {
212
+ if (contents) {
213
+ new_contents = ts_realloc(contents, new_capacity * element_size);
187
214
  } else {
188
- self->contents = ts_malloc(new_capacity * element_size);
215
+ new_contents = ts_malloc(new_capacity * element_size);
189
216
  }
190
- self->capacity = new_capacity;
217
+ *capacity = new_capacity;
191
218
  }
219
+ return new_contents;
192
220
  }
193
221
 
194
222
  /// 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);
223
+ static inline void *_array__assign(void* self_contents, uint32_t *self_size, uint32_t *self_capacity,
224
+ const void *other_contents, uint32_t other_size, size_t element_size) {
225
+ void *new_contents = _array__reserve(self_contents, self_capacity, element_size, other_size);
226
+ *self_size = other_size;
227
+ memcpy(new_contents, other_contents, *self_size * element_size);
228
+ return new_contents;
199
229
  }
200
230
 
231
+ struct Swap {
232
+ void *self_contents;
233
+ void *other_contents;
234
+ };
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(Array *self, Array *other) {
238
+ static inline struct Swap _array__swap(void *self_contents, uint32_t *self_size, uint32_t *self_capacity,
239
+ void *other_contents, uint32_t *other_size, uint32_t *other_capacity) {
240
+ void *new_self_contents = other_contents;
241
+ uint32_t new_self_size = *other_size;
242
+ uint32_t new_self_capacity = *other_capacity;
243
+
244
+ void *new_other_contents = self_contents;
245
+ *other_size = *self_size;
246
+ *other_capacity = *self_capacity;
247
+
248
+ *self_size = new_self_size;
249
+ *self_capacity = new_self_capacity;
250
+
251
+ struct Swap out = {
252
+ .self_contents = new_self_contents,
253
+ .other_contents = new_other_contents,
254
+ };
255
+ return out;
206
256
  }
207
257
 
208
258
  /// 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;
259
+ static inline void *_array__grow(void *contents, uint32_t size, uint32_t *capacity,
260
+ uint32_t count, size_t element_size) {
261
+ void *new_contents = contents;
262
+ uint32_t new_size = size + count;
263
+ if (new_size > *capacity) {
264
+ uint32_t new_capacity = *capacity * 2;
213
265
  if (new_capacity < 8) new_capacity = 8;
214
266
  if (new_capacity < new_size) new_capacity = new_size;
215
- _array__reserve(self, element_size, new_capacity);
267
+ new_contents = _array__reserve(contents, capacity, element_size, new_capacity);
216
268
  }
269
+ return new_contents;
217
270
  }
218
271
 
219
272
  /// This is not what you're looking for, see `array_splice`.
220
- static inline void _array__splice(Array *self, size_t element_size,
273
+ static inline void *_array__splice(void *self_contents, uint32_t *size, uint32_t *capacity,
274
+ size_t element_size,
221
275
  uint32_t index, uint32_t old_count,
222
276
  uint32_t new_count, const void *elements) {
223
- uint32_t new_size = self->size + new_count - old_count;
277
+ uint32_t new_size = *size + new_count - old_count;
224
278
  uint32_t old_end = index + old_count;
225
279
  uint32_t new_end = index + new_count;
226
- assert(old_end <= self->size);
280
+ assert(old_end <= *size);
227
281
 
228
- _array__reserve(self, element_size, new_size);
282
+ void *new_contents = _array__reserve(self_contents, capacity, element_size, new_size);
229
283
 
230
- char *contents = (char *)self->contents;
231
- if (self->size > old_end) {
284
+ char *contents = (char *)new_contents;
285
+ if (*size > old_end) {
232
286
  memmove(
233
287
  contents + new_end * element_size,
234
288
  contents + old_end * element_size,
235
- (self->size - old_end) * element_size
289
+ (*size - old_end) * element_size
236
290
  );
237
291
  }
238
292
  if (new_count > 0) {
@@ -250,7 +304,9 @@ static inline void _array__splice(Array *self, size_t element_size,
250
304
  );
251
305
  }
252
306
  }
253
- self->size += new_count - old_count;
307
+ *size += new_count - old_count;
308
+
309
+ return new_contents;
254
310
  }
255
311
 
256
312
  /// A binary search routine, based on Rust's `std::slice::binary_search_by`.
Binary file
@@ -0,0 +1,29 @@
1
+ {
2
+ "grammars": [
3
+ {
4
+ "name": "gram",
5
+ "camelcase": "Gram",
6
+ "scope": "source.gram",
7
+ "path": ".",
8
+ "file-types": [
9
+ "gram"
10
+ ]
11
+ }
12
+ ],
13
+ "metadata": {
14
+ "version": "0.3.4",
15
+ "license": "ISC",
16
+ "description": "subject-oriented notation for structured data",
17
+ "links": {
18
+ "repository": "https://github.com/gram-data/tree-sitter-gram"
19
+ }
20
+ },
21
+ "bindings": {
22
+ "c": true,
23
+ "go": true,
24
+ "node": true,
25
+ "python": true,
26
+ "rust": true,
27
+ "swift": true
28
+ }
29
+ }