@apple/tree-sitter-pkl 0.16.0 → 0.17.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.
Files changed (58) hide show
  1. package/CHANGELOG.adoc +40 -0
  2. package/Cargo.lock +1 -1
  3. package/Cargo.toml +1 -1
  4. package/Package.swift +24 -0
  5. package/README.md +42 -0
  6. package/bindings/c/tree-sitter-pkl.h +16 -0
  7. package/bindings/swift/TreeSitterPkl/pkl.h +16 -0
  8. package/grammar.js +147 -63
  9. package/package.json +3 -4
  10. package/queries/highlights.scm +3 -8
  11. package/queries/injections.scm +3 -3
  12. package/src/grammar.json +750 -353
  13. package/src/node-types.json +1768 -1043
  14. package/src/parser.c +45772 -50670
  15. package/src/scanner.c +60 -28
  16. package/src/tree_sitter/alloc.h +54 -0
  17. package/src/tree_sitter/array.h +290 -0
  18. package/src/tree_sitter/parser.h +54 -13
  19. package/test/corpus/basic/annotation.txt +243 -0
  20. package/test/corpus/basic/comments.txt +41 -0
  21. package/test/corpus/basic/shebangComment.txt +15 -0
  22. package/test/corpus/basic/types.txt +232 -0
  23. package/test/corpus/class/constModifier.txt +24 -0
  24. package/test/corpus/class/fixedModifier.txt +24 -0
  25. package/test/corpus/expr/binary.txt +109 -0
  26. package/test/corpus/expr/functionLiteral.txt +23 -0
  27. package/test/corpus/expr/if.txt +15 -0
  28. package/test/corpus/expr/import.txt +20 -0
  29. package/test/corpus/expr/let.txt +17 -0
  30. package/test/corpus/expr/new.txt +36 -0
  31. package/test/corpus/expr/qualifiedAccess.txt +95 -0
  32. package/test/corpus/expr/read.txt +29 -0
  33. package/test/corpus/expr/throw.txt +14 -0
  34. package/test/corpus/expr/trace.txt +14 -0
  35. package/test/corpus/module/moduleHeader1.txt +23 -0
  36. package/test/corpus/module/moduleHeader2.txt +29 -0
  37. package/test/corpus/module/moduleHeader3.error.txt +23 -0
  38. package/test/corpus/module/moduleHeader4.txt +34 -0
  39. package/test/corpus/module/moduleHeader5.txt +16 -0
  40. package/test/corpus/module/moduleHeader6.txt +17 -0
  41. package/test/corpus/number/underscores.txt +99 -0
  42. package/test/corpus/object/objectAmendChain.txt +38 -0
  43. package/test/corpus/object/objectElementsWithParens.txt +51 -0
  44. package/test/corpus/object/objectGenerator.txt +53 -0
  45. package/test/corpus/object/objectMember.txt +129 -0
  46. package/test/corpus/object/objectMemberPredicate.txt +41 -0
  47. package/test/corpus/object/objectSpread.txt +51 -0
  48. package/test/corpus/object/objectWhenGenerator.txt +71 -0
  49. package/test/corpus/string/customStringDelimiters.txt +397 -0
  50. package/test/corpus/string/missingDelimiter.txt +28 -0
  51. package/test/corpus/string/multiline.txt +39 -0
  52. package/test/corpus/string/multilineInterpolation.txt +102 -0
  53. package/test/corpus/string/simple.txt +13 -0
  54. package/test/corpus/string/singleLineEscapes.txt +64 -0
  55. package/test/corpus/string/singleLineInterpolation.txt +94 -0
  56. package/test/corpus/string/stringWithTwoSlashes.txt +20 -0
  57. package/README.adoc +0 -61
  58. package/src/synctests.ts +0 -44
package/src/scanner.c CHANGED
@@ -2,6 +2,8 @@
2
2
  #include <stdio.h>
3
3
 
4
4
  enum TokenType {
5
+ // sequence of "normal" characters in a single line string without a pound sign
6
+ SL_STRING_CHARS,
5
7
  // sequence of "normal" characters in single line string with one pound sign
6
8
  SL1_STRING_CHARS,
7
9
  // sequence of "normal" characters in single line string with two pound signs
@@ -28,10 +30,10 @@ enum TokenType {
28
30
  ML5_STRING_CHARS,
29
31
  // sequence of "normal" characters in multiline string with six pound signs
30
32
  ML6_STRING_CHARS,
31
- // `[` as used in subscript expressions
32
- OPEN_SQUARE_BRACKET,
33
- // `[` as used in object entries
34
- OPEN_ENTRY_BRACKET,
33
+ // '[' at the start of a subscript
34
+ OPEN_SUBSCRIPT_BRACKET,
35
+ // '(' at the start of a method call, or a type constraint
36
+ OPEN_ARGUMENT_PAREN,
35
37
  };
36
38
 
37
39
  void *tree_sitter_pkl_external_scanner_create() { return NULL; }
@@ -41,6 +43,25 @@ unsigned tree_sitter_pkl_external_scanner_serialize(void *p, char *buffer) { ret
41
43
  void tree_sitter_pkl_external_scanner_deserialize(void *p, const char *b, unsigned n) {}
42
44
 
43
45
  static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
46
+ static void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
47
+
48
+ static bool parse_sl_string_chars(TSLexer *lexer) {
49
+ bool has_content = false;
50
+ while (true) {
51
+ switch (lexer->lookahead) {
52
+ case '"':
53
+ case '\\':
54
+ return has_content;
55
+ case '\n':
56
+ case '\r':
57
+ case 0:
58
+ return has_content;
59
+ default:
60
+ has_content = true;
61
+ advance(lexer);
62
+ }
63
+ }
64
+ }
44
65
 
45
66
  static bool parse_slx_string_chars(TSLexer *lexer, int num_pounds) {
46
67
  bool has_content = false;
@@ -193,30 +214,38 @@ static bool parse_mlx_string_chars(TSLexer *lexer, int num_pounds) {
193
214
  }
194
215
  }
195
216
 
196
- bool parse_square_bracket_variant(TSLexer *lexer, bool open_square_bracket, bool open_entry_bracket) {
197
- while (
198
- lexer->lookahead == ' ' ||
199
- lexer->lookahead == '\t' ||
200
- (open_entry_bracket && (
201
- lexer->lookahead == ';' ||
202
- lexer->lookahead == '\n'
203
- ))
204
- ) {
205
- open_square_bracket = open_square_bracket && (lexer->lookahead != '\n' && lexer->lookahead != ';');
206
- lexer->advance(lexer, true);
217
+ static bool parse_open_subscript_or_argument(TSLexer *lexer, bool open_subscript_bracket, bool open_argument_paren) {
218
+ if (lexer->eof(lexer)) {
219
+ return false;
207
220
  }
208
- if (lexer->lookahead == '[') {
209
- lexer->result_symbol = open_square_bracket ? OPEN_SQUARE_BRACKET : OPEN_ENTRY_BRACKET;
210
- lexer->advance(lexer, false);
211
- if (lexer->lookahead != '[') {
212
- lexer->mark_end(lexer);
213
- return true;
221
+ while (true) {
222
+ switch (lexer->lookahead) {
223
+ case ' ':
224
+ case '\t':
225
+ case '\r':
226
+ case '\f':
227
+ skip(lexer);
228
+ break;
229
+ case '[':
230
+ if (open_subscript_bracket) {
231
+ advance(lexer);
232
+ lexer->result_symbol = OPEN_SUBSCRIPT_BRACKET;
233
+ return true;
234
+ }
235
+ case '(':
236
+ if (open_argument_paren) {
237
+ advance(lexer);
238
+ lexer->result_symbol = OPEN_ARGUMENT_PAREN;
239
+ return true;
240
+ }
241
+ default:
242
+ return false;
214
243
  }
215
244
  }
216
- return false;
217
245
  }
218
246
 
219
247
  bool tree_sitter_pkl_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
248
+ bool sl = valid_symbols[SL_STRING_CHARS];
220
249
  bool sl1 = valid_symbols[SL1_STRING_CHARS];
221
250
  bool sl2 = valid_symbols[SL2_STRING_CHARS];
222
251
  bool sl3 = valid_symbols[SL3_STRING_CHARS];
@@ -230,14 +259,17 @@ bool tree_sitter_pkl_external_scanner_scan(void *payload, TSLexer *lexer, const
230
259
  bool ml4 = valid_symbols[ML4_STRING_CHARS];
231
260
  bool ml5 = valid_symbols[ML5_STRING_CHARS];
232
261
  bool ml6 = valid_symbols[ML6_STRING_CHARS];
233
- bool osb = valid_symbols[OPEN_SQUARE_BRACKET];
234
- bool oeb = valid_symbols[OPEN_ENTRY_BRACKET];
262
+ bool osb = valid_symbols[OPEN_SUBSCRIPT_BRACKET];
263
+ bool oap = valid_symbols[OPEN_ARGUMENT_PAREN];
235
264
 
236
- if (sl1 && sl2 && sl3 && sl4 && sl5 && sl6 && ml && ml1 && ml2 && ml3 && ml4 && ml5 && ml6 && osb && oeb) {
265
+ if (sl && sl1 && sl2 && sl3 && sl4 && sl5 && sl6 && ml && ml1 && ml2 && ml3 && ml4 && ml5 && ml6 && osb && oap) {
237
266
  // error recovery mode -> don't match any string chars
238
267
  return false;
239
268
  }
240
269
 
270
+ if (sl) {
271
+ return parse_sl_string_chars(lexer);
272
+ }
241
273
  if (ml) {
242
274
  return parse_ml_string_chars(lexer);
243
275
  }
@@ -277,9 +309,9 @@ bool tree_sitter_pkl_external_scanner_scan(void *payload, TSLexer *lexer, const
277
309
  if (ml6) {
278
310
  return parse_mlx_string_chars(lexer, 6);
279
311
  }
280
- if (osb || oeb) {
281
- return parse_square_bracket_variant(lexer, osb, oeb);
312
+ // both can possibly be true
313
+ if (osb || oap) {
314
+ return parse_open_subscript_or_argument(lexer, osb, oap);
282
315
  }
283
316
  return false;
284
317
  }
285
-
@@ -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);
16
+ extern void *(*ts_current_calloc)(size_t, size_t);
17
+ extern void *(*ts_current_realloc)(void *, size_t);
18
+ extern void (*ts_current_free)(void *);
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,290 @@
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(disable : 4101)
18
+ #elif defined(__GNUC__) || defined(__clang__)
19
+ #pragma GCC diagnostic push
20
+ #pragma GCC diagnostic ignored "-Wunused-variable"
21
+ #endif
22
+
23
+ #define Array(T) \
24
+ struct { \
25
+ T *contents; \
26
+ uint32_t size; \
27
+ uint32_t capacity; \
28
+ }
29
+
30
+ /// Initialize an array.
31
+ #define array_init(self) \
32
+ ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
33
+
34
+ /// Create an empty array.
35
+ #define array_new() \
36
+ { NULL, 0, 0 }
37
+
38
+ /// Get a pointer to the element at a given `index` in the array.
39
+ #define array_get(self, _index) \
40
+ (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
41
+
42
+ /// Get a pointer to the first element in the array.
43
+ #define array_front(self) array_get(self, 0)
44
+
45
+ /// Get a pointer to the last element in the array.
46
+ #define array_back(self) array_get(self, (self)->size - 1)
47
+
48
+ /// Clear the array, setting its size to zero. Note that this does not free any
49
+ /// memory allocated for the array's contents.
50
+ #define array_clear(self) ((self)->size = 0)
51
+
52
+ /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
53
+ /// less than the array's current capacity, this function has no effect.
54
+ #define array_reserve(self, new_capacity) \
55
+ _array__reserve((Array *)(self), array_elem_size(self), new_capacity)
56
+
57
+ /// Free any memory allocated for this array. Note that this does not free any
58
+ /// memory allocated for the array's contents.
59
+ #define array_delete(self) _array__delete((Array *)(self))
60
+
61
+ /// Push a new `element` onto the end of the array.
62
+ #define array_push(self, element) \
63
+ (_array__grow((Array *)(self), 1, array_elem_size(self)), \
64
+ (self)->contents[(self)->size++] = (element))
65
+
66
+ /// Increase the array's size by `count` elements.
67
+ /// New elements are zero-initialized.
68
+ #define array_grow_by(self, count) \
69
+ do { \
70
+ if ((count) == 0) break; \
71
+ _array__grow((Array *)(self), count, array_elem_size(self)); \
72
+ memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
73
+ (self)->size += (count); \
74
+ } while (0)
75
+
76
+ /// Append all elements from one array to the end of another.
77
+ #define array_push_all(self, other) \
78
+ array_extend((self), (other)->size, (other)->contents)
79
+
80
+ /// Append `count` elements to the end of the array, reading their values from the
81
+ /// `contents` pointer.
82
+ #define array_extend(self, count, contents) \
83
+ _array__splice( \
84
+ (Array *)(self), array_elem_size(self), (self)->size, \
85
+ 0, count, contents \
86
+ )
87
+
88
+ /// Remove `old_count` elements from the array starting at the given `index`. At
89
+ /// the same index, insert `new_count` new elements, reading their values from the
90
+ /// `new_contents` pointer.
91
+ #define array_splice(self, _index, old_count, new_count, new_contents) \
92
+ _array__splice( \
93
+ (Array *)(self), array_elem_size(self), _index, \
94
+ old_count, new_count, new_contents \
95
+ )
96
+
97
+ /// Insert one `element` into the array at the given `index`.
98
+ #define array_insert(self, _index, element) \
99
+ _array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
100
+
101
+ /// Remove one element from the array at the given `index`.
102
+ #define array_erase(self, _index) \
103
+ _array__erase((Array *)(self), array_elem_size(self), _index)
104
+
105
+ /// Pop the last element off the array, returning the element by value.
106
+ #define array_pop(self) ((self)->contents[--(self)->size])
107
+
108
+ /// Assign the contents of one array to another, reallocating if necessary.
109
+ #define array_assign(self, other) \
110
+ _array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
111
+
112
+ /// Swap one array with another
113
+ #define array_swap(self, other) \
114
+ _array__swap((Array *)(self), (Array *)(other))
115
+
116
+ /// Get the size of the array contents
117
+ #define array_elem_size(self) (sizeof *(self)->contents)
118
+
119
+ /// Search a sorted array for a given `needle` value, using the given `compare`
120
+ /// callback to determine the order.
121
+ ///
122
+ /// If an existing element is found to be equal to `needle`, then the `index`
123
+ /// out-parameter is set to the existing value's index, and the `exists`
124
+ /// out-parameter is set to true. Otherwise, `index` is set to an index where
125
+ /// `needle` should be inserted in order to preserve the sorting, and `exists`
126
+ /// is set to false.
127
+ #define array_search_sorted_with(self, compare, needle, _index, _exists) \
128
+ _array__search_sorted(self, 0, compare, , needle, _index, _exists)
129
+
130
+ /// Search a sorted array for a given `needle` value, using integer comparisons
131
+ /// of a given struct field (specified with a leading dot) to determine the order.
132
+ ///
133
+ /// See also `array_search_sorted_with`.
134
+ #define array_search_sorted_by(self, field, needle, _index, _exists) \
135
+ _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
136
+
137
+ /// Insert a given `value` into a sorted array, using the given `compare`
138
+ /// callback to determine the order.
139
+ #define array_insert_sorted_with(self, compare, value) \
140
+ do { \
141
+ unsigned _index, _exists; \
142
+ array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
143
+ if (!_exists) array_insert(self, _index, value); \
144
+ } while (0)
145
+
146
+ /// Insert a given `value` into a sorted array, using integer comparisons of
147
+ /// a given struct field (specified with a leading dot) to determine the order.
148
+ ///
149
+ /// See also `array_search_sorted_by`.
150
+ #define array_insert_sorted_by(self, field, value) \
151
+ do { \
152
+ unsigned _index, _exists; \
153
+ array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
154
+ if (!_exists) array_insert(self, _index, value); \
155
+ } while (0)
156
+
157
+ // Private
158
+
159
+ typedef Array(void) Array;
160
+
161
+ /// This is not what you're looking for, see `array_delete`.
162
+ static inline void _array__delete(Array *self) {
163
+ if (self->contents) {
164
+ ts_free(self->contents);
165
+ self->contents = NULL;
166
+ self->size = 0;
167
+ self->capacity = 0;
168
+ }
169
+ }
170
+
171
+ /// This is not what you're looking for, see `array_erase`.
172
+ static inline void _array__erase(Array *self, size_t element_size,
173
+ uint32_t index) {
174
+ assert(index < self->size);
175
+ char *contents = (char *)self->contents;
176
+ memmove(contents + index * element_size, contents + (index + 1) * element_size,
177
+ (self->size - index - 1) * element_size);
178
+ self->size--;
179
+ }
180
+
181
+ /// This is not what you're looking for, see `array_reserve`.
182
+ static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
183
+ if (new_capacity > self->capacity) {
184
+ if (self->contents) {
185
+ self->contents = ts_realloc(self->contents, new_capacity * element_size);
186
+ } else {
187
+ self->contents = ts_malloc(new_capacity * element_size);
188
+ }
189
+ self->capacity = new_capacity;
190
+ }
191
+ }
192
+
193
+ /// This is not what you're looking for, see `array_assign`.
194
+ static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
195
+ _array__reserve(self, element_size, other->size);
196
+ self->size = other->size;
197
+ memcpy(self->contents, other->contents, self->size * element_size);
198
+ }
199
+
200
+ /// This is not what you're looking for, see `array_swap`.
201
+ static inline void _array__swap(Array *self, Array *other) {
202
+ Array swap = *other;
203
+ *other = *self;
204
+ *self = swap;
205
+ }
206
+
207
+ /// This is not what you're looking for, see `array_push` or `array_grow_by`.
208
+ static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
209
+ uint32_t new_size = self->size + count;
210
+ if (new_size > self->capacity) {
211
+ uint32_t new_capacity = self->capacity * 2;
212
+ if (new_capacity < 8) new_capacity = 8;
213
+ if (new_capacity < new_size) new_capacity = new_size;
214
+ _array__reserve(self, element_size, new_capacity);
215
+ }
216
+ }
217
+
218
+ /// This is not what you're looking for, see `array_splice`.
219
+ static inline void _array__splice(Array *self, size_t element_size,
220
+ uint32_t index, uint32_t old_count,
221
+ uint32_t new_count, const void *elements) {
222
+ uint32_t new_size = self->size + new_count - old_count;
223
+ uint32_t old_end = index + old_count;
224
+ uint32_t new_end = index + new_count;
225
+ assert(old_end <= self->size);
226
+
227
+ _array__reserve(self, element_size, new_size);
228
+
229
+ char *contents = (char *)self->contents;
230
+ if (self->size > old_end) {
231
+ memmove(
232
+ contents + new_end * element_size,
233
+ contents + old_end * element_size,
234
+ (self->size - old_end) * element_size
235
+ );
236
+ }
237
+ if (new_count > 0) {
238
+ if (elements) {
239
+ memcpy(
240
+ (contents + index * element_size),
241
+ elements,
242
+ new_count * element_size
243
+ );
244
+ } else {
245
+ memset(
246
+ (contents + index * element_size),
247
+ 0,
248
+ new_count * element_size
249
+ );
250
+ }
251
+ }
252
+ self->size += new_count - old_count;
253
+ }
254
+
255
+ /// A binary search routine, based on Rust's `std::slice::binary_search_by`.
256
+ /// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
257
+ #define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
258
+ do { \
259
+ *(_index) = start; \
260
+ *(_exists) = false; \
261
+ uint32_t size = (self)->size - *(_index); \
262
+ if (size == 0) break; \
263
+ int comparison; \
264
+ while (size > 1) { \
265
+ uint32_t half_size = size / 2; \
266
+ uint32_t mid_index = *(_index) + half_size; \
267
+ comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
268
+ if (comparison <= 0) *(_index) = mid_index; \
269
+ size -= half_size; \
270
+ } \
271
+ comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
272
+ if (comparison == 0) *(_exists) = true; \
273
+ else if (comparison < 0) *(_index) += 1; \
274
+ } while (0)
275
+
276
+ /// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
277
+ /// parameter by reference in order to work with the generic sorting function above.
278
+ #define _compare_int(a, b) ((int)*(a) - (int)(b))
279
+
280
+ #ifdef _MSC_VER
281
+ #pragma warning(default : 4101)
282
+ #elif defined(__GNUC__) || defined(__clang__)
283
+ #pragma GCC diagnostic pop
284
+ #endif
285
+
286
+ #ifdef __cplusplus
287
+ }
288
+ #endif
289
+
290
+ #endif // TREE_SITTER_ARRAY_H_
@@ -13,9 +13,8 @@ extern "C" {
13
13
  #define ts_builtin_sym_end 0
14
14
  #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
15
15
 
16
- typedef uint16_t TSStateId;
17
-
18
16
  #ifndef TREE_SITTER_API_H_
17
+ typedef uint16_t TSStateId;
19
18
  typedef uint16_t TSSymbol;
20
19
  typedef uint16_t TSFieldId;
21
20
  typedef struct TSLanguage TSLanguage;
@@ -87,6 +86,11 @@ typedef union {
87
86
  } entry;
88
87
  } TSParseActionEntry;
89
88
 
89
+ typedef struct {
90
+ int32_t start;
91
+ int32_t end;
92
+ } TSCharacterRange;
93
+
90
94
  struct TSLanguage {
91
95
  uint32_t version;
92
96
  uint32_t symbol_count;
@@ -126,13 +130,38 @@ struct TSLanguage {
126
130
  const TSStateId *primary_state_ids;
127
131
  };
128
132
 
133
+ static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
134
+ uint32_t index = 0;
135
+ uint32_t size = len - index;
136
+ while (size > 1) {
137
+ uint32_t half_size = size / 2;
138
+ uint32_t mid_index = index + half_size;
139
+ TSCharacterRange *range = &ranges[mid_index];
140
+ if (lookahead >= range->start && lookahead <= range->end) {
141
+ return true;
142
+ } else if (lookahead > range->end) {
143
+ index = mid_index;
144
+ }
145
+ size -= half_size;
146
+ }
147
+ TSCharacterRange *range = &ranges[index];
148
+ return (lookahead >= range->start && lookahead <= range->end);
149
+ }
150
+
129
151
  /*
130
152
  * Lexer Macros
131
153
  */
132
154
 
155
+ #ifdef _MSC_VER
156
+ #define UNUSED __pragma(warning(suppress : 4101))
157
+ #else
158
+ #define UNUSED __attribute__((unused))
159
+ #endif
160
+
133
161
  #define START_LEXER() \
134
162
  bool result = false; \
135
163
  bool skip = false; \
164
+ UNUSED \
136
165
  bool eof = false; \
137
166
  int32_t lookahead; \
138
167
  goto start; \
@@ -148,6 +177,17 @@ struct TSLanguage {
148
177
  goto next_state; \
149
178
  }
150
179
 
180
+ #define ADVANCE_MAP(...) \
181
+ { \
182
+ static const uint16_t map[] = { __VA_ARGS__ }; \
183
+ for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
184
+ if (map[i] == lookahead) { \
185
+ state = map[i + 1]; \
186
+ goto next_state; \
187
+ } \
188
+ } \
189
+ }
190
+
151
191
  #define SKIP(state_value) \
152
192
  { \
153
193
  skip = true; \
@@ -166,7 +206,7 @@ struct TSLanguage {
166
206
  * Parse Table Macros
167
207
  */
168
208
 
169
- #define SMALL_STATE(id) id - LARGE_STATE_COUNT
209
+ #define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
170
210
 
171
211
  #define STATE(id) id
172
212
 
@@ -176,7 +216,7 @@ struct TSLanguage {
176
216
  {{ \
177
217
  .shift = { \
178
218
  .type = TSParseActionTypeShift, \
179
- .state = state_value \
219
+ .state = (state_value) \
180
220
  } \
181
221
  }}
182
222
 
@@ -184,7 +224,7 @@ struct TSLanguage {
184
224
  {{ \
185
225
  .shift = { \
186
226
  .type = TSParseActionTypeShift, \
187
- .state = state_value, \
227
+ .state = (state_value), \
188
228
  .repetition = true \
189
229
  } \
190
230
  }}
@@ -197,14 +237,15 @@ struct TSLanguage {
197
237
  } \
198
238
  }}
199
239
 
200
- #define REDUCE(symbol_val, child_count_val, ...) \
201
- {{ \
202
- .reduce = { \
203
- .type = TSParseActionTypeReduce, \
204
- .symbol = symbol_val, \
205
- .child_count = child_count_val, \
206
- __VA_ARGS__ \
207
- }, \
240
+ #define REDUCE(symbol_name, children, precedence, prod_id) \
241
+ {{ \
242
+ .reduce = { \
243
+ .type = TSParseActionTypeReduce, \
244
+ .symbol = symbol_name, \
245
+ .child_count = children, \
246
+ .dynamic_precedence = precedence, \
247
+ .production_id = prod_id \
248
+ }, \
208
249
  }}
209
250
 
210
251
  #define RECOVER() \