Ruby: prism/options.h Source File

Ruby 3.4.8p72 (2025-12-17 revision 995b59f66677d44767ce9faac6957e5543617ff9)

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
41 pm_string_t *locals;
42
44 uint8_t forwarding;
45} pm_options_scope_t;
46
48 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE = 0x0;
49
51 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS = 0x1;
52
54 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS = 0x2;
55
57 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK = 0x4;
58
60 static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL = 0x8;
61
62// Forward declaration needed by the callback typedef.
63struct pm_options;
64
77 typedef void (*pm_options_shebang_callback_t)(struct pm_options *options, const uint8_t *source, size_t length, void *shebang_callback_data);
78
84 typedef enum {
86 PM_OPTIONS_VERSION_UNSET = 0,
87
89 PM_OPTIONS_VERSION_CRUBY_3_3 = 1,
90
92 PM_OPTIONS_VERSION_CRUBY_3_4 = 2,
93
95 PM_OPTIONS_VERSION_CRUBY_3_5 = 3,
96
98 PM_OPTIONS_VERSION_LATEST = PM_OPTIONS_VERSION_CRUBY_3_5
99} pm_options_version_t;
100
104 typedef struct pm_options {
109 pm_options_shebang_callback_t shebang_callback;
110
115 void *shebang_callback_data;
116
118 pm_string_t filepath;
119
124 int32_t line;
125
130 pm_string_t encoding;
131
135 size_t scopes_count;
136
143 pm_options_scope_t *scopes;
144
150 pm_options_version_t version;
151
153 uint8_t command_line;
154
162 int8_t frozen_string_literal;
163
169 bool encoding_locked;
170
176 bool main_script;
177
186 bool partial_script;
187
193 bool freeze;
194} pm_options_t;
195
200 static const uint8_t PM_OPTIONS_COMMAND_LINE_A = 0x1;
201
207 static const uint8_t PM_OPTIONS_COMMAND_LINE_E = 0x2;
208
213 static const uint8_t PM_OPTIONS_COMMAND_LINE_L = 0x4;
214
219 static const uint8_t PM_OPTIONS_COMMAND_LINE_N = 0x8;
220
225 static const uint8_t PM_OPTIONS_COMMAND_LINE_P = 0x10;
226
231 static const uint8_t PM_OPTIONS_COMMAND_LINE_X = 0x20;
232
243 PRISM_EXPORTED_FUNCTION void pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data);
244
253 PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, const char *filepath);
254
263 PRISM_EXPORTED_FUNCTION void pm_options_line_set(pm_options_t *options, int32_t line);
264
273 PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, const char *encoding);
274
283 PRISM_EXPORTED_FUNCTION void pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked);
284
293 PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal);
294
303 PRISM_EXPORTED_FUNCTION void pm_options_command_line_set(pm_options_t *options, uint8_t command_line);
304
317 PRISM_EXPORTED_FUNCTION bool pm_options_version_set(pm_options_t *options, const char *version, size_t length);
318
327 PRISM_EXPORTED_FUNCTION void pm_options_main_script_set(pm_options_t *options, bool main_script);
328
337 PRISM_EXPORTED_FUNCTION void pm_options_partial_script_set(pm_options_t *options, bool partial_script);
338
347 PRISM_EXPORTED_FUNCTION void pm_options_freeze_set(pm_options_t *options, bool freeze);
348
358 PRISM_EXPORTED_FUNCTION bool pm_options_scopes_init(pm_options_t *options, size_t scopes_count);
359
369 PRISM_EXPORTED_FUNCTION const pm_options_scope_t * pm_options_scope_get(const pm_options_t *options, size_t index);
370
381 PRISM_EXPORTED_FUNCTION bool pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count);
382
392 PRISM_EXPORTED_FUNCTION const pm_string_t * pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index);
393
402 PRISM_EXPORTED_FUNCTION void pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding);
403
411 PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options);
412
480void pm_options_read(pm_options_t *options, const char *data);
481
482#endif
pm_options_shebang_callback_set
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
pm_options_encoding_set
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
pm_options_free
PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options)
Free the internal memory associated with the options.
Definition options.c:208
pm_options_partial_script_set
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:138
pm_options_main_script_set
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:130
pm_options_scope_local_get
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:192
pm_options_version_set
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
pm_options_scope_init
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:181
pm_options_line_set
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
pm_options_scope_get
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:172
pm_options_freeze_set
PRISM_EXPORTED_FUNCTION void pm_options_freeze_set(pm_options_t *options, bool freeze)
Set the freeze option on the given options struct.
Definition options.c:146
pm_options_encoding_locked_set
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
pm_options_frozen_string_literal_set
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
pm_options_command_line_set
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
pm_options_filepath_set
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
pm_options_scopes_init
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:162
pm_options_scope_forwarding_set
PRISM_EXPORTED_FUNCTION void pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding)
Set the forwarding option on the given scope struct.
Definition options.c:200
pm_options_scope_t
struct pm_options_scope pm_options_scope_t
A scope of locals surrounding the code that is being parsed.
PM_OPTIONS_COMMAND_LINE_E
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:207
PM_OPTIONS_COMMAND_LINE_L
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:213
pm_options_t
struct pm_options pm_options_t
The options that can be passed to the parser.
pm_options_shebang_callback_t
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:77
PM_OPTIONS_COMMAND_LINE_A
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:200
pm_options_read
void pm_options_read(pm_options_t *options, const char *data)
Deserialize an options struct from the given binary string.
Definition options.c:265
PM_OPTIONS_SCOPE_FORWARDING_NONE
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE
The default value for parameters.
Definition options.h:48
PM_OPTIONS_SCOPE_FORWARDING_ALL
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL
When the scope is fowarding with the ... parameter.
Definition options.h:60
PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS
When the scope is fowarding with the * parameter.
Definition options.h:51
PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS
When the scope is fowarding with the ** parameter.
Definition options.h:54
PM_OPTIONS_COMMAND_LINE_N
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:219
PM_OPTIONS_COMMAND_LINE_X
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:231
PM_OPTIONS_SCOPE_FORWARDING_BLOCK
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK
When the scope is fowarding with the & parameter.
Definition options.h:57
PM_OPTIONS_COMMAND_LINE_P
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:225
pm_options_version_t
pm_options_version_t
The version of Ruby syntax that we should be parsing with.
Definition options.h:84
PM_OPTIONS_VERSION_CRUBY_3_3
@ PM_OPTIONS_VERSION_CRUBY_3_3
The vendored version of prism in CRuby 3.3.x.
Definition options.h:89
PM_OPTIONS_VERSION_LATEST
@ PM_OPTIONS_VERSION_LATEST
The current version of prism.
Definition options.h:98
PM_OPTIONS_VERSION_UNSET
@ PM_OPTIONS_VERSION_UNSET
If an explicit version is not provided, the current version of prism will be used.
Definition options.h:86
PM_OPTIONS_VERSION_CRUBY_3_4
@ PM_OPTIONS_VERSION_CRUBY_3_4
The vendored version of prism in CRuby 3.4.x.
Definition options.h:92
PM_OPTIONS_VERSION_CRUBY_3_5
@ PM_OPTIONS_VERSION_CRUBY_3_5
The vendored version of prism in CRuby 3.5.x.
Definition options.h:95
pm_char.h
Functions for working with characters and strings.
pm_string.h
A generic string type that can have various ownership semantics.
defines.h
Macro definitions used throughout the prism library.
PRISM_EXPORTED_FUNCTION
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:53
stdbool.h
C99 shim for <stdbool.h>
pm_options_scope
A scope of locals surrounding the code that is being parsed.
Definition options.h:36
pm_options_scope::locals_count
size_t locals_count
The number of locals in the scope.
Definition options.h:38
pm_options_scope::forwarding
uint8_t forwarding
Flags for the set of forwarding parameters in this scope.
Definition options.h:44
pm_options_scope::locals
pm_string_t * locals
The names of the locals in the scope.
Definition options.h:41
pm_options
The options that can be passed to the parser.
Definition options.h:104
pm_options::freeze
bool freeze
Whether or not the parser should freeze the nodes that it creates.
Definition options.h:193
pm_options::command_line
uint8_t command_line
A bitset of the various options that were set on the command line.
Definition options.h:153
pm_options::shebang_callback_data
void * shebang_callback_data
Any additional data that should be passed along to the shebang callback if one was set.
Definition options.h:115
pm_options::encoding_locked
bool encoding_locked
Whether or not the encoding magic comments should be respected.
Definition options.h:169
pm_options::scopes
pm_options_scope_t * scopes
The scopes surrounding the code that is being parsed.
Definition options.h:143
pm_options::main_script
bool main_script
When the file being parsed is the main script, the shebang will be considered for command-line flags ...
Definition options.h:176
pm_options::encoding
pm_string_t encoding
The name of the encoding that the source file is in.
Definition options.h:130
pm_options::line
int32_t line
The line within the file that the parse starts on.
Definition options.h:124
pm_options::shebang_callback
pm_options_shebang_callback_t shebang_callback
The callback to call when additional switches are found in a shebang comment.
Definition options.h:109
pm_options::frozen_string_literal
int8_t frozen_string_literal
Whether or not the frozen string literal option has been set.
Definition options.h:162
pm_options::partial_script
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:186
pm_options::scopes_count
size_t scopes_count
The number of scopes surrounding the code that is being parsed.
Definition options.h:135
pm_options::filepath
pm_string_t filepath
The name of the file that is currently being parsed.
Definition options.h:118
pm_options::version
pm_options_version_t version
The version of prism that we should be parsing with.
Definition options.h:150
pm_string_t
A generic string type that can have various ownership semantics.
Definition pm_string.h:33