backburn 0.1.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 (185) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/Makefile +50 -0
  4. data/README.md +520 -0
  5. data/docs/cli-and-outputs.md +226 -0
  6. data/docs/technical-spec.md +656 -0
  7. data/exe/backburn +8 -0
  8. data/ext/backburn/extconf.rb +31 -0
  9. data/ext/backburn/prism/COMMIT +1 -0
  10. data/ext/backburn/prism/LICENSE.md +7 -0
  11. data/ext/backburn/prism/Makefile +213 -0
  12. data/ext/backburn/prism/include/prism/arena.h +45 -0
  13. data/ext/backburn/prism/include/prism/arena.hpp +42 -0
  14. data/ext/backburn/prism/include/prism/ast.h +10395 -0
  15. data/ext/backburn/prism/include/prism/ast.hpp +641 -0
  16. data/ext/backburn/prism/include/prism/buffer.h +60 -0
  17. data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
  18. data/ext/backburn/prism/include/prism/comments.h +72 -0
  19. data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
  20. data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
  21. data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
  22. data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
  23. data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
  24. data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
  25. data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
  26. data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
  27. data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
  28. data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
  29. data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
  30. data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
  31. data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
  32. data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
  33. data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
  34. data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
  35. data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
  36. data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
  37. data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
  38. data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
  39. data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
  40. data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
  41. data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
  42. data/ext/backburn/prism/include/prism/errors_format.h +63 -0
  43. data/ext/backburn/prism/include/prism/excludes.h +29 -0
  44. data/ext/backburn/prism/include/prism/integer.h +79 -0
  45. data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
  46. data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
  47. data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
  48. data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
  49. data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
  50. data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
  51. data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
  52. data/ext/backburn/prism/include/prism/internal/char.h +145 -0
  53. data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
  54. data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
  55. data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
  56. data/ext/backburn/prism/include/prism/internal/double.h +19 -0
  57. data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
  58. data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
  59. data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
  60. data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
  61. data/ext/backburn/prism/include/prism/internal/list.h +62 -0
  62. data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
  63. data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
  64. data/ext/backburn/prism/include/prism/internal/node.h +197 -0
  65. data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
  66. data/ext/backburn/prism/include/prism/internal/options.h +248 -0
  67. data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
  68. data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
  69. data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
  70. data/ext/backburn/prism/include/prism/internal/source.h +72 -0
  71. data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
  72. data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
  73. data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
  74. data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
  75. data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
  76. data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
  77. data/ext/backburn/prism/include/prism/json.h +40 -0
  78. data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
  79. data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
  80. data/ext/backburn/prism/include/prism/node.h +139 -0
  81. data/ext/backburn/prism/include/prism/node.hpp +272 -0
  82. data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
  83. data/ext/backburn/prism/include/prism/options.h +393 -0
  84. data/ext/backburn/prism/include/prism/options.hpp +43 -0
  85. data/ext/backburn/prism/include/prism/parser.h +433 -0
  86. data/ext/backburn/prism/include/prism/parser.hpp +274 -0
  87. data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
  88. data/ext/backburn/prism/include/prism/serialize.h +123 -0
  89. data/ext/backburn/prism/include/prism/source.h +163 -0
  90. data/ext/backburn/prism/include/prism/stream.h +36 -0
  91. data/ext/backburn/prism/include/prism/string_query.h +97 -0
  92. data/ext/backburn/prism/include/prism/stringy.h +61 -0
  93. data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
  94. data/ext/backburn/prism/include/prism/version.h +46 -0
  95. data/ext/backburn/prism/include/prism.h +123 -0
  96. data/ext/backburn/prism/include/prism.hpp +24 -0
  97. data/ext/backburn/prism/src/arena.c +200 -0
  98. data/ext/backburn/prism/src/bignum.c +399 -0
  99. data/ext/backburn/prism/src/buffer.c +405 -0
  100. data/ext/backburn/prism/src/char.c +289 -0
  101. data/ext/backburn/prism/src/comments.c +455 -0
  102. data/ext/backburn/prism/src/constant_pool.c +410 -0
  103. data/ext/backburn/prism/src/diagnostic.c +918 -0
  104. data/ext/backburn/prism/src/double.c +394 -0
  105. data/ext/backburn/prism/src/encoding.c +1701 -0
  106. data/ext/backburn/prism/src/errors_format.c +650 -0
  107. data/ext/backburn/prism/src/integer.c +675 -0
  108. data/ext/backburn/prism/src/json.c +5745 -0
  109. data/ext/backburn/prism/src/line_offset_list.c +99 -0
  110. data/ext/backburn/prism/src/list.c +24 -0
  111. data/ext/backburn/prism/src/memchr.c +37 -0
  112. data/ext/backburn/prism/src/node.c +4464 -0
  113. data/ext/backburn/prism/src/options.c +475 -0
  114. data/ext/backburn/prism/src/parser.c +371 -0
  115. data/ext/backburn/prism/src/prettyprint.c +9038 -0
  116. data/ext/backburn/prism/src/prism.c +23615 -0
  117. data/ext/backburn/prism/src/regexp.c +3833 -0
  118. data/ext/backburn/prism/src/serialize.c +2656 -0
  119. data/ext/backburn/prism/src/source.c +525 -0
  120. data/ext/backburn/prism/src/static_literals.c +562 -0
  121. data/ext/backburn/prism/src/string_query.c +195 -0
  122. data/ext/backburn/prism/src/stringy.c +77 -0
  123. data/ext/backburn/prism/src/strncasecmp.c +37 -0
  124. data/ext/backburn/prism/src/strpbrk.c +465 -0
  125. data/ext/backburn/prism/src/tokens.c +724 -0
  126. data/ext/backburn/prism/src/xxh3.c +292 -0
  127. data/lib/backburn/version.rb +7 -0
  128. data/presets/devise.roots +11 -0
  129. data/presets/fast-mcp.roots +5 -0
  130. data/presets/graphql.roots +6 -0
  131. data/presets/phlex.roots +7 -0
  132. data/presets/pundit.roots +3 -0
  133. data/presets/rails-framework.roots +10 -0
  134. data/presets/rails.roots +83 -0
  135. data/presets/sidekiq.roots +4 -0
  136. data/presets/thor.roots +3 -0
  137. data/rails.roots +99 -0
  138. data/scripts/embed-presets.sh +13 -0
  139. data/scripts/version-h.sh +7 -0
  140. data/src/analysis/analysis.c +95 -0
  141. data/src/analysis/analysis.h +24 -0
  142. data/src/analysis/batches.c +127 -0
  143. data/src/analysis/batches.h +17 -0
  144. data/src/analysis/graph.c +238 -0
  145. data/src/analysis/graph.h +34 -0
  146. data/src/analysis/mark.c +300 -0
  147. data/src/analysis/mark.h +21 -0
  148. data/src/analysis/roots.c +229 -0
  149. data/src/analysis/roots.h +29 -0
  150. data/src/cli/cli.c +237 -0
  151. data/src/cli/cli.h +9 -0
  152. data/src/cli/config.c +112 -0
  153. data/src/cli/config.h +14 -0
  154. data/src/cli/options.c +17 -0
  155. data/src/cli/options.h +35 -0
  156. data/src/commands/commands.h +11 -0
  157. data/src/commands/explain.c +255 -0
  158. data/src/commands/gemfile.c +126 -0
  159. data/src/commands/gemfile.h +27 -0
  160. data/src/commands/gems.c +408 -0
  161. data/src/commands/init.c +92 -0
  162. data/src/commands/roots.c +82 -0
  163. data/src/commands/scan.c +91 -0
  164. data/src/core/intern.c +94 -0
  165. data/src/core/intern.h +40 -0
  166. data/src/core/json.c +17 -0
  167. data/src/core/json.h +9 -0
  168. data/src/core/model.c +25 -0
  169. data/src/core/model.h +85 -0
  170. data/src/core/util.c +116 -0
  171. data/src/core/util.h +48 -0
  172. data/src/index/erb.c +40 -0
  173. data/src/index/erb.h +9 -0
  174. data/src/index/haml.c +334 -0
  175. data/src/index/haml.h +10 -0
  176. data/src/index/indexer.c +702 -0
  177. data/src/index/indexer.h +19 -0
  178. data/src/index/walk.c +144 -0
  179. data/src/index/walk.h +14 -0
  180. data/src/main.c +81 -0
  181. data/src/output/baseline.c +101 -0
  182. data/src/output/baseline.h +15 -0
  183. data/src/output/report.c +440 -0
  184. data/src/output/report.h +28 -0
  185. metadata +231 -0
@@ -0,0 +1,405 @@
1
+ #include "prism/internal/buffer.h"
2
+
3
+ #include "prism/compiler/inline.h"
4
+
5
+ #include "prism/internal/allocator.h"
6
+ #include "prism/internal/assert.h"
7
+ #include "prism/internal/char.h"
8
+ #include "prism/internal/oom.h"
9
+
10
+ #include <stdarg.h>
11
+ #include <stdlib.h>
12
+ #include <string.h>
13
+
14
+ /**
15
+ * Initialize a pm_buffer_t with the given capacity.
16
+ */
17
+ void
18
+ pm_buffer_init(pm_buffer_t *buffer, size_t capacity) {
19
+ buffer->length = 0;
20
+ buffer->capacity = capacity;
21
+
22
+ buffer->value = (char *) xmalloc(capacity);
23
+ if (buffer->value == NULL) PRISM_OOM();
24
+ }
25
+
26
+ /**
27
+ * Allocate and initialize a new buffer.
28
+ */
29
+ pm_buffer_t *
30
+ pm_buffer_new(void) {
31
+ pm_buffer_t *buffer = (pm_buffer_t *) xmalloc(sizeof(pm_buffer_t));
32
+ if (buffer == NULL) PRISM_OOM();
33
+
34
+ pm_buffer_init(buffer, 1024);
35
+ return buffer;
36
+ }
37
+
38
+ /**
39
+ * Return the value of the buffer.
40
+ */
41
+ char *
42
+ pm_buffer_value(const pm_buffer_t *buffer) {
43
+ return buffer->value;
44
+ }
45
+
46
+ /**
47
+ * Return the length of the buffer.
48
+ */
49
+ size_t
50
+ pm_buffer_length(const pm_buffer_t *buffer) {
51
+ return buffer->length;
52
+ }
53
+
54
+ /**
55
+ * Append the given amount of space to the buffer. Returns false when there is
56
+ * nothing to write because the length is zero. A zero-initialized buffer has a
57
+ * NULL value until its first allocation, and passing NULL to
58
+ * memcpy/memset/memmove is undefined behavior even for zero bytes, so callers
59
+ * must not write in the zero case.
60
+ *
61
+ * The value and capacity are only updated together once the allocation has
62
+ * succeeded, so the buffer remains valid to free if PRISM_OOM unwinds.
63
+ */
64
+ static PRISM_INLINE bool
65
+ pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
66
+ if (length == 0) return false;
67
+
68
+ size_t next_length = buffer->length + length;
69
+ if (next_length < buffer->length) PRISM_OOM();
70
+
71
+ if (next_length > buffer->capacity) {
72
+ size_t next_capacity = buffer->capacity == 0 ? 1 : buffer->capacity;
73
+
74
+ while (next_length > next_capacity) {
75
+ if (next_capacity > SIZE_MAX / 2) PRISM_OOM();
76
+ next_capacity *= 2;
77
+ }
78
+
79
+ char *next_value = xrealloc_sized(buffer->value, next_capacity, buffer->capacity);
80
+ if (next_value == NULL) PRISM_OOM();
81
+
82
+ buffer->value = next_value;
83
+ buffer->capacity = next_capacity;
84
+ }
85
+
86
+ buffer->length = next_length;
87
+ return true;
88
+ }
89
+
90
+ /**
91
+ * Append a generic pointer to memory to the buffer.
92
+ */
93
+ static PRISM_INLINE void
94
+ pm_buffer_append(pm_buffer_t *buffer, const void *source, size_t length) {
95
+ size_t cursor = buffer->length;
96
+ if (pm_buffer_append_length(buffer, length)) {
97
+ memcpy(buffer->value + cursor, source, length);
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Append the given amount of space as zeroes to the buffer.
103
+ */
104
+ void
105
+ pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length) {
106
+ size_t cursor = buffer->length;
107
+ if (pm_buffer_append_length(buffer, length)) {
108
+ memset(buffer->value + cursor, 0, length);
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Append the given byte to the buffer the given number of times.
114
+ */
115
+ void
116
+ pm_buffer_append_repeated(pm_buffer_t *buffer, uint8_t value, size_t length) {
117
+ size_t cursor = buffer->length;
118
+ if (pm_buffer_append_length(buffer, length)) {
119
+ memset(buffer->value + cursor, value, length);
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Append a formatted string to the buffer.
125
+ */
126
+ void
127
+ pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) {
128
+ va_list arguments;
129
+ va_start(arguments, format);
130
+ int result = vsnprintf(NULL, 0, format, arguments);
131
+ va_end(arguments);
132
+
133
+ if (result < 0) return;
134
+ size_t length = (size_t) (result + 1);
135
+
136
+ size_t cursor = buffer->length;
137
+ if (pm_buffer_append_length(buffer, length)) {
138
+ va_start(arguments, format);
139
+ vsnprintf(buffer->value + cursor, length, format, arguments);
140
+ va_end(arguments);
141
+ buffer->length--;
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Append a string to the buffer.
147
+ */
148
+ void
149
+ pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length) {
150
+ pm_buffer_append(buffer, value, length);
151
+ }
152
+
153
+ /**
154
+ * Append a list of bytes to the buffer.
155
+ */
156
+ void
157
+ pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length) {
158
+ pm_buffer_append(buffer, (const char *) value, length);
159
+ }
160
+
161
+ /**
162
+ * Append a single byte to the buffer.
163
+ */
164
+ void
165
+ pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value) {
166
+ const void *source = &value;
167
+ pm_buffer_append(buffer, source, sizeof(uint8_t));
168
+ }
169
+
170
+ /**
171
+ * Append a 32-bit unsigned integer to the buffer as a variable-length integer.
172
+ */
173
+ void
174
+ pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value) {
175
+ if (value < 128) {
176
+ pm_buffer_append_byte(buffer, (uint8_t) value);
177
+ } else {
178
+ uint32_t n = value;
179
+ while (n >= 128) {
180
+ pm_buffer_append_byte(buffer, (uint8_t) (n | 128));
181
+ n >>= 7;
182
+ }
183
+ pm_buffer_append_byte(buffer, (uint8_t) n);
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Append a 32-bit signed integer to the buffer as a variable-length integer.
189
+ */
190
+ void
191
+ pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value) {
192
+ uint32_t unsigned_int = ((uint32_t)(value) << 1) ^ ((uint32_t)(value >> 31));
193
+ pm_buffer_append_varuint(buffer, unsigned_int);
194
+ }
195
+
196
+ /**
197
+ * Append a double to the buffer.
198
+ */
199
+ void
200
+ pm_buffer_append_double(pm_buffer_t *buffer, double value) {
201
+ const void *source = &value;
202
+ pm_buffer_append(buffer, source, sizeof(double));
203
+ }
204
+
205
+ /**
206
+ * Append a 64-bit unsigned integer to the buffer in native endian order.
207
+ */
208
+ void
209
+ pm_buffer_append_uint64(pm_buffer_t *buffer, uint64_t value) {
210
+ const void *source = &value;
211
+ pm_buffer_append(buffer, source, sizeof(uint64_t));
212
+ }
213
+
214
+ /**
215
+ * Append a unicode codepoint to the buffer.
216
+ */
217
+ bool
218
+ pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value) {
219
+ if (value <= 0x7F) {
220
+ pm_buffer_append_byte(buffer, (uint8_t) value); // 0xxxxxxx
221
+ return true;
222
+ } else if (value <= 0x7FF) {
223
+ uint8_t bytes[] = {
224
+ (uint8_t) (0xC0 | ((value >> 6) & 0x3F)), // 110xxxxx
225
+ (uint8_t) (0x80 | (value & 0x3F)) // 10xxxxxx
226
+ };
227
+
228
+ pm_buffer_append_bytes(buffer, bytes, 2);
229
+ return true;
230
+ } else if (value <= 0xFFFF) {
231
+ uint8_t bytes[] = {
232
+ (uint8_t) (0xE0 | ((value >> 12) & 0x3F)), // 1110xxxx
233
+ (uint8_t) (0x80 | ((value >> 6) & 0x3F)), // 10xxxxxx
234
+ (uint8_t) (0x80 | (value & 0x3F)) // 10xxxxxx
235
+ };
236
+
237
+ pm_buffer_append_bytes(buffer, bytes, 3);
238
+ return true;
239
+ } else if (value <= 0x10FFFF) {
240
+ uint8_t bytes[] = {
241
+ (uint8_t) (0xF0 | ((value >> 18) & 0x3F)), // 11110xxx
242
+ (uint8_t) (0x80 | ((value >> 12) & 0x3F)), // 10xxxxxx
243
+ (uint8_t) (0x80 | ((value >> 6) & 0x3F)), // 10xxxxxx
244
+ (uint8_t) (0x80 | (value & 0x3F)) // 10xxxxxx
245
+ };
246
+
247
+ pm_buffer_append_bytes(buffer, bytes, 4);
248
+ return true;
249
+ } else {
250
+ return false;
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Append a slice of source code to the buffer.
256
+ */
257
+ void
258
+ pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping) {
259
+ for (size_t index = 0; index < length; index++) {
260
+ const uint8_t byte = source[index];
261
+
262
+ if ((byte <= 0x06) || (byte >= 0x0E && byte <= 0x1F) || (byte >= 0x7F)) {
263
+ if (escaping == PM_BUFFER_ESCAPING_RUBY) {
264
+ pm_buffer_append_format(buffer, "\\x%02X", byte);
265
+ } else {
266
+ pm_buffer_append_format(buffer, "\\u%04X", byte);
267
+ }
268
+ } else {
269
+ switch (byte) {
270
+ case '\a':
271
+ if (escaping == PM_BUFFER_ESCAPING_RUBY) {
272
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\a");
273
+ } else {
274
+ pm_buffer_append_format(buffer, "\\u%04X", byte);
275
+ }
276
+ break;
277
+ case '\b':
278
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\b");
279
+ break;
280
+ case '\t':
281
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\t");
282
+ break;
283
+ case '\n':
284
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\n");
285
+ break;
286
+ case '\v':
287
+ if (escaping == PM_BUFFER_ESCAPING_RUBY) {
288
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\v");
289
+ } else {
290
+ pm_buffer_append_format(buffer, "\\u%04X", byte);
291
+ }
292
+ break;
293
+ case '\f':
294
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\f");
295
+ break;
296
+ case '\r':
297
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\r");
298
+ break;
299
+ case '"':
300
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\\"");
301
+ break;
302
+ case '#': {
303
+ if (escaping == PM_BUFFER_ESCAPING_RUBY && index + 1 < length) {
304
+ const uint8_t next_byte = source[index + 1];
305
+ if (next_byte == '{' || next_byte == '@' || next_byte == '$') {
306
+ pm_buffer_append_byte(buffer, '\\');
307
+ }
308
+ }
309
+
310
+ pm_buffer_append_byte(buffer, '#');
311
+ break;
312
+ }
313
+ case '\\':
314
+ PM_BUFFER_APPEND_LITERAL(buffer, "\\\\");
315
+ break;
316
+ default:
317
+ pm_buffer_append_byte(buffer, byte);
318
+ break;
319
+ }
320
+ }
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Prepend the given string to the buffer.
326
+ */
327
+ void
328
+ pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length) {
329
+ size_t cursor = buffer->length;
330
+ if (pm_buffer_append_length(buffer, length)) {
331
+ memmove(buffer->value + length, buffer->value, cursor);
332
+ memcpy(buffer->value, value, length);
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Concatenate one buffer onto another.
338
+ */
339
+ void
340
+ pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source) {
341
+ if (source->length > 0) {
342
+ pm_buffer_append(destination, source->value, source->length);
343
+ }
344
+ }
345
+
346
+ /**
347
+ * Clear the buffer by reducing its size to 0. This does not free the allocated
348
+ * memory, but it does allow the buffer to be reused.
349
+ */
350
+ void
351
+ pm_buffer_clear(pm_buffer_t *buffer) {
352
+ buffer->length = 0;
353
+ }
354
+
355
+ /**
356
+ * Strip the whitespace from the end of the buffer.
357
+ */
358
+ void
359
+ pm_buffer_rstrip(pm_buffer_t *buffer) {
360
+ while (buffer->length > 0 && pm_char_is_whitespace((uint8_t) buffer->value[buffer->length - 1])) {
361
+ buffer->length--;
362
+ }
363
+ }
364
+
365
+ /**
366
+ * Checks if the buffer includes the given value.
367
+ */
368
+ size_t
369
+ pm_buffer_index(const pm_buffer_t *buffer, char value) {
370
+ const char *first = memchr(buffer->value, value, buffer->length);
371
+ return (first == NULL) ? SIZE_MAX : (size_t) (first - buffer->value);
372
+ }
373
+
374
+ /**
375
+ * Insert the given string into the buffer at the given index.
376
+ */
377
+ void
378
+ pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length) {
379
+ PRISM_ASSERT(index <= buffer->length);
380
+
381
+ if (index == buffer->length) {
382
+ pm_buffer_append_string(buffer, value, length);
383
+ } else {
384
+ pm_buffer_append_zeroes(buffer, length);
385
+ memmove(buffer->value + index + length, buffer->value + index, buffer->length - length - index);
386
+ memcpy(buffer->value + index, value, length);
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Free the memory held by the buffer.
392
+ */
393
+ void
394
+ pm_buffer_cleanup(pm_buffer_t *buffer) {
395
+ xfree_sized(buffer->value, buffer->capacity);
396
+ }
397
+
398
+ /**
399
+ * Free both the memory held by the buffer and the buffer itself.
400
+ */
401
+ void
402
+ pm_buffer_free(pm_buffer_t *buffer) {
403
+ pm_buffer_cleanup(buffer);
404
+ xfree_sized(buffer, sizeof(pm_buffer_t));
405
+ }
@@ -0,0 +1,289 @@
1
+ #include "prism/internal/char.h"
2
+
3
+ #include "prism/compiler/inline.h"
4
+ #include "prism/internal/line_offset_list.h"
5
+
6
+ #define PRISM_CHAR_BIT_REGEXP_OPTION (1 << 2)
7
+
8
+ #define PRISM_NUMBER_BIT_BINARY_DIGIT (1 << 0)
9
+ #define PRISM_NUMBER_BIT_BINARY_NUMBER (1 << 1)
10
+ #define PRISM_NUMBER_BIT_OCTAL_DIGIT (1 << 2)
11
+ #define PRISM_NUMBER_BIT_OCTAL_NUMBER (1 << 3)
12
+ #define PRISM_NUMBER_BIT_DECIMAL_DIGIT (1 << 4)
13
+ #define PRISM_NUMBER_BIT_DECIMAL_NUMBER (1 << 5)
14
+ #define PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT (1 << 6)
15
+ #define PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER (1 << 7)
16
+
17
+ const uint8_t pm_byte_table[256] = {
18
+ // 0 1 2 3 4 5 6 7 8 9 A B C D E F
19
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, 0, 0, // 0x
20
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
21
+ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x
22
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3x
23
+ 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 4x
24
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, // 5x
25
+ 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 6x
26
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, // 7x
27
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x
28
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x
29
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax
30
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx
31
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx
32
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx
33
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex
34
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx
35
+ };
36
+
37
+ static const uint8_t pm_number_table[256] = {
38
+ // 0 1 2 3 4 5 6 7 8 9 A B C D E F
39
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x
40
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 1x
41
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 2x
42
+ 0xff, 0xff, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xf0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 3x
43
+ 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 4x
44
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, // 5x
45
+ 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 6x
46
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 7x
47
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 8x
48
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9x
49
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Ax
50
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Bx
51
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Cx
52
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Dx
53
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Ex
54
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Fx
55
+ };
56
+
57
+ /**
58
+ * Returns the number of characters at the start of the string that match the
59
+ * given kind. Disallows searching past the given maximum number of characters.
60
+ */
61
+ static PRISM_INLINE size_t
62
+ pm_strspn_char_kind(const uint8_t *string, ptrdiff_t length, uint8_t kind) {
63
+ if (length <= 0) return 0;
64
+
65
+ size_t size = 0;
66
+ size_t maximum = (size_t) length;
67
+
68
+ while (size < maximum && (pm_byte_table[string[size]] & kind)) size++;
69
+ return size;
70
+ }
71
+
72
+ /**
73
+ * Returns the number of characters at the start of the string that are
74
+ * whitespace. Disallows searching past the given maximum number of characters.
75
+ */
76
+ size_t
77
+ pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length) {
78
+ return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_WHITESPACE);
79
+ }
80
+
81
+ /**
82
+ * Returns the number of characters at the end of the string that are
83
+ * whitespace. Disallows searching past the given maximum number of characters.
84
+ */
85
+ size_t
86
+ pm_strrspn_whitespace(const uint8_t *string, ptrdiff_t length) {
87
+ if (length <= 0) return 0;
88
+
89
+ size_t size = 0;
90
+ size_t maximum = (size_t) length;
91
+
92
+ while (size < maximum && (pm_byte_table[string[maximum - size - 1]] & PRISM_CHAR_BIT_WHITESPACE)) size++;
93
+ return size;
94
+ }
95
+
96
+ /**
97
+ * Returns the number of characters at the start of the string that are
98
+ * whitespace while also tracking the location of each newline. Disallows
99
+ * searching past the given maximum number of characters.
100
+ */
101
+ size_t
102
+ pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_arena_t *arena, pm_line_offset_list_t *line_offsets, uint32_t start_offset) {
103
+ if (length <= 0) return 0;
104
+
105
+ uint32_t size = 0;
106
+ uint32_t maximum = (uint32_t) length;
107
+
108
+ while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_WHITESPACE)) {
109
+ if (string[size] == '\n') {
110
+ pm_line_offset_list_append(arena, line_offsets, start_offset + size + 1);
111
+ }
112
+
113
+ size++;
114
+ }
115
+
116
+ return size;
117
+ }
118
+
119
+ /**
120
+ * Returns the number of characters at the start of the string that are regexp
121
+ * options. Disallows searching past the given maximum number of characters.
122
+ */
123
+ size_t
124
+ pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length) {
125
+ return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_REGEXP_OPTION);
126
+ }
127
+
128
+
129
+ /**
130
+ * Scan through the string and return the number of characters at the start of
131
+ * the string that match the given kind. Disallows searching past the given
132
+ * maximum number of characters.
133
+ */
134
+ static PRISM_INLINE size_t
135
+ pm_strspn_number_kind(const uint8_t *string, ptrdiff_t length, uint8_t kind) {
136
+ if (length <= 0) return 0;
137
+
138
+ size_t size = 0;
139
+ size_t maximum = (size_t) length;
140
+
141
+ while (size < maximum && (pm_number_table[string[size]] & kind)) size++;
142
+ return size;
143
+ }
144
+
145
+ /**
146
+ * Scan through the string and return the number of characters at the start of
147
+ * the string that match the given kind. Disallows searching past the given
148
+ * maximum number of characters.
149
+ *
150
+ * Additionally, report the location of the last invalid underscore character
151
+ * found in the string through the out invalid parameter.
152
+ */
153
+ static PRISM_INLINE size_t
154
+ pm_strspn_number_kind_underscores(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid, uint8_t kind) {
155
+ if (length <= 0) return 0;
156
+
157
+ size_t size = 0;
158
+ size_t maximum = (size_t) length;
159
+
160
+ bool underscore = false;
161
+ while (size < maximum && (pm_number_table[string[size]] & kind)) {
162
+ if (string[size] == '_') {
163
+ if (underscore) *invalid = string + size;
164
+ underscore = true;
165
+ } else {
166
+ underscore = false;
167
+ }
168
+
169
+ size++;
170
+ }
171
+
172
+ if (size > 0 && string[size - 1] == '_') *invalid = string + size - 1;
173
+ return size;
174
+ }
175
+
176
+ /**
177
+ * Returns the number of characters at the start of the string that are binary
178
+ * digits or underscores. Disallows searching past the given maximum number of
179
+ * characters.
180
+ *
181
+ * If multiple underscores are found in a row or if an underscore is
182
+ * found at the end of the number, then the invalid pointer is set to the index
183
+ * of the first invalid underscore.
184
+ */
185
+ size_t
186
+ pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) {
187
+ return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_BINARY_NUMBER);
188
+ }
189
+
190
+ /**
191
+ * Returns the number of characters at the start of the string that are octal
192
+ * digits or underscores. Disallows searching past the given maximum number of
193
+ * characters.
194
+ *
195
+ * If multiple underscores are found in a row or if an underscore is
196
+ * found at the end of the number, then the invalid pointer is set to the index
197
+ * of the first invalid underscore.
198
+ */
199
+ size_t
200
+ pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) {
201
+ return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_OCTAL_NUMBER);
202
+ }
203
+
204
+ /**
205
+ * Returns the number of characters at the start of the string that are decimal
206
+ * digits. Disallows searching past the given maximum number of characters.
207
+ */
208
+ size_t
209
+ pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length) {
210
+ return pm_strspn_number_kind(string, length, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
211
+ }
212
+
213
+ /**
214
+ * Returns the number of characters at the start of the string that are decimal
215
+ * digits or underscores. Disallows searching past the given maximum number of
216
+ * characters.
217
+ *
218
+ * If multiple underscores are found in a row or if an underscore is
219
+ * found at the end of the number, then the invalid pointer is set to the index
220
+ * of the first invalid underscore
221
+ */
222
+ size_t
223
+ pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) {
224
+ return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_DECIMAL_NUMBER);
225
+ }
226
+
227
+ /**
228
+ * Returns the number of characters at the start of the string that are
229
+ * hexadecimal digits. Disallows searching past the given maximum number of
230
+ * characters.
231
+ */
232
+ size_t
233
+ pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length) {
234
+ return pm_strspn_number_kind(string, length, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
235
+ }
236
+
237
+ /**
238
+ * Returns the number of characters at the start of the string that are
239
+ * hexadecimal digits or underscores. Disallows searching past the given maximum
240
+ * number of characters.
241
+ *
242
+ * If multiple underscores are found in a row or if an underscore is
243
+ * found at the end of the number, then the invalid pointer is set to the index
244
+ * of the first invalid underscore.
245
+ */
246
+ size_t
247
+ pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) {
248
+ return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER);
249
+ }
250
+
251
+ /**
252
+ * Returns true if the given character matches the given kind.
253
+ */
254
+ static PRISM_INLINE bool
255
+ pm_char_is_number_kind(const uint8_t b, uint8_t kind) {
256
+ return (pm_number_table[b] & kind) != 0;
257
+ }
258
+
259
+ /**
260
+ * Returns true if the given character is a binary digit.
261
+ */
262
+ bool
263
+ pm_char_is_binary_digit(const uint8_t b) {
264
+ return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_BINARY_DIGIT);
265
+ }
266
+
267
+ /**
268
+ * Returns true if the given character is an octal digit.
269
+ */
270
+ bool
271
+ pm_char_is_octal_digit(const uint8_t b) {
272
+ return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_OCTAL_DIGIT);
273
+ }
274
+
275
+ /**
276
+ * Returns true if the given character is a decimal digit.
277
+ */
278
+ bool
279
+ pm_char_is_decimal_digit(const uint8_t b) {
280
+ return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
281
+ }
282
+
283
+ /**
284
+ * Returns true if the given character is a hexadecimal digit.
285
+ */
286
+ bool
287
+ pm_char_is_hexadecimal_digit(const uint8_t b) {
288
+ return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
289
+ }