Debian Codegraph @

include/prism/options.h

raw symbols SCIP
1 /**
2 * @file options.h
3 *
4 * The options that can be passed to parsing.
5 */
6 #ifndef PRISM_OPTIONS_H
7 #define PRISM_OPTIONS_H
8
9 #include "prism/defines.h"
10 #include "prism/util/pm_char.h"
11 #include "prism/util/pm_string.h"
12
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <stdint.h>
16
17 /**
18 * String literals should be made frozen.
19 */
20 #define PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED ((int8_t) -1)
21
22 /**
23 * String literals may be frozen or mutable depending on the implementation
24 * default.
25 */
26 #define PM_OPTIONS_FROZEN_STRING_LITERAL_UNSET ((int8_t) 0)
27
28 /**
29 * String literals should be made mutable.
30 */
31 #define PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED ((int8_t) 1)
32
33 /**
34 * A scope of locals surrounding the code that is being parsed.
35 */
36 typedef struct pm_options_scope {
37 /** The number of locals in the scope. */
38 size_t locals_count;
39
40 /** The names of the locals in the scope. */
41 pm_string_t *locals;
42
43 /** Flags for the set of forwarding parameters in this scope. */
44 uint8_t forwarding;
45 } pm_options_scope_t;
46
47 /** The default value for parameters. */
48 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE = 0x0;
49
50 /** When the scope is fowarding with the * parameter. */
51 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS = 0x1;
52
53 /** When the scope is fowarding with the ** parameter. */
54 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS = 0x2;
55
56 /** When the scope is fowarding with the & parameter. */
57 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK = 0x4;
58
59 /** When the scope is fowarding with the ... parameter. */
60 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL = 0x8;
61
62 // Forward declaration needed by the callback typedef.
63 struct pm_options;
64
65 /**
66 * The callback called when additional switches are found in a shebang comment
67 * that need to be processed by the runtime.
68 *
69 * @param options The options struct that may be updated by this callback.
70 * Certain fields will be checked for changes, specifically encoding,
71 * command_line, and frozen_string_literal.
72 * @param source The source of the shebang comment.
73 * @param length The length of the source.
74 * @param shebang_callback_data Any additional data that should be passed along
75 * to the callback.
76 */
77 typedef void (*pm_options_shebang_callback_t)(struct pm_options *options, const uint8_t *source, size_t length, void *shebang_callback_data);
78
79 /**
80 * The version of Ruby syntax that we should be parsing with. This is used to
81 * allow consumers to specify which behavior they want in case they need to
82 * parse in the same way as a specific version of CRuby would have.
83 */
84 typedef enum {
85 /** If an explicit version is not provided, the current version of prism will be used. */
86 PM_OPTIONS_VERSION_UNSET = 0,
87
88 /** The vendored version of prism in CRuby 3.3.x. */
89 PM_OPTIONS_VERSION_CRUBY_3_3 = 1,
90
91 /** The vendored version of prism in CRuby 3.4.x. */
92 PM_OPTIONS_VERSION_CRUBY_3_4 = 2,
93
94 /** The vendored version of prism in CRuby 4.0.x. */
95 PM_OPTIONS_VERSION_CRUBY_3_5 = 3,
96
97 /** The vendored version of prism in CRuby 4.0.x. */
98 PM_OPTIONS_VERSION_CRUBY_4_0 = 3,
99
100 /** The vendored version of prism in CRuby 4.1.x. */
101 PM_OPTIONS_VERSION_CRUBY_4_1 = 4,
102
103 /** The current version of prism. */
104 PM_OPTIONS_VERSION_LATEST = PM_OPTIONS_VERSION_CRUBY_4_1
105 } pm_options_version_t;
106
107 /**
108 * The options that can be passed to the parser.
109 */
110 typedef struct pm_options {
111 /**
112 * The callback to call when additional switches are found in a shebang
113 * comment.
114 */
115 pm_options_shebang_callback_t shebang_callback;
116
117 /**
118 * Any additional data that should be passed along to the shebang callback
119 * if one was set.
120 */
121 void *shebang_callback_data;
122
123 /** The name of the file that is currently being parsed. */
124 pm_string_t filepath;
125
126 /**
127 * The line within the file that the parse starts on. This value is
128 * 1-indexed.
129 */
130 int32_t line;
131
132 /**
133 * The name of the encoding that the source file is in. Note that this must
134 * correspond to a name that can be found with Encoding.find in Ruby.
135 */
136 pm_string_t encoding;
137
138 /**
139 * The number of scopes surrounding the code that is being parsed.
140 */
141 size_t scopes_count;
142
143 /**
144 * The scopes surrounding the code that is being parsed. For most parses
145 * this will be NULL, but for evals it will be the locals that are in scope
146 * surrounding the eval. Scopes are ordered from the outermost scope to the
147 * innermost one.
148 */
149 pm_options_scope_t *scopes;
150
151 /**
152 * The version of prism that we should be parsing with. This is used to
153 * allow consumers to specify which behavior they want in case they need to
154 * parse exactly as a specific version of CRuby.
155 */
156 pm_options_version_t version;
157
158 /** A bitset of the various options that were set on the command line. */
159 uint8_t command_line;
160
161 /**
162 * Whether or not the frozen string literal option has been set.
163 * May be:
164 * - PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED
165 * - PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED
166 * - PM_OPTIONS_FROZEN_STRING_LITERAL_UNSET
167 */
168 int8_t frozen_string_literal;
169
170 /**
171 * Whether or not the encoding magic comments should be respected. This is a
172 * niche use-case where you want to parse a file with a specific encoding
173 * but ignore any encoding magic comments at the top of the file.
174 */
175 bool encoding_locked;
176
177 /**
178 * When the file being parsed is the main script, the shebang will be
179 * considered for command-line flags (or for implicit -x). The caller needs
180 * to pass this information to the parser so that it can behave correctly.
181 */
182 bool main_script;
183
184 /**
185 * When the file being parsed is considered a "partial" script, jumps will
186 * not be marked as errors if they are not contained within loops/blocks.
187 * This is used in the case that you're parsing a script that you know will
188 * be embedded inside another script later, but you do not have that context
189 * yet. For example, when parsing an ERB template that will be evaluated
190 * inside another script.
191 */
192 bool partial_script;
193
194 /**
195 * Whether or not the parser should freeze the nodes that it creates. This
196 * makes it possible to have a deeply frozen AST that is safe to share
197 * between concurrency primitives.
198 */
199 bool freeze;
200 } pm_options_t;
201
202 /**
203 * A bit representing whether or not the command line -a option was set. -a
204 * splits the input line $_ into $F.
205 */
206 static const uint8_t PM_OPTIONS_COMMAND_LINE_A = 0x1;
207
208 /**
209 * A bit representing whether or not the command line -e option was set. -e
210 * allow the user to specify a script to be executed. This is necessary for
211 * prism to know because certain warnings are not generated when -e is used.
212 */
213 static const uint8_t PM_OPTIONS_COMMAND_LINE_E = 0x2;
214
215 /**
216 * A bit representing whether or not the command line -l option was set. -l
217 * chomps the input line by default.
218 */
219 static const uint8_t PM_OPTIONS_COMMAND_LINE_L = 0x4;
220
221 /**
222 * A bit representing whether or not the command line -n option was set. -n
223 * wraps the script in a while gets loop.
224 */
225 static const uint8_t PM_OPTIONS_COMMAND_LINE_N = 0x8;
226
227 /**
228 * A bit representing whether or not the command line -p option was set. -p
229 * prints the value of $_ at the end of each loop.
230 */
231 static const uint8_t PM_OPTIONS_COMMAND_LINE_P = 0x10;
232
233 /**
234 * A bit representing whether or not the command line -x option was set. -x
235 * searches the input file for a shebang that matches the current Ruby engine.
236 */
237 static const uint8_t PM_OPTIONS_COMMAND_LINE_X = 0x20;
238
239 /**
240 * Set the shebang callback option on the given options struct.
241 *
242 * @param options The options struct to set the shebang callback on.
243 * @param shebang_callback The shebang callback to set.
244 * @param shebang_callback_data Any additional data that should be passed along
245 * to the callback.
246 *
247 * \public \memberof pm_options
248 */
249 PRISM_EXPORTED_FUNCTION void pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data);
250
251 /**
252 * Set the filepath option on the given options struct.
253 *
254 * @param options The options struct to set the filepath on.
255 * @param filepath The filepath to set.
256 *
257 * \public \memberof pm_options
258 */
259 PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, const char *filepath);
260
261 /**
262 * Set the line option on the given options struct.
263 *
264 * @param options The options struct to set the line on.
265 * @param line The line to set.
266 *
267 * \public \memberof pm_options
268 */
269 PRISM_EXPORTED_FUNCTION void pm_options_line_set(pm_options_t *options, int32_t line);
270
271 /**
272 * Set the encoding option on the given options struct.
273 *
274 * @param options The options struct to set the encoding on.
275 * @param encoding The encoding to set.
276 *
277 * \public \memberof pm_options
278 */
279 PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, const char *encoding);
280
281 /**
282 * Set the encoding_locked option on the given options struct.
283 *
284 * @param options The options struct to set the encoding_locked value on.
285 * @param encoding_locked The encoding_locked value to set.
286 *
287 * \public \memberof pm_options
288 */
289 PRISM_EXPORTED_FUNCTION void pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked);
290
291 /**
292 * Set the frozen string literal option on the given options struct.
293 *
294 * @param options The options struct to set the frozen string literal value on.
295 * @param frozen_string_literal The frozen string literal value to set.
296 *
297 * \public \memberof pm_options
298 */
299 PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal);
300
301 /**
302 * Sets the command line option on the given options struct.
303 *
304 * @param options The options struct to set the command line option on.
305 * @param command_line The command_line value to set.
306 *
307 * \public \memberof pm_options
308 */
309 PRISM_EXPORTED_FUNCTION void pm_options_command_line_set(pm_options_t *options, uint8_t command_line);
310
311 /**
312 * Set the version option on the given options struct by parsing the given
313 * string. If the string contains an invalid option, this returns false.
314 * Otherwise, it returns true.
315 *
316 * @param options The options struct to set the version on.
317 * @param version The version to set.
318 * @param length The length of the version string.
319 * @return Whether or not the version was parsed successfully.
320 *
321 * \public \memberof pm_options
322 */
323 PRISM_EXPORTED_FUNCTION bool pm_options_version_set(pm_options_t *options, const char *version, size_t length);
324
325 /**
326 * Set the main script option on the given options struct.
327 *
328 * @param options The options struct to set the main script value on.
329 * @param main_script The main script value to set.
330 *
331 * \public \memberof pm_options
332 */
333 PRISM_EXPORTED_FUNCTION void pm_options_main_script_set(pm_options_t *options, bool main_script);
334
335 /**
336 * Set the partial script option on the given options struct.
337 *
338 * @param options The options struct to set the partial script value on.
339 * @param partial_script The partial script value to set.
340 *
341 * \public \memberof pm_options
342 */
343 PRISM_EXPORTED_FUNCTION void pm_options_partial_script_set(pm_options_t *options, bool partial_script);
344
345 /**
346 * Set the freeze option on the given options struct.
347 *
348 * @param options The options struct to set the freeze value on.
349 * @param freeze The freeze value to set.
350 *
351 * \public \memberof pm_options
352 */
353 PRISM_EXPORTED_FUNCTION void pm_options_freeze_set(pm_options_t *options, bool freeze);
354
355 /**
356 * Allocate and zero out the scopes array on the given options struct.
357 *
358 * @param options The options struct to initialize the scopes array on.
359 * @param scopes_count The number of scopes to allocate.
360 * @return Whether or not the scopes array was initialized successfully.
361 *
362 * \public \memberof pm_options
363 */
364 PRISM_EXPORTED_FUNCTION bool pm_options_scopes_init(pm_options_t *options, size_t scopes_count);
365
366 /**
367 * Return a pointer to the scope at the given index within the given options.
368 *
369 * @param options The options struct to get the scope from.
370 * @param index The index of the scope to get.
371 * @return A pointer to the scope at the given index.
372 *
373 * \public \memberof pm_options
374 */
375 PRISM_EXPORTED_FUNCTION const pm_options_scope_t * pm_options_scope_get(const pm_options_t *options, size_t index);
376
377 /**
378 * Create a new options scope struct. This will hold a set of locals that are in
379 * scope surrounding the code that is being parsed.
380 *
381 * @param scope The scope struct to initialize.
382 * @param locals_count The number of locals to allocate.
383 * @return Whether or not the scope was initialized successfully.
384 *
385 * \public \memberof pm_options
386 */
387 PRISM_EXPORTED_FUNCTION bool pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count);
388
389 /**
390 * Return a pointer to the local at the given index within the given scope.
391 *
392 * @param scope The scope struct to get the local from.
393 * @param index The index of the local to get.
394 * @return A pointer to the local at the given index.
395 *
396 * \public \memberof pm_options
397 */
398 PRISM_EXPORTED_FUNCTION const pm_string_t * pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index);
399
400 /**
401 * Set the forwarding option on the given scope struct.
402 *
403 * @param scope The scope struct to set the forwarding on.
404 * @param forwarding The forwarding value to set.
405 *
406 * \public \memberof pm_options
407 */
408 PRISM_EXPORTED_FUNCTION void pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding);
409
410 /**
411 * Free the internal memory associated with the options.
412 *
413 * @param options The options struct whose internal memory should be freed.
414 *
415 * \public \memberof pm_options
416 */
417 PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options);
418
419 /**
420 * Deserialize an options struct from the given binary string. This is used to
421 * pass options to the parser from an FFI call so that consumers of the library
422 * from an FFI perspective don't have to worry about the structure of our
423 * options structs. Since the source of these calls will be from Ruby
424 * implementation internals we assume it is from a trusted source.
425 *
426 * `data` is assumed to be a valid pointer pointing to well-formed data. The
427 * layout of this data should be the same every time, and is described below:
428 *
429 * | # bytes | field |
430 * | ------- | -------------------------- |
431 * | `4` | the length of the filepath |
432 * | ... | the filepath bytes |
433 * | `4` | the line number |
434 * | `4` | the length the encoding |
435 * | ... | the encoding bytes |
436 * | `1` | frozen string literal |
437 * | `1` | -p command line option |
438 * | `1` | -n command line option |
439 * | `1` | -l command line option |
440 * | `1` | -a command line option |
441 * | `1` | the version |
442 * | `1` | encoding locked |
443 * | `1` | main script |
444 * | `1` | partial script |
445 * | `1` | freeze |
446 * | `4` | the number of scopes |
447 * | ... | the scopes |
448 *
449 * The version field is an enum, so it should be one of the following values:
450 *
451 * | value | version |
452 * | ----- | ------------------------- |
453 * | `0` | use the latest version of prism |
454 * | `1` | use the version of prism that is vendored in CRuby 3.3.0 |
455 *
456 * Each scope is laid out as follows:
457 *
458 * | # bytes | field |
459 * | ------- | -------------------------- |
460 * | `4` | the number of locals |
461 * | `1` | the forwarding flags |
462 * | ... | the locals |
463 *
464 * Each local is laid out as follows:
465 *
466 * | # bytes | field |
467 * | ------- | -------------------------- |
468 * | `4` | the length of the local |
469 * | ... | the local bytes |
470 *
471 * Some additional things to note about this layout:
472 *
473 * * The filepath can have a length of 0, in which case we'll consider it an
474 * empty string.
475 * * The line number should be 0-indexed.
476 * * The encoding can have a length of 0, in which case we'll use the default
477 * encoding (UTF-8). If it's not 0, it should correspond to a name of an
478 * encoding that can be passed to `Encoding.find` in Ruby.
479 * * The frozen string literal, encoding locked, main script, and partial script
480 * fields are booleans, so their values should be either 0 or 1.
481 * * The number of scopes can be 0.
482 *
483 * @param options The options struct to deserialize into.
484 * @param data The binary string to deserialize from.
485 */
486 void pm_options_read(pm_options_t *options, const char *data);
487
488 #endif