Ruby 4.1.0dev (2026-11-18 revision bcf3b03b44ae598e3adb59d359cf32202091a318)
extension.c
1#include "prism/extension.h"
2
3#ifdef _WIN32
4#include <ruby/win32.h>
5#endif
6
7#include <errno.h>
8
9// NOTE: this file should contain only bindings. All non-trivial logic should be
10// in libprism so it can be shared its the various callers.
11
12VALUE rb_cPrism;
13VALUE rb_cPrismNode;
14VALUE rb_cPrismSource;
15VALUE rb_cPrismToken;
16VALUE rb_cPrismLocation;
17
18VALUE rb_cPrismComment;
19VALUE rb_cPrismInlineComment;
20VALUE rb_cPrismEmbDocComment;
21VALUE rb_cPrismMagicComment;
22VALUE rb_cPrismParseError;
23VALUE rb_cPrismParseWarning;
24VALUE rb_cPrismResult;
25VALUE rb_cPrismParseResult;
26VALUE rb_cPrismLexResult;
27VALUE rb_cPrismParseLexResult;
28VALUE rb_cPrismStringQuery;
29VALUE rb_cPrismScope;
30VALUE rb_cPrismCurrentVersionError;
31
32VALUE rb_cPrismDebugEncoding;
33
34ID rb_id_option_attach_comments;
35ID rb_id_option_command_line;
36ID rb_id_option_encoding;
37ID rb_id_option_filepath;
38ID rb_id_option_freeze;
39ID rb_id_option_frozen_string_literal;
40ID rb_id_option_line;
41ID rb_id_option_main_script;
42ID rb_id_option_partial_script;
43ID rb_id_option_raise_error;
44ID rb_id_option_scopes;
45ID rb_id_option_version;
46
47ID rb_id_source_for;
48
49ID rb_id_forwarding_positionals;
50ID rb_id_forwarding_keywords;
51ID rb_id_forwarding_block;
52ID rb_id_forwarding_all;
53
54ID rb_id_raise_error_plain;
55ID rb_id_raise_error_style;
56ID rb_id_raise_error_color;
57
58/******************************************************************************/
59/* Result struct for working with functions that may need to raise errors. */
60/******************************************************************************/
61
62typedef struct {
63 enum {
64 RESULT_OK,
65 RESULT_ERR
66 } type;
67 VALUE value;
68} result_t;
69
70static result_t
71result_ok(VALUE value) {
72 return (result_t) { .type = RESULT_OK, .value = value };
73}
74
75static result_t
76result_err(VALUE value) {
77 return (result_t) { .type = RESULT_ERR, .value = value };
78}
79
80static VALUE
81result_get(result_t result) {
82 if (result.type == RESULT_OK) {
83 return result.value;
84 } else {
85 rb_exc_raise(result.value);
86 }
87}
88
89/******************************************************************************/
90/* IO of Ruby code */
91/******************************************************************************/
92
97static const char *
98check_string(VALUE value) {
99 // Check if the value is a string. If it's not, then raise a type error.
100 if (!RB_TYPE_P(value, T_STRING)) {
101 rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(value));
102 }
103
104 // Otherwise, return the value as a C string.
105 return RSTRING_PTR(value);
106}
107
108/******************************************************************************/
109/* Building C options from Ruby options */
110/******************************************************************************/
111
115static void
116build_options_scopes(pm_options_t *options, VALUE scopes) {
117 // Check if the value is an array. If it's not, then raise a type error.
118 if (!RB_TYPE_P(scopes, T_ARRAY)) {
119 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(scopes));
120 }
121
122 // Initialize the scopes array.
123 size_t scopes_count = RARRAY_LEN(scopes);
124 if (!pm_options_scopes_init(options, scopes_count)) {
125 rb_raise(rb_eNoMemError, "failed to allocate memory");
126 }
127
128 // Iterate over the scopes and add them to the options.
129 for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
130 VALUE scope = rb_ary_entry(scopes, scope_index);
131
132 // The scope can be either an array or it can be a Prism::Scope object.
133 // Parse out the correct values here from either.
134 VALUE locals;
135 uint8_t forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE;
136
137 if (RB_TYPE_P(scope, T_ARRAY)) {
138 locals = scope;
139 } else if (rb_obj_is_kind_of(scope, rb_cPrismScope)) {
140 locals = rb_ivar_get(scope, rb_intern("@locals"));
141 if (!RB_TYPE_P(locals, T_ARRAY)) {
142 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(locals));
143 }
144
145 VALUE names = rb_ivar_get(scope, rb_intern("@forwarding"));
146 if (!RB_TYPE_P(names, T_ARRAY)) {
147 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(names));
148 }
149
150 size_t names_count = RARRAY_LEN(names);
151 for (size_t name_index = 0; name_index < names_count; name_index++) {
152 VALUE name = rb_ary_entry(names, name_index);
153
154 // Check that the name is a symbol. If it's not, then raise
155 // a type error.
156 if (!RB_TYPE_P(name, T_SYMBOL)) {
157 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Symbol)", rb_obj_class(name));
158 }
159
160 ID id = SYM2ID(name);
161 if (id == rb_id_forwarding_positionals) {
163 } else if (id == rb_id_forwarding_keywords) {
165 } else if (id == rb_id_forwarding_block) {
167 } else if (id == rb_id_forwarding_all) {
169 } else {
170 rb_raise(rb_eArgError, "invalid forwarding value: %" PRIsVALUE, name);
171 }
172 }
173 } else {
174 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array or Prism::Scope)", rb_obj_class(scope));
175 }
176
177 // Initialize the scope array.
178 size_t locals_count = RARRAY_LEN(locals);
179 pm_options_scope_t *options_scope = pm_options_scope_mut(options, scope_index);
180 pm_options_scope_init(options_scope, locals_count);
181
182 // Iterate over the locals and add them to the scope.
183 for (size_t local_index = 0; local_index < locals_count; local_index++) {
184 VALUE local = rb_ary_entry(locals, local_index);
185
186 // Check that the local is a symbol. If it's not, then raise a
187 // type error.
188 if (!RB_TYPE_P(local, T_SYMBOL)) {
189 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Symbol)", rb_obj_class(local));
190 }
191
192 // Add the local to the scope.
193 pm_string_t *scope_local = pm_options_scope_local_mut(options_scope, local_index);
194 const char *name = rb_id2name(SYM2ID(local));
195 pm_string_init(scope_local, (const uint8_t *) name, strlen(name));
196 }
197
198 // Now set the forwarding options.
199 pm_options_scope_forwarding_set(options_scope, forwarding);
200 }
201}
202
207static bool
208ruby_version_before_lowest_p(void) {
209 return ruby_version[0] < '3' || (ruby_version[0] == '3' && ruby_version[2] < '3');
210}
211
216static void
217options_version_set_nearest(pm_options_t *options) {
218 if (!pm_options_version_set(options, ruby_version, 3)) {
219 // Prism doesn't know this specific version. Is it lower?
220 if (ruby_version_before_lowest_p()) {
221 pm_options_version_set_lowest(options);
222 } else {
223 // Must be higher.
224 pm_options_version_set_highest(options);
225 }
226 }
227}
228
232static int
233build_options_i(VALUE key, VALUE value, VALUE argument) {
234 pm_options_t *options = (pm_options_t *) argument;
235 ID key_id = SYM2ID(key);
236
237 if (key_id == rb_id_option_filepath) {
238 if (!NIL_P(value)) pm_options_filepath_set(options, check_string(value));
239 } else if (key_id == rb_id_option_encoding) {
240 if (!NIL_P(value)) {
241 if (value == Qfalse) {
242 pm_options_encoding_locked_set(options, true);
243 } else {
244 pm_options_encoding_set(options, rb_enc_name(rb_to_encoding(value)));
245 }
246 }
247 } else if (key_id == rb_id_option_line) {
248 if (!NIL_P(value)) pm_options_line_set(options, NUM2INT(value));
249 } else if (key_id == rb_id_option_frozen_string_literal) {
250 if (!NIL_P(value)) pm_options_frozen_string_literal_set(options, RTEST(value));
251 } else if (key_id == rb_id_option_version) {
252 if (!NIL_P(value)) {
253 const char *version = check_string(value);
254
255 if (RSTRING_LEN(value) == 7 && strncmp(version, "current", 7) == 0) {
256 if (!pm_options_version_set(options, ruby_version, 3)) {
257 rb_exc_raise(rb_exc_new_cstr(rb_cPrismCurrentVersionError, ruby_version));
258 }
259 } else if (RSTRING_LEN(value) == 7 && strncmp(version, "nearest", 7) == 0) {
260 options_version_set_nearest(options);
261 } else if (!pm_options_version_set(options, version, RSTRING_LEN(value))) {
262 rb_raise(rb_eArgError, "invalid version: %" PRIsVALUE, value);
263 }
264 }
265 } else if (key_id == rb_id_option_scopes) {
266 if (!NIL_P(value)) build_options_scopes(options, value);
267 } else if (key_id == rb_id_option_command_line) {
268 if (!NIL_P(value)) {
269 const char *string = check_string(value);
270 uint8_t command_line = 0;
271
272 for (size_t index = 0; index < strlen(string); index++) {
273 switch (string[index]) {
274 case 'a': command_line |= PM_OPTIONS_COMMAND_LINE_A; break;
275 case 'e': command_line |= PM_OPTIONS_COMMAND_LINE_E; break;
276 case 'l': command_line |= PM_OPTIONS_COMMAND_LINE_L; break;
277 case 'n': command_line |= PM_OPTIONS_COMMAND_LINE_N; break;
278 case 'p': command_line |= PM_OPTIONS_COMMAND_LINE_P; break;
279 case 'x': command_line |= PM_OPTIONS_COMMAND_LINE_X; break;
280 default: rb_raise(rb_eArgError, "invalid command line flag: '%c'", string[index]); break;
281 }
282 }
283
284 pm_options_command_line_set(options, command_line);
285 }
286 } else if (key_id == rb_id_option_main_script) {
287 if (!NIL_P(value)) pm_options_main_script_set(options, RTEST(value));
288 } else if (key_id == rb_id_option_partial_script) {
289 if (!NIL_P(value)) pm_options_partial_script_set(options, RTEST(value));
290 } else if (key_id == rb_id_option_freeze) {
291 if (!NIL_P(value)) pm_options_freeze_set(options, RTEST(value));
292 } else if (key_id == rb_id_option_attach_comments) {
293 if (!NIL_P(value)) pm_options_attach_comments_set(options, RTEST(value));
294 } else if (key_id == rb_id_option_raise_error) {
295 if (!NIL_P(value)) {
296 if (value == Qtrue) {
297 /* When given true, color when $stderr is a terminal (unless
298 * NO_COLOR is set), bold styling when NO_COLOR is set, plain
299 * otherwise.
300 *
301 * rb_stderr_tty_p is not available to extensions, so instead
302 * we call the tty? method on $stderr, guarding against it
303 * having been replaced by an object that does not respond to
304 * tty?. */
306 const char *no_color = getenv("NO_COLOR");
307 if (no_color == NULL || no_color[0] == '\0') {
308 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_COLOR);
309 } else {
310 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_STYLE);
311 }
312 } else {
313 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_PLAIN);
314 }
315 } else {
316 if (!SYMBOL_P(value)) {
317 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Symbol)", rb_obj_class(value));
318 }
319
320 ID value_id = SYM2ID(value);
321 if (value_id == rb_id_raise_error_plain) {
322 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_PLAIN);
323 } else if (value_id == rb_id_raise_error_style) {
324 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_STYLE);
325 } else if (value_id == rb_id_raise_error_color) {
326 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_COLOR);
327 } else {
328 rb_raise(rb_eArgError, "invalid raise_error value: %" PRIsVALUE, value);
329 }
330 }
331 }
332 } else {
333 rb_raise(rb_eArgError, "unknown keyword: %" PRIsVALUE, key);
334 }
335
336 return ST_CONTINUE;
337}
338
345 pm_options_t *options;
346 VALUE keywords;
347};
348
353static VALUE
354build_options(VALUE argument) {
355 struct build_options_data *data = (struct build_options_data *) argument;
356 rb_hash_foreach(data->keywords, build_options_i, (VALUE) data->options);
357 return Qnil;
358}
359
363static void
364extract_options(pm_options_t *options, VALUE filepath, VALUE keywords) {
365 pm_options_line_set(options, 1); /* default */
366 options_version_set_nearest(options); /* default */
367
368 if (!NIL_P(keywords)) {
369 struct build_options_data data = { .options = options, .keywords = keywords };
370 struct build_options_data *argument = &data;
371
372 int state = 0;
373 rb_protect(build_options, (VALUE) argument, &state);
374
375 if (state != 0) {
376 pm_options_free(options);
377 rb_jump_tag(state);
378 }
379 }
380
381 if (!NIL_P(filepath)) {
382 if (!RB_TYPE_P(filepath, T_STRING)) {
383 pm_options_free(options);
384 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected String)", rb_obj_class(filepath));
385 }
386
387 pm_options_filepath_set(options, RSTRING_PTR(filepath));
388 }
389}
390
394static VALUE
395string_options(int argc, VALUE *argv, pm_options_t *options) {
396 VALUE string;
397 VALUE keywords;
398 rb_scan_args(argc, argv, "1:", &string, &keywords);
399
400 if (!RB_TYPE_P(string, T_STRING)) {
401 pm_options_free(options);
402 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected String)", rb_obj_class(string));
403 }
404
405 extract_options(options, Qnil, keywords);
406 return string;
407}
408
412static pm_source_t *
413file_options(int argc, VALUE *argv, pm_options_t *options, VALUE *encoded_filepath) {
414 VALUE filepath;
415 VALUE keywords;
416 rb_scan_args(argc, argv, "1:", &filepath, &keywords);
417
418 int state = 0;
419 filepath = rb_protect(rb_get_path, filepath, &state);
420 if (state != 0) {
421 pm_options_free(options);
422 rb_jump_tag(state);
423 }
424
425 *encoded_filepath = rb_str_encode_ospath(filepath);
426 extract_options(options, *encoded_filepath, keywords);
427
428 const char *source = (const char *) pm_string_source(pm_options_filepath(options));
430 pm_source_t *pm_src = pm_source_file_new(source, &result);
431
432 switch (result) {
434 break;
436 pm_options_free(options);
437
438#ifdef _WIN32
439 int e = rb_w32_map_errno(GetLastError());
440#else
441 int e = errno;
442#endif
443
444 rb_syserr_fail(e, source);
445 break;
446 }
448 pm_options_free(options);
449 rb_syserr_fail(EISDIR, source);
450 break;
451 default:
452 pm_options_free(options);
453 rb_raise(rb_eRuntimeError, "Unknown error (%d) initializing file: %s", result, source);
454 break;
455 }
456
457 return pm_src;
458}
459
465static result_t
466check_raise_error_option(pm_parser_t *parser, const pm_options_t *options, rb_encoding *path_encoding) {
467 if (pm_parser_errors_size(parser) == 0) return result_ok(Qnil);
468
469 uint8_t raise_error = pm_options_raise_error(options);
470 if (raise_error == 0) return result_ok(Qnil);
471
472 pm_buffer_t *buffer = pm_buffer_new();
473 if (buffer == NULL) {
474 return result_err(rb_exc_new_cstr(rb_eNoMemError, "failed to allocate memory"));
475 }
476
477 pm_error_level_t error_level = pm_errors_format(parser, buffer, (pm_errors_format_type_t) raise_error);
478
479 /* Copy the message out of the buffer and free it before building any
480 * other Ruby objects so that the buffer cannot be leaked if building the
481 * objects raises. */
482 rb_encoding *message_encoding = (error_level == PM_ERROR_LEVEL_LOAD) ? rb_locale_encoding() : rb_enc_find(pm_parser_encoding_name(parser));
483 VALUE message = rb_enc_str_new(pm_buffer_value(buffer), (long) pm_buffer_length(buffer), message_encoding);
484 pm_buffer_free(buffer);
485
486 VALUE error = Qnil;
487 switch (error_level) {
489 error = rb_exc_new_str(rb_eSyntaxError, message);
490
491 rb_encoding *path_encoding_normal = path_encoding == NULL ? rb_utf8_encoding() : path_encoding;
492 VALUE path = rb_enc_str_new((const char *) pm_string_source(pm_options_filepath(options)), pm_string_length(pm_options_filepath(options)), path_encoding_normal);
493 rb_ivar_set(error, rb_intern_const("@path"), path);
494 break;
495 }
497 error = rb_exc_new_str(rb_eArgError, message);
498 break;
499 }
500 case PM_ERROR_LEVEL_LOAD: {
501 error = rb_exc_new_str(rb_eLoadError, message);
502 rb_ivar_set(error, rb_intern_const("@path"), Qnil);
503 break;
504 }
506 error = rb_exc_new_str(rb_eSysStackError, message);
507 break;
508 }
509 }
510
511 return result_err(error);
512}
513
514#ifndef PRISM_EXCLUDE_SERIALIZATION
515
516/******************************************************************************/
517/* Serializing the AST */
518/******************************************************************************/
519
523static result_t
524dump_input(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
525 pm_arena_t *arena = pm_arena_new();
526 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
527 pm_node_t *node = pm_parse(parser);
528
529 result_t result = check_raise_error_option(parser, options, path_encoding);
530 if (result.type == RESULT_OK) {
531 pm_buffer_t *buffer = pm_buffer_new();
532 if (buffer) {
533 pm_serialize(parser, node, buffer);
534 result = result_ok(rb_str_new(pm_buffer_value(buffer), pm_buffer_length(buffer)));
535 pm_buffer_free(buffer);
536
537 if (pm_options_freeze(options)) rb_obj_freeze(result.value);
538 } else {
539 result = result_err(rb_exc_new_cstr(rb_eNoMemError, "failed to allocate memory"));
540 }
541 }
542
543 pm_parser_free(parser);
544 pm_arena_free(arena);
545
546 return result;
547}
548
557static VALUE
558dump(int argc, VALUE *argv, VALUE self) {
559 pm_options_t *options = pm_options_new();
560 VALUE string = string_options(argc, argv, options);
561
562 const uint8_t *source = (const uint8_t *) RSTRING_PTR(string);
563 size_t length = RSTRING_LEN(string);
564
565#ifdef PRISM_BUILD_DEBUG
566 char* dup = xmalloc(length);
567 memcpy(dup, source, length);
568 source = (const uint8_t *) dup;
569#endif
570
571 result_t result = dump_input(source, length, options, NULL);
572
573#ifdef PRISM_BUILD_DEBUG
574#ifdef xfree_sized
575 xfree_sized(dup, length);
576#else
577 xfree(dup);
578#endif
579#endif
580
581 pm_options_free(options);
582 return result_get(result);
583}
584
593static VALUE
594dump_file(int argc, VALUE *argv, VALUE self) {
595 pm_options_t *options = pm_options_new();
596
597 VALUE encoded_filepath;
598 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
599
600 result_t result = dump_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
601 pm_source_free(src);
602 pm_options_free(options);
603
604 return result_get(result);
605}
606
607#endif
608
609/******************************************************************************/
610/* Extracting values for the parse result */
611/******************************************************************************/
612
617static inline VALUE
618rb_class_new_instance_freeze(int argc, const VALUE *argv, VALUE klass, bool freeze) {
619 VALUE value = rb_class_new_instance(argc, argv, klass);
620 if (freeze) rb_obj_freeze(value);
621 return value;
622}
623
627static inline VALUE
628parser_location(VALUE source, bool freeze, uint32_t start, uint32_t length) {
629 VALUE argv[] = { source, LONG2FIX(start), LONG2FIX(length) };
630 return rb_class_new_instance_freeze(3, argv, rb_cPrismLocation, freeze);
631}
632
636#define PARSER_LOCATION(source, freeze, location) \
637 parser_location(source, freeze, location.start, location.length)
638
642static inline VALUE
643parser_comment(VALUE source, bool freeze, const pm_comment_t *comment) {
644 VALUE argv[] = { PARSER_LOCATION(source, freeze, pm_comment_location(comment)) };
645 VALUE type = (pm_comment_type(comment) == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment;
646 return rb_class_new_instance_freeze(1, argv, type, freeze);
647}
648
655static void
656parser_comments(const pm_parser_t *parser, VALUE comments, VALUE source, bool freeze) {
657 long index = 0;
658 for (const pm_comment_t *comment = pm_parser_comments_first(parser); comment != NULL; comment = pm_comment_next(comment)) {
659 rb_ary_store(comments, index++, parser_comment(source, freeze, comment));
660 }
661
662 if (freeze) rb_obj_freeze(comments);
663}
664
668static inline VALUE
669parser_magic_comment(VALUE source, bool freeze, const pm_magic_comment_t *magic_comment) {
672
673 VALUE key_loc = parser_location(source, freeze, key.start, key.length);
674 VALUE value_loc = parser_location(source, freeze, value.start, value.length);
675
676 VALUE argv[] = { key_loc, value_loc };
677 return rb_class_new_instance_freeze(2, argv, rb_cPrismMagicComment, freeze);
678}
679
683static VALUE
684parser_magic_comments(const pm_parser_t *parser, VALUE source, bool freeze) {
685 VALUE magic_comments = rb_ary_new_capa(pm_parser_magic_comments_size(parser));
686
687 for (const pm_magic_comment_t *magic_comment = pm_parser_magic_comments_first(parser); magic_comment != NULL; magic_comment = pm_magic_comment_next(magic_comment)) {
688 rb_ary_push(magic_comments, parser_magic_comment(source, freeze, magic_comment));
689 }
690
691 if (freeze) rb_obj_freeze(magic_comments);
692 return magic_comments;
693}
694
699static VALUE
700parser_data_loc(const pm_parser_t *parser, VALUE source, bool freeze) {
701 const pm_location_t *data_loc = pm_parser_data_loc(parser);
702
703 if (data_loc->length == 0) {
704 return Qnil;
705 } else {
706 return parser_location(source, freeze, data_loc->start, data_loc->length);
707 }
708}
709
713static VALUE
714parser_error(const pm_diagnostic_t *diagnostic, rb_encoding *encoding, VALUE source, bool freeze) {
715 VALUE type = ID2SYM(rb_intern(pm_diagnostic_type(diagnostic)));
716 VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), encoding));
717 VALUE location = PARSER_LOCATION(source, freeze, pm_diagnostic_location(diagnostic));
718
719 pm_error_level_t error_level = pm_diagnostic_error_level(diagnostic);
720 VALUE level = Qnil;
721
722 switch (error_level) {
724 level = ID2SYM(rb_intern("syntax"));
725 break;
727 level = ID2SYM(rb_intern("argument"));
728 break;
730 level = ID2SYM(rb_intern("load"));
731 break;
733 level = ID2SYM(rb_intern("stack"));
734 break;
735 default:
736 rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, error_level);
737 }
738
739 VALUE argv[] = { type, message, location, level };
740 return rb_class_new_instance_freeze(4, argv, rb_cPrismParseError, freeze);
741}
742
746static VALUE
747parser_errors(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) {
748 VALUE errors = rb_ary_new_capa(pm_parser_errors_size(parser));
749
750 for (const pm_diagnostic_t *error = pm_parser_errors_first(parser); error != NULL; error = pm_diagnostic_next(error)) {
751 rb_ary_push(errors, parser_error(error, encoding, source, freeze));
752 }
753
754 if (freeze) rb_obj_freeze(errors);
755 return errors;
756}
757
761static VALUE
762parser_warning(const pm_diagnostic_t *diagnostic, rb_encoding *encoding, VALUE source, bool freeze) {
763 VALUE type = ID2SYM(rb_intern(pm_diagnostic_type(diagnostic)));
764 VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), encoding));
765 VALUE location = PARSER_LOCATION(source, freeze, pm_diagnostic_location(diagnostic));
766
767 pm_warning_level_t warning_level = pm_diagnostic_warning_level(diagnostic);
768 VALUE level = Qnil;
769
770 switch (warning_level) {
772 level = ID2SYM(rb_intern("default"));
773 break;
775 level = ID2SYM(rb_intern("verbose"));
776 break;
777 default:
778 rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, warning_level);
779 }
780
781 VALUE argv[] = { type, message, location, level };
782 return rb_class_new_instance_freeze(4, argv, rb_cPrismParseWarning, freeze);
783}
784
788static VALUE
789parser_warnings(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) {
790 VALUE warnings = rb_ary_new_capa(pm_parser_warnings_size(parser));
791
792 for (const pm_diagnostic_t *warning = pm_parser_warnings_first(parser); warning != NULL; warning = pm_diagnostic_next(warning)) {
793 rb_ary_push(warnings, parser_warning(warning, encoding, source, freeze));
794 }
795
796 if (freeze) rb_obj_freeze(warnings);
797 return warnings;
798}
799
805static void
806parser_attachments_new(const pm_parser_t *parser, VALUE *attachments, VALUE *ranges) {
807 uint32_t ranges_size = pm_parser_comment_ranges_size(parser);
808 *attachments = rb_ary_new_capa(ranges_size == 0 ? 0 : (long) pm_parser_comments_size(parser));
809 *ranges = rb_ary_new_capa(ranges_size == 0 ? 0 : (long) ranges_size + 1);
810}
811
819static void
820parser_attachments(const pm_parser_t *parser, VALUE comments, VALUE attachments, VALUE ranges, bool freeze) {
821 uint32_t ranges_size = pm_parser_comment_ranges_size(parser);
822
823 if (ranges_size > 0) {
824 long size = RARRAY_LEN(comments);
825
826 for (long index = 0; index < size; index++) {
827 rb_ary_store(attachments, index, RARRAY_AREF(comments, pm_parser_comment_attachment(parser, (uint32_t) index)));
828 }
829
830 for (uint32_t node_id = 0; node_id < ranges_size; node_id++) {
831 pm_comment_range_t range = pm_parser_comment_range(parser, node_id);
832
833 if (range.length > 0) {
834 rb_ary_store(ranges, (long) node_id, LONG2FIX(range.index));
835 rb_ary_store(ranges, (long) node_id + 1, LONG2FIX(range.index + range.length));
836 }
837 }
838 }
839
840 if (freeze) {
841 rb_obj_freeze(attachments);
842 rb_obj_freeze(ranges);
843 }
844}
845
849static VALUE
850parse_result_create(VALUE class, const pm_parser_t *parser, VALUE value, rb_encoding *encoding, VALUE source, bool freeze) {
851 VALUE result_argv[] = {
852 value,
853 parser_magic_comments(parser, source, freeze),
854 parser_data_loc(parser, source, freeze),
855 parser_errors(parser, encoding, source, freeze),
856 parser_warnings(parser, encoding, source, freeze),
857 pm_parser_continuable(parser) ? Qtrue : Qfalse,
858 source
859 };
860
861 return rb_class_new_instance_freeze(7, result_argv, class, freeze);
862}
863
864/******************************************************************************/
865/* Lexing Ruby code */
866/******************************************************************************/
867
873typedef struct {
874 VALUE source;
875 VALUE tokens;
876 rb_encoding *encoding;
877 bool freeze;
879
885static void
886parse_lex_token(pm_parser_t *parser, pm_token_t *token, void *data) {
887 parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) data;
888
889 VALUE value = pm_token_new(parser, token, parse_lex_data->encoding, parse_lex_data->source, parse_lex_data->freeze);
890 rb_ary_push(parse_lex_data->tokens, value);
891}
892
898static void
899parse_lex_encoding_changed_callback(pm_parser_t *parser) {
900 parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) pm_parser_lex_callback_data(parser);
901 parse_lex_data->encoding = rb_enc_find(pm_parser_encoding_name(parser));
902
903 // Since the encoding changed, we need to go back and change the encoding of
904 // the tokens that were already lexed. This is only going to end up being
905 // one or two tokens, since the encoding can only change at the top of the
906 // file.
907 VALUE tokens = parse_lex_data->tokens;
908 VALUE next_tokens = rb_ary_new();
909
910 for (long index = 0; index < RARRAY_LEN(tokens); index++) {
911 VALUE token = rb_ary_entry(tokens, index);
912 VALUE value = rb_ivar_get(token, rb_intern("@value"));
913 VALUE next_value = rb_str_dup(value);
914
915 rb_enc_associate(next_value, parse_lex_data->encoding);
916 if (parse_lex_data->freeze) rb_obj_freeze(next_value);
917
918 VALUE next_token_argv[] = {
919 parse_lex_data->source,
920 rb_ivar_get(token, rb_intern("@type")),
921 next_value,
922 rb_ivar_get(token, rb_intern("@location")),
923 rb_ivar_get(token, rb_intern("@state")),
924 };
925
926 VALUE next_token = rb_class_new_instance(5, next_token_argv, rb_cPrismToken);
927
928 if (parse_lex_data->freeze) {
929 rb_obj_freeze(next_token);
930 }
931
932 rb_ary_push(next_tokens, next_token);
933 }
934
935 rb_ary_replace(parse_lex_data->tokens, next_tokens);
936}
937
942static result_t
943parse_lex_input(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding, bool return_nodes) {
944 pm_arena_t *arena = pm_arena_new();
945 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
946 pm_parser_encoding_changed_callback_set(parser, parse_lex_encoding_changed_callback);
947
948 VALUE source_string = rb_str_new((const char *) input, input_length);
949 VALUE offsets = rb_ary_new_capa(pm_parser_line_offsets(parser)->size);
951 VALUE attachments = rb_ary_new();
952 VALUE ranges = rb_ary_new();
953 VALUE source = rb_funcall(rb_cPrismSource, rb_id_source_for, 6, source_string, LONG2NUM(pm_parser_start_line(parser)), offsets, comments, attachments, ranges);
954
955 parse_lex_data_t parse_lex_data = {
956 .source = source,
957 .tokens = rb_ary_new(),
958 .encoding = rb_enc_find(pm_parser_encoding_name(parser)),
959 .freeze = pm_options_freeze(options),
960 };
961
962 parse_lex_data_t *data = &parse_lex_data;
963 pm_parser_lex_callback_set(parser, parse_lex_token, data);
964
965 pm_node_t *node = pm_parse(parser);
966
967 result_t result = check_raise_error_option(parser, options, path_encoding);
968 if (result.type == RESULT_OK) {
969 /* Update the Source object with the correct encoding, line offsets,
970 * and source hash, which are only available after pm_parse()
971 * completes. */
972 rb_encoding *encoding = rb_enc_find(pm_parser_encoding_name(parser));
973 rb_enc_associate(source_string, encoding);
974
975 const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(parser);
976 for (size_t index = 0; index < line_offsets->size; index++) {
977 rb_ary_store(offsets, (long) index, ULONG2NUM(line_offsets->offsets[index]));
978 }
979
980 rb_funcall(source, rb_intern("replace_checksum"), 1, ULL2NUM(pm_parser_checksum(parser)));
981
982 if (pm_options_freeze(options)) {
983 rb_obj_freeze(source_string);
984 rb_obj_freeze(offsets);
985 rb_obj_freeze(source);
986 rb_obj_freeze(parse_lex_data.tokens);
987 }
988
989 /* The comment lists were created along with the source before parsing,
990 * so they are grown to their final sizes now that those are known. */
991 uint32_t ranges_size = pm_parser_comment_ranges_size(parser);
992 rb_ary_resize(comments, (long) pm_parser_comments_size(parser));
993
994 if (ranges_size > 0) {
995 rb_ary_resize(attachments, (long) pm_parser_comments_size(parser));
996 rb_ary_resize(ranges, (long) ranges_size + 1);
997 }
998
999 parser_comments(parser, comments, source, pm_options_freeze(options));
1000 parser_attachments(parser, comments, attachments, ranges, pm_options_freeze(options));
1001
1002 if (return_nodes) {
1003 VALUE value = rb_ary_new_capa(2);
1004 rb_ary_push(value, pm_ast_new(parser, node, parse_lex_data.encoding, source, pm_options_freeze(options)));
1005 rb_ary_push(value, parse_lex_data.tokens);
1006 if (pm_options_freeze(options)) rb_obj_freeze(value);
1007 result = result_ok(parse_result_create(rb_cPrismParseLexResult, parser, value, parse_lex_data.encoding, source, pm_options_freeze(options)));
1008 } else {
1009 result = result_ok(parse_result_create(rb_cPrismLexResult, parser, parse_lex_data.tokens, parse_lex_data.encoding, source, pm_options_freeze(options)));
1010 }
1011 }
1012
1013 pm_parser_free(parser);
1014 pm_arena_free(arena);
1015
1016 return result;
1017}
1018
1027static VALUE
1028lex(int argc, VALUE *argv, VALUE self) {
1029 pm_options_t *options = pm_options_new();
1030 VALUE string = string_options(argc, argv, options);
1031
1032 result_t result = parse_lex_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL, false);
1033 pm_options_free(options);
1034
1035 return result_get(result);
1036}
1037
1046static VALUE
1047lex_file(int argc, VALUE *argv, VALUE self) {
1048 pm_options_t *options = pm_options_new();
1049
1050 VALUE encoded_filepath;
1051 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1052
1053 result_t result = parse_lex_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath), false);
1054 pm_source_free(src);
1055 pm_options_free(options);
1056
1057 return result_get(result);
1058}
1059
1060/******************************************************************************/
1061/* Parsing Ruby code */
1062/******************************************************************************/
1063
1067static result_t
1068parse_input(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
1069 pm_arena_t *arena = pm_arena_new();
1070 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
1071
1072 pm_node_t *node = pm_parse(parser);
1073
1074 result_t result = check_raise_error_option(parser, options, path_encoding);
1075 if (result.type == RESULT_OK) {
1076 rb_encoding *encoding = rb_enc_find(pm_parser_encoding_name(parser));
1077
1078 bool freeze = pm_options_freeze(options);
1079 VALUE comments = rb_ary_new_capa(pm_parser_comments_size(parser));
1080 VALUE attachments, ranges;
1081 parser_attachments_new(parser, &attachments, &ranges);
1082
1083 VALUE source = pm_source_new(parser, encoding, comments, attachments, ranges, freeze);
1084 parser_comments(parser, comments, source, freeze);
1085 parser_attachments(parser, comments, attachments, ranges, freeze);
1086
1087 VALUE value = pm_ast_new(parser, node, encoding, source, freeze);
1088 result = result_ok(parse_result_create(rb_cPrismParseResult, parser, value, encoding, source, freeze));
1089 }
1090
1091 pm_parser_free(parser);
1092 pm_arena_free(arena);
1093
1094 return result;
1095}
1096
1161static VALUE
1162parse(int argc, VALUE *argv, VALUE self) {
1163 pm_options_t *options = pm_options_new();
1164 VALUE string = string_options(argc, argv, options);
1165
1166 const uint8_t *source = (const uint8_t *) RSTRING_PTR(string);
1167 size_t length = RSTRING_LEN(string);
1168
1169#ifdef PRISM_BUILD_DEBUG
1170 char* dup = xmalloc(length);
1171 memcpy(dup, source, length);
1172 source = (const uint8_t *) dup;
1173#endif
1174
1175 result_t result = parse_input(source, length, options, NULL);
1176
1177#ifdef PRISM_BUILD_DEBUG
1178#ifdef xfree_sized
1179 xfree_sized(dup, length);
1180#else
1181 xfree(dup);
1182#endif
1183#endif
1184
1185 pm_options_free(options);
1186 return result_get(result);
1187}
1188
1197static VALUE
1198parse_file(int argc, VALUE *argv, VALUE self) {
1199 pm_options_t *options = pm_options_new();
1200
1201 VALUE encoded_filepath;
1202 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1203
1204 result_t result = parse_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
1205 pm_source_free(src);
1206 pm_options_free(options);
1207
1208 return result_get(result);
1209}
1210
1214static result_t
1215profile_input(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
1216 pm_arena_t *arena = pm_arena_new();
1217 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
1218
1219 pm_parse(parser);
1220
1221 result_t result = check_raise_error_option(parser, options, path_encoding);
1222 pm_parser_free(parser);
1223 pm_arena_free(arena);
1224
1225 return result;
1226}
1227
1237static VALUE
1238profile(int argc, VALUE *argv, VALUE self) {
1239 pm_options_t *options = pm_options_new();
1240 VALUE string = string_options(argc, argv, options);
1241
1242 result_t result = profile_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
1243 pm_options_free(options);
1244 result_get(result);
1245
1246 return Qnil;
1247}
1248
1258static VALUE
1259profile_file(int argc, VALUE *argv, VALUE self) {
1260 pm_options_t *options = pm_options_new();
1261
1262 VALUE encoded_filepath;
1263 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1264
1265 result_t result = profile_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
1266 pm_source_free(src);
1267 pm_options_free(options);
1268 result_get(result);
1269
1270 return Qnil;
1271}
1272
1273static int
1274parse_stream_eof(void *stream) {
1275 if (rb_funcall((VALUE) stream, rb_intern("eof?"), 0)) {
1276 return 1;
1277 }
1278 return 0;
1279}
1280
1293#define MAX_ENC_LEN 6
1294
1300static char *
1301parse_stream_fgets(char *string, int size, void *stream) {
1302 RUBY_ASSERT(size > MAX_ENC_LEN);
1303
1304 /*
1305 * Request fewer bytes than the buffer can hold. `gets` may return more
1306 * bytes than requested when the limit falls in the middle of a multi-byte
1307 * character, and the reserved headroom guarantees the result still fits.
1308 */
1309 VALUE line = rb_funcall((VALUE) stream, rb_intern("gets"), 1, INT2FIX(size - MAX_ENC_LEN));
1310
1311 /*
1312 * A well-behaved stream returns a String, or nil at EOF. Coerce anything
1313 * else to nil so that we never treat a non-String as a byte buffer.
1314 */
1315 line = rb_check_string_type(line);
1316 if (NIL_P(line)) {
1317 return NULL;
1318 }
1319
1320 const char *cstr = RSTRING_PTR(line);
1321 size_t length = (size_t) RSTRING_LEN(line);
1322
1323 /*
1324 * Defensively clamp the copy. A misbehaving `gets` may ignore the limit
1325 * entirely and return an arbitrarily long string; we must never write past
1326 * the caller's buffer. One byte is reserved for the NUL terminator.
1327 */
1328 if (length > (size_t) (size - 1)) {
1329 length = (size_t) (size - 1);
1330 }
1331
1332 memcpy(string, cstr, length);
1333 string[length] = '\0';
1334
1335 return string;
1336}
1337
1338#undef MAX_ENC_LEN
1339
1348static VALUE
1349parse_stream(int argc, VALUE *argv, VALUE self) {
1350 VALUE stream;
1351 VALUE keywords;
1352 rb_scan_args(argc, argv, "1:", &stream, &keywords);
1353
1354 pm_options_t *options = pm_options_new();
1355 extract_options(options, Qnil, keywords);
1356
1357 pm_source_t *src = pm_source_stream_new((void *) stream, parse_stream_fgets, parse_stream_eof);
1358 pm_arena_t *arena = pm_arena_new();
1359 pm_parser_t *parser;
1360
1361 pm_node_t *node = pm_parse_stream(&parser, arena, src, options);
1362
1363 result_t result = check_raise_error_option(parser, options, NULL);
1364 if (result.type == RESULT_OK) {
1365 rb_encoding *encoding = rb_enc_find(pm_parser_encoding_name(parser));
1366
1367 VALUE comments = rb_ary_new_capa(pm_parser_comments_size(parser));
1368 VALUE attachments, ranges;
1369 parser_attachments_new(parser, &attachments, &ranges);
1370
1371 VALUE source = pm_source_new(parser, encoding, comments, attachments, ranges, pm_options_freeze(options));
1372 parser_comments(parser, comments, source, pm_options_freeze(options));
1373 parser_attachments(parser, comments, attachments, ranges, pm_options_freeze(options));
1374
1375 VALUE value = pm_ast_new(parser, node, encoding, source, pm_options_freeze(options));
1376
1377 result = result_ok(parse_result_create(rb_cPrismParseResult, parser, value, encoding, source, pm_options_freeze(options)));
1378 }
1379
1380 pm_source_free(src);
1381 pm_parser_free(parser);
1382 pm_arena_free(arena);
1383 pm_options_free(options);
1384
1385 return result_get(result);
1386}
1387
1391static result_t
1392parse_input_comments(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
1393 pm_arena_t *arena = pm_arena_new();
1394 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
1395
1396 pm_parse(parser);
1397
1398 result_t result = check_raise_error_option(parser, options, path_encoding);
1399 if (result.type == RESULT_OK) {
1400 rb_encoding *encoding = rb_enc_find(pm_parser_encoding_name(parser));
1401
1402 VALUE comments = rb_ary_new_capa(pm_parser_comments_size(parser));
1403 VALUE attachments, ranges;
1404 parser_attachments_new(parser, &attachments, &ranges);
1405
1406 VALUE source = pm_source_new(parser, encoding, comments, attachments, ranges, pm_options_freeze(options));
1407 parser_comments(parser, comments, source, pm_options_freeze(options));
1408 parser_attachments(parser, comments, attachments, ranges, pm_options_freeze(options));
1409
1410 result = result_ok(comments);
1411 }
1412
1413 pm_parser_free(parser);
1414 pm_arena_free(arena);
1415
1416 return result;
1417}
1418
1427static VALUE
1428parse_comments(int argc, VALUE *argv, VALUE self) {
1429 pm_options_t *options = pm_options_new();
1430 VALUE string = string_options(argc, argv, options);
1431
1432 result_t result = parse_input_comments((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
1433 pm_options_free(options);
1434
1435 return result_get(result);
1436}
1437
1446static VALUE
1447parse_file_comments(int argc, VALUE *argv, VALUE self) {
1448 pm_options_t *options = pm_options_new();
1449
1450 VALUE encoded_filepath;
1451 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1452
1453 result_t result = parse_input_comments(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
1454 pm_source_free(src);
1455 pm_options_free(options);
1456
1457 return result_get(result);
1458}
1459
1475static VALUE
1476parse_lex(int argc, VALUE *argv, VALUE self) {
1477 pm_options_t *options = pm_options_new();
1478 VALUE string = string_options(argc, argv, options);
1479
1480 result_t result = parse_lex_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL, true);
1481 pm_options_free(options);
1482
1483 return result_get(result);
1484}
1485
1501static VALUE
1502parse_lex_file(int argc, VALUE *argv, VALUE self) {
1503 pm_options_t *options = pm_options_new();
1504
1505 VALUE encoded_filepath;
1506 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1507
1508 result_t result = parse_lex_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath), true);
1509 pm_source_free(src);
1510 pm_options_free(options);
1511
1512 return result_get(result);
1513}
1514
1518static result_t
1519parse_input_success_p(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
1520 pm_arena_t *arena = pm_arena_new();
1521 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
1522 pm_parse(parser);
1523
1524 result_t result = check_raise_error_option(parser, options, path_encoding);
1525 if (result.type == RESULT_OK) {
1526 result = result_ok(pm_parser_errors_size(parser) == 0 ? Qtrue : Qfalse);
1527 }
1528
1529 pm_parser_free(parser);
1530 pm_arena_free(arena);
1531
1532 return result;
1533}
1534
1543static VALUE
1544parse_success_p(int argc, VALUE *argv, VALUE self) {
1545 pm_options_t *options = pm_options_new();
1546 VALUE string = string_options(argc, argv, options);
1547
1548 result_t result = parse_input_success_p((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
1549 pm_options_free(options);
1550
1551 return result_get(result);
1552}
1553
1562static VALUE
1563parse_failure_p(int argc, VALUE *argv, VALUE self) {
1564 return RTEST(parse_success_p(argc, argv, self)) ? Qfalse : Qtrue;
1565}
1566
1575static VALUE
1576parse_file_success_p(int argc, VALUE *argv, VALUE self) {
1577 pm_options_t *options = pm_options_new();
1578
1579 VALUE encoded_filepath;
1580 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1581
1582 result_t result = parse_input_success_p(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
1583 pm_source_free(src);
1584 pm_options_free(options);
1585
1586 return result_get(result);
1587}
1588
1597static VALUE
1598parse_file_failure_p(int argc, VALUE *argv, VALUE self) {
1599 return RTEST(parse_file_success_p(argc, argv, self)) ? Qfalse : Qtrue;
1600}
1601
1602/******************************************************************************/
1603/* String query methods */
1604/******************************************************************************/
1605
1611static const char *
1612string_query_nearest_version(void) {
1613 if (pm_options_version_supported_p(ruby_version, 3)) return ruby_version;
1614 return ruby_version_before_lowest_p() ? "3.3" : NULL;
1615}
1616
1625static const char *
1626string_query_arguments(int argc, VALUE *argv, VALUE *string) {
1627 VALUE keywords;
1628 rb_scan_args(argc, argv, "1:", string, &keywords);
1629 check_string(*string);
1630
1631 VALUE value = Qundef;
1632 if (!NIL_P(keywords)) {
1633 ID keys[1] = { rb_id_option_version };
1634 rb_get_kwargs(keywords, keys, 0, 1, &value);
1635 }
1636
1637 if (value == Qundef || NIL_P(value)) return string_query_nearest_version();
1638
1639 const char *version = StringValueCStr(value);
1640 if (strcmp(version, "nearest") == 0) return string_query_nearest_version();
1641
1642 if (strcmp(version, "current") == 0) {
1643 if (!pm_options_version_supported_p(ruby_version, 3)) {
1644 rb_exc_raise(rb_exc_new_cstr(rb_cPrismCurrentVersionError, ruby_version));
1645 }
1646
1647 return ruby_version;
1648 }
1649
1650 return version;
1651}
1652
1657static VALUE
1658string_query(pm_string_query_t result, const char *version) {
1659 switch (result) {
1661 rb_raise(rb_eArgError, "invalid version: %s", version);
1662 return Qfalse;
1664 rb_raise(rb_eArgError, "Invalid or non ascii-compatible encoding");
1665 return Qfalse;
1667 return Qfalse;
1669 return Qtrue;
1670 }
1671
1672 return Qfalse;
1673}
1674
1688static VALUE
1689string_query_local_p(int argc, VALUE *argv, VALUE self) {
1690 VALUE string;
1691 const char *version = string_query_arguments(argc, argv, &string);
1692 return string_query(pm_string_query_local((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), rb_enc_get(string)->name, version), version);
1693}
1694
1708static VALUE
1709string_query_constant_p(int argc, VALUE *argv, VALUE self) {
1710 VALUE string;
1711 const char *version = string_query_arguments(argc, argv, &string);
1712 return string_query(pm_string_query_constant((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), rb_enc_get(string)->name, version), version);
1713}
1714
1726static VALUE
1727string_query_method_name_p(int argc, VALUE *argv, VALUE self) {
1728 VALUE string;
1729 const char *version = string_query_arguments(argc, argv, &string);
1730 return string_query(pm_string_query_method_name((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), rb_enc_get(string)->name, version), version);
1731}
1732
1745static VALUE
1746string_query_keyword_p(int argc, VALUE *argv, VALUE self) {
1747 VALUE string;
1748 const char *version = string_query_arguments(argc, argv, &string);
1749 return string_query(pm_string_query_keyword((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), rb_enc_get(string)->name, version), version);
1750}
1751
1752/******************************************************************************/
1753/* Initialization of the extension */
1754/******************************************************************************/
1755
1759RUBY_FUNC_EXPORTED void
1760Init_prism(void) {
1761 /* Make sure that the prism library version matches the expected version.
1762 * Otherwise something was compiled incorrectly. */
1763 if (strcmp(pm_version(), EXPECTED_PRISM_VERSION) != 0) {
1764 rb_raise(
1766 "The prism library version (%s) does not match the expected version (%s)",
1767 pm_version(),
1768 EXPECTED_PRISM_VERSION
1769 );
1770 }
1771
1772#ifdef HAVE_RB_EXT_RACTOR_SAFE
1773 /* Mark this extension as Ractor-safe. */
1774 rb_ext_ractor_safe(true);
1775#endif
1776
1777 VALUE mRuby = rb_define_module("Ruby"); rb_cPrism = rb_define_module_under(mRuby, "Prism");
1778 rb_cPrismNode = rb_define_class_under(rb_cPrism, "Node", rb_cObject);
1779 rb_cPrismSource = rb_define_class_under(rb_cPrism, "Source", rb_cObject);
1780 rb_cPrismToken = rb_define_class_under(rb_cPrism, "Token", rb_cObject);
1781 rb_cPrismLocation = rb_define_class_under(rb_cPrism, "Location", rb_cObject);
1782 rb_cPrismComment = rb_define_class_under(rb_cPrism, "Comment", rb_cObject);
1783 rb_cPrismInlineComment = rb_define_class_under(rb_cPrism, "InlineComment", rb_cPrismComment);
1784 rb_cPrismEmbDocComment = rb_define_class_under(rb_cPrism, "EmbDocComment", rb_cPrismComment);
1785 rb_cPrismMagicComment = rb_define_class_under(rb_cPrism, "MagicComment", rb_cObject);
1786 rb_cPrismParseError = rb_define_class_under(rb_cPrism, "ParseError", rb_cObject);
1787 rb_cPrismParseWarning = rb_define_class_under(rb_cPrism, "ParseWarning", rb_cObject);
1788 rb_cPrismResult = rb_define_class_under(rb_cPrism, "Result", rb_cObject);
1789 rb_cPrismParseResult = rb_define_class_under(rb_cPrism, "ParseResult", rb_cPrismResult);
1790 rb_cPrismLexResult = rb_define_class_under(rb_cPrism, "LexResult", rb_cPrismResult);
1791 rb_cPrismParseLexResult = rb_define_class_under(rb_cPrism, "ParseLexResult", rb_cPrismResult);
1792 rb_cPrismStringQuery = rb_define_class_under(rb_cPrism, "StringQuery", rb_cObject);
1793 rb_cPrismScope = rb_define_class_under(rb_cPrism, "Scope", rb_cObject);
1794
1795 rb_cPrismCurrentVersionError = rb_const_get(rb_cPrism, rb_intern("CurrentVersionError"));
1796
1797 /* Intern all of the IDs eagerly that we support so that we do not have to
1798 * do it every time we parse. */
1799 rb_id_option_attach_comments = rb_intern_const("attach_comments");
1800 rb_id_option_command_line = rb_intern_const("command_line");
1801 rb_id_option_encoding = rb_intern_const("encoding");
1802 rb_id_option_filepath = rb_intern_const("filepath");
1803 rb_id_option_freeze = rb_intern_const("freeze");
1804 rb_id_option_frozen_string_literal = rb_intern_const("frozen_string_literal");
1805 rb_id_option_line = rb_intern_const("line");
1806 rb_id_option_main_script = rb_intern_const("main_script");
1807 rb_id_option_partial_script = rb_intern_const("partial_script");
1808 rb_id_option_raise_error = rb_intern_const("raise_error");
1809 rb_id_option_scopes = rb_intern_const("scopes");
1810 rb_id_option_version = rb_intern_const("version");
1811
1812 rb_id_source_for = rb_intern("for");
1813
1814 rb_id_forwarding_positionals = rb_intern("*");
1815 rb_id_forwarding_keywords = rb_intern("**");
1816 rb_id_forwarding_block = rb_intern("&");
1817 rb_id_forwarding_all = rb_intern("...");
1818
1819 rb_id_raise_error_plain = rb_intern_const("plain");
1820 rb_id_raise_error_style = rb_intern_const("style");
1821 rb_id_raise_error_color = rb_intern_const("color");
1822
1826 rb_define_const(rb_cPrism, "VERSION", rb_str_freeze(rb_str_new_cstr(EXPECTED_PRISM_VERSION)));
1827
1828 rb_define_singleton_method(rb_cPrism, "lex", lex, -1);
1829 rb_define_singleton_method(rb_cPrism, "lex_file", lex_file, -1);
1830 rb_define_singleton_method(rb_cPrism, "parse", parse, -1);
1831 rb_define_singleton_method(rb_cPrism, "parse_file", parse_file, -1);
1832 rb_define_singleton_method(rb_cPrism, "profile", profile, -1);
1833 rb_define_singleton_method(rb_cPrism, "profile_file", profile_file, -1);
1834 rb_define_singleton_method(rb_cPrism, "parse_stream", parse_stream, -1);
1835 rb_define_singleton_method(rb_cPrism, "parse_comments", parse_comments, -1);
1836 rb_define_singleton_method(rb_cPrism, "parse_file_comments", parse_file_comments, -1);
1837 rb_define_singleton_method(rb_cPrism, "parse_lex", parse_lex, -1);
1838 rb_define_singleton_method(rb_cPrism, "parse_lex_file", parse_lex_file, -1);
1839 rb_define_singleton_method(rb_cPrism, "parse_success?", parse_success_p, -1);
1840 rb_define_singleton_method(rb_cPrism, "parse_failure?", parse_failure_p, -1);
1841 rb_define_singleton_method(rb_cPrism, "parse_file_success?", parse_file_success_p, -1);
1842 rb_define_singleton_method(rb_cPrism, "parse_file_failure?", parse_file_failure_p, -1);
1843
1844#ifndef PRISM_EXCLUDE_SERIALIZATION
1845 rb_define_singleton_method(rb_cPrism, "dump", dump, -1);
1846 rb_define_singleton_method(rb_cPrism, "dump_file", dump_file, -1);
1847#endif
1848
1849 rb_define_singleton_method(rb_cPrismStringQuery, "local?", string_query_local_p, -1);
1850 rb_define_singleton_method(rb_cPrismStringQuery, "constant?", string_query_constant_p, -1);
1851 rb_define_singleton_method(rb_cPrismStringQuery, "method_name?", string_query_method_name_p, -1);
1852 rb_define_singleton_method(rb_cPrismStringQuery, "keyword?", string_query_keyword_p, -1);
1853
1854 Init_prism_api_node();
1855}
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_arena_t * pm_arena_new(void)
Returns a newly allocated and initialized arena.
Definition arena.c:177
PRISM_EXPORTED_FUNCTION pm_location_t pm_comment_location(const pm_comment_t *comment) PRISM_NONNULL(1)
Returns the location associated with the given comment.
Definition parser.c:190
PRISM_EXPORTED_FUNCTION pm_comment_type_t pm_comment_type(const pm_comment_t *comment) PRISM_NONNULL(1)
Returns the type associated with the given comment.
Definition parser.c:198
PRISM_EXPORTED_FUNCTION const pm_comment_t * pm_comment_next(const pm_comment_t *comment) PRISM_NONNULL(1)
Returns the comment that follows the given comment among the comments associated with the parser that...
Definition parser.c:206
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
pm_warning_level_t
The levels of warnings generated during parsing.
Definition diagnostic.h:48
@ PM_WARNING_LEVEL_DEFAULT
For warnings which should be emitted if $VERBOSE != nil.
Definition diagnostic.h:50
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
Definition diagnostic.h:53
pm_error_level_t
The levels of errors generated during parsing.
Definition diagnostic.h:27
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
Definition diagnostic.h:32
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
Definition diagnostic.h:35
@ PM_ERROR_LEVEL_STACK
For errors that should raise a system stack error.
Definition diagnostic.h:42
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
Definition diagnostic.h:29
pm_errors_format_type_t
The type of formatting to use when formatting errors.
@ PM_ERRORS_FORMAT_PLAIN
Format errors in a plain format with no colors or styles.
@ PM_ERRORS_FORMAT_COLOR
Format errors in a color format with bold and colors.
@ PM_ERRORS_FORMAT_STYLE
Format errors in a style format with bold only.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3358
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:3147
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:678
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
Definition error.c:4053
VALUE rb_eNoMemError
NoMemoryError exception.
Definition error.c:1468
VALUE rb_eLoadError
LoadError exception.
Definition error.c:1475
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1457
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1455
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1508
VALUE rb_eSysStackError
SystemStackError exception.
Definition eval.c:51
VALUE rb_eSyntaxError
SyntaxError exception.
Definition error.c:1474
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2305
VALUE rb_stderr
STDERR constant.
Definition io.c:194
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:230
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:902
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
Definition object.c:1305
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
Definition encoding.c:1578
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
Definition encoding.c:1630
VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
Definition string.c:1189
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1149
VALUE rb_ary_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_ary_resize(VALUE ary, rb_len_t len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_replace(VALUE copy, VALUE orig)
Replaces the contents of the former object with the contents of the latter.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
void rb_ext_ractor_safe(bool flag)
Asserts that the extension library that calls this function is aware of Ractor.
Definition load.c:1587
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1523
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1695
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2052
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3413
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:3069
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1539
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3535
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2168
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1651
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3743
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
const char ruby_version[]
Stringised version.
Definition version.c:82
PRISM_EXPORTED_FUNCTION pm_location_t pm_magic_comment_value(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1)
Returns the location of the value associated with the given magic comment.
Definition parser.c:285
PRISM_EXPORTED_FUNCTION pm_location_t pm_magic_comment_key(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1)
Returns the location of the key associated with the given magic comment.
Definition parser.c:277
PRISM_EXPORTED_FUNCTION const pm_magic_comment_t * pm_magic_comment_next(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1)
Returns the magic comment that follows the given magic comment among the magic comments associated wi...
Definition parser.c:293
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
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:88
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:94
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:81
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE
The default value for parameters.
Definition options.h:49
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL
When the scope is forwarding with the ... parameter.
Definition options.h:61
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS
When the scope is forwarding with the * parameter.
Definition options.h:52
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS
When the scope is forwarding with the ** parameter.
Definition options.h:55
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:100
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:112
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK
When the scope is forwarding with the & parameter.
Definition options.h:58
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_options_t * pm_options_new(void)
Allocate a new options struct.
Definition options.c:18
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:106
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_parser_t * pm_parser_new(pm_arena_t *arena, const uint8_t *source, size_t size, const pm_options_t *options) PRISM_NONNULL(1)
Allocate and initialize a parser with the given start and end pointers.
Definition prism.c:23467
PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser) PRISM_NONNULL(1)
Free both the memory held by the given parser and the parser itself.
Definition prism.c:23488
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser) PRISM_NONNULL(1)
Initiate the parser with the given parser.
Definition prism.c:23659
comment_range comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The comments found while parsing, in source order.
Definition parser.hpp:257
diagnostic_range errors(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The errors found while parsing.
Definition parser.hpp:235
diagnostic_range warnings(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The warnings found while parsing.
Definition parser.hpp:246
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_buffer_t * pm_buffer_new(void)
Allocate and initialize a new buffer.
Definition buffer.c:30
PRISM_EXPORTED_FUNCTION const char * pm_version(void)
The prism version and the serialization format.
Definition prism.c:74
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
#define RARRAY_AREF(a, i)
References an object in an array.
Definition rarray.h:422
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
pm_source_init_result_t
Represents the result of initializing a source from a file.
Definition source.h:50
@ PM_SOURCE_INIT_ERROR_GENERIC
Indicates a generic error from a source init function, where the type of error should be read from er...
Definition source.h:58
@ PM_SOURCE_INIT_SUCCESS
Indicates that the source was successfully initialized.
Definition source.h:52
@ PM_SOURCE_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
Definition source.h:63
#define RTEST
This is an old name of RB_TEST.
pm_string_query_t
Represents the results of a slice query.
@ PM_STRING_QUERY_TRUE
Returned if the result of the slice query is true.
@ PM_STRING_QUERY_ERROR
Returned if the encoding given to a slice query was invalid.
@ PM_STRING_QUERY_FALSE
Returned if the result of the slice query is false.
@ PM_STRING_QUERY_ERROR_VERSION
Returned if the version given to a slice query was invalid.
We need a struct here to pass through rb_protect and it has to be a single value.
Definition extension.c:344
This struct gets stored in the parser and passed in to the lex callback any time a new token is found...
Definition extension.c:873
The comments owned by a node, as a slice of the comment attachments of the parser that produced it.
Definition comments.h:34
A list of offsets of the start of lines in a string.
uint32_t * offsets
The list of offsets.
size_t size
The number of offsets in the list.
This struct represents a slice in the source code, defined by an offset and a length.
Definition ast.h:579
uint32_t start
The offset of the location from the start of the source.
Definition ast.h:581
uint32_t length
The length of the location.
Definition ast.h:584
A view of a sequence of bytes.
Definition stringy.h:23
This struct represents a token in the Ruby source.
Definition ast.h:551
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376