Ruby 3.3.0p0 (2023-12-25 revision 5124f9ac7513eb590c37717337c430cb93caa151)
options.c
1#include "prism/options.h"
2
7pm_options_filepath_set(pm_options_t *options, const char *filepath) {
8 pm_string_constant_init(&options->filepath, filepath, strlen(filepath));
9}
10
15pm_options_encoding_set(pm_options_t *options, const char *encoding) {
16 pm_string_constant_init(&options->encoding, encoding, strlen(encoding));
17}
18
23pm_options_line_set(pm_options_t *options, int32_t line) {
24 options->line = line;
25}
26
31pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal) {
32 options->frozen_string_literal = frozen_string_literal;
33}
34
39pm_options_suppress_warnings_set(pm_options_t *options, bool suppress_warnings) {
40 options->suppress_warnings = suppress_warnings;
41}
42
47pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
48 options->scopes_count = scopes_count;
49 options->scopes = calloc(scopes_count, sizeof(pm_options_scope_t));
50 if (options->scopes == NULL) abort();
51}
52
57pm_options_scope_get(const pm_options_t *options, size_t index) {
58 return &options->scopes[index];
59}
60
66pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
67 scope->locals_count = locals_count;
68 scope->locals = calloc(locals_count, sizeof(pm_string_t));
69 if (scope->locals == NULL) abort();
70}
71
76pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index) {
77 return &scope->locals[index];
78}
79
84pm_options_free(pm_options_t *options) {
85 pm_string_free(&options->filepath);
86 pm_string_free(&options->encoding);
87
88 for (size_t scope_index = 0; scope_index < options->scopes_count; scope_index++) {
89 pm_options_scope_t *scope = &options->scopes[scope_index];
90
91 for (size_t local_index = 0; local_index < scope->locals_count; local_index++) {
92 pm_string_free(&scope->locals[local_index]);
93 }
94
95 free(scope->locals);
96 }
97
98 free(options->scopes);
99}
100
106static uint32_t
107pm_options_read_u32(const char *data) {
108 if (((uintptr_t) data) % sizeof(uint32_t) == 0) {
109 return *((uint32_t *) data);
110 } else {
111 uint32_t value;
112 memcpy(&value, data, sizeof(uint32_t));
113 return value;
114 }
115}
116
122static int32_t
123pm_options_read_s32(const char *data) {
124 if (((uintptr_t) data) % sizeof(int32_t) == 0) {
125 return *((int32_t *) data);
126 } else {
127 int32_t value;
128 memcpy(&value, data, sizeof(int32_t));
129 return value;
130 }
131}
132
140void
141pm_options_read(pm_options_t *options, const char *data) {
142 options->line = 1; // default
143 if (data == NULL) return;
144
145 uint32_t filepath_length = pm_options_read_u32(data);
146 data += 4;
147
148 if (filepath_length > 0) {
149 pm_string_constant_init(&options->filepath, data, filepath_length);
150 data += filepath_length;
151 }
152
153 options->line = pm_options_read_s32(data);
154 data += 4;
155
156 uint32_t encoding_length = pm_options_read_u32(data);
157 data += 4;
158
159 if (encoding_length > 0) {
160 pm_string_constant_init(&options->encoding, data, encoding_length);
161 data += encoding_length;
162 }
163
164 options->frozen_string_literal = *data++;
165 options->suppress_warnings = *data++;
166
167 uint32_t scopes_count = pm_options_read_u32(data);
168 data += 4;
169
170 if (scopes_count > 0) {
171 pm_options_scopes_init(options, scopes_count);
172
173 for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
174 uint32_t locals_count = pm_options_read_u32(data);
175 data += 4;
176
177 pm_options_scope_t *scope = &options->scopes[scope_index];
178 pm_options_scope_init(scope, locals_count);
179
180 for (size_t local_index = 0; local_index < locals_count; local_index++) {
181 uint32_t local_length = pm_options_read_u32(data);
182 data += 4;
183
184 pm_string_constant_init(&scope->locals[local_index], data, local_length);
185 data += local_length;
186 }
187 }
188 }
189}
The options that can be passed to parsing.
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:32
A scope of locals surrounding the code that is being parsed.
Definition options.h:19
size_t locals_count
The number of locals in the scope.
Definition options.h:21
pm_string_t * locals
The names of the locals in the scope.
Definition options.h:24
The options that can be passed to the parser.
Definition options.h:30
pm_options_scope_t * scopes
The scopes surrounding the code that is being parsed.
Definition options.h:56
size_t scopes_count
The number of scopes surrounding the code that is being parsed.
Definition options.h:49
bool suppress_warnings
Whether or not we should suppress warnings.
Definition options.h:66
pm_string_t encoding
The name of the encoding that the source file is in.
Definition options.h:44
bool frozen_string_literal
Whether or not the frozen string literal option has been set.
Definition options.h:59
int32_t line
The line within the file that the parse starts on.
Definition options.h:38
pm_string_t filepath
The name of the file that is currently being parsed.
Definition options.h:32
A generic string type that can have various ownership semantics.
Definition pm_string.h:30