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,562 @@
1
+ #include "prism/internal/static_literals.h"
2
+
3
+ #include "prism/compiler/inline.h"
4
+ #include "prism/compiler/unused.h"
5
+
6
+ #include "prism/internal/allocator.h"
7
+ #include "prism/internal/assert.h"
8
+ #include "prism/internal/buffer.h"
9
+ #include "prism/internal/integer.h"
10
+ #include "prism/internal/isinf.h"
11
+ #include "prism/internal/oom.h"
12
+ #include "prism/internal/parser.h"
13
+ #include "prism/internal/stringy.h"
14
+ #include "prism/internal/xxh3.h"
15
+
16
+ #include <inttypes.h>
17
+ #include <math.h>
18
+ #include <stdlib.h>
19
+ #include <string.h>
20
+
21
+ /**
22
+ * Hash the key built from the given values. Nodes that compare as equal must
23
+ * produce identical keys, so each value is either held directly or is the
24
+ * digest of a variable-length value. The key is an array so that its values are
25
+ * contiguous with no padding between them, which makes every hashed byte
26
+ * defined.
27
+ */
28
+ static PRISM_INLINE uint64_t
29
+ key_hash(uint64_t type, uint64_t a, uint64_t b) {
30
+ const uint64_t key[3] = { type, a, b };
31
+ return pm_xxh3((const uint8_t *) key, sizeof(key));
32
+ }
33
+
34
+ /**
35
+ * Return the hash of the value of an integer. An integer whose words are stored
36
+ * inline keys on its value directly, and a larger one keys on the digest of its
37
+ * words.
38
+ */
39
+ static uint64_t
40
+ integer_hash(const pm_integer_t *integer) {
41
+ uint64_t magnitude = (integer->length > 2)
42
+ ? pm_xxh3((const uint8_t *) integer->words.values, sizeof(uint32_t) * integer->length)
43
+ : pm_integer_uint64_value(integer);
44
+ return key_hash(PM_INTEGER_NODE, integer->negative, magnitude);
45
+ }
46
+
47
+ /**
48
+ * Return the hash of the key of the given node. It is important that nodes that
49
+ * have equivalent static literal values have the same key. This is because we
50
+ * hash these keys to look for duplicates.
51
+ */
52
+ static uint64_t
53
+ node_hash(const pm_parser_t *parser, const pm_node_t *node) {
54
+ switch (PM_NODE_TYPE(node)) {
55
+ case PM_INTEGER_NODE:
56
+ return integer_hash(&((const pm_integer_node_t *) node)->value);
57
+ case PM_SOURCE_LINE_NODE: {
58
+ int64_t line = (int64_t) pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line;
59
+ return key_hash(PM_INTEGER_NODE, line < 0, (uint64_t) (line < 0 ? -line : line));
60
+ }
61
+ case PM_FLOAT_NODE: {
62
+ const pm_float_node_t *cast = (const pm_float_node_t *) node;
63
+ double value = cast->value;
64
+ if (value == 0.0) value = 0.0; /* Normalize -0.0 to 0.0. */
65
+
66
+ uint64_t bits;
67
+ memcpy(&bits, &value, sizeof(bits));
68
+ return key_hash(PM_FLOAT_NODE, bits, 0);
69
+ }
70
+ case PM_RATIONAL_NODE: {
71
+ const pm_rational_node_t *cast = (const pm_rational_node_t *) node;
72
+ return key_hash(PM_RATIONAL_NODE, integer_hash(&cast->numerator), integer_hash(&cast->denominator));
73
+ }
74
+ case PM_IMAGINARY_NODE: {
75
+ const pm_imaginary_node_t *cast = (const pm_imaginary_node_t *) node;
76
+ return key_hash(PM_IMAGINARY_NODE, node_hash(parser, cast->numeric), 0);
77
+ }
78
+ case PM_STRING_NODE: {
79
+ const pm_string_node_t *cast = (const pm_string_node_t *) node;
80
+
81
+ pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
82
+ if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING;
83
+
84
+ return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & mask);
85
+ }
86
+ case PM_SOURCE_FILE_NODE: {
87
+ const pm_source_file_node_t *cast = (const pm_source_file_node_t *) node;
88
+ return key_hash(PM_STRING_NODE, pm_xxh3(pm_string_source(&cast->filepath), pm_string_length(&cast->filepath)), 0);
89
+ }
90
+ case PM_REGULAR_EXPRESSION_NODE: {
91
+ const pm_regular_expression_node_t *cast = (const pm_regular_expression_node_t *) node;
92
+ return key_hash(PM_REGULAR_EXPRESSION_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & (pm_node_flags_t) ~PM_REGULAR_EXPRESSION_FLAGS_ONCE);
93
+ }
94
+ case PM_SYMBOL_NODE: {
95
+ const pm_symbol_node_t *cast = (const pm_symbol_node_t *) node;
96
+
97
+ pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
98
+ if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING;
99
+
100
+ return key_hash(PM_SYMBOL_NODE, pm_xxh3(pm_string_source(&cast->unescaped), pm_string_length(&cast->unescaped)), node->flags & mask);
101
+ }
102
+ case PM_FALSE_NODE:
103
+ case PM_NIL_NODE:
104
+ case PM_SOURCE_ENCODING_NODE:
105
+ case PM_TRUE_NODE:
106
+ return key_hash(PM_NODE_TYPE(node), 0, 0);
107
+ default:
108
+ PRISM_ASSERT(false && "unreachable");
109
+ return 0;
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Compare two values that can be compared with a simple numeric comparison.
115
+ */
116
+ #define COMPARE(left_, right_) (((left_) < (right_)) ? -1 : ((left_) > (right_)) ? 1 : 0)
117
+
118
+ /**
119
+ * Return the integer value of the given node as an int64_t.
120
+ */
121
+ static int64_t
122
+ int64_value(const pm_parser_t *parser, const pm_node_t *node) {
123
+ switch (PM_NODE_TYPE(node)) {
124
+ case PM_INTEGER_NODE: {
125
+ const pm_integer_t *integer = &((const pm_integer_node_t *) node)->value;
126
+ if (integer->length > 1) return integer->negative ? INT64_MIN : INT64_MAX;
127
+
128
+ int64_t value = (int64_t) pm_integer_uint64_value(integer);
129
+ return integer->negative ? -value : value;
130
+ }
131
+ case PM_SOURCE_LINE_NODE:
132
+ return (int64_t) pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line;
133
+ default:
134
+ PRISM_ASSERT(false && "unreachable");
135
+ return 0;
136
+ }
137
+ }
138
+
139
+ /**
140
+ * A comparison function for comparing two IntegerNode or SourceLineNode
141
+ * instances.
142
+ */
143
+ static int
144
+ compare_integer_nodes(const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
145
+ if (PM_NODE_TYPE_P(left, PM_SOURCE_LINE_NODE) || PM_NODE_TYPE_P(right, PM_SOURCE_LINE_NODE)) {
146
+ return COMPARE(int64_value(parser, left), int64_value(parser, right));
147
+ }
148
+
149
+ const pm_integer_t *left_integer = &((const pm_integer_node_t *) left)->value;
150
+ const pm_integer_t *right_integer = &((const pm_integer_node_t *) right)->value;
151
+ return pm_integer_compare(left_integer, right_integer);
152
+ }
153
+
154
+ /**
155
+ * A comparison function for comparing two FloatNode instances.
156
+ */
157
+ static int
158
+ compare_float_nodes(PRISM_UNUSED const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
159
+ return COMPARE(((const pm_float_node_t *) left)->value, ((const pm_float_node_t *) right)->value);
160
+ }
161
+
162
+ /**
163
+ * Return a pointer to the string value of the given node.
164
+ */
165
+ static const pm_string_t *
166
+ string_value(const pm_node_t *node) {
167
+ switch (PM_NODE_TYPE(node)) {
168
+ case PM_STRING_NODE:
169
+ return &((const pm_string_node_t *) node)->unescaped;
170
+ case PM_SOURCE_FILE_NODE:
171
+ return &((const pm_source_file_node_t *) node)->filepath;
172
+ case PM_SYMBOL_NODE:
173
+ return &((const pm_symbol_node_t *) node)->unescaped;
174
+ default:
175
+ PRISM_ASSERT(false && "unreachable");
176
+ return NULL;
177
+ }
178
+ }
179
+
180
+ /**
181
+ * A comparison function for comparing two nodes that have attached strings.
182
+ * Only the flags in the given mask take part in the comparison.
183
+ *
184
+ * The FORCED_* flags record how a literal was written. An escape that locks the
185
+ * encoding to UTF-8 (a `\u` escape above U+007F, or any `\u` in a character
186
+ * literal) sets FORCED_UTF8 whether or not the file is already UTF-8. So in a
187
+ * UTF-8 file a `\u00E9` escape and a literal e-acute carry different flags
188
+ * while being the same string, and the flag is left out of the mask for them
189
+ * to compare equally. A source file node never carries a FORCED_* flag, because
190
+ * every instance of __FILE__ in a parse resolves to the same encoding.
191
+ */
192
+ static int
193
+ compare_string_nodes(const pm_node_t *left, const pm_node_t *right, pm_node_flags_t mask) {
194
+ const pm_string_t *left_string = string_value(left);
195
+ const pm_string_t *right_string = string_value(right);
196
+
197
+ int result = pm_string_compare(left_string, right_string);
198
+ if (result != 0) return result;
199
+
200
+ /*
201
+ * Equal bytes are not enough. In a binary source file the two bytes written
202
+ * as `"\xC3\xA9"` stay BINARY while a `"\u00E9"` escape is forced to UTF-8,
203
+ * so those are distinct keys even though the bytes match.
204
+ */
205
+ return COMPARE(left->flags & mask, right->flags & mask);
206
+ }
207
+
208
+ /**
209
+ * A comparison function for comparing two RegularExpressionNode instances. The
210
+ * once flag is left out because it only controls how many times an
211
+ * interpolated regular expression is compiled, not its value.
212
+ */
213
+ static int
214
+ compare_regular_expression_nodes(PRISM_UNUSED const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
215
+ const pm_regular_expression_node_t *left_regexp = (const pm_regular_expression_node_t *) left;
216
+ const pm_regular_expression_node_t *right_regexp = (const pm_regular_expression_node_t *) right;
217
+
218
+ int result = pm_string_compare(&left_regexp->unescaped, &right_regexp->unescaped);
219
+ if (result != 0) return result;
220
+
221
+ pm_node_flags_t mask = (pm_node_flags_t) ~PM_REGULAR_EXPRESSION_FLAGS_ONCE;
222
+ return COMPARE(left->flags & mask, right->flags & mask);
223
+ }
224
+
225
+ /**
226
+ * Return the type that the given node is compared as. A source line node can
227
+ * equal an integer node and a source file node can equal a string node, so
228
+ * each takes the type of the node it can equal.
229
+ */
230
+ static enum pm_node_type
231
+ static_literal_type(const pm_node_t *node) {
232
+ switch (PM_NODE_TYPE(node)) {
233
+ case PM_SOURCE_LINE_NODE:
234
+ return PM_INTEGER_NODE;
235
+ case PM_SOURCE_FILE_NODE:
236
+ return PM_STRING_NODE;
237
+ default:
238
+ return PM_NODE_TYPE(node);
239
+ }
240
+ }
241
+
242
+ /**
243
+ * A comparison function for comparing any two static literal nodes. Nodes that
244
+ * are compared as different types are never equal.
245
+ */
246
+ static int
247
+ compare_nodes(const pm_parser_t *parser, const pm_node_t *left, const pm_node_t *right) {
248
+ enum pm_node_type left_type = static_literal_type(left);
249
+ enum pm_node_type right_type = static_literal_type(right);
250
+ if (left_type != right_type) return COMPARE(left_type, right_type);
251
+
252
+ switch (left_type) {
253
+ case PM_INTEGER_NODE:
254
+ return compare_integer_nodes(parser, left, right);
255
+ case PM_FLOAT_NODE:
256
+ return compare_float_nodes(parser, left, right);
257
+ case PM_IMAGINARY_NODE:
258
+ return compare_nodes(parser, ((const pm_imaginary_node_t *) left)->numeric, ((const pm_imaginary_node_t *) right)->numeric);
259
+ case PM_RATIONAL_NODE: {
260
+ const pm_rational_node_t *left_rational = (const pm_rational_node_t *) left;
261
+ const pm_rational_node_t *right_rational = (const pm_rational_node_t *) right;
262
+
263
+ int result = pm_integer_compare(&left_rational->denominator, &right_rational->denominator);
264
+ if (result != 0) return result;
265
+
266
+ return pm_integer_compare(&left_rational->numerator, &right_rational->numerator);
267
+ }
268
+ case PM_STRING_NODE: {
269
+ pm_node_flags_t mask = PM_STRING_FLAGS_FORCED_BINARY_ENCODING;
270
+ if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) mask |= PM_STRING_FLAGS_FORCED_UTF8_ENCODING;
271
+ return compare_string_nodes(left, right, mask);
272
+ }
273
+ case PM_SYMBOL_NODE: {
274
+ pm_node_flags_t mask = PM_SYMBOL_FLAGS_FORCED_BINARY_ENCODING | PM_SYMBOL_FLAGS_FORCED_US_ASCII_ENCODING;
275
+ if (parser->encoding != PM_ENCODING_UTF_8_ENTRY) mask |= PM_SYMBOL_FLAGS_FORCED_UTF8_ENCODING;
276
+ return compare_string_nodes(left, right, mask);
277
+ }
278
+ case PM_REGULAR_EXPRESSION_NODE:
279
+ return compare_regular_expression_nodes(parser, left, right);
280
+ case PM_FALSE_NODE:
281
+ case PM_NIL_NODE:
282
+ case PM_SOURCE_ENCODING_NODE:
283
+ case PM_TRUE_NODE:
284
+ return 0;
285
+ default:
286
+ PRISM_ASSERT(false && "unreachable");
287
+ return 0;
288
+ }
289
+ }
290
+
291
+ #undef COMPARE
292
+
293
+ /**
294
+ * Place an entry into a hash table that is known not to hold an equal node, so
295
+ * no comparisons are needed to find its slot.
296
+ */
297
+ static void
298
+ pm_static_literals_place(pm_static_literals_entry_t *entries, uint32_t mask, pm_static_literals_entry_t entry) {
299
+ uint32_t index = (uint32_t) (entry.hash & mask);
300
+ while (entries[index].node != NULL) {
301
+ index = (index + 1) & mask;
302
+ }
303
+ entries[index] = entry;
304
+ }
305
+
306
+ /**
307
+ * Move the nodes of the set into a new hash table with the given capacity. A
308
+ * set that has no hash table yet hashes its inline nodes. A set that already
309
+ * has one moves its entries, which keep their hashes.
310
+ */
311
+ static void
312
+ pm_static_literals_resize(const pm_parser_t *parser, pm_static_literals_t *literals, uint32_t capacity) {
313
+ pm_static_literals_entry_t *entries = xcalloc(capacity, sizeof(pm_static_literals_entry_t));
314
+ if (entries == NULL) PRISM_OOM();
315
+
316
+ /* It turns out to be more efficient to mask the hash value than to use the
317
+ * modulo operator. Because our capacities are always powers of two, we can
318
+ * use a bitwise AND to get the same result as the modulo operator. */
319
+ uint32_t mask = capacity - 1;
320
+
321
+ if (literals->capacity == 0) {
322
+ for (uint32_t index = 0; index < literals->size; index++) {
323
+ pm_node_t *node = literals->nodes[index];
324
+ pm_static_literals_place(entries, mask, (pm_static_literals_entry_t) { .node = node, .hash = node_hash(parser, node) });
325
+ }
326
+ } else {
327
+ for (uint32_t index = 0; index < literals->capacity; index++) {
328
+ if (literals->entries[index].node != NULL) {
329
+ pm_static_literals_place(entries, mask, literals->entries[index]);
330
+ }
331
+ }
332
+
333
+ xfree_sized(literals->entries, literals->capacity * sizeof(pm_static_literals_entry_t));
334
+ }
335
+
336
+ literals->entries = entries;
337
+ literals->capacity = capacity;
338
+ }
339
+
340
+ /**
341
+ * Add a node to the set of static literals. If an equal node is already in the
342
+ * set, it is returned (and replaced by the new node if replace is set).
343
+ * Otherwise the new node is inserted and NULL is returned. Nodes that are not
344
+ * static literals are ignored.
345
+ */
346
+ pm_node_t *
347
+ pm_static_literals_add(const pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node, bool replace) {
348
+ switch (PM_NODE_TYPE(node)) {
349
+ case PM_FALSE_NODE:
350
+ case PM_FLOAT_NODE:
351
+ case PM_IMAGINARY_NODE:
352
+ case PM_INTEGER_NODE:
353
+ case PM_NIL_NODE:
354
+ case PM_RATIONAL_NODE:
355
+ case PM_REGULAR_EXPRESSION_NODE:
356
+ case PM_SOURCE_ENCODING_NODE:
357
+ case PM_SOURCE_FILE_NODE:
358
+ case PM_SOURCE_LINE_NODE:
359
+ case PM_STRING_NODE:
360
+ case PM_SYMBOL_NODE:
361
+ case PM_TRUE_NODE:
362
+ break;
363
+ default:
364
+ return NULL;
365
+ }
366
+
367
+ if (literals->capacity == 0) {
368
+ /* A set without a hash table compares the new node against each of
369
+ * its nodes directly. Comparing nodes of different types returns
370
+ * early, so this costs about as much as hashing the new node. */
371
+ for (uint32_t index = 0; index < literals->size; index++) {
372
+ pm_node_t *existing = literals->nodes[index];
373
+
374
+ if (compare_nodes(parser, existing, node) == 0) {
375
+ if (replace) literals->nodes[index] = node;
376
+ return existing;
377
+ }
378
+ }
379
+
380
+ if (literals->size < PM_STATIC_LITERALS_INLINE_CAPACITY) {
381
+ literals->nodes[literals->size++] = node;
382
+ return NULL;
383
+ }
384
+
385
+ /* The set is full, so its nodes move into a hash table. The table is
386
+ * sized to stay at most half full after this node is added, with room
387
+ * to spare before its first resize. */
388
+ pm_static_literals_resize(parser, literals, PM_STATIC_LITERALS_INLINE_CAPACITY * 4);
389
+ } else if (literals->size * 2 >= literals->capacity) {
390
+ /* If we are out of space, we need to resize the hash. */
391
+ pm_static_literals_resize(parser, literals, literals->capacity * 2);
392
+ }
393
+
394
+ uint64_t hash = node_hash(parser, node);
395
+ uint32_t mask = literals->capacity - 1;
396
+ uint32_t index = (uint32_t) (hash & mask);
397
+
398
+ /* We use linear probing to resolve collisions. This means that if the
399
+ * current index is occupied, we will move to the next index and try again.
400
+ * We are guaranteed that this will eventually find an empty slot because we
401
+ * resize the hash when it gets too full. Entries whose hash differs cannot
402
+ * be equal, so only entries with a matching hash are compared. */
403
+ while (literals->entries[index].node != NULL) {
404
+ const pm_static_literals_entry_t *entry = &literals->entries[index];
405
+ if (entry->hash == hash && compare_nodes(parser, entry->node, node) == 0) break;
406
+ index = (index + 1) & mask;
407
+ }
408
+
409
+ /* If the current index is occupied, we need to return the node that was
410
+ * already in the hash. Otherwise, we can just increment the size and insert
411
+ * the new node. */
412
+ pm_node_t *result = literals->entries[index].node;
413
+
414
+ if (result == NULL) {
415
+ literals->size++;
416
+ literals->entries[index] = (pm_static_literals_entry_t) { .node = node, .hash = hash };
417
+ } else if (replace) {
418
+ literals->entries[index].node = node;
419
+ }
420
+
421
+ return result;
422
+ }
423
+
424
+ /**
425
+ * Free the internal memory associated with the given static literals set.
426
+ */
427
+ void
428
+ pm_static_literals_free(pm_static_literals_t *literals) {
429
+ if (literals->capacity > 0) xfree_sized(literals->entries, literals->capacity * sizeof(pm_static_literals_entry_t));
430
+ }
431
+
432
+ /**
433
+ * A helper to determine if the given node is a static literal that is positive.
434
+ * This is used for formatting imaginary nodes.
435
+ */
436
+ static bool
437
+ pm_static_literal_positive_p(const pm_node_t *node) {
438
+ switch (PM_NODE_TYPE(node)) {
439
+ case PM_FLOAT_NODE:
440
+ return ((const pm_float_node_t *) node)->value > 0;
441
+ case PM_INTEGER_NODE:
442
+ return !((const pm_integer_node_t *) node)->value.negative;
443
+ case PM_RATIONAL_NODE:
444
+ return !((const pm_rational_node_t *) node)->numerator.negative;
445
+ case PM_IMAGINARY_NODE:
446
+ return pm_static_literal_positive_p(((const pm_imaginary_node_t *) node)->numeric);
447
+ default:
448
+ PRISM_ASSERT(false && "unreachable");
449
+ return false;
450
+ }
451
+ }
452
+
453
+ /**
454
+ * Create a string-based representation of the given static literal.
455
+ */
456
+ void
457
+ pm_static_literal_inspect(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node) {
458
+ switch (PM_NODE_TYPE(node)) {
459
+ case PM_FALSE_NODE:
460
+ PM_BUFFER_APPEND_LITERAL(buffer, "false");
461
+ break;
462
+ case PM_FLOAT_NODE: {
463
+ const double value = ((const pm_float_node_t *) node)->value;
464
+
465
+ if (PRISM_ISINF(value)) {
466
+ if (parser->start[node->location.start] == '-') {
467
+ pm_buffer_append_byte(buffer, '-');
468
+ }
469
+ PM_BUFFER_APPEND_LITERAL(buffer, "Infinity");
470
+ } else if (value == 0.0) {
471
+ if (parser->start[node->location.start] == '-') {
472
+ pm_buffer_append_byte(buffer, '-');
473
+ }
474
+ PM_BUFFER_APPEND_LITERAL(buffer, "0.0");
475
+ } else {
476
+ pm_buffer_append_format(buffer, "%g", value);
477
+
478
+ /* %g will not insert a .0 for 1e100 (we'll get back 1e+100). So
479
+ * we check for the decimal point and add it in here if it's not
480
+ * present. */
481
+ if (pm_buffer_index(buffer, '.') == SIZE_MAX) {
482
+ size_t exponent_index = pm_buffer_index(buffer, 'e');
483
+ size_t index = exponent_index == SIZE_MAX ? pm_buffer_length(buffer) : exponent_index;
484
+ pm_buffer_insert(buffer, index, ".0", 2);
485
+ }
486
+ }
487
+
488
+ break;
489
+ }
490
+ case PM_IMAGINARY_NODE: {
491
+ const pm_node_t *numeric = ((const pm_imaginary_node_t *) node)->numeric;
492
+ PM_BUFFER_APPEND_LITERAL(buffer, "(0");
493
+ if (pm_static_literal_positive_p(numeric)) pm_buffer_append_byte(buffer, '+');
494
+ pm_static_literal_inspect(buffer, parser, numeric);
495
+ if (PM_NODE_TYPE_P(numeric, PM_RATIONAL_NODE)) {
496
+ pm_buffer_append_byte(buffer, '*');
497
+ }
498
+ PM_BUFFER_APPEND_LITERAL(buffer, "i)");
499
+ break;
500
+ }
501
+ case PM_INTEGER_NODE:
502
+ pm_integer_string(buffer, &((const pm_integer_node_t *) node)->value);
503
+ break;
504
+ case PM_NIL_NODE:
505
+ PM_BUFFER_APPEND_LITERAL(buffer, "nil");
506
+ break;
507
+ case PM_RATIONAL_NODE: {
508
+ const pm_rational_node_t *rational = (const pm_rational_node_t *) node;
509
+ pm_buffer_append_byte(buffer, '(');
510
+ pm_integer_string(buffer, &rational->numerator);
511
+ pm_buffer_append_byte(buffer, '/');
512
+ pm_integer_string(buffer, &rational->denominator);
513
+ pm_buffer_append_byte(buffer, ')');
514
+ break;
515
+ }
516
+ case PM_REGULAR_EXPRESSION_NODE: {
517
+ const pm_string_t *unescaped = &((const pm_regular_expression_node_t *) node)->unescaped;
518
+ pm_buffer_append_byte(buffer, '/');
519
+ pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
520
+ pm_buffer_append_byte(buffer, '/');
521
+
522
+ if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE)) pm_buffer_append_byte(buffer, 'm');
523
+ if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE)) pm_buffer_append_byte(buffer, 'i');
524
+ if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_EXTENDED)) pm_buffer_append_byte(buffer, 'x');
525
+ if (PM_NODE_FLAG_P(node, PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT)) pm_buffer_append_byte(buffer, 'n');
526
+
527
+ break;
528
+ }
529
+ case PM_SOURCE_ENCODING_NODE:
530
+ pm_buffer_append_format(buffer, "#<Encoding:%s>", parser->encoding->name);
531
+ break;
532
+ case PM_SOURCE_FILE_NODE: {
533
+ const pm_string_t *filepath = &((const pm_source_file_node_t *) node)->filepath;
534
+ pm_buffer_append_byte(buffer, '"');
535
+ pm_buffer_append_source(buffer, pm_string_source(filepath), pm_string_length(filepath), PM_BUFFER_ESCAPING_RUBY);
536
+ pm_buffer_append_byte(buffer, '"');
537
+ break;
538
+ }
539
+ case PM_SOURCE_LINE_NODE:
540
+ pm_buffer_append_format(buffer, "%" PRIi32, pm_line_offset_list_line_column(&parser->line_offsets, node->location.start, parser->start_line).line);
541
+ break;
542
+ case PM_STRING_NODE: {
543
+ const pm_string_t *unescaped = &((const pm_string_node_t *) node)->unescaped;
544
+ pm_buffer_append_byte(buffer, '"');
545
+ pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
546
+ pm_buffer_append_byte(buffer, '"');
547
+ break;
548
+ }
549
+ case PM_SYMBOL_NODE: {
550
+ const pm_string_t *unescaped = &((const pm_symbol_node_t *) node)->unescaped;
551
+ pm_buffer_append_byte(buffer, ':');
552
+ pm_buffer_append_source(buffer, pm_string_source(unescaped), pm_string_length(unescaped), PM_BUFFER_ESCAPING_RUBY);
553
+ break;
554
+ }
555
+ case PM_TRUE_NODE:
556
+ PM_BUFFER_APPEND_LITERAL(buffer, "true");
557
+ break;
558
+ default:
559
+ PRISM_ASSERT(false && "unreachable");
560
+ break;
561
+ }
562
+ }