prism 2.0.0-arm-linux-gnu

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 (233) hide show
  1. checksums.yaml +7 -0
  2. data/BSDmakefile +58 -0
  3. data/CHANGELOG.md +881 -0
  4. data/CODE_OF_CONDUCT.md +76 -0
  5. data/CONTRIBUTING.md +58 -0
  6. data/LICENSE.md +7 -0
  7. data/Makefile +145 -0
  8. data/README.md +81 -0
  9. data/config.yml +4752 -0
  10. data/docs/build_system.md +119 -0
  11. data/docs/configuration.md +69 -0
  12. data/docs/cruby_compilation.md +27 -0
  13. data/docs/design.md +53 -0
  14. data/docs/encoding.md +121 -0
  15. data/docs/fuzzing.md +81 -0
  16. data/docs/heredocs.md +36 -0
  17. data/docs/javascript.md +133 -0
  18. data/docs/local_variable_depth.md +229 -0
  19. data/docs/mapping.md +117 -0
  20. data/docs/parser_translation.md +24 -0
  21. data/docs/parsing_rules.md +22 -0
  22. data/docs/releasing.md +147 -0
  23. data/docs/relocation.md +32 -0
  24. data/docs/ripper_translation.md +63 -0
  25. data/docs/ruby_api.md +45 -0
  26. data/docs/ruby_parser_translation.md +19 -0
  27. data/docs/serialization.md +244 -0
  28. data/docs/testing.md +72 -0
  29. data/ext/prism/api_node.c +7005 -0
  30. data/ext/prism/extconf.rb +126 -0
  31. data/ext/prism/extension.c +1768 -0
  32. data/ext/prism/extension.h +19 -0
  33. data/include/prism/arena.h +45 -0
  34. data/include/prism/arena.hpp +42 -0
  35. data/include/prism/ast.h +10381 -0
  36. data/include/prism/ast.hpp +641 -0
  37. data/include/prism/buffer.h +60 -0
  38. data/include/prism/buffer.hpp +55 -0
  39. data/include/prism/comments.h +72 -0
  40. data/include/prism/compiler/accel.h +26 -0
  41. data/include/prism/compiler/align.h +36 -0
  42. data/include/prism/compiler/assume.h +25 -0
  43. data/include/prism/compiler/branch.h +26 -0
  44. data/include/prism/compiler/bswap.h +46 -0
  45. data/include/prism/compiler/cplusplus.hpp +16 -0
  46. data/include/prism/compiler/endianness.h +34 -0
  47. data/include/prism/compiler/exceptions.hpp +17 -0
  48. data/include/prism/compiler/exported.h +24 -0
  49. data/include/prism/compiler/fallthrough.h +22 -0
  50. data/include/prism/compiler/filesystem.h +32 -0
  51. data/include/prism/compiler/flex_array.h +19 -0
  52. data/include/prism/compiler/force_inline.h +21 -0
  53. data/include/prism/compiler/format.h +27 -0
  54. data/include/prism/compiler/inline.h +17 -0
  55. data/include/prism/compiler/lifetimebound.hpp +25 -0
  56. data/include/prism/compiler/nodiscard.h +22 -0
  57. data/include/prism/compiler/nonnull.h +18 -0
  58. data/include/prism/compiler/unused.h +18 -0
  59. data/include/prism/constant_pool.h +106 -0
  60. data/include/prism/diagnostic.h +111 -0
  61. data/include/prism/errors_format.h +61 -0
  62. data/include/prism/excludes.h +29 -0
  63. data/include/prism/integer.h +41 -0
  64. data/include/prism/internal/allocator.h +68 -0
  65. data/include/prism/internal/allocator_debug.h +88 -0
  66. data/include/prism/internal/arena.h +108 -0
  67. data/include/prism/internal/bit.h +42 -0
  68. data/include/prism/internal/buffer.h +94 -0
  69. data/include/prism/internal/char.h +145 -0
  70. data/include/prism/internal/comments.h +53 -0
  71. data/include/prism/internal/constant_pool.h +152 -0
  72. data/include/prism/internal/diagnostic.h +389 -0
  73. data/include/prism/internal/encoding.h +242 -0
  74. data/include/prism/internal/integer.h +68 -0
  75. data/include/prism/internal/isinf.h +16 -0
  76. data/include/prism/internal/line_offset_list.h +34 -0
  77. data/include/prism/internal/list.h +62 -0
  78. data/include/prism/internal/magic_comments.h +23 -0
  79. data/include/prism/internal/memchr.h +15 -0
  80. data/include/prism/internal/node.h +42 -0
  81. data/include/prism/internal/options.h +232 -0
  82. data/include/prism/internal/parser.h +1004 -0
  83. data/include/prism/internal/regexp.h +26 -0
  84. data/include/prism/internal/serialize.h +39 -0
  85. data/include/prism/internal/source.h +72 -0
  86. data/include/prism/internal/static_literals.h +100 -0
  87. data/include/prism/internal/stringy.h +30 -0
  88. data/include/prism/internal/strncasecmp.h +18 -0
  89. data/include/prism/internal/strpbrk.h +33 -0
  90. data/include/prism/internal/tokens.h +11 -0
  91. data/include/prism/internal/xxh3.h +16 -0
  92. data/include/prism/json.h +40 -0
  93. data/include/prism/line_offset_list.h +69 -0
  94. data/include/prism/magic_comments.h +53 -0
  95. data/include/prism/node.h +139 -0
  96. data/include/prism/node.hpp +272 -0
  97. data/include/prism/node_type.hpp +38 -0
  98. data/include/prism/options.h +363 -0
  99. data/include/prism/options.hpp +43 -0
  100. data/include/prism/parser.h +433 -0
  101. data/include/prism/parser.hpp +274 -0
  102. data/include/prism/prettyprint.h +39 -0
  103. data/include/prism/serialize.h +123 -0
  104. data/include/prism/source.h +163 -0
  105. data/include/prism/stream.h +36 -0
  106. data/include/prism/string_query.h +82 -0
  107. data/include/prism/stringy.h +61 -0
  108. data/include/prism/stringy.hpp +31 -0
  109. data/include/prism/version.h +46 -0
  110. data/include/prism.h +123 -0
  111. data/include/prism.hpp +24 -0
  112. data/lib/prism/3.3/prism.so +0 -0
  113. data/lib/prism/3.4/prism.so +0 -0
  114. data/lib/prism/4.0/prism.so +0 -0
  115. data/lib/prism/compiler.rb +815 -0
  116. data/lib/prism/desugar_compiler.rb +463 -0
  117. data/lib/prism/dispatcher.rb +2383 -0
  118. data/lib/prism/dot_visitor.rb +4848 -0
  119. data/lib/prism/dsl.rb +1369 -0
  120. data/lib/prism/ffi.rb +746 -0
  121. data/lib/prism/inspect_visitor.rb +2425 -0
  122. data/lib/prism/lex_compat.rb +953 -0
  123. data/lib/prism/mutation_compiler.rb +780 -0
  124. data/lib/prism/node.rb +33940 -0
  125. data/lib/prism/node_ext.rb +504 -0
  126. data/lib/prism/node_find.rb +215 -0
  127. data/lib/prism/parse_result/errors.rb +72 -0
  128. data/lib/prism/parse_result/newlines.rb +423 -0
  129. data/lib/prism/parse_result.rb +1236 -0
  130. data/lib/prism/pattern.rb +314 -0
  131. data/lib/prism/polyfill/append_as_bytes.rb +15 -0
  132. data/lib/prism/polyfill/byteindex.rb +13 -0
  133. data/lib/prism/polyfill/scan_byte.rb +14 -0
  134. data/lib/prism/polyfill/unpack1.rb +14 -0
  135. data/lib/prism/polyfill/warn.rb +36 -0
  136. data/lib/prism/reflection.rb +427 -0
  137. data/lib/prism/relocation.rb +567 -0
  138. data/lib/prism/serialize.rb +4589 -0
  139. data/lib/prism/string_query.rb +54 -0
  140. data/lib/prism/translation/parser/builder.rb +70 -0
  141. data/lib/prism/translation/parser/compiler.rb +2226 -0
  142. data/lib/prism/translation/parser/lexer.rb +769 -0
  143. data/lib/prism/translation/parser.rb +376 -0
  144. data/lib/prism/translation/parser_current.rb +26 -0
  145. data/lib/prism/translation/parser_versions.rb +36 -0
  146. data/lib/prism/translation/ripper/filter.rb +53 -0
  147. data/lib/prism/translation/ripper/lexer.rb +133 -0
  148. data/lib/prism/translation/ripper/sexp.rb +118 -0
  149. data/lib/prism/translation/ripper/shim.rb +7 -0
  150. data/lib/prism/translation/ripper.rb +4278 -0
  151. data/lib/prism/translation/ruby_parser.rb +1721 -0
  152. data/lib/prism/translation.rb +20 -0
  153. data/lib/prism/visitor.rb +1440 -0
  154. data/lib/prism.rb +168 -0
  155. data/prism.gemspec +251 -0
  156. data/rbi/generated/prism/compiler.rbi +489 -0
  157. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  158. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  159. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  160. data/rbi/generated/prism/dsl.rbi +751 -0
  161. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  162. data/rbi/generated/prism/lex_compat.rbi +162 -0
  163. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  164. data/rbi/generated/prism/node.rbi +15143 -0
  165. data/rbi/generated/prism/node_ext.rbi +259 -0
  166. data/rbi/generated/prism/node_find.rbi +79 -0
  167. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  168. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  169. data/rbi/generated/prism/parse_result.rbi +776 -0
  170. data/rbi/generated/prism/pattern.rbi +134 -0
  171. data/rbi/generated/prism/reflection.rbi +89 -0
  172. data/rbi/generated/prism/relocation.rbi +329 -0
  173. data/rbi/generated/prism/serialize.rbi +185 -0
  174. data/rbi/generated/prism/string_query.rbi +43 -0
  175. data/rbi/generated/prism/translation.rbi +7 -0
  176. data/rbi/generated/prism/visitor.rbi +650 -0
  177. data/rbi/generated/prism.rbi +86 -0
  178. data/rbi/prism/translation/parser.rbi +11 -0
  179. data/rbi/prism/translation/parser_versions.rbi +28 -0
  180. data/rbi/prism/translation/ripper.rbi +15 -0
  181. data/rbi/rubyvm/node_find.rbi +14 -0
  182. data/sig/generated/prism/compiler.rbs +492 -0
  183. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  184. data/sig/generated/prism/dispatcher.rbs +1292 -0
  185. data/sig/generated/prism/dot_visitor.rbs +635 -0
  186. data/sig/generated/prism/dsl.rbs +928 -0
  187. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  188. data/sig/generated/prism/lex_compat.rbs +166 -0
  189. data/sig/generated/prism/mutation_compiler.rbs +464 -0
  190. data/sig/generated/prism/node.rbs +21632 -0
  191. data/sig/generated/prism/node_ext.rbs +290 -0
  192. data/sig/generated/prism/node_find.rbs +90 -0
  193. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  194. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  195. data/sig/generated/prism/parse_result.rbs +871 -0
  196. data/sig/generated/prism/pattern.rbs +156 -0
  197. data/sig/generated/prism/reflection.rbs +101 -0
  198. data/sig/generated/prism/relocation.rbs +428 -0
  199. data/sig/generated/prism/serialize.rbs +200 -0
  200. data/sig/generated/prism/string_query.rbs +43 -0
  201. data/sig/generated/prism/translation.rbs +8 -0
  202. data/sig/generated/prism/visitor.rbs +1111 -0
  203. data/sig/generated/prism.rbs +84 -0
  204. data/sig/manifest.yaml +2 -0
  205. data/src/arena.c +117 -0
  206. data/src/buffer.c +389 -0
  207. data/src/char.c +289 -0
  208. data/src/comments.c +454 -0
  209. data/src/constant_pool.c +406 -0
  210. data/src/diagnostic.c +894 -0
  211. data/src/encoding.c +5345 -0
  212. data/src/errors_format.c +519 -0
  213. data/src/integer.c +681 -0
  214. data/src/json.c +5733 -0
  215. data/src/line_offset_list.c +100 -0
  216. data/src/list.c +24 -0
  217. data/src/memchr.c +37 -0
  218. data/src/node.c +4363 -0
  219. data/src/options.c +445 -0
  220. data/src/parser.c +371 -0
  221. data/src/prettyprint.c +9024 -0
  222. data/src/prism.c +23901 -0
  223. data/src/regexp.c +1705 -0
  224. data/src/serialize.c +2660 -0
  225. data/src/source.c +524 -0
  226. data/src/static_literals.c +685 -0
  227. data/src/string_query.c +180 -0
  228. data/src/stringy.c +61 -0
  229. data/src/strncasecmp.c +37 -0
  230. data/src/strpbrk.c +447 -0
  231. data/src/tokens.c +724 -0
  232. data/src/xxh3.c +292 -0
  233. metadata +276 -0
data/src/options.c ADDED
@@ -0,0 +1,445 @@
1
+ #include "prism/internal/options.h"
2
+
3
+ #include "prism/compiler/inline.h"
4
+
5
+ #include "prism/internal/allocator.h"
6
+ #include "prism/internal/char.h"
7
+ #include "prism/internal/stringy.h"
8
+
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+
12
+ /**
13
+ * Allocate a new options struct. If the options struct cannot be allocated,
14
+ * this function aborts the process.
15
+ */
16
+ pm_options_t *
17
+ pm_options_new(void) {
18
+ pm_options_t *options = xcalloc(1, sizeof(pm_options_t));
19
+ if (options == NULL) abort();
20
+
21
+ options->line = 1;
22
+ return options;
23
+ }
24
+
25
+ /**
26
+ * Free the internal memory associated with the options.
27
+ */
28
+ void
29
+ pm_options_cleanup(pm_options_t *options) {
30
+ for (size_t scope_index = 0; scope_index < options->scopes_count; scope_index++) {
31
+ pm_options_scope_t *scope = &options->scopes[scope_index];
32
+ xfree_sized(scope->locals, scope->locals_count * sizeof(pm_string_t));
33
+ }
34
+
35
+ xfree_sized(options->scopes, options->scopes_count * sizeof(pm_options_scope_t));
36
+ }
37
+
38
+ /**
39
+ * Free both the held memory of the given options struct and the struct itself.
40
+ *
41
+ * @param options The options struct to free.
42
+ */
43
+ void
44
+ pm_options_free(pm_options_t *options) {
45
+ pm_options_cleanup(options);
46
+ xfree_sized(options, sizeof(pm_options_t));
47
+ }
48
+
49
+ /**
50
+ * Set the shebang callback option on the given options struct.
51
+ */
52
+ void
53
+ pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data) {
54
+ options->shebang_callback = shebang_callback;
55
+ options->shebang_callback_data = shebang_callback_data;
56
+ }
57
+
58
+ /**
59
+ * Get the filepath option on the given options struct.
60
+ */
61
+ const pm_string_t *
62
+ pm_options_filepath(const pm_options_t *options) {
63
+ return &options->filepath;
64
+ }
65
+
66
+ /**
67
+ * Set the filepath option on the given options struct.
68
+ */
69
+ void
70
+ pm_options_filepath_set(pm_options_t *options, const char *filepath) {
71
+ pm_string_init(&options->filepath, (const uint8_t *) filepath, strlen(filepath));
72
+ }
73
+
74
+ /**
75
+ * Set the encoding option on the given options struct.
76
+ */
77
+ void
78
+ pm_options_encoding_set(pm_options_t *options, const char *encoding) {
79
+ pm_string_init(&options->encoding, (const uint8_t *) encoding, strlen(encoding));
80
+ }
81
+
82
+ /**
83
+ * Set the encoding_locked option on the given options struct.
84
+ */
85
+ void
86
+ pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked) {
87
+ options->encoding_locked = encoding_locked;
88
+ }
89
+
90
+ /**
91
+ * Set the line option on the given options struct.
92
+ */
93
+ void
94
+ pm_options_line_set(pm_options_t *options, int32_t line) {
95
+ options->line = line;
96
+ }
97
+
98
+ /**
99
+ * Set the frozen string literal option on the given options struct.
100
+ */
101
+ void
102
+ pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal) {
103
+ options->frozen_string_literal = frozen_string_literal ? PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED : PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED;
104
+ }
105
+
106
+ /**
107
+ * Sets the command line option on the given options struct.
108
+ */
109
+ void
110
+ pm_options_command_line_set(pm_options_t *options, uint8_t command_line) {
111
+ options->command_line = command_line;
112
+ }
113
+
114
+ /**
115
+ * Checks if the given slice represents a number.
116
+ */
117
+ static PRISM_INLINE bool
118
+ is_number(const char *string, size_t length) {
119
+ return pm_strspn_decimal_digit((const uint8_t *) string, (ptrdiff_t) length) == length;
120
+ }
121
+
122
+ /**
123
+ * Set the version option on the given options struct by parsing the given
124
+ * string. If the string contains an invalid option, this returns false.
125
+ * Otherwise, it returns true.
126
+ */
127
+ bool
128
+ pm_options_version_set(pm_options_t *options, const char *version, size_t length) {
129
+ if (version == NULL) {
130
+ options->version = PM_OPTIONS_VERSION_LATEST;
131
+ return true;
132
+ }
133
+
134
+ if (length == 3) {
135
+ if (strncmp(version, "3.3", 3) == 0) {
136
+ options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
137
+ return true;
138
+ }
139
+
140
+ if (strncmp(version, "3.4", 3) == 0) {
141
+ options->version = PM_OPTIONS_VERSION_CRUBY_3_4;
142
+ return true;
143
+ }
144
+
145
+ if (strncmp(version, "3.5", 3) == 0 || strncmp(version, "4.0", 3) == 0) {
146
+ options->version = PM_OPTIONS_VERSION_CRUBY_4_0;
147
+ return true;
148
+ }
149
+
150
+ if (strncmp(version, "4.1", 3) == 0) {
151
+ options->version = PM_OPTIONS_VERSION_CRUBY_4_1;
152
+ return true;
153
+ }
154
+
155
+ return false;
156
+ }
157
+
158
+ if (length >= 4 && is_number(version + 4, length - 4)) {
159
+ if (strncmp(version, "3.3.", 4) == 0) {
160
+ options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
161
+ return true;
162
+ }
163
+
164
+ if (strncmp(version, "3.4.", 4) == 0) {
165
+ options->version = PM_OPTIONS_VERSION_CRUBY_3_4;
166
+ return true;
167
+ }
168
+
169
+ if (strncmp(version, "3.5.", 4) == 0 || strncmp(version, "4.0.", 4) == 0) {
170
+ options->version = PM_OPTIONS_VERSION_CRUBY_4_0;
171
+ return true;
172
+ }
173
+
174
+ if (strncmp(version, "4.1.", 4) == 0) {
175
+ options->version = PM_OPTIONS_VERSION_CRUBY_4_1;
176
+ return true;
177
+ }
178
+ }
179
+
180
+ if (length == 6) {
181
+ if (strncmp(version, "latest", 6) == 0) {
182
+ options->version = PM_OPTIONS_VERSION_LATEST;
183
+ return true;
184
+ }
185
+ }
186
+
187
+ return false;
188
+ }
189
+
190
+ /**
191
+ * Set the version option on the given options struct to the lowest version of
192
+ * Ruby that prism supports.
193
+ */
194
+ void
195
+ pm_options_version_set_lowest(pm_options_t *options) {
196
+ options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
197
+ }
198
+
199
+ /**
200
+ * Set the version option on the given options struct to the highest version of
201
+ * Ruby that prism supports.
202
+ */
203
+ void
204
+ pm_options_version_set_highest(pm_options_t *options) {
205
+ options->version = PM_OPTIONS_VERSION_LATEST;
206
+ }
207
+
208
+ /**
209
+ * Set the main script option on the given options struct.
210
+ */
211
+ void
212
+ pm_options_main_script_set(pm_options_t *options, bool main_script) {
213
+ options->main_script = main_script;
214
+ }
215
+
216
+ /**
217
+ * Set the partial script option on the given options struct.
218
+ */
219
+ void
220
+ pm_options_partial_script_set(pm_options_t *options, bool partial_script) {
221
+ options->partial_script = partial_script;
222
+ }
223
+
224
+ /**
225
+ * Get the freeze option on the given options struct.
226
+ */
227
+ bool
228
+ pm_options_freeze(const pm_options_t *options) {
229
+ return options->freeze;
230
+ }
231
+
232
+ /**
233
+ * Set the freeze option on the given options struct.
234
+ */
235
+ void
236
+ pm_options_freeze_set(pm_options_t *options, bool freeze) {
237
+ options->freeze = freeze;
238
+ }
239
+
240
+ /**
241
+ * Get the attach_comments option on the given options struct.
242
+ */
243
+ bool
244
+ pm_options_attach_comments(const pm_options_t *options) {
245
+ return options->attach_comments;
246
+ }
247
+
248
+ /**
249
+ * Set the attach_comments option on the given options struct.
250
+ */
251
+ void
252
+ pm_options_attach_comments_set(pm_options_t *options, bool attach_comments) {
253
+ options->attach_comments = attach_comments;
254
+ }
255
+
256
+ /**
257
+ * Get the raise_error option on the given options struct.
258
+ */
259
+ uint8_t pm_options_raise_error(const pm_options_t *options) {
260
+ return options->raise_error;
261
+ }
262
+
263
+ /**
264
+ * Set the raise_error option on the given options struct.
265
+ */
266
+ void pm_options_raise_error_set(pm_options_t *options, uint8_t raise_error) {
267
+ options->raise_error = raise_error;
268
+ }
269
+
270
+ // For some reason, GCC analyzer thinks we're leaking allocated scopes and
271
+ // locals here, even though we definitely aren't. This is a false positive.
272
+ // Ideally we wouldn't need to suppress this.
273
+ #if defined(__GNUC__) && (__GNUC__ >= 10)
274
+ #pragma GCC diagnostic push
275
+ #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
276
+ #endif
277
+
278
+ /**
279
+ * Allocate and zero out the scopes array on the given options struct.
280
+ */
281
+ bool
282
+ pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
283
+ options->scopes_count = scopes_count;
284
+ options->scopes = xcalloc(scopes_count, sizeof(pm_options_scope_t));
285
+ return options->scopes != NULL;
286
+ }
287
+
288
+ /**
289
+ * Return a constant pointer to the scope at the given index within the given
290
+ * options.
291
+ */
292
+ const pm_options_scope_t *
293
+ pm_options_scope(const pm_options_t *options, size_t index) {
294
+ return &options->scopes[index];
295
+ }
296
+
297
+ /**
298
+ * Return a mutable pointer to the scope at the given index within the given
299
+ * options.
300
+ */
301
+ pm_options_scope_t *
302
+ pm_options_scope_mut(pm_options_t *options, size_t index) {
303
+ return &options->scopes[index];
304
+ }
305
+
306
+ /**
307
+ * Create a new options scope struct. This will hold a set of locals that are in
308
+ * scope surrounding the code that is being parsed.
309
+ */
310
+ void
311
+ pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
312
+ scope->locals_count = locals_count;
313
+ scope->locals = xcalloc(locals_count, sizeof(pm_string_t));
314
+ scope->forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE;
315
+ if (scope->locals == NULL) abort();
316
+ }
317
+
318
+ /**
319
+ * Return a constant pointer to the local at the given index within the given
320
+ * scope.
321
+ */
322
+ const pm_string_t *
323
+ pm_options_scope_local(const pm_options_scope_t *scope, size_t index) {
324
+ return &scope->locals[index];
325
+ }
326
+
327
+ /**
328
+ * Return a mutable pointer to the local at the given index within the given
329
+ * scope.
330
+ */
331
+ pm_string_t *
332
+ pm_options_scope_local_mut(pm_options_scope_t *scope, size_t index) {
333
+ return &scope->locals[index];
334
+ }
335
+
336
+ /**
337
+ * Set the forwarding option on the given scope struct.
338
+ */
339
+ void
340
+ pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding) {
341
+ scope->forwarding = forwarding;
342
+ }
343
+
344
+ /**
345
+ * Read a 32-bit unsigned integer from a pointer. This function is used to read
346
+ * the options that are passed into the parser from the Ruby implementation. It
347
+ * handles aligned and unaligned reads.
348
+ */
349
+ static uint32_t
350
+ pm_options_read_u32(const char *data) {
351
+ if (((uintptr_t) data) % sizeof(uint32_t) == 0) {
352
+ return *((uint32_t *) data);
353
+ } else {
354
+ uint32_t value;
355
+ memcpy(&value, data, sizeof(uint32_t));
356
+ return value;
357
+ }
358
+ }
359
+
360
+ /**
361
+ * Read a 32-bit signed integer from a pointer. This function is used to read
362
+ * the options that are passed into the parser from the Ruby implementation. It
363
+ * handles aligned and unaligned reads.
364
+ */
365
+ static int32_t
366
+ pm_options_read_s32(const char *data) {
367
+ if (((uintptr_t) data) % sizeof(int32_t) == 0) {
368
+ return *((int32_t *) data);
369
+ } else {
370
+ int32_t value;
371
+ memcpy(&value, data, sizeof(int32_t));
372
+ return value;
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Deserialize an options struct from the given binary string. This is used to
378
+ * pass options to the parser from an FFI call so that consumers of the library
379
+ * from an FFI perspective don't have to worry about the structure of our
380
+ * options structs. Since the source of these calls will be from Ruby
381
+ * implementation internals we assume it is from a trusted source.
382
+ */
383
+ void
384
+ pm_options_read(pm_options_t *options, const char *data) {
385
+ options->line = 1; // default
386
+ if (data == NULL) return;
387
+
388
+ uint32_t filepath_length = pm_options_read_u32(data);
389
+ data += 4;
390
+
391
+ if (filepath_length > 0) {
392
+ pm_string_init(&options->filepath, (const uint8_t *) data, filepath_length);
393
+ data += filepath_length;
394
+ }
395
+
396
+ options->line = pm_options_read_s32(data);
397
+ data += 4;
398
+
399
+ uint32_t encoding_length = pm_options_read_u32(data);
400
+ data += 4;
401
+
402
+ if (encoding_length > 0) {
403
+ pm_string_init(&options->encoding, (const uint8_t *) data, encoding_length);
404
+ data += encoding_length;
405
+ }
406
+
407
+ options->frozen_string_literal = (int8_t) *data++;
408
+ options->command_line = (uint8_t) *data++;
409
+ options->version = (pm_options_version_t) *data++;
410
+ options->encoding_locked = ((uint8_t) *data++) > 0;
411
+ options->main_script = ((uint8_t) *data++) > 0;
412
+ options->partial_script = ((uint8_t) *data++) > 0;
413
+ options->freeze = ((uint8_t) *data++) > 0;
414
+ options->attach_comments = ((uint8_t) *data++) > 0;
415
+
416
+ uint32_t scopes_count = pm_options_read_u32(data);
417
+ data += 4;
418
+
419
+ if (scopes_count > 0) {
420
+ if (!pm_options_scopes_init(options, scopes_count)) return;
421
+
422
+ for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
423
+ uint32_t locals_count = pm_options_read_u32(data);
424
+ data += 4;
425
+
426
+ pm_options_scope_t *scope = &options->scopes[scope_index];
427
+ pm_options_scope_init(scope, locals_count);
428
+
429
+ uint8_t forwarding = (uint8_t) *data++;
430
+ pm_options_scope_forwarding_set(&options->scopes[scope_index], forwarding);
431
+
432
+ for (size_t local_index = 0; local_index < locals_count; local_index++) {
433
+ uint32_t local_length = pm_options_read_u32(data);
434
+ data += 4;
435
+
436
+ pm_string_init(&scope->locals[local_index], (const uint8_t *) data, local_length);
437
+ data += local_length;
438
+ }
439
+ }
440
+ }
441
+ }
442
+
443
+ #if defined(__GNUC__) && (__GNUC__ >= 10)
444
+ #pragma GCC diagnostic pop
445
+ #endif