Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
options.h
Go to the documentation of this file.
1 
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 
20 #define PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED ((int8_t) -1)
21 
26 #define PM_OPTIONS_FROZEN_STRING_LITERAL_UNSET ((int8_t) 0)
27 
31 #define PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED ((int8_t) 1)
32 
36 typedef struct pm_options_scope {
38  size_t locals_count;
39 
43 
44 // Forward declaration needed by the callback typedef.
45 struct pm_options;
46 
59 typedef void (*pm_options_shebang_callback_t)(struct pm_options *options, const uint8_t *source, size_t length, void *shebang_callback_data);
60 
66 typedef enum {
69 
73 
77 typedef struct pm_options {
83 
89 
92 
97  int32_t line;
98 
104 
108  size_t scopes_count;
109 
117 
124 
126  uint8_t command_line;
127 
136 
143 
150 
161 
166 static const uint8_t PM_OPTIONS_COMMAND_LINE_A = 0x1;
167 
173 static const uint8_t PM_OPTIONS_COMMAND_LINE_E = 0x2;
174 
179 static const uint8_t PM_OPTIONS_COMMAND_LINE_L = 0x4;
180 
185 static const uint8_t PM_OPTIONS_COMMAND_LINE_N = 0x8;
186 
191 static const uint8_t PM_OPTIONS_COMMAND_LINE_P = 0x10;
192 
197 static const uint8_t PM_OPTIONS_COMMAND_LINE_X = 0x20;
198 
207 PRISM_EXPORTED_FUNCTION void pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data);
208 
215 PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, const char *filepath);
216 
223 PRISM_EXPORTED_FUNCTION void pm_options_line_set(pm_options_t *options, int32_t line);
224 
231 PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, const char *encoding);
232 
239 PRISM_EXPORTED_FUNCTION void pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked);
240 
247 PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal);
248 
255 PRISM_EXPORTED_FUNCTION void pm_options_command_line_set(pm_options_t *options, uint8_t command_line);
256 
267 PRISM_EXPORTED_FUNCTION bool pm_options_version_set(pm_options_t *options, const char *version, size_t length);
268 
275 PRISM_EXPORTED_FUNCTION void pm_options_main_script_set(pm_options_t *options, bool main_script);
276 
283 PRISM_EXPORTED_FUNCTION void pm_options_partial_script_set(pm_options_t *options, bool partial_script);
284 
292 PRISM_EXPORTED_FUNCTION bool pm_options_scopes_init(pm_options_t *options, size_t scopes_count);
293 
302 
311 PRISM_EXPORTED_FUNCTION bool pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count);
312 
321 
328 
394 void pm_options_read(pm_options_t *options, const char *data);
395 
396 #endif
PRISM_EXPORTED_FUNCTION void pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data)
Set the shebang callback option on the given options struct.
Definition: options.c:7
PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, const char *encoding)
Set the encoding option on the given options struct.
Definition: options.c:24
PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options)
Free the internal memory associated with the options.
Definition: options.c:181
struct pm_options_scope pm_options_scope_t
A scope of locals surrounding the code that is being parsed.
PRISM_EXPORTED_FUNCTION void pm_options_partial_script_set(pm_options_t *options, bool partial_script)
Set the partial script option on the given options struct.
Definition: options.c:128
static const uint8_t PM_OPTIONS_COMMAND_LINE_E
A bit representing whether or not the command line -e option was set.
Definition: options.h:173
static const uint8_t PM_OPTIONS_COMMAND_LINE_L
A bit representing whether or not the command line -l option was set.
Definition: options.h:179
struct pm_options pm_options_t
The options that can be passed to the parser.
PRISM_EXPORTED_FUNCTION void pm_options_main_script_set(pm_options_t *options, bool main_script)
Set the main script option on the given options struct.
Definition: options.c:120
void(* pm_options_shebang_callback_t)(struct pm_options *options, const uint8_t *source, size_t length, void *shebang_callback_data)
The callback called when additional switches are found in a shebang comment that need to be processed...
Definition: options.h:59
static const uint8_t PM_OPTIONS_COMMAND_LINE_A
A bit representing whether or not the command line -a option was set.
Definition: options.h:166
void pm_options_read(pm_options_t *options, const char *data)
Deserialize an options struct from the given binary string.
Definition: options.c:238
PRISM_EXPORTED_FUNCTION bool pm_options_version_set(pm_options_t *options, const char *version, size_t length)
Set the version option on the given options struct by parsing the given string.
Definition: options.c:74
PRISM_EXPORTED_FUNCTION bool pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count)
Create a new options scope struct.
Definition: options.c:163
PRISM_EXPORTED_FUNCTION void pm_options_line_set(pm_options_t *options, int32_t line)
Set the line option on the given options struct.
Definition: options.c:40
static const uint8_t PM_OPTIONS_COMMAND_LINE_N
A bit representing whether or not the command line -n option was set.
Definition: options.h:185
PRISM_EXPORTED_FUNCTION void pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked)
Set the encoding_locked option on the given options struct.
Definition: options.c:32
PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal)
Set the frozen string literal option on the given options struct.
Definition: options.c:48
static const uint8_t PM_OPTIONS_COMMAND_LINE_X
A bit representing whether or not the command line -x option was set.
Definition: options.h:197
PRISM_EXPORTED_FUNCTION const pm_options_scope_t * pm_options_scope_get(const pm_options_t *options, size_t index)
Return a pointer to the scope at the given index within the given options.
Definition: options.c:154
PRISM_EXPORTED_FUNCTION const pm_string_t * pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index)
Return a pointer to the local at the given index within the given scope.
Definition: options.c:173
PRISM_EXPORTED_FUNCTION void pm_options_command_line_set(pm_options_t *options, uint8_t command_line)
Sets the command line option on the given options struct.
Definition: options.c:56
PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, const char *filepath)
Set the filepath option on the given options struct.
Definition: options.c:16
PRISM_EXPORTED_FUNCTION bool pm_options_scopes_init(pm_options_t *options, size_t scopes_count)
Allocate and zero out the scopes array on the given options struct.
Definition: options.c:144
static const uint8_t PM_OPTIONS_COMMAND_LINE_P
A bit representing whether or not the command line -p option was set.
Definition: options.h:191
pm_options_version_t
The version of Ruby syntax that we should be parsing with.
Definition: options.h:66
@ PM_OPTIONS_VERSION_CRUBY_3_3
The vendored version of prism in CRuby 3.3.x.
Definition: options.h:71
@ PM_OPTIONS_VERSION_LATEST
The current version of prism.
Definition: options.h:68
Functions for working with characters and strings.
A generic string type that can have various ownership semantics.
Macro definitions used throughout the prism library.
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition: defines.h:50
C99 shim for <stdbool.h>
A scope of locals surrounding the code that is being parsed.
Definition: options.h:36
size_t locals_count
The number of locals in the scope.
Definition: options.h:38
pm_string_t * locals
The names of the locals in the scope.
Definition: options.h:41
The options that can be passed to the parser.
Definition: options.h:77
uint8_t command_line
A bitset of the various options that were set on the command line.
Definition: options.h:126
void * shebang_callback_data
Any additional data that should be passed along to the shebang callback if one was set.
Definition: options.h:88
bool encoding_locked
Whether or not the encoding magic comments should be respected.
Definition: options.h:142
pm_options_scope_t * scopes
The scopes surrounding the code that is being parsed.
Definition: options.h:116
bool main_script
When the file being parsed is the main script, the shebang will be considered for command-line flags ...
Definition: options.h:149
pm_string_t encoding
The name of the encoding that the source file is in.
Definition: options.h:103
int32_t line
The line within the file that the parse starts on.
Definition: options.h:97
pm_options_shebang_callback_t shebang_callback
The callback to call when additional switches are found in a shebang comment.
Definition: options.h:82
int8_t frozen_string_literal
Whether or not the frozen string literal option has been set.
Definition: options.h:135
bool partial_script
When the file being parsed is considered a "partial" script, jumps will not be marked as errors if th...
Definition: options.h:159
size_t scopes_count
The number of scopes surrounding the code that is being parsed.
Definition: options.h:108
pm_string_t filepath
The name of the file that is currently being parsed.
Definition: options.h:91
pm_options_version_t version
The version of prism that we should be parsing with.
Definition: options.h:123
A generic string type that can have various ownership semantics.
Definition: pm_string.h:33