herb 0.7.2-x86_64-darwin → 0.7.3-x86_64-darwin

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 (130) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +2 -0
  3. data/README.md +1 -1
  4. data/Rakefile +46 -1
  5. data/config.yml +714 -0
  6. data/ext/herb/extconf.rb +2 -1
  7. data/ext/herb/nodes.c +1 -1
  8. data/herb.gemspec +3 -0
  9. data/lib/herb/3.0/herb.bundle +0 -0
  10. data/lib/herb/3.1/herb.bundle +0 -0
  11. data/lib/herb/3.2/herb.bundle +0 -0
  12. data/lib/herb/3.3/herb.bundle +0 -0
  13. data/lib/herb/3.4/herb.bundle +0 -0
  14. data/lib/herb/version.rb +1 -1
  15. data/src/analyze.c +5 -9
  16. data/src/analyze_helpers.c +17 -6
  17. data/src/include/pretty_print.h +1 -1
  18. data/src/include/version.h +1 -1
  19. data/src/parser.c +1 -0
  20. data/src/pretty_print.c +1 -1
  21. data/templates/ext/herb/error_helpers.c.erb +85 -0
  22. data/templates/ext/herb/error_helpers.h.erb +12 -0
  23. data/templates/ext/herb/nodes.c.erb +90 -0
  24. data/templates/ext/herb/nodes.h.erb +9 -0
  25. data/templates/javascript/packages/core/src/errors.ts.erb +193 -0
  26. data/templates/javascript/packages/core/src/node-type-guards.ts.erb +325 -0
  27. data/templates/javascript/packages/core/src/nodes.ts.erb +414 -0
  28. data/templates/javascript/packages/core/src/visitor.ts.erb +29 -0
  29. data/templates/javascript/packages/node/extension/error_helpers.cpp.erb +113 -0
  30. data/templates/javascript/packages/node/extension/error_helpers.h.erb +17 -0
  31. data/templates/javascript/packages/node/extension/nodes.cpp.erb +111 -0
  32. data/templates/javascript/packages/node/extension/nodes.h.erb +17 -0
  33. data/templates/lib/herb/ast/nodes.rb.erb +117 -0
  34. data/templates/lib/herb/errors.rb.erb +106 -0
  35. data/templates/lib/herb/visitor.rb.erb +28 -0
  36. data/templates/sig/serialized_ast_errors.rbs.erb +10 -0
  37. data/templates/sig/serialized_ast_nodes.rbs.erb +10 -0
  38. data/templates/src/ast_nodes.c.erb +145 -0
  39. data/templates/src/ast_pretty_print.c.erb +97 -0
  40. data/templates/src/errors.c.erb +245 -0
  41. data/templates/src/include/ast_nodes.h.erb +46 -0
  42. data/templates/src/include/ast_pretty_print.h.erb +14 -0
  43. data/templates/src/include/errors.h.erb +58 -0
  44. data/templates/src/visitor.c.erb +47 -0
  45. data/templates/template.rb +406 -0
  46. data/templates/wasm/error_helpers.cpp.erb +93 -0
  47. data/templates/wasm/error_helpers.h.erb +15 -0
  48. data/templates/wasm/nodes.cpp.erb +79 -0
  49. data/templates/wasm/nodes.h.erb +15 -0
  50. data/vendor/prism/Rakefile +75 -0
  51. data/vendor/prism/config.yml +4713 -0
  52. data/vendor/prism/include/prism/ast.h +8190 -0
  53. data/vendor/prism/include/prism/defines.h +260 -0
  54. data/vendor/prism/include/prism/diagnostic.h +455 -0
  55. data/vendor/prism/include/prism/encoding.h +283 -0
  56. data/vendor/prism/include/prism/node.h +129 -0
  57. data/vendor/prism/include/prism/options.h +482 -0
  58. data/vendor/prism/include/prism/pack.h +163 -0
  59. data/vendor/prism/include/prism/parser.h +933 -0
  60. data/vendor/prism/include/prism/prettyprint.h +34 -0
  61. data/vendor/prism/include/prism/regexp.h +43 -0
  62. data/vendor/prism/include/prism/static_literals.h +121 -0
  63. data/vendor/prism/include/prism/util/pm_buffer.h +236 -0
  64. data/vendor/prism/include/prism/util/pm_char.h +204 -0
  65. data/vendor/prism/include/prism/util/pm_constant_pool.h +218 -0
  66. data/vendor/prism/include/prism/util/pm_integer.h +130 -0
  67. data/vendor/prism/include/prism/util/pm_list.h +103 -0
  68. data/vendor/prism/include/prism/util/pm_memchr.h +29 -0
  69. data/vendor/prism/include/prism/util/pm_newline_list.h +113 -0
  70. data/vendor/prism/include/prism/util/pm_string.h +200 -0
  71. data/vendor/prism/include/prism/util/pm_strncasecmp.h +32 -0
  72. data/vendor/prism/include/prism/util/pm_strpbrk.h +46 -0
  73. data/vendor/prism/include/prism/version.h +29 -0
  74. data/vendor/prism/include/prism.h +408 -0
  75. data/vendor/prism/src/diagnostic.c +848 -0
  76. data/vendor/prism/src/encoding.c +5235 -0
  77. data/vendor/prism/src/node.c +8676 -0
  78. data/vendor/prism/src/options.c +328 -0
  79. data/vendor/prism/src/pack.c +509 -0
  80. data/vendor/prism/src/prettyprint.c +8941 -0
  81. data/vendor/prism/src/prism.c +23302 -0
  82. data/vendor/prism/src/regexp.c +790 -0
  83. data/vendor/prism/src/serialize.c +2268 -0
  84. data/vendor/prism/src/static_literals.c +617 -0
  85. data/vendor/prism/src/token_type.c +703 -0
  86. data/vendor/prism/src/util/pm_buffer.c +357 -0
  87. data/vendor/prism/src/util/pm_char.c +318 -0
  88. data/vendor/prism/src/util/pm_constant_pool.c +342 -0
  89. data/vendor/prism/src/util/pm_integer.c +670 -0
  90. data/vendor/prism/src/util/pm_list.c +49 -0
  91. data/vendor/prism/src/util/pm_memchr.c +35 -0
  92. data/vendor/prism/src/util/pm_newline_list.c +125 -0
  93. data/vendor/prism/src/util/pm_string.c +383 -0
  94. data/vendor/prism/src/util/pm_strncasecmp.c +36 -0
  95. data/vendor/prism/src/util/pm_strpbrk.c +206 -0
  96. data/vendor/prism/templates/ext/prism/api_node.c.erb +282 -0
  97. data/vendor/prism/templates/include/prism/ast.h.erb +226 -0
  98. data/vendor/prism/templates/include/prism/diagnostic.h.erb +130 -0
  99. data/vendor/prism/templates/java/org/prism/AbstractNodeVisitor.java.erb +22 -0
  100. data/vendor/prism/templates/java/org/prism/Loader.java.erb +434 -0
  101. data/vendor/prism/templates/java/org/prism/Nodes.java.erb +403 -0
  102. data/vendor/prism/templates/javascript/src/deserialize.js.erb +448 -0
  103. data/vendor/prism/templates/javascript/src/nodes.js.erb +197 -0
  104. data/vendor/prism/templates/javascript/src/visitor.js.erb +78 -0
  105. data/vendor/prism/templates/lib/prism/compiler.rb.erb +43 -0
  106. data/vendor/prism/templates/lib/prism/dispatcher.rb.erb +103 -0
  107. data/vendor/prism/templates/lib/prism/dot_visitor.rb.erb +189 -0
  108. data/vendor/prism/templates/lib/prism/dsl.rb.erb +133 -0
  109. data/vendor/prism/templates/lib/prism/inspect_visitor.rb.erb +131 -0
  110. data/vendor/prism/templates/lib/prism/mutation_compiler.rb.erb +19 -0
  111. data/vendor/prism/templates/lib/prism/node.rb.erb +515 -0
  112. data/vendor/prism/templates/lib/prism/reflection.rb.erb +136 -0
  113. data/vendor/prism/templates/lib/prism/serialize.rb.erb +602 -0
  114. data/vendor/prism/templates/lib/prism/visitor.rb.erb +55 -0
  115. data/vendor/prism/templates/rbi/prism/dsl.rbi.erb +68 -0
  116. data/vendor/prism/templates/rbi/prism/node.rbi.erb +164 -0
  117. data/vendor/prism/templates/rbi/prism/visitor.rbi.erb +18 -0
  118. data/vendor/prism/templates/sig/prism/_private/dot_visitor.rbs.erb +45 -0
  119. data/vendor/prism/templates/sig/prism/dsl.rbs.erb +31 -0
  120. data/vendor/prism/templates/sig/prism/mutation_compiler.rbs.erb +7 -0
  121. data/vendor/prism/templates/sig/prism/node.rbs.erb +132 -0
  122. data/vendor/prism/templates/sig/prism/visitor.rbs.erb +17 -0
  123. data/vendor/prism/templates/sig/prism.rbs.erb +89 -0
  124. data/vendor/prism/templates/src/diagnostic.c.erb +523 -0
  125. data/vendor/prism/templates/src/node.c.erb +333 -0
  126. data/vendor/prism/templates/src/prettyprint.c.erb +166 -0
  127. data/vendor/prism/templates/src/serialize.c.erb +406 -0
  128. data/vendor/prism/templates/src/token_type.c.erb +369 -0
  129. data/vendor/prism/templates/template.rb +689 -0
  130. metadata +112 -2
@@ -0,0 +1,342 @@
1
+ #include "prism/util/pm_constant_pool.h"
2
+
3
+ /**
4
+ * Initialize a list of constant ids.
5
+ */
6
+ void
7
+ pm_constant_id_list_init(pm_constant_id_list_t *list) {
8
+ list->ids = NULL;
9
+ list->size = 0;
10
+ list->capacity = 0;
11
+ }
12
+
13
+ /**
14
+ * Initialize a list of constant ids with a given capacity.
15
+ */
16
+ void
17
+ pm_constant_id_list_init_capacity(pm_constant_id_list_t *list, size_t capacity) {
18
+ if (capacity) {
19
+ list->ids = xcalloc(capacity, sizeof(pm_constant_id_t));
20
+ if (list->ids == NULL) abort();
21
+ } else {
22
+ list->ids = NULL;
23
+ }
24
+
25
+ list->size = 0;
26
+ list->capacity = capacity;
27
+ }
28
+
29
+ /**
30
+ * Append a constant id to a list of constant ids. Returns false if any
31
+ * potential reallocations fail.
32
+ */
33
+ bool
34
+ pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id) {
35
+ if (list->size >= list->capacity) {
36
+ list->capacity = list->capacity == 0 ? 8 : list->capacity * 2;
37
+ list->ids = (pm_constant_id_t *) xrealloc(list->ids, sizeof(pm_constant_id_t) * list->capacity);
38
+ if (list->ids == NULL) return false;
39
+ }
40
+
41
+ list->ids[list->size++] = id;
42
+ return true;
43
+ }
44
+
45
+ /**
46
+ * Insert a constant id into a list of constant ids at the specified index.
47
+ */
48
+ void
49
+ pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id) {
50
+ assert(index < list->capacity);
51
+ assert(list->ids[index] == PM_CONSTANT_ID_UNSET);
52
+
53
+ list->ids[index] = id;
54
+ list->size++;
55
+ }
56
+
57
+ /**
58
+ * Checks if the current constant id list includes the given constant id.
59
+ */
60
+ bool
61
+ pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id) {
62
+ for (size_t index = 0; index < list->size; index++) {
63
+ if (list->ids[index] == id) return true;
64
+ }
65
+ return false;
66
+ }
67
+
68
+ /**
69
+ * Free the memory associated with a list of constant ids.
70
+ */
71
+ void
72
+ pm_constant_id_list_free(pm_constant_id_list_t *list) {
73
+ if (list->ids != NULL) {
74
+ xfree(list->ids);
75
+ }
76
+ }
77
+
78
+ /**
79
+ * A relatively simple hash function (djb2) that is used to hash strings. We are
80
+ * optimizing here for simplicity and speed.
81
+ */
82
+ static inline uint32_t
83
+ pm_constant_pool_hash(const uint8_t *start, size_t length) {
84
+ // This is a prime number used as the initial value for the hash function.
85
+ uint32_t value = 5381;
86
+
87
+ for (size_t index = 0; index < length; index++) {
88
+ value = ((value << 5) + value) + start[index];
89
+ }
90
+
91
+ return value;
92
+ }
93
+
94
+ /**
95
+ * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
96
+ */
97
+ static uint32_t
98
+ next_power_of_two(uint32_t v) {
99
+ // Avoid underflow in subtraction on next line.
100
+ if (v == 0) {
101
+ // 1 is the nearest power of 2 to 0 (2^0)
102
+ return 1;
103
+ }
104
+ v--;
105
+ v |= v >> 1;
106
+ v |= v >> 2;
107
+ v |= v >> 4;
108
+ v |= v >> 8;
109
+ v |= v >> 16;
110
+ v++;
111
+ return v;
112
+ }
113
+
114
+ #ifndef NDEBUG
115
+ static bool
116
+ is_power_of_two(uint32_t size) {
117
+ return (size & (size - 1)) == 0;
118
+ }
119
+ #endif
120
+
121
+ /**
122
+ * Resize a constant pool to a given capacity.
123
+ */
124
+ static inline bool
125
+ pm_constant_pool_resize(pm_constant_pool_t *pool) {
126
+ assert(is_power_of_two(pool->capacity));
127
+
128
+ uint32_t next_capacity = pool->capacity * 2;
129
+ if (next_capacity < pool->capacity) return false;
130
+
131
+ const uint32_t mask = next_capacity - 1;
132
+ const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
133
+
134
+ void *next = xcalloc(next_capacity, element_size);
135
+ if (next == NULL) return false;
136
+
137
+ pm_constant_pool_bucket_t *next_buckets = next;
138
+ pm_constant_t *next_constants = (void *)(((char *) next) + next_capacity * sizeof(pm_constant_pool_bucket_t));
139
+
140
+ // For each bucket in the current constant pool, find the index in the
141
+ // next constant pool, and insert it.
142
+ for (uint32_t index = 0; index < pool->capacity; index++) {
143
+ pm_constant_pool_bucket_t *bucket = &pool->buckets[index];
144
+
145
+ // If an id is set on this constant, then we know we have content here.
146
+ // In this case we need to insert it into the next constant pool.
147
+ if (bucket->id != PM_CONSTANT_ID_UNSET) {
148
+ uint32_t next_index = bucket->hash & mask;
149
+
150
+ // This implements linear scanning to find the next available slot
151
+ // in case this index is already taken. We don't need to bother
152
+ // comparing the values since we know that the hash is unique.
153
+ while (next_buckets[next_index].id != PM_CONSTANT_ID_UNSET) {
154
+ next_index = (next_index + 1) & mask;
155
+ }
156
+
157
+ // Here we copy over the entire bucket, which includes the id so
158
+ // that they are consistent between resizes.
159
+ next_buckets[next_index] = *bucket;
160
+ }
161
+ }
162
+
163
+ // The constants are stable with respect to hash table resizes.
164
+ memcpy(next_constants, pool->constants, pool->size * sizeof(pm_constant_t));
165
+
166
+ // pool->constants and pool->buckets are allocated out of the same chunk
167
+ // of memory, with the buckets coming first.
168
+ xfree(pool->buckets);
169
+ pool->constants = next_constants;
170
+ pool->buckets = next_buckets;
171
+ pool->capacity = next_capacity;
172
+ return true;
173
+ }
174
+
175
+ /**
176
+ * Initialize a new constant pool with a given capacity.
177
+ */
178
+ bool
179
+ pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity) {
180
+ const uint32_t maximum = (~((uint32_t) 0));
181
+ if (capacity >= ((maximum / 2) + 1)) return false;
182
+
183
+ capacity = next_power_of_two(capacity);
184
+ const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
185
+ void *memory = xcalloc(capacity, element_size);
186
+ if (memory == NULL) return false;
187
+
188
+ pool->buckets = memory;
189
+ pool->constants = (void *)(((char *)memory) + capacity * sizeof(pm_constant_pool_bucket_t));
190
+ pool->size = 0;
191
+ pool->capacity = capacity;
192
+ return true;
193
+ }
194
+
195
+ /**
196
+ * Return a pointer to the constant indicated by the given constant id.
197
+ */
198
+ pm_constant_t *
199
+ pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id) {
200
+ assert(constant_id != PM_CONSTANT_ID_UNSET && constant_id <= pool->size);
201
+ return &pool->constants[constant_id - 1];
202
+ }
203
+
204
+ /**
205
+ * Find a constant in a constant pool. Returns the id of the constant, or 0 if
206
+ * the constant is not found.
207
+ */
208
+ pm_constant_id_t
209
+ pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
210
+ assert(is_power_of_two(pool->capacity));
211
+ const uint32_t mask = pool->capacity - 1;
212
+
213
+ uint32_t hash = pm_constant_pool_hash(start, length);
214
+ uint32_t index = hash & mask;
215
+ pm_constant_pool_bucket_t *bucket;
216
+
217
+ while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
218
+ pm_constant_t *constant = &pool->constants[bucket->id - 1];
219
+ if ((constant->length == length) && memcmp(constant->start, start, length) == 0) {
220
+ return bucket->id;
221
+ }
222
+
223
+ index = (index + 1) & mask;
224
+ }
225
+
226
+ return PM_CONSTANT_ID_UNSET;
227
+ }
228
+
229
+ /**
230
+ * Insert a constant into a constant pool and return its index in the pool.
231
+ */
232
+ static inline pm_constant_id_t
233
+ pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t length, pm_constant_pool_bucket_type_t type) {
234
+ if (pool->size >= (pool->capacity / 4 * 3)) {
235
+ if (!pm_constant_pool_resize(pool)) return PM_CONSTANT_ID_UNSET;
236
+ }
237
+
238
+ assert(is_power_of_two(pool->capacity));
239
+ const uint32_t mask = pool->capacity - 1;
240
+
241
+ uint32_t hash = pm_constant_pool_hash(start, length);
242
+ uint32_t index = hash & mask;
243
+ pm_constant_pool_bucket_t *bucket;
244
+
245
+ while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
246
+ // If there is a collision, then we need to check if the content is the
247
+ // same as the content we are trying to insert. If it is, then we can
248
+ // return the id of the existing constant.
249
+ pm_constant_t *constant = &pool->constants[bucket->id - 1];
250
+
251
+ if ((constant->length == length) && memcmp(constant->start, start, length) == 0) {
252
+ // Since we have found a match, we need to check if this is
253
+ // attempting to insert a shared or an owned constant. We want to
254
+ // prefer shared constants since they don't require allocations.
255
+ if (type == PM_CONSTANT_POOL_BUCKET_OWNED) {
256
+ // If we're attempting to insert an owned constant and we have
257
+ // an existing constant, then either way we don't want the given
258
+ // memory. Either it's duplicated with the existing constant or
259
+ // it's not necessary because we have a shared version.
260
+ xfree((void *) start);
261
+ } else if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
262
+ // If we're attempting to insert a shared constant and the
263
+ // existing constant is owned, then we can free the owned
264
+ // constant and replace it with the shared constant.
265
+ xfree((void *) constant->start);
266
+ constant->start = start;
267
+ bucket->type = (unsigned int) (PM_CONSTANT_POOL_BUCKET_DEFAULT & 0x3);
268
+ }
269
+
270
+ return bucket->id;
271
+ }
272
+
273
+ index = (index + 1) & mask;
274
+ }
275
+
276
+ // IDs are allocated starting at 1, since the value 0 denotes a non-existent
277
+ // constant.
278
+ uint32_t id = ++pool->size;
279
+ assert(pool->size < ((uint32_t) (1 << 30)));
280
+
281
+ *bucket = (pm_constant_pool_bucket_t) {
282
+ .id = (unsigned int) (id & 0x3fffffff),
283
+ .type = (unsigned int) (type & 0x3),
284
+ .hash = hash
285
+ };
286
+
287
+ pool->constants[id - 1] = (pm_constant_t) {
288
+ .start = start,
289
+ .length = length,
290
+ };
291
+
292
+ return id;
293
+ }
294
+
295
+ /**
296
+ * Insert a constant into a constant pool. Returns the id of the constant, or
297
+ * PM_CONSTANT_ID_UNSET if any potential calls to resize fail.
298
+ */
299
+ pm_constant_id_t
300
+ pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
301
+ return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_DEFAULT);
302
+ }
303
+
304
+ /**
305
+ * Insert a constant into a constant pool from memory that is now owned by the
306
+ * constant pool. Returns the id of the constant, or PM_CONSTANT_ID_UNSET if any
307
+ * potential calls to resize fail.
308
+ */
309
+ pm_constant_id_t
310
+ pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length) {
311
+ return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED);
312
+ }
313
+
314
+ /**
315
+ * Insert a constant into a constant pool from memory that is constant. Returns
316
+ * the id of the constant, or PM_CONSTANT_ID_UNSET if any potential calls to
317
+ * resize fail.
318
+ */
319
+ pm_constant_id_t
320
+ pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
321
+ return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_CONSTANT);
322
+ }
323
+
324
+ /**
325
+ * Free the memory associated with a constant pool.
326
+ */
327
+ void
328
+ pm_constant_pool_free(pm_constant_pool_t *pool) {
329
+ // For each constant in the current constant pool, free the contents if the
330
+ // contents are owned.
331
+ for (uint32_t index = 0; index < pool->capacity; index++) {
332
+ pm_constant_pool_bucket_t *bucket = &pool->buckets[index];
333
+
334
+ // If an id is set on this constant, then we know we have content here.
335
+ if (bucket->id != PM_CONSTANT_ID_UNSET && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
336
+ pm_constant_t *constant = &pool->constants[bucket->id - 1];
337
+ xfree((void *) constant->start);
338
+ }
339
+ }
340
+
341
+ xfree(pool->buckets);
342
+ }