prism 1.9.0 → 2.0.0.rc2

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 (244) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +88 -0
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/LICENSE.md +1 -1
  5. data/Makefile +41 -12
  6. data/README.md +35 -97
  7. data/config.yml +174 -161
  8. data/docs/build_system.md +2 -2
  9. data/docs/configuration.md +5 -4
  10. data/docs/encoding.md +2 -2
  11. data/docs/fuzzing.md +4 -11
  12. data/docs/javascript.md +19 -4
  13. data/docs/mapping.md +1 -1
  14. data/docs/releasing.md +77 -5
  15. data/docs/relocation.md +2 -4
  16. data/docs/serialization.md +22 -24
  17. data/docs/testing.md +46 -29
  18. data/ext/prism/api_node.c +1461 -1401
  19. data/ext/prism/extconf.rb +3 -4
  20. data/ext/prism/extension.c +796 -422
  21. data/ext/prism/extension.h +3 -3
  22. data/include/prism/arena.h +37 -0
  23. data/include/prism/ast.h +2727 -610
  24. data/include/prism/buffer.h +52 -0
  25. data/include/prism/comments.h +55 -0
  26. data/include/prism/compiler/accel.h +26 -0
  27. data/include/prism/compiler/align.h +36 -0
  28. data/include/prism/compiler/assume.h +25 -0
  29. data/include/prism/compiler/bswap.h +46 -0
  30. data/include/prism/compiler/endianness.h +34 -0
  31. data/include/prism/compiler/exported.h +24 -0
  32. data/include/prism/compiler/fallthrough.h +22 -0
  33. data/include/prism/compiler/filesystem.h +32 -0
  34. data/include/prism/compiler/flex_array.h +19 -0
  35. data/include/prism/compiler/force_inline.h +21 -0
  36. data/include/prism/compiler/format.h +25 -0
  37. data/include/prism/compiler/inline.h +17 -0
  38. data/include/prism/compiler/nodiscard.h +22 -0
  39. data/include/prism/compiler/nonnull.h +18 -0
  40. data/include/prism/compiler/unused.h +18 -0
  41. data/include/prism/constant_pool.h +81 -0
  42. data/include/prism/diagnostic.h +38 -403
  43. data/include/prism/errors_format.h +53 -0
  44. data/include/prism/excludes.h +29 -0
  45. data/include/prism/integer.h +41 -0
  46. data/include/prism/internal/allocator.h +68 -0
  47. data/include/prism/internal/allocator_debug.h +88 -0
  48. data/include/prism/internal/arena.h +108 -0
  49. data/include/prism/internal/bit.h +42 -0
  50. data/include/prism/internal/buffer.h +94 -0
  51. data/include/prism/internal/char.h +145 -0
  52. data/include/prism/internal/comments.h +53 -0
  53. data/include/prism/internal/constant_pool.h +152 -0
  54. data/include/prism/internal/diagnostic.h +389 -0
  55. data/include/prism/{encoding.h → internal/encoding.h} +27 -68
  56. data/include/prism/internal/integer.h +68 -0
  57. data/include/prism/internal/isinf.h +16 -0
  58. data/include/prism/internal/line_offset_list.h +34 -0
  59. data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  60. data/include/prism/internal/magic_comments.h +23 -0
  61. data/include/prism/internal/memchr.h +15 -0
  62. data/include/prism/internal/node.h +38 -0
  63. data/include/prism/internal/options.h +232 -0
  64. data/include/prism/internal/parser.h +1004 -0
  65. data/include/prism/internal/regexp.h +26 -0
  66. data/include/prism/internal/serialize.h +39 -0
  67. data/include/prism/internal/source.h +72 -0
  68. data/include/prism/{static_literals.h → internal/static_literals.h} +26 -47
  69. data/include/prism/internal/stringy.h +35 -0
  70. data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  71. data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  72. data/include/prism/internal/tokens.h +11 -0
  73. data/include/prism/internal/xxh3.h +16 -0
  74. data/include/prism/json.h +32 -0
  75. data/include/prism/line_offset_list.h +61 -0
  76. data/include/prism/magic_comments.h +35 -0
  77. data/include/prism/node.h +18 -53
  78. data/include/prism/options.h +146 -280
  79. data/include/prism/parser.h +356 -867
  80. data/include/prism/prettyprint.h +6 -9
  81. data/include/prism/serialize.h +115 -0
  82. data/include/prism/source.h +155 -0
  83. data/include/prism/stream.h +28 -0
  84. data/include/prism/string_query.h +74 -0
  85. data/include/prism/stringy.h +72 -0
  86. data/include/prism/version.h +12 -3
  87. data/include/prism.h +43 -320
  88. data/lib/prism/compiler.rb +319 -305
  89. data/lib/prism/desugar_compiler.rb +105 -34
  90. data/lib/prism/dispatcher.rb +646 -473
  91. data/lib/prism/dot_visitor.rb +592 -511
  92. data/lib/prism/dsl.rb +391 -25
  93. data/lib/prism/ffi.rb +258 -90
  94. data/lib/prism/inspect_visitor.rb +357 -325
  95. data/lib/prism/lex_compat.rb +254 -212
  96. data/lib/prism/mutation_compiler.rb +313 -305
  97. data/lib/prism/node.rb +20966 -6992
  98. data/lib/prism/node_ext.rb +180 -187
  99. data/lib/prism/node_find.rb +215 -0
  100. data/lib/prism/parse_result/errors.rb +7 -1
  101. data/lib/prism/parse_result/newlines.rb +276 -8
  102. data/lib/prism/parse_result.rb +468 -139
  103. data/lib/prism/pattern.rb +65 -20
  104. data/lib/prism/reflection.rb +23 -12
  105. data/lib/prism/relocation.rb +149 -87
  106. data/lib/prism/serialize.rb +3460 -539
  107. data/lib/prism/string_query.rb +24 -1
  108. data/lib/prism/translation/parser/builder.rb +10 -2
  109. data/lib/prism/translation/parser/compiler.rb +44 -52
  110. data/lib/prism/translation/parser/lexer.rb +41 -92
  111. data/lib/prism/translation/parser.rb +1 -1
  112. data/lib/prism/translation/parser_current.rb +1 -1
  113. data/lib/prism/translation/ripper/lexer.rb +8 -10
  114. data/lib/prism/translation/ripper/sexp.rb +2 -10
  115. data/lib/prism/translation/ripper/shim.rb +2 -0
  116. data/lib/prism/translation/ripper.rb +967 -209
  117. data/lib/prism/translation/ruby_parser.rb +73 -316
  118. data/lib/prism/translation.rb +2 -0
  119. data/lib/prism/visitor.rb +632 -5
  120. data/lib/prism.rb +79 -9
  121. data/prism.gemspec +128 -62
  122. data/rbi/generated/prism/compiler.rbi +489 -0
  123. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  124. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  125. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  126. data/rbi/generated/prism/dsl.rbi +751 -0
  127. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  128. data/rbi/generated/prism/lex_compat.rbi +162 -0
  129. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  130. data/rbi/generated/prism/node.rbi +14407 -0
  131. data/rbi/generated/prism/node_ext.rbi +259 -0
  132. data/rbi/generated/prism/node_find.rbi +79 -0
  133. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  134. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  135. data/rbi/generated/prism/parse_result.rbi +776 -0
  136. data/rbi/generated/prism/pattern.rbi +134 -0
  137. data/rbi/generated/prism/reflection.rbi +89 -0
  138. data/rbi/generated/prism/relocation.rbi +329 -0
  139. data/rbi/generated/prism/serialize.rbi +184 -0
  140. data/rbi/generated/prism/string_query.rbi +43 -0
  141. data/rbi/generated/prism/translation.rbi +7 -0
  142. data/rbi/generated/prism/visitor.rbi +650 -0
  143. data/rbi/generated/prism.rbi +86 -0
  144. data/rbi/prism/translation/parser_versions.rbi +5 -0
  145. data/rbi/rubyvm/node_find.rbi +14 -0
  146. data/sig/generated/prism/compiler.rbs +492 -0
  147. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  148. data/sig/generated/prism/dispatcher.rbs +1292 -0
  149. data/sig/generated/prism/dot_visitor.rbs +635 -0
  150. data/sig/generated/prism/dsl.rbs +928 -0
  151. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  152. data/sig/generated/prism/lex_compat.rbs +166 -0
  153. data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
  154. data/sig/generated/prism/node.rbs +20896 -0
  155. data/sig/generated/prism/node_ext.rbs +290 -0
  156. data/sig/generated/prism/node_find.rbs +90 -0
  157. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  158. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  159. data/sig/generated/prism/parse_result.rbs +871 -0
  160. data/sig/generated/prism/pattern.rbs +156 -0
  161. data/sig/generated/prism/reflection.rbs +101 -0
  162. data/sig/generated/prism/relocation.rbs +428 -0
  163. data/sig/generated/prism/serialize.rbs +198 -0
  164. data/sig/generated/prism/string_query.rbs +43 -0
  165. data/sig/generated/prism/translation.rbs +8 -0
  166. data/sig/generated/prism/visitor.rbs +1111 -0
  167. data/sig/generated/prism.rbs +84 -0
  168. data/sig/manifest.yaml +2 -0
  169. data/src/arena.c +117 -0
  170. data/src/{util/pm_buffer.c → buffer.c} +53 -21
  171. data/src/{util/pm_char.c → char.c} +28 -57
  172. data/src/comments.c +454 -0
  173. data/src/constant_pool.c +406 -0
  174. data/src/diagnostic.c +97 -65
  175. data/src/encoding.c +8 -3
  176. data/src/errors_format.c +519 -0
  177. data/src/{util/pm_integer.c → integer.c} +33 -22
  178. data/src/json.c +5733 -0
  179. data/src/line_offset_list.c +100 -0
  180. data/src/list.c +24 -0
  181. data/src/{util/pm_memchr.c → memchr.c} +8 -6
  182. data/src/node.c +3504 -7674
  183. data/src/options.c +162 -49
  184. data/src/parser.c +367 -0
  185. data/src/prettyprint.c +421 -354
  186. data/src/prism.c +8402 -7161
  187. data/src/regexp.c +961 -45
  188. data/src/serialize.c +460 -424
  189. data/src/source.c +518 -0
  190. data/src/static_literals.c +111 -43
  191. data/src/string_query.c +180 -0
  192. data/src/stringy.c +91 -0
  193. data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  194. data/src/strpbrk.c +447 -0
  195. data/src/{token_type.c → tokens.c} +39 -18
  196. data/src/xxh3.c +292 -0
  197. metadata +128 -63
  198. data/ext/prism/api_pack.c +0 -276
  199. data/include/prism/defines.h +0 -260
  200. data/include/prism/pack.h +0 -163
  201. data/include/prism/regexp.h +0 -43
  202. data/include/prism/util/pm_buffer.h +0 -236
  203. data/include/prism/util/pm_char.h +0 -204
  204. data/include/prism/util/pm_constant_pool.h +0 -218
  205. data/include/prism/util/pm_integer.h +0 -130
  206. data/include/prism/util/pm_memchr.h +0 -29
  207. data/include/prism/util/pm_newline_list.h +0 -113
  208. data/include/prism/util/pm_string.h +0 -200
  209. data/lib/prism/pack.rb +0 -230
  210. data/lib/prism/parse_result/comments.rb +0 -188
  211. data/rbi/prism/compiler.rbi +0 -12
  212. data/rbi/prism/dsl.rbi +0 -524
  213. data/rbi/prism/inspect_visitor.rbi +0 -12
  214. data/rbi/prism/node.rbi +0 -8750
  215. data/rbi/prism/node_ext.rbi +0 -107
  216. data/rbi/prism/parse_result.rbi +0 -404
  217. data/rbi/prism/reflection.rbi +0 -58
  218. data/rbi/prism/string_query.rbi +0 -12
  219. data/rbi/prism/visitor.rbi +0 -473
  220. data/rbi/prism.rbi +0 -63
  221. data/sig/prism/compiler.rbs +0 -9
  222. data/sig/prism/dispatcher.rbs +0 -19
  223. data/sig/prism/dot_visitor.rbs +0 -6
  224. data/sig/prism/dsl.rbs +0 -351
  225. data/sig/prism/inspect_visitor.rbs +0 -22
  226. data/sig/prism/lex_compat.rbs +0 -10
  227. data/sig/prism/node.rbs +0 -4042
  228. data/sig/prism/node_ext.rbs +0 -149
  229. data/sig/prism/pack.rbs +0 -43
  230. data/sig/prism/parse_result/comments.rbs +0 -38
  231. data/sig/prism/parse_result.rbs +0 -197
  232. data/sig/prism/pattern.rbs +0 -13
  233. data/sig/prism/reflection.rbs +0 -50
  234. data/sig/prism/relocation.rbs +0 -185
  235. data/sig/prism/serialize.rbs +0 -8
  236. data/sig/prism/string_query.rbs +0 -11
  237. data/sig/prism/visitor.rbs +0 -169
  238. data/sig/prism.rbs +0 -272
  239. data/src/pack.c +0 -509
  240. data/src/util/pm_constant_pool.c +0 -342
  241. data/src/util/pm_list.c +0 -49
  242. data/src/util/pm_newline_list.c +0 -125
  243. data/src/util/pm_string.c +0 -381
  244. data/src/util/pm_strpbrk.c +0 -206
@@ -0,0 +1,88 @@
1
+ #ifndef PRISM_INTERNAL_ALLOCATOR_DEBUG_H
2
+ #define PRISM_INTERNAL_ALLOCATOR_DEBUG_H
3
+
4
+ #include <string.h>
5
+ #include <stdio.h>
6
+ #include <stdlib.h>
7
+
8
+ static inline void *
9
+ pm_allocator_debug_malloc(size_t size) {
10
+ size_t *memory = xmalloc(size + sizeof(size_t));
11
+ memory[0] = size;
12
+ return memory + 1;
13
+ }
14
+
15
+ static inline void *
16
+ pm_allocator_debug_calloc(size_t nmemb, size_t size) {
17
+ size_t total_size = nmemb * size;
18
+ void *ptr = pm_allocator_debug_malloc(total_size);
19
+ memset(ptr, 0, total_size);
20
+ return ptr;
21
+ }
22
+
23
+ static inline void *
24
+ pm_allocator_debug_realloc(void *ptr, size_t size) {
25
+ if (ptr == NULL) {
26
+ return pm_allocator_debug_malloc(size);
27
+ }
28
+
29
+ size_t *memory = (size_t *)ptr;
30
+ void *raw_memory = memory - 1;
31
+ memory = (size_t *)xrealloc(raw_memory, size + sizeof(size_t));
32
+ memory[0] = size;
33
+ return memory + 1;
34
+ }
35
+
36
+ static inline void
37
+ pm_allocator_debug_free(void *ptr) {
38
+ if (ptr != NULL) {
39
+ size_t *memory = (size_t *)ptr;
40
+ xfree(memory - 1);
41
+ }
42
+ }
43
+
44
+ static inline void
45
+ pm_allocator_debug_free_sized(void *ptr, size_t old_size) {
46
+ if (ptr != NULL) {
47
+ size_t *memory = (size_t *)ptr;
48
+ if (old_size != memory[-1]) {
49
+ fprintf(stderr, "[BUG] buffer %p was allocated with size %lu but freed with size %lu\n", ptr, memory[-1], old_size);
50
+ abort();
51
+ }
52
+ xfree_sized(memory - 1, old_size + sizeof(size_t));
53
+ }
54
+ }
55
+
56
+ static inline void *
57
+ pm_allocator_debug_realloc_sized(void *ptr, size_t size, size_t old_size) {
58
+ if (ptr == NULL) {
59
+ if (old_size != 0) {
60
+ fprintf(stderr, "[BUG] realloc_sized called with NULL pointer and old size %lu\n", old_size);
61
+ abort();
62
+ }
63
+ return pm_allocator_debug_malloc(size);
64
+ }
65
+
66
+ size_t *memory = (size_t *)ptr;
67
+ if (old_size != memory[-1]) {
68
+ fprintf(stderr, "[BUG] buffer %p was allocated with size %lu but realloced with size %lu\n", ptr, memory[-1], old_size);
69
+ abort();
70
+ }
71
+ return pm_allocator_debug_realloc(ptr, size);
72
+ }
73
+
74
+ #undef xmalloc
75
+ #undef xrealloc
76
+ #undef xcalloc
77
+ #undef xfree
78
+ #undef xrealloc_sized
79
+ #undef xfree_sized
80
+
81
+ #define xmalloc pm_allocator_debug_malloc
82
+ #define xrealloc pm_allocator_debug_realloc
83
+ #define xcalloc pm_allocator_debug_calloc
84
+ #define xfree pm_allocator_debug_free
85
+ #define xrealloc_sized pm_allocator_debug_realloc_sized
86
+ #define xfree_sized pm_allocator_debug_free_sized
87
+
88
+ #endif
@@ -0,0 +1,108 @@
1
+ #ifndef PRISM_INTERNAL_ARENA_H
2
+ #define PRISM_INTERNAL_ARENA_H
3
+
4
+ #include "prism/compiler/exported.h"
5
+ #include "prism/compiler/flex_array.h"
6
+ #include "prism/compiler/force_inline.h"
7
+ #include "prism/compiler/inline.h"
8
+
9
+ #include "prism/arena.h"
10
+
11
+ #include <stddef.h>
12
+ #include <string.h>
13
+
14
+ /*
15
+ * A single block of memory in the arena. Blocks are linked via prev pointers so
16
+ * they can be freed by walking the chain.
17
+ */
18
+ typedef struct pm_arena_block {
19
+ /* The previous block in the chain (for freeing). */
20
+ struct pm_arena_block *prev;
21
+
22
+ /* The total usable bytes in data[]. */
23
+ size_t capacity;
24
+
25
+ /* The number of bytes consumed so far. */
26
+ size_t used;
27
+
28
+ /* The block's data. */
29
+ char data[PM_FLEX_ARRAY_LENGTH];
30
+ } pm_arena_block_t;
31
+
32
+ /*
33
+ * A bump allocator. Allocations are made by bumping a pointer within the
34
+ * current block. When a block is full, a new block is allocated and linked to
35
+ * the previous one. All blocks are freed at once by walking the chain.
36
+ */
37
+ struct pm_arena_t {
38
+ /* The active block (allocate from here). */
39
+ pm_arena_block_t *current;
40
+
41
+ /* The number of blocks allocated. */
42
+ size_t block_count;
43
+ };
44
+
45
+ /*
46
+ * Free all blocks in the arena. After this call, all pointers returned by
47
+ * pm_arena_alloc and pm_arena_zalloc are invalid.
48
+ */
49
+ void pm_arena_cleanup(pm_arena_t *arena);
50
+
51
+ /*
52
+ * Ensure the arena has at least `capacity` bytes available in its current
53
+ * block, allocating a new block if necessary. This allows callers to
54
+ * pre-size the arena to avoid repeated small block allocations.
55
+ */
56
+ void pm_arena_reserve(pm_arena_t *arena, size_t capacity);
57
+
58
+ /*
59
+ * Slow path for pm_arena_alloc: allocate a new block and return a pointer to
60
+ * the first `size` bytes. Do not call directly — use pm_arena_alloc instead.
61
+ */
62
+ void * pm_arena_alloc_slow(pm_arena_t *arena, size_t size);
63
+
64
+ /*
65
+ * Allocate memory from the arena. The returned memory is NOT zeroed. This
66
+ * function is infallible — it aborts on allocation failure.
67
+ *
68
+ * The fast path (bump pointer within the current block) is inlined at each
69
+ * call site. The slow path (new block allocation) is out-of-line.
70
+ */
71
+ static PRISM_FORCE_INLINE void *
72
+ pm_arena_alloc(pm_arena_t *arena, size_t size, size_t alignment) {
73
+ if (arena->current != NULL) {
74
+ size_t used_aligned = (arena->current->used + alignment - 1) & ~(alignment - 1);
75
+ size_t needed = used_aligned + size;
76
+
77
+ if (used_aligned >= arena->current->used && needed >= used_aligned && needed <= arena->current->capacity) {
78
+ arena->current->used = needed;
79
+ return arena->current->data + used_aligned;
80
+ }
81
+ }
82
+
83
+ return pm_arena_alloc_slow(arena, size);
84
+ }
85
+
86
+ /*
87
+ * Allocate zero-initialized memory from the arena. This function is infallible
88
+ * — it aborts on allocation failure.
89
+ */
90
+ static PRISM_INLINE void *
91
+ pm_arena_zalloc(pm_arena_t *arena, size_t size, size_t alignment) {
92
+ void *ptr = pm_arena_alloc(arena, size, alignment);
93
+ memset(ptr, 0, size);
94
+ return ptr;
95
+ }
96
+
97
+ /*
98
+ * Allocate memory from the arena and copy the given data into it. This is a
99
+ * convenience wrapper around pm_arena_alloc + memcpy.
100
+ */
101
+ static PRISM_INLINE void *
102
+ pm_arena_memdup(pm_arena_t *arena, const void *src, size_t size, size_t alignment) {
103
+ void *dst = pm_arena_alloc(arena, size, alignment);
104
+ memcpy(dst, src, size);
105
+ return dst;
106
+ }
107
+
108
+ #endif
@@ -0,0 +1,42 @@
1
+ #ifndef PRISM_INTERNAL_BIT_H
2
+ #define PRISM_INTERNAL_BIT_H
3
+
4
+ #include "prism/compiler/inline.h"
5
+
6
+ /*
7
+ * Count trailing zero bits in a 64-bit value. Used by SWAR identifier scanning
8
+ * to find the first non-matching byte in a word.
9
+ *
10
+ * Precondition: v must be nonzero. The result is undefined when v == 0
11
+ * (matching the behavior of __builtin_ctzll and _BitScanForward64).
12
+ */
13
+ #if defined(__GNUC__) || defined(__clang__)
14
+ #define pm_ctzll(v) ((unsigned) __builtin_ctzll(v))
15
+ #elif defined(_MSC_VER) && defined(_WIN64)
16
+ #include <intrin.h>
17
+ #include <stdint.h>
18
+
19
+ static PRISM_INLINE unsigned
20
+ pm_ctzll(uint64_t v) {
21
+ unsigned long index;
22
+ _BitScanForward64(&index, v);
23
+ return (unsigned) index;
24
+ }
25
+ #else
26
+ #include <stdint.h>
27
+
28
+ static PRISM_INLINE unsigned
29
+ pm_ctzll(uint64_t v) {
30
+ unsigned c = 0;
31
+ v &= (uint64_t) (-(int64_t) v);
32
+ if (v & 0x00000000FFFFFFFFULL) c += 0; else c += 32;
33
+ if (v & 0x0000FFFF0000FFFFULL) c += 0; else c += 16;
34
+ if (v & 0x00FF00FF00FF00FFULL) c += 0; else c += 8;
35
+ if (v & 0x0F0F0F0F0F0F0F0FULL) c += 0; else c += 4;
36
+ if (v & 0x3333333333333333ULL) c += 0; else c += 2;
37
+ if (v & 0x5555555555555555ULL) c += 0; else c += 1;
38
+ return c;
39
+ }
40
+ #endif
41
+
42
+ #endif
@@ -0,0 +1,94 @@
1
+ #ifndef PRISM_INTERNAL_BUFFER_H
2
+ #define PRISM_INTERNAL_BUFFER_H
3
+
4
+ #include "prism/compiler/format.h"
5
+
6
+ #include "prism/buffer.h"
7
+
8
+ #include <stdbool.h>
9
+ #include <stdint.h>
10
+
11
+ /*
12
+ * A simple memory buffer that stores data in a contiguous block of memory.
13
+ */
14
+ struct pm_buffer_t {
15
+ /* The length of the buffer in bytes. */
16
+ size_t length;
17
+
18
+ /* The capacity of the buffer in bytes that has been allocated. */
19
+ size_t capacity;
20
+
21
+ /* A pointer to the start of the buffer. */
22
+ char *value;
23
+ };
24
+
25
+ /* Initialize a pm_buffer_t with the given capacity. */
26
+ void pm_buffer_init(pm_buffer_t *buffer, size_t capacity);
27
+
28
+ /* Free the memory held by the buffer. */
29
+ void pm_buffer_cleanup(pm_buffer_t *buffer);
30
+
31
+ /* Append the given amount of space as zeroes to the buffer. */
32
+ void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
33
+
34
+ /* Append a formatted string to the buffer. */
35
+ void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
36
+
37
+ /* Append a string to the buffer. */
38
+ void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
39
+
40
+ /* Append a list of bytes to the buffer. */
41
+ void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
42
+
43
+ /* Append a single byte to the buffer. */
44
+ void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
45
+
46
+ /* Append a 32-bit unsigned integer to the buffer as a variable-length integer. */
47
+ void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
48
+
49
+ /* Append a 32-bit signed integer to the buffer as a variable-length integer. */
50
+ void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
51
+
52
+ /* Append a double to the buffer. */
53
+ void pm_buffer_append_double(pm_buffer_t *buffer, double value);
54
+
55
+ /* Append a 64-bit unsigned integer to the buffer in native endian order. */
56
+ void pm_buffer_append_uint64(pm_buffer_t *buffer, uint64_t value);
57
+
58
+ /* Append a unicode codepoint to the buffer. */
59
+ bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
60
+
61
+ /*
62
+ * The different types of escaping that can be performed by the buffer when
63
+ * appending a slice of Ruby source code.
64
+ */
65
+ typedef enum {
66
+ PM_BUFFER_ESCAPING_RUBY,
67
+ PM_BUFFER_ESCAPING_JSON
68
+ } pm_buffer_escaping_t;
69
+
70
+ /* Append a slice of source code to the buffer. */
71
+ void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
72
+
73
+ /* Prepend the given string to the buffer. */
74
+ void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
75
+
76
+ /* Concatenate one buffer onto another. */
77
+ void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
78
+
79
+ /*
80
+ * Clear the buffer by reducing its size to 0. This does not free the allocated
81
+ * memory, but it does allow the buffer to be reused.
82
+ */
83
+ void pm_buffer_clear(pm_buffer_t *buffer);
84
+
85
+ /* Strip the whitespace from the end of the buffer. */
86
+ void pm_buffer_rstrip(pm_buffer_t *buffer);
87
+
88
+ /* Checks if the buffer includes the given value. */
89
+ size_t pm_buffer_index(const pm_buffer_t *buffer, char value);
90
+
91
+ /* Insert the given string into the buffer at the given index. */
92
+ void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length);
93
+
94
+ #endif
@@ -0,0 +1,145 @@
1
+ #ifndef PRISM_INTERNAL_CHAR_H
2
+ #define PRISM_INTERNAL_CHAR_H
3
+
4
+ #include "prism/compiler/force_inline.h"
5
+
6
+ #include "prism/arena.h"
7
+ #include "prism/line_offset_list.h"
8
+
9
+ #include <stdbool.h>
10
+ #include <stddef.h>
11
+ #include <stdint.h>
12
+
13
+ /* Bit flag for whitespace characters in pm_byte_table. */
14
+ #define PRISM_CHAR_BIT_WHITESPACE (1 << 0)
15
+
16
+ /* Bit flag for inline whitespace characters in pm_byte_table. */
17
+ #define PRISM_CHAR_BIT_INLINE_WHITESPACE (1 << 1)
18
+
19
+ /*
20
+ * A lookup table for classifying bytes. Each entry is a bitfield of
21
+ * PRISM_CHAR_BIT_* flags. Defined in char.c.
22
+ */
23
+ extern const uint8_t pm_byte_table[256];
24
+
25
+ /* Returns true if the given character is a whitespace character. */
26
+ static PRISM_FORCE_INLINE bool
27
+ pm_char_is_whitespace(const uint8_t b) {
28
+ return (pm_byte_table[b] & PRISM_CHAR_BIT_WHITESPACE) != 0;
29
+ }
30
+
31
+ /* Returns true if the given character is an inline whitespace character. */
32
+ static PRISM_FORCE_INLINE bool
33
+ pm_char_is_inline_whitespace(const uint8_t b) {
34
+ return (pm_byte_table[b] & PRISM_CHAR_BIT_INLINE_WHITESPACE) != 0;
35
+ }
36
+
37
+ /*
38
+ * Returns the number of characters at the start of the string that are inline
39
+ * whitespace (space/tab). Scans the byte table directly for use in hot paths.
40
+ */
41
+ static PRISM_FORCE_INLINE size_t
42
+ pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length) {
43
+ if (length <= 0) return 0;
44
+ size_t size = 0;
45
+ size_t maximum = (size_t) length;
46
+ while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_INLINE_WHITESPACE)) size++;
47
+ return size;
48
+ }
49
+
50
+ /*
51
+ * Returns the number of characters at the start of the string that are
52
+ * whitespace. Disallows searching past the given maximum number of characters.
53
+ */
54
+ size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length);
55
+
56
+ /*
57
+ * Returns the number of characters at the end of the string that are
58
+ * whitespace. Disallows searching past the given maximum number of characters.
59
+ */
60
+ size_t pm_strrspn_whitespace(const uint8_t *string, ptrdiff_t length);
61
+
62
+ /*
63
+ * Returns the number of characters at the start of the string that are
64
+ * whitespace while also tracking the location of each newline. Disallows
65
+ * searching past the given maximum number of characters.
66
+ */
67
+ size_t 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);
68
+
69
+ /*
70
+ * Returns the number of characters at the start of the string that are decimal
71
+ * digits. Disallows searching past the given maximum number of characters.
72
+ */
73
+ size_t pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length);
74
+
75
+ /*
76
+ * Returns the number of characters at the start of the string that are
77
+ * hexadecimal digits. Disallows searching past the given maximum number of
78
+ * characters.
79
+ */
80
+ size_t pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length);
81
+
82
+ /*
83
+ * Returns the number of characters at the start of the string that are octal
84
+ * digits or underscores. Disallows searching past the given maximum number of
85
+ * characters.
86
+ *
87
+ * If multiple underscores are found in a row or if an underscore is
88
+ * found at the end of the number, then the invalid pointer is set to the index
89
+ * of the first invalid underscore.
90
+ */
91
+ size_t pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
92
+
93
+ /*
94
+ * Returns the number of characters at the start of the string that are decimal
95
+ * digits or underscores. Disallows searching past the given maximum number of
96
+ * characters.
97
+ *
98
+ * If multiple underscores are found in a row or if an underscore is
99
+ * found at the end of the number, then the invalid pointer is set to the index
100
+ * of the first invalid underscore.
101
+ */
102
+ size_t pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
103
+
104
+ /*
105
+ * Returns the number of characters at the start of the string that are
106
+ * hexadecimal digits or underscores. Disallows searching past the given maximum
107
+ * number of characters.
108
+ *
109
+ * If multiple underscores are found in a row or if an underscore is
110
+ * found at the end of the number, then the invalid pointer is set to the index
111
+ * of the first invalid underscore.
112
+ */
113
+ size_t pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
114
+
115
+ /*
116
+ * Returns the number of characters at the start of the string that are regexp
117
+ * options. Disallows searching past the given maximum number of characters.
118
+ */
119
+ size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length);
120
+
121
+ /*
122
+ * Returns the number of characters at the start of the string that are binary
123
+ * digits or underscores. Disallows searching past the given maximum number of
124
+ * characters.
125
+ *
126
+ * If multiple underscores are found in a row or if an underscore is
127
+ * found at the end of the number, then the invalid pointer is set to the index
128
+ * of the first invalid underscore.
129
+ */
130
+ size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
131
+
132
+
133
+ /* Returns true if the given character is a binary digit. */
134
+ bool pm_char_is_binary_digit(const uint8_t b);
135
+
136
+ /* Returns true if the given character is an octal digit. */
137
+ bool pm_char_is_octal_digit(const uint8_t b);
138
+
139
+ /* Returns true if the given character is a decimal digit. */
140
+ bool pm_char_is_decimal_digit(const uint8_t b);
141
+
142
+ /* Returns true if the given character is a hexadecimal digit. */
143
+ bool pm_char_is_hexadecimal_digit(const uint8_t b);
144
+
145
+ #endif
@@ -0,0 +1,53 @@
1
+ #ifndef PRISM_INTERNAL_COMMENTS_H
2
+ #define PRISM_INTERNAL_COMMENTS_H
3
+
4
+ #include "prism/comments.h"
5
+ #include "prism/parser.h"
6
+
7
+ #include "prism/internal/list.h"
8
+
9
+ #include <stdint.h>
10
+
11
+ /* A comment found while parsing. */
12
+ struct pm_comment_t {
13
+ /* The embedded base node. */
14
+ pm_list_node_t node;
15
+
16
+ /* The location of the comment in the source. */
17
+ pm_location_t location;
18
+
19
+ /* The type of the comment. */
20
+ pm_comment_type_t type;
21
+ };
22
+
23
+ /*
24
+ * The result of attaching comments to the nodes that own them. All of the
25
+ * arrays are allocated in the parser's metadata arena.
26
+ */
27
+ typedef struct {
28
+ /* The comments found while parsing, sorted by start offset. */
29
+ const pm_comment_t **comments;
30
+
31
+ /*
32
+ * Indices into comments, grouped by owning node in order of node id and
33
+ * sorted by start offset within each group. A node's comments are not
34
+ * necessarily contiguous in comments, because a child can own a comment
35
+ * that falls between two comments owned by its parent.
36
+ */
37
+ uint32_t *attachments;
38
+
39
+ /* The slice of attachments for each node, indexed by node id. */
40
+ pm_comment_range_t *ranges;
41
+
42
+ /* The number of entries in ranges. */
43
+ uint32_t ranges_size;
44
+ } pm_comment_attachments_t;
45
+
46
+ /*
47
+ * Sort the parser's comment list in place by start offset, then assign every
48
+ * comment to the node that owns it. This is called after parsing when the
49
+ * attach_comments option is set.
50
+ */
51
+ void pm_comments_attach(pm_parser_t *parser, const pm_node_t *root);
52
+
53
+ #endif
@@ -0,0 +1,152 @@
1
+ #ifndef PRISM_INTERNAL_CONSTANT_POOL_H
2
+ #define PRISM_INTERNAL_CONSTANT_POOL_H
3
+
4
+ #include "prism/constant_pool.h"
5
+
6
+ #include "prism/arena.h"
7
+ #include "prism/compiler/inline.h"
8
+
9
+ #include <stdbool.h>
10
+
11
+ /* A constant in the pool which effectively stores a string. */
12
+ struct pm_constant_t {
13
+ /* A pointer to the start of the string. */
14
+ const uint8_t *start;
15
+
16
+ /* The length of the string. */
17
+ size_t length;
18
+ };
19
+
20
+ /*
21
+ * The type of bucket in the constant pool hash map. This determines how the
22
+ * bucket should be freed.
23
+ */
24
+ typedef unsigned int pm_constant_pool_bucket_type_t;
25
+
26
+ /* By default, each constant is a slice of the source. */
27
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0;
28
+
29
+ /* An owned constant is one for which memory has been allocated. */
30
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1;
31
+
32
+ /* A constant constant is known at compile time. */
33
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2;
34
+
35
+ /* A bucket in the hash map. */
36
+ typedef struct {
37
+ /* The incremental ID used for indexing back into the pool. */
38
+ unsigned int id: 30;
39
+
40
+ /* The type of the bucket, which determines how to free it. */
41
+ pm_constant_pool_bucket_type_t type: 2;
42
+
43
+ /* The hash of the bucket. */
44
+ uint32_t hash;
45
+
46
+ /*
47
+ * A pointer to the start of the string, stored directly in the bucket to
48
+ * avoid a pointer chase to the constants array during probing.
49
+ */
50
+ const uint8_t *start;
51
+
52
+ /* The length of the string. */
53
+ size_t length;
54
+ } pm_constant_pool_bucket_t;
55
+
56
+ /* The overall constant pool, which stores constants found while parsing. */
57
+ struct pm_constant_pool_t {
58
+ /* The buckets in the hash map. */
59
+ pm_constant_pool_bucket_t *buckets;
60
+
61
+ /* The constants that are stored in the buckets. */
62
+ pm_constant_t *constants;
63
+
64
+ /* The number of buckets in the hash map. */
65
+ uint32_t size;
66
+
67
+ /* The number of buckets that have been allocated in the hash map. */
68
+ uint32_t capacity;
69
+ };
70
+
71
+ /*
72
+ * When we allocate constants into the pool, we reserve 0 to mean that the slot
73
+ * is not yet filled. This constant is reused in other places to indicate the
74
+ * lack of a constant id.
75
+ */
76
+ #define PM_CONSTANT_ID_UNSET 0
77
+
78
+ /* Initialize a list of constant ids with a given capacity. */
79
+ void pm_constant_id_list_init_capacity(pm_arena_t *arena, pm_constant_id_list_t *list, size_t capacity);
80
+
81
+ /* Insert a constant id into a list of constant ids at the specified index. */
82
+ void pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id);
83
+
84
+ /*
85
+ * Mix the bits of a constant id so that ids which are close together, as
86
+ * sequentially assigned ids are, land in unrelated slots of a hash table.
87
+ */
88
+ static PRISM_INLINE uint32_t
89
+ pm_constant_id_hash(pm_constant_id_t id) {
90
+ id = ((id >> 16) ^ id) * 0x45d9f3b;
91
+ id = ((id >> 16) ^ id) * 0x45d9f3b;
92
+ id = (id >> 16) ^ id;
93
+ return id;
94
+ }
95
+
96
+ /*
97
+ * A set of constant ids, for where the parser only needs to know whether it has
98
+ * already seen a name, as with the variables a pattern captures or the named
99
+ * capture groups of a regular expression.
100
+ */
101
+ typedef struct {
102
+ /* The number of ids in the set. */
103
+ size_t size;
104
+
105
+ /* The number of slots in `ids`, either zero or a power of two. */
106
+ size_t capacity;
107
+
108
+ /*
109
+ * An open-addressed table of the ids in the set, where PM_CONSTANT_ID_UNSET
110
+ * marks an empty slot. It is NULL until the first id is added.
111
+ */
112
+ pm_constant_id_t *ids;
113
+ } pm_constant_id_set_t;
114
+
115
+ /*
116
+ * Add a constant id, which must not be PM_CONSTANT_ID_UNSET, to the set.
117
+ * Returns whether the set did not already contain it.
118
+ */
119
+ bool pm_constant_id_set_insert(pm_arena_t *arena, pm_constant_id_set_t *set, pm_constant_id_t id);
120
+
121
+ /* Initialize a new constant pool with a given capacity. */
122
+ void pm_constant_pool_init(pm_arena_t *arena, pm_constant_pool_t *pool, uint32_t capacity);
123
+
124
+ /* Return a pointer to the constant indicated by the given constant id. */
125
+ pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id);
126
+
127
+ /*
128
+ * Find a constant in a constant pool. Returns the id of the constant, or 0 if
129
+ * the constant is not found.
130
+ */
131
+ pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length);
132
+
133
+ /*
134
+ * Insert a constant into a constant pool that is a slice of a source string.
135
+ * Returns the id of the constant, or 0 if any potential calls to resize fail.
136
+ */
137
+ pm_constant_id_t pm_constant_pool_insert_shared(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length);
138
+
139
+ /*
140
+ * Insert a constant into a constant pool from memory that is now owned by the
141
+ * constant pool. Returns the id of the constant, or 0 if any potential calls to
142
+ * resize fail.
143
+ */
144
+ pm_constant_id_t pm_constant_pool_insert_owned(pm_arena_t *arena, pm_constant_pool_t *pool, uint8_t *start, size_t length);
145
+
146
+ /*
147
+ * Insert a constant into a constant pool from memory that is constant. Returns
148
+ * the id of the constant, or 0 if any potential calls to resize fail.
149
+ */
150
+ pm_constant_id_t pm_constant_pool_insert_constant(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length);
151
+
152
+ #endif