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,326 @@
1
+ #ifndef PRISM_INTERNAL_ENCODING_H
2
+ #define PRISM_INTERNAL_ENCODING_H
3
+
4
+ #include "prism/compiler/inline.h"
5
+ #include "prism/excludes.h"
6
+ #include "prism/internal/options.h"
7
+
8
+ #include <stdbool.h>
9
+ #include <stddef.h>
10
+ #include <stdint.h>
11
+
12
+ /*
13
+ * The ways in which an encoding answers questions about its characters. Every
14
+ * kind after UTF-8 is a multibyte encoding that is ASCII below 0x80 and whose
15
+ * other characters take the forms listed for it in encoding.c.
16
+ */
17
+ typedef enum {
18
+ /* Every byte is a character. */
19
+ PM_ENCODING_KIND_SINGLE_BYTE,
20
+
21
+ /* Every byte below 0x80 is a character, and every other byte is invalid. */
22
+ PM_ENCODING_KIND_ASCII,
23
+
24
+ /* UTF-8, whose characters have the properties of their code points. */
25
+ PM_ENCODING_KIND_UTF_8,
26
+
27
+ /* EUC-JP and the encodings that share its forms. */
28
+ PM_ENCODING_KIND_EUC_JP,
29
+
30
+ /* Shift_JIS and the encodings that share its forms. */
31
+ PM_ENCODING_KIND_SHIFT_JIS,
32
+
33
+ /* Big5 and the encodings that share its forms. */
34
+ PM_ENCODING_KIND_BIG5,
35
+
36
+ /* CESU-8, whose characters have the properties of their code points. */
37
+ PM_ENCODING_KIND_CESU_8,
38
+
39
+ /* CP949. */
40
+ PM_ENCODING_KIND_CP949,
41
+
42
+ /* Emacs-Mule and the encodings that share its forms. */
43
+ PM_ENCODING_KIND_EMACS_MULE,
44
+
45
+ /* EUC-KR and the encodings that share its forms. */
46
+ PM_ENCODING_KIND_EUC_KR,
47
+
48
+ /* EUC-TW. */
49
+ PM_ENCODING_KIND_EUC_TW,
50
+
51
+ /* GB18030. */
52
+ PM_ENCODING_KIND_GB18030,
53
+
54
+ /* GBK. */
55
+ PM_ENCODING_KIND_GBK
56
+ } pm_encoding_kind_t;
57
+
58
+ /*
59
+ * An encoding that prism can parse source in. It holds what the answers to the
60
+ * questions about its characters come from, which pm_encoding_char_width and
61
+ * the functions after it read.
62
+ */
63
+ typedef struct {
64
+ /*
65
+ * The name of the encoding. This should correspond to a value that can be
66
+ * passed to Encoding.find in Ruby.
67
+ */
68
+ const char *name;
69
+
70
+ /*
71
+ * In a single-byte encoding, the bytes from 0x80 up that begin a constant
72
+ * name, one bit per byte, or NULL if none do.
73
+ */
74
+ const uint8_t *uppercase;
75
+
76
+ /*
77
+ * How the encoding answers questions about its characters, as a
78
+ * pm_encoding_kind_t stored in a byte to keep the table of encodings small.
79
+ */
80
+ uint8_t kind;
81
+ } pm_encoding_t;
82
+
83
+ /*
84
+ * Return true if the encoding is a multibyte encoding. Every kind other than
85
+ * single-byte and ASCII has characters that span more than one byte.
86
+ */
87
+ static PRISM_INLINE bool
88
+ pm_encoding_multibyte_p(const pm_encoding_t *encoding) {
89
+ return encoding->kind != PM_ENCODING_KIND_SINGLE_BYTE && encoding->kind != PM_ENCODING_KIND_ASCII;
90
+ }
91
+
92
+ /* The bit of the ASCII table that marks a letter. */
93
+ #define PRISM_ENCODING_ALPHABETIC_BIT (1 << 0)
94
+
95
+ /* The bit of the ASCII table that marks a letter or a digit. */
96
+ #define PRISM_ENCODING_ALPHANUMERIC_BIT (1 << 1)
97
+
98
+ /*
99
+ * The bit that marks a character that begins a constant name, in the ASCII
100
+ * table, the unicode tables, and the forms of the multibyte encodings.
101
+ */
102
+ #define PRISM_ENCODING_UPPERCASE_BIT (1 << 2)
103
+
104
+ /*
105
+ * Return the number of bytes in the next character in the given encoding, or 0
106
+ * if the bytes do not form one.
107
+ */
108
+ size_t pm_encoding_char_width(const pm_encoding_t *encoding, const uint8_t *b, ptrdiff_t n);
109
+
110
+ /*
111
+ * Return true if the given bytes begin a character in the given encoding that
112
+ * takes more bytes than there are. Together with pm_encoding_char_width, this
113
+ * tells bytes that are cut short of a character from bytes that are invalid,
114
+ * which matters where a character is built up one byte at a time, such as from
115
+ * the escapes of a regular expression.
116
+ */
117
+ bool pm_encoding_char_prefix_p(const pm_encoding_t *encoding, const uint8_t *b, ptrdiff_t n);
118
+
119
+ /*
120
+ * Return the code point of the valid character of the given width at the
121
+ * start of b, if the characters of the encoding are Unicode code points, as in
122
+ * UTF-8 and CESU-8. Otherwise return -1.
123
+ */
124
+ int32_t pm_encoding_unicode_codepoint(const pm_encoding_t *encoding, const uint8_t *b, size_t width);
125
+
126
+ /*
127
+ * Return true if the code point is a decimal digit in the version of Unicode of
128
+ * the given version of Ruby.
129
+ */
130
+ bool pm_encoding_unicode_digit_p(uint32_t codepoint, pm_options_version_t version);
131
+
132
+ /*
133
+ * Return true if the next character in the given encoding is uppercase, or
134
+ * titlecase, in the given version of Ruby. Such a character begins a constant
135
+ * name. The version only matters for an encoding whose characters are Unicode
136
+ * code points, since each version of Ruby uses a version of Unicode.
137
+ */
138
+ bool pm_encoding_isupper_char(const pm_encoding_t *encoding, const uint8_t *b, ptrdiff_t n, pm_options_version_t version);
139
+
140
+ /* Return the size of the next character in the UTF-8 encoding. */
141
+ size_t pm_encoding_utf_8_char_width(const uint8_t *b, ptrdiff_t n);
142
+
143
+ /*
144
+ * Return true if the next character in the UTF-8 encoding is uppercase, or
145
+ * titlecase, in the given version of Ruby.
146
+ */
147
+ bool pm_encoding_utf_8_isupper_char(const uint8_t *b, ptrdiff_t n, pm_options_version_t version);
148
+
149
+ /*
150
+ * This lookup table indicates whether a US-ASCII character is alphabetical,
151
+ * alphanumeric, or uppercase. Every encoding that prism supports is
152
+ * ASCII-compatible, so this table answers for the bytes below 0x80 in all of
153
+ * them.
154
+ */
155
+ extern const uint8_t pm_encoding_ascii_table[256];
156
+
157
+ /*
158
+ * Return true if the byte is an ASCII letter. A byte that is not ASCII is not
159
+ * one, in every encoding.
160
+ */
161
+ static PRISM_INLINE bool
162
+ pm_encoding_ascii_alpha_p(uint8_t byte) {
163
+ return pm_encoding_ascii_table[byte] & PRISM_ENCODING_ALPHABETIC_BIT;
164
+ }
165
+
166
+ /*
167
+ * Return true if the byte is an ASCII uppercase letter. A byte that is not
168
+ * ASCII is not one, in every encoding.
169
+ */
170
+ static PRISM_INLINE bool
171
+ pm_encoding_ascii_upper_p(uint8_t byte) {
172
+ return pm_encoding_ascii_table[byte] & PRISM_ENCODING_UPPERCASE_BIT;
173
+ }
174
+
175
+ /*
176
+ * Return true if the byte is an ASCII letter or digit. A byte that is not ASCII
177
+ * is neither, in every encoding.
178
+ */
179
+ static PRISM_INLINE bool
180
+ pm_encoding_ascii_alnum_p(uint8_t byte) {
181
+ return pm_encoding_ascii_table[byte] & PRISM_ENCODING_ALPHANUMERIC_BIT;
182
+ }
183
+
184
+ /* These are all of the encodings that prism supports. */
185
+ typedef enum {
186
+ PM_ENCODING_UTF_8 = 0,
187
+ PM_ENCODING_US_ASCII,
188
+ PM_ENCODING_ASCII_8BIT,
189
+ PM_ENCODING_EUC_JP,
190
+ PM_ENCODING_WINDOWS_31J,
191
+
192
+ /* We optionally support excluding the full set of encodings to only support the
193
+ * minimum necessary to process Ruby code without encoding comments. */
194
+ #ifndef PRISM_ENCODING_EXCLUDE_FULL
195
+ PM_ENCODING_BIG5,
196
+ PM_ENCODING_BIG5_HKSCS,
197
+ PM_ENCODING_BIG5_UAO,
198
+ PM_ENCODING_CESU_8,
199
+ PM_ENCODING_CP51932,
200
+ PM_ENCODING_CP850,
201
+ PM_ENCODING_CP852,
202
+ PM_ENCODING_CP855,
203
+ PM_ENCODING_CP949,
204
+ PM_ENCODING_CP950,
205
+ PM_ENCODING_CP951,
206
+ PM_ENCODING_EMACS_MULE,
207
+ PM_ENCODING_EUC_JP_MS,
208
+ PM_ENCODING_EUC_JIS_2004,
209
+ PM_ENCODING_EUC_KR,
210
+ PM_ENCODING_EUC_TW,
211
+ PM_ENCODING_GB12345,
212
+ PM_ENCODING_GB18030,
213
+ PM_ENCODING_GB1988,
214
+ PM_ENCODING_GB2312,
215
+ PM_ENCODING_GBK,
216
+ PM_ENCODING_IBM437,
217
+ PM_ENCODING_IBM720,
218
+ PM_ENCODING_IBM737,
219
+ PM_ENCODING_IBM775,
220
+ PM_ENCODING_IBM852,
221
+ PM_ENCODING_IBM855,
222
+ PM_ENCODING_IBM857,
223
+ PM_ENCODING_IBM860,
224
+ PM_ENCODING_IBM861,
225
+ PM_ENCODING_IBM862,
226
+ PM_ENCODING_IBM863,
227
+ PM_ENCODING_IBM864,
228
+ PM_ENCODING_IBM865,
229
+ PM_ENCODING_IBM866,
230
+ PM_ENCODING_IBM869,
231
+ PM_ENCODING_ISO_8859_1,
232
+ PM_ENCODING_ISO_8859_2,
233
+ PM_ENCODING_ISO_8859_3,
234
+ PM_ENCODING_ISO_8859_4,
235
+ PM_ENCODING_ISO_8859_5,
236
+ PM_ENCODING_ISO_8859_6,
237
+ PM_ENCODING_ISO_8859_7,
238
+ PM_ENCODING_ISO_8859_8,
239
+ PM_ENCODING_ISO_8859_9,
240
+ PM_ENCODING_ISO_8859_10,
241
+ PM_ENCODING_ISO_8859_11,
242
+ PM_ENCODING_ISO_8859_13,
243
+ PM_ENCODING_ISO_8859_14,
244
+ PM_ENCODING_ISO_8859_15,
245
+ PM_ENCODING_ISO_8859_16,
246
+ PM_ENCODING_KOI8_R,
247
+ PM_ENCODING_KOI8_U,
248
+ PM_ENCODING_MAC_CENT_EURO,
249
+ PM_ENCODING_MAC_CROATIAN,
250
+ PM_ENCODING_MAC_CYRILLIC,
251
+ PM_ENCODING_MAC_GREEK,
252
+ PM_ENCODING_MAC_ICELAND,
253
+ PM_ENCODING_MAC_JAPANESE,
254
+ PM_ENCODING_MAC_ROMAN,
255
+ PM_ENCODING_MAC_ROMANIA,
256
+ PM_ENCODING_MAC_THAI,
257
+ PM_ENCODING_MAC_TURKISH,
258
+ PM_ENCODING_MAC_UKRAINE,
259
+ PM_ENCODING_SHIFT_JIS,
260
+ PM_ENCODING_SJIS_DOCOMO,
261
+ PM_ENCODING_SJIS_KDDI,
262
+ PM_ENCODING_SJIS_SOFTBANK,
263
+ PM_ENCODING_STATELESS_ISO_2022_JP,
264
+ PM_ENCODING_STATELESS_ISO_2022_JP_KDDI,
265
+ PM_ENCODING_TIS_620,
266
+ PM_ENCODING_UTF8_MAC,
267
+ PM_ENCODING_UTF8_DOCOMO,
268
+ PM_ENCODING_UTF8_KDDI,
269
+ PM_ENCODING_UTF8_SOFTBANK,
270
+ PM_ENCODING_WINDOWS_1250,
271
+ PM_ENCODING_WINDOWS_1251,
272
+ PM_ENCODING_WINDOWS_1252,
273
+ PM_ENCODING_WINDOWS_1253,
274
+ PM_ENCODING_WINDOWS_1254,
275
+ PM_ENCODING_WINDOWS_1255,
276
+ PM_ENCODING_WINDOWS_1256,
277
+ PM_ENCODING_WINDOWS_1257,
278
+ PM_ENCODING_WINDOWS_1258,
279
+ PM_ENCODING_WINDOWS_874,
280
+ #endif
281
+
282
+ PM_ENCODING_MAXIMUM
283
+ } pm_encoding_type_t;
284
+
285
+ /* This is the table of all of the encodings that prism supports. */
286
+ extern const pm_encoding_t pm_encodings[PM_ENCODING_MAXIMUM];
287
+
288
+ /*
289
+ * This is the default UTF-8 encoding. We need a reference to it to quickly
290
+ * create parsers.
291
+ */
292
+ #define PM_ENCODING_UTF_8_ENTRY (&pm_encodings[PM_ENCODING_UTF_8])
293
+
294
+ /*
295
+ * This is the US-ASCII encoding. We need a reference to it to be able to
296
+ * compare against it when a string is being created because it could possibly
297
+ * need to fall back to ASCII-8BIT.
298
+ */
299
+ #define PM_ENCODING_US_ASCII_ENTRY (&pm_encodings[PM_ENCODING_US_ASCII])
300
+
301
+ /*
302
+ * This is the ASCII-8BIT encoding. We need a reference to it so that pm_strpbrk
303
+ * can compare against it because invalid multibyte characters are not a thing
304
+ * in this encoding. It is also needed for handling Regexp encoding flags.
305
+ */
306
+ #define PM_ENCODING_ASCII_8BIT_ENTRY (&pm_encodings[PM_ENCODING_ASCII_8BIT])
307
+
308
+ /*
309
+ * This is the EUC-JP encoding. We need a reference to it to quickly process
310
+ * regular expression modifiers.
311
+ */
312
+ #define PM_ENCODING_EUC_JP_ENTRY (&pm_encodings[PM_ENCODING_EUC_JP])
313
+
314
+ /*
315
+ * This is the Windows-31J encoding. We need a reference to it to quickly
316
+ * process regular expression modifiers.
317
+ */
318
+ #define PM_ENCODING_WINDOWS_31J_ENTRY (&pm_encodings[PM_ENCODING_WINDOWS_31J])
319
+
320
+ /*
321
+ * Parse the given name of an encoding and return a pointer to the corresponding
322
+ * encoding struct if one can be found, otherwise return NULL.
323
+ */
324
+ const pm_encoding_t * pm_encoding_find(const uint8_t *start, const uint8_t *end);
325
+
326
+ #endif
@@ -0,0 +1,66 @@
1
+ /*
2
+ * This module provides functions for working with arbitrary-sized integers.
3
+ */
4
+ #ifndef PRISM_INTERNAL_INTEGER_H
5
+ #define PRISM_INTERNAL_INTEGER_H
6
+
7
+ #include "prism/arena.h"
8
+ #include "prism/buffer.h"
9
+ #include "prism/integer.h"
10
+
11
+ #include <stdint.h>
12
+
13
+ /* Return an integer holding the given non-negative value. */
14
+ static PRISM_INLINE pm_integer_t
15
+ pm_integer_uint64(uint64_t value) {
16
+ return (pm_integer_t) {
17
+ .length = value == 0 ? 0 : value <= UINT32_MAX ? 1 : 2,
18
+ .words = { .value = { (uint32_t) value, (uint32_t) (value >> 32) } }
19
+ };
20
+ }
21
+
22
+ /*
23
+ * An enum controlling the base of an integer. It is expected that the base is
24
+ * already known before parsing the integer, even though it could be derived
25
+ * from the string itself.
26
+ */
27
+ typedef enum {
28
+ /* The binary base, indicated by a 0b or 0B prefix. */
29
+ PM_INTEGER_BASE_BINARY = 2,
30
+
31
+ /* The octal base, indicated by a 0, 0o, or 0O prefix. */
32
+ PM_INTEGER_BASE_OCTAL = 8,
33
+
34
+ /* The decimal base, indicated by a 0d, 0D, or empty prefix. */
35
+ PM_INTEGER_BASE_DECIMAL = 10,
36
+
37
+ /* The hexadecimal base, indicated by a 0x or 0X prefix. */
38
+ PM_INTEGER_BASE_HEXADECIMAL = 16
39
+ } pm_integer_base_t;
40
+
41
+ /*
42
+ * Parse an integer from a string. This assumes that the format of the integer
43
+ * has already been validated, as internal validation checks are not performed
44
+ * here. The words of an integer that does not fit in two words are allocated
45
+ * from the given arena.
46
+ */
47
+ void pm_integer_parse(pm_arena_t *arena, pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end);
48
+
49
+ /*
50
+ * Compare two integers. This function returns -1 if the left integer is less
51
+ * than the right integer, 0 if they are equal, and 1 if the left integer is
52
+ * greater than the right integer.
53
+ */
54
+ int pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right);
55
+
56
+ /*
57
+ * Parse a float rational literal like 1.5r, given its digits and decimal point
58
+ * without the r suffix, into a numerator and denominator in lowest terms. Words
59
+ * of either that do not fit in two words are allocated from the given arena.
60
+ */
61
+ void pm_integer_parse_rational(pm_arena_t *arena, pm_integer_t *numerator, pm_integer_t *denominator, const uint8_t *start, const uint8_t *end);
62
+
63
+ /* Convert an integer to a decimal string. */
64
+ void pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer);
65
+
66
+ #endif
@@ -0,0 +1,16 @@
1
+ #ifndef PRISM_INTERNAL_ISINF_H
2
+ #define PRISM_INTERNAL_ISINF_H
3
+
4
+ /*
5
+ * isinf on POSIX systems accepts a float, a double, or a long double. But mingw
6
+ * didn't provide an isinf macro, only an isinf function that only accepts
7
+ * floats, so we need to use _finite instead.
8
+ */
9
+ #ifdef __MINGW64__
10
+ #include <float.h>
11
+ #define PRISM_ISINF(x) (!_finite(x))
12
+ #else
13
+ #define PRISM_ISINF(x) isinf(x)
14
+ #endif
15
+
16
+ #endif
@@ -0,0 +1,50 @@
1
+ #ifndef PRISM_INTERNAL_LINE_OFFSET_LIST_H
2
+ #define PRISM_INTERNAL_LINE_OFFSET_LIST_H
3
+
4
+ #include "prism/compiler/force_inline.h"
5
+
6
+ #include "prism/internal/assert.h"
7
+
8
+ #include "prism/arena.h"
9
+ #include "prism/line_offset_list.h"
10
+
11
+ /* Initialize a new line offset list with the given capacity. */
12
+ void pm_line_offset_list_init(pm_arena_t *arena, pm_line_offset_list_t *list, size_t capacity);
13
+
14
+ /* Clear out the offsets that have been appended to the list. */
15
+ void pm_line_offset_list_clear(pm_line_offset_list_t *list);
16
+
17
+ /* Append a new offset to the list (slow path with resize). */
18
+ void pm_line_offset_list_append_slow(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor);
19
+
20
+ /* Add the zero-based line index to the starting line with defined 32-bit
21
+ * wraparound. Keep both the addition and conversion free of signed overflow. */
22
+ static PRISM_FORCE_INLINE int32_t
23
+ pm_line_offset_list_line_number(size_t index, int32_t start_line) {
24
+ uint32_t line = (uint32_t) index + (uint32_t) start_line;
25
+ if (line <= INT32_MAX) return (int32_t) line;
26
+
27
+ return INT32_MIN + (int32_t) (line - ((uint32_t) INT32_MAX + 1));
28
+ }
29
+
30
+ /* Append a new offset to the list. */
31
+ static PRISM_FORCE_INLINE void
32
+ pm_line_offset_list_append(pm_arena_t *arena, pm_line_offset_list_t *list, uint32_t cursor) {
33
+ /* Line and column lookups binary search this list, so it must stay strictly
34
+ * increasing. */
35
+ PRISM_ASSERT(list->size == 0 || cursor > list->offsets[list->size - 1]);
36
+
37
+ if (list->size < list->capacity) {
38
+ list->offsets[list->size++] = cursor;
39
+ } else {
40
+ pm_line_offset_list_append_slow(arena, list, cursor);
41
+ }
42
+ }
43
+
44
+ /*
45
+ * Returns the line of the given offset. If the offset is not in the list, the
46
+ * line of the closest offset less than the given offset is returned.
47
+ */
48
+ int32_t pm_line_offset_list_line(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line);
49
+
50
+ #endif
@@ -0,0 +1,62 @@
1
+ #ifndef PRISM_INTERNAL_LIST_H
2
+ #define PRISM_INTERNAL_LIST_H
3
+
4
+ #include <stddef.h>
5
+
6
+ /*
7
+ * This struct represents an abstract linked list that provides common
8
+ * functionality. It is meant to be used any time a linked list is necessary to
9
+ * store data.
10
+ *
11
+ * The linked list itself operates off a set of pointers. Because the pointers
12
+ * are not necessarily sequential, they can be of any size. We use this fact to
13
+ * allow the consumer of this linked list to extend the node struct to include
14
+ * any data they want. This is done by using the pm_list_node_t as the first
15
+ * member of the struct.
16
+ *
17
+ * For example, if we want to store a list of integers, we can do the following:
18
+ *
19
+ * ```c
20
+ * typedef struct {
21
+ * pm_list_node_t node;
22
+ * int value;
23
+ * } pm_int_node_t;
24
+ *
25
+ * pm_list_t list = { 0 };
26
+ * pm_int_node_t *node = xmalloc(sizeof(pm_int_node_t));
27
+ * node->value = 5;
28
+ *
29
+ * pm_list_append(&list, &node->node);
30
+ * ```
31
+ *
32
+ * The pm_list_t struct is used to represent the overall linked list. It
33
+ * contains a pointer to the head and tail of the list. This allows for easy
34
+ * iteration and appending of new nodes.
35
+ */
36
+ typedef struct pm_list_node {
37
+ /* A pointer to the next node in the list. */
38
+ struct pm_list_node *next;
39
+ } pm_list_node_t;
40
+
41
+ /*
42
+ * This represents the overall linked list. It keeps a pointer to the head and
43
+ * tail so that iteration is easy and pushing new nodes is easy.
44
+ */
45
+ typedef struct {
46
+ /* The size of the list. */
47
+ size_t size;
48
+
49
+ /* A pointer to the head of the list. */
50
+ pm_list_node_t *head;
51
+
52
+ /* A pointer to the tail of the list. */
53
+ pm_list_node_t *tail;
54
+ } pm_list_t;
55
+
56
+ /* Returns the size of the list. */
57
+ size_t pm_list_size(pm_list_t *list);
58
+
59
+ /* Append a node to the given list. */
60
+ void pm_list_append(pm_list_t *list, pm_list_node_t *node);
61
+
62
+ #endif
@@ -0,0 +1,23 @@
1
+ #ifndef PRISM_INTERNAL_MAGIC_COMMENTS_H
2
+ #define PRISM_INTERNAL_MAGIC_COMMENTS_H
3
+
4
+ #include "prism/magic_comments.h"
5
+
6
+ #include "prism/internal/list.h"
7
+
8
+ /*
9
+ * This is a node in the linked list of magic comments that we've found while
10
+ * parsing.
11
+ */
12
+ struct pm_magic_comment_t {
13
+ /* The embedded base node. */
14
+ pm_list_node_t node;
15
+
16
+ /* The key of the magic comment. */
17
+ pm_location_t key;
18
+
19
+ /* The value of the magic comment. */
20
+ pm_location_t value;
21
+ };
22
+
23
+ #endif
@@ -0,0 +1,15 @@
1
+ #ifndef PRISM_INTERNAL_MEMCHR_H
2
+ #define PRISM_INTERNAL_MEMCHR_H
3
+
4
+ #include "prism/internal/encoding.h"
5
+
6
+ #include <stddef.h>
7
+
8
+ /*
9
+ * We need to roll our own memchr to handle cases where the encoding changes and
10
+ * we need to search for a character in a buffer that could be the trailing byte
11
+ * of a multibyte character.
12
+ */
13
+ const void * pm_memchr(const void *source, int character, size_t number, bool encoding_changed, const pm_encoding_t *encoding);
14
+
15
+ #endif