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,197 @@
1
+ #ifndef PRISM_INTERNAL_NODE_H
2
+ #define PRISM_INTERNAL_NODE_H
3
+
4
+ #include "prism/node.h"
5
+
6
+ #include "prism/compiler/force_inline.h"
7
+ #include "prism/compiler/inline.h"
8
+
9
+ #include "prism/arena.h"
10
+ #include "prism/internal/assert.h"
11
+
12
+ #include <stddef.h>
13
+ #include <stdint.h>
14
+
15
+ /* The offset of the start of the given location. */
16
+ #define PM_LOCATION_START(location_) ((location_)->start)
17
+
18
+ /* The offset just past the end of the given location. */
19
+ #define PM_LOCATION_END(location_) ((location_)->start + (location_)->length)
20
+
21
+ /* Return the span covering two initialized locations. Read both endpoints
22
+ * before changing either field, so moving the start preserves the old end.
23
+ * An unset location must be initialized separately from the first child. */
24
+ static PRISM_FORCE_INLINE pm_location_t
25
+ pm_location_join(pm_location_t left, pm_location_t right) {
26
+ uint32_t start = left.start < right.start ? left.start : right.start;
27
+ uint32_t left_end = PM_LOCATION_END(&left);
28
+ uint32_t right_end = PM_LOCATION_END(&right);
29
+ uint32_t end = left_end > right_end ? left_end : right_end;
30
+ pm_location_t location = { .start = start, .length = end - start };
31
+ return location;
32
+ }
33
+
34
+ /*
35
+ * Marks an interpolated string whose value is known while parsing. That is
36
+ * true when every part is a string literal or another string with this flag,
37
+ * whether or not the string is frozen. A regular expression or symbol that
38
+ * interpolates the string uses it to decide whether it is a static literal.
39
+ * This flag is only used while parsing, so it is not exposed with the others.
40
+ */
41
+ static const pm_node_flags_t PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE = ((PM_INTERPOLATED_STRING_NODE_FLAGS_LAST - 1) << 1);
42
+
43
+ /*
44
+ * A growable list of nodes for bookkeeping that never becomes part of the tree,
45
+ * such as the block exits and implicit parameters that are tracked while
46
+ * parsing. These lists are read, truncated, and edited in the middle while
47
+ * they are still being built, so unlike node lists they keep a capacity and
48
+ * grow by doubling.
49
+ */
50
+ typedef struct {
51
+ /* The number of nodes in the list. */
52
+ uint32_t size;
53
+
54
+ /* The number of nodes that fit in the list's array. */
55
+ uint32_t capacity;
56
+
57
+ /* The nodes in the list. */
58
+ pm_node_t **nodes;
59
+ } pm_scratch_node_list_t;
60
+
61
+ /*
62
+ * Slow path for pm_scratch_node_list_append: grow the list and append the
63
+ * node. Do not call directly — use pm_scratch_node_list_append instead.
64
+ */
65
+ void pm_scratch_node_list_append_slow(pm_arena_t *arena, pm_scratch_node_list_t *list, pm_node_t *node);
66
+
67
+ /*
68
+ * Append a node onto the end of a scratch list, handling the common case inline
69
+ * and growing the list through pm_scratch_node_list_append_slow.
70
+ */
71
+ static PRISM_FORCE_INLINE void
72
+ pm_scratch_node_list_append(pm_arena_t *arena, pm_scratch_node_list_t *list, pm_node_t *node) {
73
+ if (list->size < list->capacity) {
74
+ list->nodes[list->size++] = node;
75
+ } else {
76
+ pm_scratch_node_list_append_slow(arena, list, node);
77
+ }
78
+ }
79
+
80
+ /* Append every node of the other scratch list onto the end of the list. */
81
+ void pm_scratch_node_list_concat(pm_arena_t *arena, pm_scratch_node_list_t *list, const pm_scratch_node_list_t *other);
82
+
83
+ /*
84
+ * A fixed-size piece of a node list that is still being built. It is exactly
85
+ * eight pointers wide, so its size is a power of two. The type itself only
86
+ * requires pointer alignment; pm_node_chunks_carve explicitly allocates each
87
+ * batch aligned to the chunk size. Each chunk in the batch has that alignment
88
+ * because the stride is also the chunk size. The link comes first, so a cursor
89
+ * just past the last slot has zero low bits, the same as NULL. One mask test
90
+ * then tells an append that the list needs a new chunk, whether the list is
91
+ * empty or its last chunk is full.
92
+ */
93
+ typedef struct pm_node_chunk {
94
+ /* The previous chunk of the same list, or the next chunk on the freelist. */
95
+ struct pm_node_chunk *prev;
96
+
97
+ /* The nodes held by this chunk, in order. */
98
+ pm_node_t *slots[7];
99
+ } pm_node_chunk_t;
100
+
101
+ /* The size of a chunk and the alignment requested when allocating its batch. */
102
+ #define PM_NODE_CHUNK_SIZE sizeof(pm_node_chunk_t)
103
+
104
+ /* The low bits of an address that give its offset within a chunk. */
105
+ #define PM_NODE_CHUNK_MASK ((uintptr_t) (PM_NODE_CHUNK_SIZE - 1))
106
+
107
+ /* The number of nodes that fit in a chunk. */
108
+ #define PM_NODE_CHUNK_SLOTS (sizeof(((pm_node_chunk_t *) 0)->slots) / sizeof(pm_node_t *))
109
+
110
+ /* The chunk that contains the given address. */
111
+ #define PM_NODE_CHUNK_OF(pointer_) ((pm_node_chunk_t *) ((uintptr_t) (pointer_) & ~PM_NODE_CHUNK_MASK))
112
+
113
+ /* The number of chunks carved out of the arena at once. */
114
+ #define PM_NODE_CHUNK_BATCH 32
115
+
116
+ /*
117
+ * The source of chunks for open node lists. Chunks are reused through the
118
+ * freelist, and new ones come from a batch that has been carved out of the
119
+ * arena but not yet handed out. They are never freed individually; they are
120
+ * all released together when the arena is cleaned up.
121
+ */
122
+ typedef struct {
123
+ /* Chunks returned by finalized lists, linked through prev. */
124
+ pm_node_chunk_t *free;
125
+
126
+ /* The next chunk in the current batch that has not been handed out. */
127
+ pm_node_chunk_t *next;
128
+
129
+ /* One past the last chunk in the current batch. */
130
+ pm_node_chunk_t *end;
131
+
132
+ /* The arena that batches are carved from. */
133
+ pm_arena_t *arena;
134
+ } pm_node_chunks_t;
135
+
136
+ /*
137
+ * Slow path for pm_node_list_open_append: link a new chunk onto the end of the
138
+ * list and append the node to it. Do not call directly — use
139
+ * pm_node_list_open_append instead.
140
+ */
141
+ void pm_node_list_open_append_slow(pm_node_chunks_t *chunks, pm_node_list_t *list, pm_node_t *node);
142
+
143
+ /*
144
+ * Append a node onto the end of an open list. While a list is open, its nodes
145
+ * field is a cursor to the next free slot of its last chunk, or NULL when the
146
+ * list is empty. A zeroed list is an empty open list.
147
+ */
148
+ static PRISM_INLINE void
149
+ pm_node_list_open_append(pm_node_chunks_t *chunks, pm_node_list_t *list, pm_node_t *node) {
150
+ if (((uintptr_t) list->nodes & PM_NODE_CHUNK_MASK) != 0) {
151
+ *list->nodes++ = node;
152
+ list->size++;
153
+ } else {
154
+ pm_node_list_open_append_slow(chunks, list, node);
155
+ }
156
+ }
157
+
158
+ /*
159
+ * The last node in an open list, which must not be empty. The cursor is always
160
+ * just past it, in the same chunk.
161
+ */
162
+ static PRISM_INLINE pm_node_t *
163
+ pm_node_list_open_last(const pm_node_list_t *list) {
164
+ return list->nodes[-1];
165
+ }
166
+
167
+ /*
168
+ * Copy an open list into an array of exactly its size allocated from the given
169
+ * arena, and return its chunks to the freelist. Afterward the list holds that
170
+ * array, and it must not be appended to through pm_node_list_open_append or
171
+ * finalized again.
172
+ */
173
+ void pm_node_list_finalize(pm_node_chunks_t *chunks, pm_arena_t *arena, pm_node_list_t *list);
174
+
175
+ /*
176
+ * A finalized list holding just the given node, in an array allocated from the
177
+ * given arena without going through chunks.
178
+ */
179
+ pm_node_list_t pm_node_list_single(pm_arena_t *arena, pm_node_t *node);
180
+
181
+ /*
182
+ * Prepend a node onto the beginning of a finalized list, copying it into a new
183
+ * array in the given arena of exactly the new size.
184
+ */
185
+ void pm_node_list_prepend_finalized(pm_arena_t *arena, pm_node_list_t *list, pm_node_t *node);
186
+
187
+ /*
188
+ * A finalized list of the size nodes starting at the given index of another
189
+ * finalized list. It shares that list's array instead of copying it.
190
+ */
191
+ static PRISM_INLINE pm_node_list_t
192
+ pm_node_list_slice(const pm_node_list_t *list, uint32_t start, uint32_t size) {
193
+ PRISM_ASSERT(start + size <= list->size);
194
+ return (pm_node_list_t) { .size = size, .nodes = size == 0 ? NULL : list->nodes + start };
195
+ }
196
+
197
+ #endif
@@ -0,0 +1,16 @@
1
+ #ifndef PRISM_INTERNAL_OOM_H
2
+ #define PRISM_INTERNAL_OOM_H
3
+
4
+ /**
5
+ * Called when prism needs memory that it cannot get, either because an
6
+ * allocation failed or because the requested size overflows size_t. By default
7
+ * it writes a message to stderr and aborts. A consumer can define PRISM_OOM
8
+ * before building prism to handle the failure itself.
9
+ */
10
+ #ifndef PRISM_OOM
11
+ #include <stdio.h>
12
+ #include <stdlib.h>
13
+ #define PRISM_OOM() do { fputs("prism out of memory; aborting\n", stderr); abort(); } while (0)
14
+ #endif
15
+
16
+ #endif
@@ -0,0 +1,248 @@
1
+ #ifndef PRISM_INTERNAL_OPTIONS_H
2
+ #define PRISM_INTERNAL_OPTIONS_H
3
+
4
+ #include "prism/options.h"
5
+
6
+ /* A scope of locals surrounding the code that is being parsed. */
7
+ struct pm_options_scope_t {
8
+ /* The number of locals in the scope. */
9
+ size_t locals_count;
10
+
11
+ /* The names of the locals in the scope. */
12
+ pm_string_t *locals;
13
+
14
+ /* Flags for the set of forwarding parameters in this scope. */
15
+ uint8_t forwarding;
16
+ };
17
+
18
+ /*
19
+ * The version of Ruby syntax that we should be parsing with. This is used to
20
+ * allow consumers to specify which behavior they want in case they need to
21
+ * parse in the same way as a specific version of CRuby would have.
22
+ */
23
+ typedef enum {
24
+ /*
25
+ * If an explicit version is not provided, the current version of prism will
26
+ * be used.
27
+ */
28
+ PM_OPTIONS_VERSION_UNSET = 0,
29
+
30
+ /* The vendored version of prism in CRuby 3.3.x. */
31
+ PM_OPTIONS_VERSION_CRUBY_3_3 = 1,
32
+
33
+ /* The vendored version of prism in CRuby 3.4.x. */
34
+ PM_OPTIONS_VERSION_CRUBY_3_4 = 2,
35
+
36
+ /* The vendored version of prism in CRuby 4.0.x. */
37
+ PM_OPTIONS_VERSION_CRUBY_3_5 = 3,
38
+
39
+ /* The vendored version of prism in CRuby 4.0.x. */
40
+ PM_OPTIONS_VERSION_CRUBY_4_0 = 3,
41
+
42
+ /* The vendored version of prism in CRuby 4.1.x. */
43
+ PM_OPTIONS_VERSION_CRUBY_4_1 = 4,
44
+
45
+ /* The current version of prism. */
46
+ PM_OPTIONS_VERSION_LATEST = PM_OPTIONS_VERSION_CRUBY_4_1
47
+ } pm_options_version_t;
48
+
49
+ /*
50
+ * Read the version of Ruby that the given string names, such as "3.4", "4.0.1",
51
+ * or "latest", into the given version. NULL names the latest version. Returns
52
+ * false, leaving the version alone, if prism does not support the version.
53
+ */
54
+ bool pm_options_version_parse(const char *version, size_t length, pm_options_version_t *result);
55
+
56
+ /* The options that can be passed to the parser. */
57
+ struct pm_options_t {
58
+ /*
59
+ * The callback to call when additional switches are found in a shebang
60
+ * comment.
61
+ */
62
+ pm_options_shebang_callback_t shebang_callback;
63
+
64
+ /*
65
+ * Any additional data that should be passed along to the shebang callback
66
+ * if one was set.
67
+ */
68
+ void *shebang_callback_data;
69
+
70
+ /* The name of the file that is currently being parsed. */
71
+ pm_string_t filepath;
72
+
73
+ /*
74
+ * The line within the file that the parse starts on. This value is
75
+ * 1-indexed.
76
+ */
77
+ int32_t line;
78
+
79
+ /*
80
+ * The name of the encoding that the source file is in. Note that this must
81
+ * correspond to a name that can be found with Encoding.find in Ruby.
82
+ */
83
+ pm_string_t encoding;
84
+
85
+ /*
86
+ * The number of bytes of C stack that the parser may use, measured from
87
+ * where pm_parse is called. When the recursive descent goes past it, the
88
+ * parser reports that the stack is exhausted instead of overflowing it.
89
+ * SIZE_MAX means no budget was given, in which case only
90
+ * PRISM_DEPTH_MAXIMUM bounds the recursion.
91
+ */
92
+ size_t stack_budget;
93
+
94
+ /* The number of scopes surrounding the code that is being parsed. */
95
+ size_t scopes_count;
96
+
97
+ /*
98
+ * The scopes surrounding the code that is being parsed. For most parses
99
+ * this will be NULL, but for evals it will be the locals that are in scope
100
+ * surrounding the eval. Scopes are ordered from the outermost scope to the
101
+ * innermost one.
102
+ */
103
+ pm_options_scope_t *scopes;
104
+
105
+ /*
106
+ * The version of prism that we should be parsing with. This is used to
107
+ * allow consumers to specify which behavior they want in case they need to
108
+ * parse exactly as a specific version of CRuby.
109
+ */
110
+ pm_options_version_t version;
111
+
112
+ /* A bitset of the various options that were set on the command line. */
113
+ uint8_t command_line;
114
+
115
+ /*
116
+ * Whether or not the frozen string literal option has been set.
117
+ * May be:
118
+ * - PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED
119
+ * - PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED
120
+ * - PM_OPTIONS_FROZEN_STRING_LITERAL_UNSET
121
+ */
122
+ int8_t frozen_string_literal;
123
+
124
+ /*
125
+ * Whether or not the parser should raise an error when it encounters an
126
+ * error. By default this is off, but can be turned on in the case that you
127
+ * want nicely formatted syntax errors. The values that this can be set to
128
+ * are:
129
+ * - 0 - do not raise an error, just return the errors in the result
130
+ * - PM_ERRORS_FORMAT_PLAIN - raise a plain error with no formatting
131
+ * - PM_ERRORS_FORMAT_STYLE - raise an error with bold formatting
132
+ * - PM_ERRORS_FORMAT_COLOR - raise an error with color formatting
133
+ */
134
+ uint8_t raise_error;
135
+
136
+ /*
137
+ * Whether or not the encoding magic comments should be respected. This is a
138
+ * niche use-case where you want to parse a file with a specific encoding
139
+ * but ignore any encoding magic comments at the top of the file.
140
+ */
141
+ bool encoding_locked;
142
+
143
+ /*
144
+ * When the file being parsed is the main script, the shebang will be
145
+ * considered for command-line flags (or for implicit -x). The caller needs
146
+ * to pass this information to the parser so that it can behave correctly.
147
+ */
148
+ bool main_script;
149
+
150
+ /*
151
+ * When the file being parsed is considered a "partial" script, jumps will
152
+ * not be marked as errors if they are not contained within loops/blocks.
153
+ * This is used in the case that you're parsing a script that you know will
154
+ * be embedded inside another script later, but you do not have that context
155
+ * yet. For example, when parsing an ERB template that will be evaluated
156
+ * inside another script.
157
+ */
158
+ bool partial_script;
159
+
160
+ /*
161
+ * Whether or not the parser should freeze the nodes that it creates. This
162
+ * makes it possible to have a deeply frozen AST that is safe to share
163
+ * between concurrency primitives.
164
+ */
165
+ bool freeze;
166
+
167
+ /*
168
+ * Whether or not the parser should attach comments to the nodes that own
169
+ * them after parsing. When this is set, the parser's comment list is
170
+ * sorted by start offset and every comment is assigned an owning node.
171
+ */
172
+ bool attach_comments;
173
+ };
174
+
175
+ /* Free the internal memory associated with the options. */
176
+ void pm_options_cleanup(pm_options_t *options);
177
+
178
+ /*
179
+ * Deserialize an options struct from the given binary string. This is used to
180
+ * pass options to the parser from an FFI call so that consumers of the library
181
+ * from an FFI perspective don't have to worry about the structure of our
182
+ * options structs. Since the source of these calls will be from Ruby
183
+ * implementation internals we assume it is from a trusted source.
184
+ *
185
+ * `data` is assumed to be a valid pointer pointing to well-formed data. The
186
+ * layout of this data should be the same every time, and is described below:
187
+ *
188
+ * | # bytes | field |
189
+ * | ------- | -------------------------- |
190
+ * | `4` | the length of the filepath |
191
+ * | ... | the filepath bytes |
192
+ * | `4` | the line number |
193
+ * | `4` | the length the encoding |
194
+ * | ... | the encoding bytes |
195
+ * | `1` | frozen string literal |
196
+ * | `1` | -p command line option |
197
+ * | `1` | -n command line option |
198
+ * | `1` | -l command line option |
199
+ * | `1` | -a command line option |
200
+ * | `1` | the version |
201
+ * | `1` | encoding locked |
202
+ * | `1` | main script |
203
+ * | `1` | partial script |
204
+ * | `1` | freeze |
205
+ * | `1` | attach comments |
206
+ * | `4` | the number of scopes |
207
+ * | ... | the scopes |
208
+ *
209
+ * The version field is an enum, so it should be one of the following values:
210
+ *
211
+ * | value | version |
212
+ * | ----- | ------------------------- |
213
+ * | `0` | use the latest version of prism |
214
+ * | `1` | use the version of prism that is vendored in CRuby 3.3.0 |
215
+ * | `2` | use the version of prism that is vendored in CRuby 3.4.0 |
216
+ * | `3` | use the version of prism that is vendored in CRuby 4.0.0 |
217
+ * | `4` | use the version of prism that is vendored in CRuby 4.1.0 |
218
+ *
219
+ * Each scope is laid out as follows:
220
+ *
221
+ * | # bytes | field |
222
+ * | ------- | -------------------------- |
223
+ * | `4` | the number of locals |
224
+ * | `1` | the forwarding flags |
225
+ * | ... | the locals |
226
+ *
227
+ * Each local is laid out as follows:
228
+ *
229
+ * | # bytes | field |
230
+ * | ------- | -------------------------- |
231
+ * | `4` | the length of the local |
232
+ * | ... | the local bytes |
233
+ *
234
+ * Some additional things to note about this layout:
235
+ *
236
+ * * The filepath can have a length of 0, in which case we'll consider it an
237
+ * empty string.
238
+ * * The line number should be 0-indexed.
239
+ * * The encoding can have a length of 0, in which case we'll use the default
240
+ * encoding (UTF-8). If it's not 0, it should correspond to a name of an
241
+ * encoding that can be passed to `Encoding.find` in Ruby.
242
+ * * The frozen string literal, encoding locked, main script, and partial script
243
+ * fields are booleans, so their values should be either 0 or 1.
244
+ * * The number of scopes can be 0.
245
+ */
246
+ void pm_options_read(pm_options_t *options, const char *data);
247
+
248
+ #endif