Ruby 4.1.0dev (2026-12-02 revision 4ff592f0ab1c35343efe112d4cc31b853f455a38)
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 VALUE name = rb_id2str(SYM2ID(local));
195 pm_string_init(scope_local, (const uint8_t *) RSTRING_PTR(name), RSTRING_LEN(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
230 pm_options_t *options;
231 VALUE keywords;
232 VALUE filepath;
233};
234
238static int
239build_options_i(VALUE key, VALUE value, VALUE argument) {
240 struct build_options_data *data = (struct build_options_data *) argument;
241 pm_options_t *options = data->options;
242 ID key_id = SYM2ID(key);
243
244 if (key_id == rb_id_option_filepath) {
245 if (!NIL_P(value)) {
246 check_string(value);
247 /* A positional filepath takes precedence over the keyword. */
248 if (NIL_P(data->filepath)) data->filepath = value;
249 }
250 } else if (key_id == rb_id_option_encoding) {
251 if (!NIL_P(value)) {
252 if (value == Qfalse) {
253 pm_options_encoding_locked_set(options, true);
254 } else {
255 const char *name = rb_enc_name(rb_to_encoding(value));
256 pm_options_encoding_set(options, name, strlen(name));
257 }
258 }
259 } else if (key_id == rb_id_option_line) {
260 if (!NIL_P(value)) pm_options_line_set(options, NUM2INT(value));
261 } else if (key_id == rb_id_option_frozen_string_literal) {
262 if (!NIL_P(value)) pm_options_frozen_string_literal_set(options, RTEST(value));
263 } else if (key_id == rb_id_option_version) {
264 if (!NIL_P(value)) {
265 const char *version = check_string(value);
266
267 if (RSTRING_LEN(value) == 7 && strncmp(version, "current", 7) == 0) {
268 if (!pm_options_version_set(options, ruby_version, 3)) {
269 rb_exc_raise(rb_exc_new_cstr(rb_cPrismCurrentVersionError, ruby_version));
270 }
271 } else if (RSTRING_LEN(value) == 7 && strncmp(version, "nearest", 7) == 0) {
272 options_version_set_nearest(options);
273 } else if (!pm_options_version_set(options, version, RSTRING_LEN(value))) {
274 rb_raise(rb_eArgError, "invalid version: %" PRIsVALUE, value);
275 }
276 }
277 } else if (key_id == rb_id_option_scopes) {
278 if (!NIL_P(value)) build_options_scopes(options, value);
279 } else if (key_id == rb_id_option_command_line) {
280 if (!NIL_P(value)) {
281 const char *flags = check_string(value);
282 long length = RSTRING_LEN(value);
283 uint8_t command_line = 0;
284
285 for (long index = 0; index < length; index++) {
286 switch (flags[index]) {
287 case 'a': command_line |= PM_OPTIONS_COMMAND_LINE_A; break;
288 case 'e': command_line |= PM_OPTIONS_COMMAND_LINE_E; break;
289 case 'l': command_line |= PM_OPTIONS_COMMAND_LINE_L; break;
290 case 'n': command_line |= PM_OPTIONS_COMMAND_LINE_N; break;
291 case 'p': command_line |= PM_OPTIONS_COMMAND_LINE_P; break;
292 case 'x': command_line |= PM_OPTIONS_COMMAND_LINE_X; break;
293 default: rb_raise(rb_eArgError, "invalid command line flag: '%c'", flags[index]); break;
294 }
295 }
296
297 pm_options_command_line_set(options, command_line);
298 }
299 } else if (key_id == rb_id_option_main_script) {
300 if (!NIL_P(value)) pm_options_main_script_set(options, RTEST(value));
301 } else if (key_id == rb_id_option_partial_script) {
302 if (!NIL_P(value)) pm_options_partial_script_set(options, RTEST(value));
303 } else if (key_id == rb_id_option_freeze) {
304 if (!NIL_P(value)) pm_options_freeze_set(options, RTEST(value));
305 } else if (key_id == rb_id_option_attach_comments) {
306 if (!NIL_P(value)) pm_options_attach_comments_set(options, RTEST(value));
307 } else if (key_id == rb_id_option_raise_error) {
308 if (!NIL_P(value)) {
309 if (value == Qtrue) {
310 /* When given true, color when $stderr is a terminal (unless
311 * NO_COLOR is set), bold styling when NO_COLOR is set, plain
312 * otherwise.
313 *
314 * rb_stderr_tty_p is not available to extensions, so instead
315 * we call the tty? method on $stderr, guarding against it
316 * having been replaced by an object that does not respond to
317 * tty?. */
319 const char *no_color = getenv("NO_COLOR");
320 if (no_color == NULL || no_color[0] == '\0') {
321 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_COLOR);
322 } else {
323 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_STYLE);
324 }
325 } else {
326 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_PLAIN);
327 }
328 } else {
329 if (!SYMBOL_P(value)) {
330 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Symbol)", rb_obj_class(value));
331 }
332
333 ID value_id = SYM2ID(value);
334 if (value_id == rb_id_raise_error_plain) {
335 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_PLAIN);
336 } else if (value_id == rb_id_raise_error_style) {
337 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_STYLE);
338 } else if (value_id == rb_id_raise_error_color) {
339 pm_options_raise_error_set(options, (uint8_t) PM_ERRORS_FORMAT_COLOR);
340 } else {
341 rb_raise(rb_eArgError, "invalid raise_error value: %" PRIsVALUE, value);
342 }
343 }
344 }
345 } else {
346 rb_raise(rb_eArgError, "unknown keyword: %" PRIsVALUE, key);
347 }
348
349 return ST_CONTINUE;
350}
351
355static VALUE
356build_options(VALUE argument) {
357 struct build_options_data *data = (struct build_options_data *) argument;
358 if (!NIL_P(data->keywords)) rb_hash_foreach(data->keywords, build_options_i, argument);
359 if (NIL_P(data->filepath)) return Qnil;
360
361 /*
362 * Option conversions can change the string. Take its bytes once they have
363 * finished, and keep them stable across any callbacks during parsing.
364 */
365 VALUE filepath = rb_str_new_frozen(data->filepath);
366 pm_options_filepath_set(data->options, RSTRING_PTR(filepath), RSTRING_LEN(filepath));
367 return filepath;
368}
369
373static VALUE
374extract_options(pm_options_t *options, VALUE filepath, VALUE keywords) {
375 pm_options_line_set(options, 1); /* default */
376 options_version_set_nearest(options); /* default */
377
378 if (NIL_P(keywords) && NIL_P(filepath)) return Qnil;
379
380 struct build_options_data data = { .options = options, .keywords = keywords, .filepath = filepath };
381 int state = 0;
382
383 VALUE snapshot = rb_protect(build_options, (VALUE) &data, &state);
384 if (state != 0) {
385 pm_options_free(options);
386 rb_jump_tag(state);
387 }
388
389 return snapshot;
390}
391
395static VALUE
396string_options(int argc, VALUE *argv, pm_options_t *options, VALUE *filepath) {
397 VALUE string;
398 VALUE keywords;
399 rb_scan_args(argc, argv, "1:", &string, &keywords);
400
401 if (!RB_TYPE_P(string, T_STRING)) {
402 pm_options_free(options);
403 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected String)", rb_obj_class(string));
404 }
405
406 *filepath = extract_options(options, Qnil, keywords);
407 return string;
408}
409
413static pm_source_t *
414file_options(int argc, VALUE *argv, pm_options_t *options, VALUE *encoded_filepath) {
415 VALUE filepath;
416 VALUE keywords;
417 rb_scan_args(argc, argv, "1:", &filepath, &keywords);
418
419 int state = 0;
420 filepath = rb_protect(rb_get_path, filepath, &state);
421 if (state != 0) {
422 pm_options_free(options);
423 rb_jump_tag(state);
424 }
425
426 *encoded_filepath = extract_options(options, rb_str_encode_ospath(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 }
510 error = rb_exc_new_str(rb_eEncCompatError, message);
511 break;
512 }
513
514 return result_err(error);
515}
516
517#ifndef PRISM_EXCLUDE_SERIALIZATION
518
519/******************************************************************************/
520/* Serializing the AST */
521/******************************************************************************/
522
526static result_t
527dump_input(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
528 pm_arena_t *arena = pm_arena_new();
529 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
530 pm_node_t *node = pm_parse(parser);
531
532 result_t result = check_raise_error_option(parser, options, path_encoding);
533 if (result.type == RESULT_OK) {
534 pm_buffer_t *buffer = pm_buffer_new();
535 if (buffer) {
536 pm_serialize(parser, node, buffer);
537 result = result_ok(rb_str_new(pm_buffer_value(buffer), pm_buffer_length(buffer)));
538 pm_buffer_free(buffer);
539
540 if (pm_options_freeze(options)) rb_obj_freeze(result.value);
541 } else {
542 result = result_err(rb_exc_new_cstr(rb_eNoMemError, "failed to allocate memory"));
543 }
544 }
545
546 pm_parser_free(parser);
547 pm_arena_free(arena);
548
549 return result;
550}
551
560static VALUE
561dump(int argc, VALUE *argv, VALUE self) {
562 pm_options_t *options = pm_options_new();
563 VALUE filepath;
564 VALUE string = string_options(argc, argv, options, &filepath);
565
566 const uint8_t *source = (const uint8_t *) RSTRING_PTR(string);
567 size_t length = RSTRING_LEN(string);
568
569#ifdef PRISM_BUILD_DEBUG
570 char* dup = xmalloc(length);
571 memcpy(dup, source, length);
572 source = (const uint8_t *) dup;
573#endif
574
575 result_t result = dump_input(source, length, options, NULL);
576
577#ifdef PRISM_BUILD_DEBUG
578#ifdef xfree_sized
579 xfree_sized(dup, length);
580#else
581 xfree(dup);
582#endif
583#endif
584
585 pm_options_free(options);
586 RB_GC_GUARD(filepath);
587 return result_get(result);
588}
589
598static VALUE
599dump_file(int argc, VALUE *argv, VALUE self) {
600 pm_options_t *options = pm_options_new();
601
602 VALUE encoded_filepath;
603 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
604
605 result_t result = dump_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
606 pm_source_free(src);
607 pm_options_free(options);
608 RB_GC_GUARD(encoded_filepath);
609
610 return result_get(result);
611}
612
613#endif
614
615/******************************************************************************/
616/* Extracting values for the parse result */
617/******************************************************************************/
618
623static inline VALUE
624rb_class_new_instance_freeze(int argc, const VALUE *argv, VALUE klass, bool freeze) {
625 VALUE value = rb_class_new_instance(argc, argv, klass);
626 if (freeze) rb_obj_freeze(value);
627 return value;
628}
629
633static inline VALUE
634parser_location(VALUE source, bool freeze, uint32_t start, uint32_t length) {
635 VALUE argv[] = { source, LONG2FIX(start), LONG2FIX(length) };
636 return rb_class_new_instance_freeze(3, argv, rb_cPrismLocation, freeze);
637}
638
642#define PARSER_LOCATION(source, freeze, location) \
643 parser_location(source, freeze, location.start, location.length)
644
648static inline VALUE
649parser_comment(VALUE source, bool freeze, const pm_comment_t *comment) {
650 VALUE argv[] = { PARSER_LOCATION(source, freeze, pm_comment_location(comment)) };
651 VALUE type = (pm_comment_type(comment) == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment;
652 return rb_class_new_instance_freeze(1, argv, type, freeze);
653}
654
661static void
662parser_comments(const pm_parser_t *parser, VALUE comments, VALUE source, bool freeze) {
663 long index = 0;
664 for (const pm_comment_t *comment = pm_parser_comments_first(parser); comment != NULL; comment = pm_comment_next(comment)) {
665 rb_ary_store(comments, index++, parser_comment(source, freeze, comment));
666 }
667
668 if (freeze) rb_obj_freeze(comments);
669}
670
674static inline VALUE
675parser_magic_comment(VALUE source, bool freeze, const pm_magic_comment_t *magic_comment) {
678
679 VALUE key_loc = parser_location(source, freeze, key.start, key.length);
680 VALUE value_loc = parser_location(source, freeze, value.start, value.length);
681
682 VALUE argv[] = { key_loc, value_loc };
683 return rb_class_new_instance_freeze(2, argv, rb_cPrismMagicComment, freeze);
684}
685
689static VALUE
690parser_magic_comments(const pm_parser_t *parser, VALUE source, bool freeze) {
691 VALUE magic_comments = rb_ary_new_capa(pm_parser_magic_comments_size(parser));
692
693 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)) {
694 rb_ary_push(magic_comments, parser_magic_comment(source, freeze, magic_comment));
695 }
696
697 if (freeze) rb_obj_freeze(magic_comments);
698 return magic_comments;
699}
700
705static VALUE
706parser_data_loc(const pm_parser_t *parser, VALUE source, bool freeze) {
707 const pm_location_t *data_loc = pm_parser_data_loc(parser);
708
709 if (data_loc->length == 0) {
710 return Qnil;
711 } else {
712 return parser_location(source, freeze, data_loc->start, data_loc->length);
713 }
714}
715
719static VALUE
720parser_error(const pm_diagnostic_t *diagnostic, rb_encoding *encoding, VALUE source, bool freeze) {
721 VALUE type = ID2SYM(rb_intern(pm_diagnostic_type(diagnostic)));
722 VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), encoding));
723 VALUE location = PARSER_LOCATION(source, freeze, pm_diagnostic_location(diagnostic));
724
725 pm_error_level_t error_level = pm_diagnostic_error_level(diagnostic);
726 VALUE level = Qnil;
727
728 switch (error_level) {
730 level = ID2SYM(rb_intern("syntax"));
731 break;
733 level = ID2SYM(rb_intern("argument"));
734 break;
736 level = ID2SYM(rb_intern("load"));
737 break;
739 level = ID2SYM(rb_intern("stack"));
740 break;
742 level = ID2SYM(rb_intern("encoding_compatibility"));
743 break;
744 default:
745 rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, error_level);
746 }
747
748 VALUE argv[] = { type, message, location, level };
749 return rb_class_new_instance_freeze(4, argv, rb_cPrismParseError, freeze);
750}
751
755static VALUE
756parser_errors(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) {
757 VALUE errors = rb_ary_new_capa(pm_parser_errors_size(parser));
758
759 for (const pm_diagnostic_t *error = pm_parser_errors_first(parser); error != NULL; error = pm_diagnostic_next(error)) {
760 rb_ary_push(errors, parser_error(error, encoding, source, freeze));
761 }
762
763 if (freeze) rb_obj_freeze(errors);
764 return errors;
765}
766
770static VALUE
771parser_warning(const pm_diagnostic_t *diagnostic, rb_encoding *encoding, VALUE source, bool freeze) {
772 VALUE type = ID2SYM(rb_intern(pm_diagnostic_type(diagnostic)));
773 VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), encoding));
774 VALUE location = PARSER_LOCATION(source, freeze, pm_diagnostic_location(diagnostic));
775
776 pm_warning_level_t warning_level = pm_diagnostic_warning_level(diagnostic);
777 VALUE level = Qnil;
778
779 switch (warning_level) {
781 level = ID2SYM(rb_intern("default"));
782 break;
784 level = ID2SYM(rb_intern("verbose"));
785 break;
786 default:
787 rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, warning_level);
788 }
789
790 VALUE argv[] = { type, message, location, level };
791 return rb_class_new_instance_freeze(4, argv, rb_cPrismParseWarning, freeze);
792}
793
797static VALUE
798parser_warnings(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) {
799 VALUE warnings = rb_ary_new_capa(pm_parser_warnings_size(parser));
800
801 for (const pm_diagnostic_t *warning = pm_parser_warnings_first(parser); warning != NULL; warning = pm_diagnostic_next(warning)) {
802 rb_ary_push(warnings, parser_warning(warning, encoding, source, freeze));
803 }
804
805 if (freeze) rb_obj_freeze(warnings);
806 return warnings;
807}
808
814static void
815parser_attachments_new(const pm_parser_t *parser, VALUE *attachments, VALUE *ranges) {
816 uint32_t ranges_size = pm_parser_comment_ranges_size(parser);
817 *attachments = rb_ary_new_capa(ranges_size == 0 ? 0 : (long) pm_parser_comments_size(parser));
818 *ranges = rb_ary_new_capa(ranges_size == 0 ? 0 : (long) ranges_size + 1);
819}
820
828static void
829parser_attachments(const pm_parser_t *parser, VALUE comments, VALUE attachments, VALUE ranges, bool freeze) {
830 uint32_t ranges_size = pm_parser_comment_ranges_size(parser);
831
832 if (ranges_size > 0) {
833 long size = RARRAY_LEN(comments);
834
835 for (long index = 0; index < size; index++) {
836 rb_ary_store(attachments, index, RARRAY_AREF(comments, pm_parser_comment_attachment(parser, (uint32_t) index)));
837 }
838
839 for (uint32_t node_id = 0; node_id < ranges_size; node_id++) {
840 pm_comment_range_t range = pm_parser_comment_range(parser, node_id);
841
842 if (range.length > 0) {
843 rb_ary_store(ranges, (long) node_id, LONG2FIX(range.index));
844 rb_ary_store(ranges, (long) node_id + 1, LONG2FIX(range.index + range.length));
845 }
846 }
847 }
848
849 if (freeze) {
850 rb_obj_freeze(attachments);
851 rb_obj_freeze(ranges);
852 }
853}
854
858static VALUE
859parse_result_create(VALUE class, const pm_parser_t *parser, VALUE value, rb_encoding *encoding, VALUE source, bool freeze) {
860 VALUE result_argv[] = {
861 value,
862 parser_magic_comments(parser, source, freeze),
863 parser_data_loc(parser, source, freeze),
864 parser_errors(parser, encoding, source, freeze),
865 parser_warnings(parser, encoding, source, freeze),
866 pm_parser_continuable(parser) ? Qtrue : Qfalse,
867 source
868 };
869
870 return rb_class_new_instance_freeze(7, result_argv, class, freeze);
871}
872
873/******************************************************************************/
874/* Lexing Ruby code */
875/******************************************************************************/
876
882typedef struct {
883 VALUE source;
884 VALUE tokens;
885 rb_encoding *encoding;
886 bool freeze;
888
894static void
895parse_lex_token(pm_parser_t *parser, pm_token_t *token, void *data) {
896 parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) data;
897
898 VALUE value = pm_token_new(parser, token, parse_lex_data->encoding, parse_lex_data->source, parse_lex_data->freeze);
899 rb_ary_push(parse_lex_data->tokens, value);
900}
901
907static void
908parse_lex_encoding_changed_callback(pm_parser_t *parser) {
909 parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) pm_parser_lex_callback_data(parser);
910 parse_lex_data->encoding = rb_enc_find(pm_parser_encoding_name(parser));
911
912 // Since the encoding changed, we need to go back and change the encoding of
913 // the tokens that were already lexed. This is only going to end up being
914 // one or two tokens, since the encoding can only change at the top of the
915 // file.
916 VALUE tokens = parse_lex_data->tokens;
917 VALUE next_tokens = rb_ary_new();
918
919 for (long index = 0; index < RARRAY_LEN(tokens); index++) {
920 VALUE token = rb_ary_entry(tokens, index);
921 VALUE value = rb_ivar_get(token, rb_intern("@value"));
922 VALUE next_value = rb_str_dup(value);
923
924 rb_enc_associate(next_value, parse_lex_data->encoding);
925 if (parse_lex_data->freeze) rb_obj_freeze(next_value);
926
927 VALUE next_token_argv[] = {
928 parse_lex_data->source,
929 rb_ivar_get(token, rb_intern("@type")),
930 next_value,
931 rb_ivar_get(token, rb_intern("@location")),
932 rb_ivar_get(token, rb_intern("@state")),
933 };
934
935 VALUE next_token = rb_class_new_instance(5, next_token_argv, rb_cPrismToken);
936
937 if (parse_lex_data->freeze) {
938 rb_obj_freeze(next_token);
939 }
940
941 rb_ary_push(next_tokens, next_token);
942 }
943
944 rb_ary_replace(parse_lex_data->tokens, next_tokens);
945}
946
951static result_t
952parse_lex_input(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding, bool return_nodes) {
953 pm_arena_t *arena = pm_arena_new();
954 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
955 pm_parser_encoding_changed_callback_set(parser, parse_lex_encoding_changed_callback);
956
957 VALUE source_string = rb_str_new((const char *) input, input_length);
958 VALUE offsets = rb_ary_new_capa(pm_parser_line_offsets(parser)->size);
960 VALUE attachments = rb_ary_new();
961 VALUE ranges = rb_ary_new();
962 VALUE source = rb_funcall(rb_cPrismSource, rb_id_source_for, 6, source_string, LONG2NUM(pm_parser_start_line(parser)), offsets, comments, attachments, ranges);
963
964 parse_lex_data_t parse_lex_data = {
965 .source = source,
966 .tokens = rb_ary_new(),
967 .encoding = rb_enc_find(pm_parser_encoding_name(parser)),
968 .freeze = pm_options_freeze(options),
969 };
970
971 parse_lex_data_t *data = &parse_lex_data;
972 pm_parser_lex_callback_set(parser, parse_lex_token, data);
973
974 pm_node_t *node = pm_parse(parser);
975
976 result_t result = check_raise_error_option(parser, options, path_encoding);
977 if (result.type == RESULT_OK) {
978 /* Update the Source object with the correct encoding, line offsets,
979 * and source hash, which are only available after pm_parse()
980 * completes. */
981 rb_encoding *encoding = rb_enc_find(pm_parser_encoding_name(parser));
982 rb_enc_associate(source_string, encoding);
983 rb_funcall(source, rb_intern("replace_encoding"), 1, rb_enc_from_encoding(encoding));
984
985 const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(parser);
986 for (size_t index = 0; index < line_offsets->size; index++) {
987 rb_ary_store(offsets, (long) index, ULONG2NUM(line_offsets->offsets[index]));
988 }
989
990 rb_funcall(source, rb_intern("replace_checksum"), 1, ULL2NUM(pm_parser_checksum(parser)));
991
992 if (pm_options_freeze(options)) {
993 rb_obj_freeze(source_string);
994 rb_obj_freeze(offsets);
995 rb_obj_freeze(source);
996 rb_obj_freeze(parse_lex_data.tokens);
997 }
998
999 /* The comment lists were created along with the source before parsing,
1000 * so they are grown to their final sizes now that those are known. */
1001 uint32_t ranges_size = pm_parser_comment_ranges_size(parser);
1002 rb_ary_resize(comments, (long) pm_parser_comments_size(parser));
1003
1004 if (ranges_size > 0) {
1005 rb_ary_resize(attachments, (long) pm_parser_comments_size(parser));
1006 rb_ary_resize(ranges, (long) ranges_size + 1);
1007 }
1008
1009 parser_comments(parser, comments, source, pm_options_freeze(options));
1010 parser_attachments(parser, comments, attachments, ranges, pm_options_freeze(options));
1011
1012 if (return_nodes) {
1013 VALUE value = rb_ary_new_capa(2);
1014 rb_ary_push(value, pm_ast_new(parser, node, parse_lex_data.encoding, source, pm_options_freeze(options)));
1015 rb_ary_push(value, parse_lex_data.tokens);
1016 if (pm_options_freeze(options)) rb_obj_freeze(value);
1017 result = result_ok(parse_result_create(rb_cPrismParseLexResult, parser, value, parse_lex_data.encoding, source, pm_options_freeze(options)));
1018 } else {
1019 result = result_ok(parse_result_create(rb_cPrismLexResult, parser, parse_lex_data.tokens, parse_lex_data.encoding, source, pm_options_freeze(options)));
1020 }
1021 }
1022
1023 pm_parser_free(parser);
1024 pm_arena_free(arena);
1025
1026 return result;
1027}
1028
1037static VALUE
1038lex(int argc, VALUE *argv, VALUE self) {
1039 pm_options_t *options = pm_options_new();
1040 VALUE filepath;
1041 VALUE string = string_options(argc, argv, options, &filepath);
1042
1043 result_t result = parse_lex_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL, false);
1044 pm_options_free(options);
1045 RB_GC_GUARD(filepath);
1046
1047 return result_get(result);
1048}
1049
1058static VALUE
1059lex_file(int argc, VALUE *argv, VALUE self) {
1060 pm_options_t *options = pm_options_new();
1061
1062 VALUE encoded_filepath;
1063 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1064
1065 result_t result = parse_lex_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath), false);
1066 pm_source_free(src);
1067 pm_options_free(options);
1068 RB_GC_GUARD(encoded_filepath);
1069
1070 return result_get(result);
1071}
1072
1073/******************************************************************************/
1074/* Parsing Ruby code */
1075/******************************************************************************/
1076
1080static result_t
1081parse_input(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
1082 pm_arena_t *arena = pm_arena_new();
1083 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
1084
1085 pm_node_t *node = pm_parse(parser);
1086
1087 result_t result = check_raise_error_option(parser, options, path_encoding);
1088 if (result.type == RESULT_OK) {
1089 rb_encoding *encoding = rb_enc_find(pm_parser_encoding_name(parser));
1090
1091 bool freeze = pm_options_freeze(options);
1092 VALUE comments = rb_ary_new_capa(pm_parser_comments_size(parser));
1093 VALUE attachments, ranges;
1094 parser_attachments_new(parser, &attachments, &ranges);
1095
1096 VALUE source = pm_source_new(parser, encoding, comments, attachments, ranges, freeze);
1097 parser_comments(parser, comments, source, freeze);
1098 parser_attachments(parser, comments, attachments, ranges, freeze);
1099
1100 VALUE value = pm_ast_new(parser, node, encoding, source, freeze);
1101 result = result_ok(parse_result_create(rb_cPrismParseResult, parser, value, encoding, source, freeze));
1102 }
1103
1104 pm_parser_free(parser);
1105 pm_arena_free(arena);
1106
1107 return result;
1108}
1109
1174static VALUE
1175parse(int argc, VALUE *argv, VALUE self) {
1176 pm_options_t *options = pm_options_new();
1177 VALUE filepath;
1178 VALUE string = string_options(argc, argv, options, &filepath);
1179
1180 const uint8_t *source = (const uint8_t *) RSTRING_PTR(string);
1181 size_t length = RSTRING_LEN(string);
1182
1183#ifdef PRISM_BUILD_DEBUG
1184 char* dup = xmalloc(length);
1185 memcpy(dup, source, length);
1186 source = (const uint8_t *) dup;
1187#endif
1188
1189 result_t result = parse_input(source, length, options, NULL);
1190
1191#ifdef PRISM_BUILD_DEBUG
1192#ifdef xfree_sized
1193 xfree_sized(dup, length);
1194#else
1195 xfree(dup);
1196#endif
1197#endif
1198
1199 pm_options_free(options);
1200 RB_GC_GUARD(filepath);
1201 return result_get(result);
1202}
1203
1212static VALUE
1213parse_file(int argc, VALUE *argv, VALUE self) {
1214 pm_options_t *options = pm_options_new();
1215
1216 VALUE encoded_filepath;
1217 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1218
1219 result_t result = parse_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
1220 pm_source_free(src);
1221 pm_options_free(options);
1222 RB_GC_GUARD(encoded_filepath);
1223
1224 return result_get(result);
1225}
1226
1230static result_t
1231profile_input(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
1232 pm_arena_t *arena = pm_arena_new();
1233 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
1234
1235 pm_parse(parser);
1236
1237 result_t result = check_raise_error_option(parser, options, path_encoding);
1238 pm_parser_free(parser);
1239 pm_arena_free(arena);
1240
1241 return result;
1242}
1243
1253static VALUE
1254profile(int argc, VALUE *argv, VALUE self) {
1255 pm_options_t *options = pm_options_new();
1256 VALUE filepath;
1257 VALUE string = string_options(argc, argv, options, &filepath);
1258
1259 result_t result = profile_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
1260 pm_options_free(options);
1261 RB_GC_GUARD(filepath);
1262 result_get(result);
1263
1264 return Qnil;
1265}
1266
1276static VALUE
1277profile_file(int argc, VALUE *argv, VALUE self) {
1278 pm_options_t *options = pm_options_new();
1279
1280 VALUE encoded_filepath;
1281 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1282
1283 result_t result = profile_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
1284 pm_source_free(src);
1285 pm_options_free(options);
1286 RB_GC_GUARD(encoded_filepath);
1287 result_get(result);
1288
1289 return Qnil;
1290}
1291
1292static int
1293parse_stream_eof(void *stream) {
1294 if (rb_funcall((VALUE) stream, rb_intern("eof?"), 0)) {
1295 return 1;
1296 }
1297 return 0;
1298}
1299
1312#define MAX_ENC_LEN 6
1313
1319static char *
1320parse_stream_fgets(char *string, int size, void *stream) {
1321 RUBY_ASSERT(size > MAX_ENC_LEN);
1322
1323 /*
1324 * Request fewer bytes than the buffer can hold. `gets` may return more
1325 * bytes than requested when the limit falls in the middle of a multi-byte
1326 * character, and the reserved headroom guarantees the result still fits.
1327 */
1328 VALUE line = rb_funcall((VALUE) stream, rb_intern("gets"), 1, INT2FIX(size - MAX_ENC_LEN));
1329
1330 /*
1331 * A well-behaved stream returns a String, or nil at EOF. Coerce anything
1332 * else to nil so that we never treat a non-String as a byte buffer.
1333 */
1334 line = rb_check_string_type(line);
1335 if (NIL_P(line)) {
1336 return NULL;
1337 }
1338
1339 const char *cstr = RSTRING_PTR(line);
1340 size_t length = (size_t) RSTRING_LEN(line);
1341
1342 /*
1343 * Defensively clamp the copy. A misbehaving `gets` may ignore the limit
1344 * entirely and return an arbitrarily long string; we must never write past
1345 * the caller's buffer. One byte is reserved for the NUL terminator.
1346 */
1347 if (length > (size_t) (size - 1)) {
1348 length = (size_t) (size - 1);
1349 }
1350
1351 memcpy(string, cstr, length);
1352 string[length] = '\0';
1353
1354 return string;
1355}
1356
1357#undef MAX_ENC_LEN
1358
1367static VALUE
1368parse_stream(int argc, VALUE *argv, VALUE self) {
1369 VALUE stream;
1370 VALUE keywords;
1371 rb_scan_args(argc, argv, "1:", &stream, &keywords);
1372
1373 pm_options_t *options = pm_options_new();
1374 VALUE filepath = extract_options(options, Qnil, keywords);
1375
1376 pm_source_t *src = pm_source_stream_new((void *) stream, parse_stream_fgets, parse_stream_eof);
1377 pm_arena_t *arena = pm_arena_new();
1378 pm_parser_t *parser;
1379
1380 pm_node_t *node = pm_parse_stream(&parser, arena, src, options);
1381
1382 result_t result = check_raise_error_option(parser, options, NULL);
1383 if (result.type == RESULT_OK) {
1384 rb_encoding *encoding = rb_enc_find(pm_parser_encoding_name(parser));
1385
1386 VALUE comments = rb_ary_new_capa(pm_parser_comments_size(parser));
1387 VALUE attachments, ranges;
1388 parser_attachments_new(parser, &attachments, &ranges);
1389
1390 VALUE source = pm_source_new(parser, encoding, comments, attachments, ranges, pm_options_freeze(options));
1391 parser_comments(parser, comments, source, pm_options_freeze(options));
1392 parser_attachments(parser, comments, attachments, ranges, pm_options_freeze(options));
1393
1394 VALUE value = pm_ast_new(parser, node, encoding, source, pm_options_freeze(options));
1395
1396 result = result_ok(parse_result_create(rb_cPrismParseResult, parser, value, encoding, source, pm_options_freeze(options)));
1397 }
1398
1399 pm_source_free(src);
1400 pm_parser_free(parser);
1401 pm_arena_free(arena);
1402 pm_options_free(options);
1403 RB_GC_GUARD(filepath);
1404
1405 return result_get(result);
1406}
1407
1411static result_t
1412parse_input_comments(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
1413 pm_arena_t *arena = pm_arena_new();
1414 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
1415
1416 pm_parse(parser);
1417
1418 result_t result = check_raise_error_option(parser, options, path_encoding);
1419 if (result.type == RESULT_OK) {
1420 rb_encoding *encoding = rb_enc_find(pm_parser_encoding_name(parser));
1421
1422 VALUE comments = rb_ary_new_capa(pm_parser_comments_size(parser));
1423 VALUE attachments, ranges;
1424 parser_attachments_new(parser, &attachments, &ranges);
1425
1426 VALUE source = pm_source_new(parser, encoding, comments, attachments, ranges, pm_options_freeze(options));
1427 parser_comments(parser, comments, source, pm_options_freeze(options));
1428 parser_attachments(parser, comments, attachments, ranges, pm_options_freeze(options));
1429
1430 result = result_ok(comments);
1431 }
1432
1433 pm_parser_free(parser);
1434 pm_arena_free(arena);
1435
1436 return result;
1437}
1438
1447static VALUE
1448parse_comments(int argc, VALUE *argv, VALUE self) {
1449 pm_options_t *options = pm_options_new();
1450 VALUE filepath;
1451 VALUE string = string_options(argc, argv, options, &filepath);
1452
1453 result_t result = parse_input_comments((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
1454 pm_options_free(options);
1455 RB_GC_GUARD(filepath);
1456
1457 return result_get(result);
1458}
1459
1468static VALUE
1469parse_file_comments(int argc, VALUE *argv, VALUE self) {
1470 pm_options_t *options = pm_options_new();
1471
1472 VALUE encoded_filepath;
1473 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1474
1475 result_t result = parse_input_comments(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
1476 pm_source_free(src);
1477 pm_options_free(options);
1478 RB_GC_GUARD(encoded_filepath);
1479
1480 return result_get(result);
1481}
1482
1498static VALUE
1499parse_lex(int argc, VALUE *argv, VALUE self) {
1500 pm_options_t *options = pm_options_new();
1501 VALUE filepath;
1502 VALUE string = string_options(argc, argv, options, &filepath);
1503
1504 result_t result = parse_lex_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL, true);
1505 pm_options_free(options);
1506 RB_GC_GUARD(filepath);
1507
1508 return result_get(result);
1509}
1510
1526static VALUE
1527parse_lex_file(int argc, VALUE *argv, VALUE self) {
1528 pm_options_t *options = pm_options_new();
1529
1530 VALUE encoded_filepath;
1531 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1532
1533 result_t result = parse_lex_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath), true);
1534 pm_source_free(src);
1535 pm_options_free(options);
1536 RB_GC_GUARD(encoded_filepath);
1537
1538 return result_get(result);
1539}
1540
1544static result_t
1545parse_input_success_p(const uint8_t *input, size_t input_length, const pm_options_t *options, rb_encoding *path_encoding) {
1546 pm_arena_t *arena = pm_arena_new();
1547 pm_parser_t *parser = pm_parser_new(arena, input, input_length, options);
1548 pm_parse(parser);
1549
1550 result_t result = check_raise_error_option(parser, options, path_encoding);
1551 if (result.type == RESULT_OK) {
1552 result = result_ok(pm_parser_errors_size(parser) == 0 ? Qtrue : Qfalse);
1553 }
1554
1555 pm_parser_free(parser);
1556 pm_arena_free(arena);
1557
1558 return result;
1559}
1560
1569static VALUE
1570parse_success_p(int argc, VALUE *argv, VALUE self) {
1571 pm_options_t *options = pm_options_new();
1572 VALUE filepath;
1573 VALUE string = string_options(argc, argv, options, &filepath);
1574
1575 result_t result = parse_input_success_p((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
1576 pm_options_free(options);
1577 RB_GC_GUARD(filepath);
1578
1579 return result_get(result);
1580}
1581
1590static VALUE
1591parse_failure_p(int argc, VALUE *argv, VALUE self) {
1592 return RTEST(parse_success_p(argc, argv, self)) ? Qfalse : Qtrue;
1593}
1594
1603static VALUE
1604parse_file_success_p(int argc, VALUE *argv, VALUE self) {
1605 pm_options_t *options = pm_options_new();
1606
1607 VALUE encoded_filepath;
1608 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
1609
1610 result_t result = parse_input_success_p(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
1611 pm_source_free(src);
1612 pm_options_free(options);
1613 RB_GC_GUARD(encoded_filepath);
1614
1615 return result_get(result);
1616}
1617
1626static VALUE
1627parse_file_failure_p(int argc, VALUE *argv, VALUE self) {
1628 return RTEST(parse_file_success_p(argc, argv, self)) ? Qfalse : Qtrue;
1629}
1630
1631/******************************************************************************/
1632/* String query methods */
1633/******************************************************************************/
1634
1640static const char *
1641string_query_nearest_version(size_t *length) {
1642 if (pm_options_version_supported_p(ruby_version, 3)) {
1643 *length = 3;
1644 return ruby_version;
1645 } else if (ruby_version_before_lowest_p()) {
1646 *length = 3;
1647 return "3.3";
1648 } else {
1649 *length = 0;
1650 return NULL;
1651 }
1652}
1653
1658static const char *
1659string_query_version(VALUE *value, size_t *length) {
1660 if (*value == Qundef || NIL_P(*value)) {
1661 return string_query_nearest_version(length);
1662 }
1663
1664 const char *version = StringValuePtr(*value);
1665 *length = RSTRING_LEN(*value);
1666
1667 if (*length != 7) {
1668 return version;
1669 } else if (memcmp(version, "nearest", 7) == 0) {
1670 return string_query_nearest_version(length);
1671 } else if (memcmp(version, "current", 7) == 0) {
1672 if (!pm_options_version_supported_p(ruby_version, 3)) {
1673 rb_exc_raise(rb_exc_new_cstr(rb_cPrismCurrentVersionError, ruby_version));
1674 }
1675
1676 *length = 3;
1677 return ruby_version;
1678 } else {
1679 return version;
1680 }
1681}
1682
1684typedef struct {
1685 const uint8_t *source; size_t source_length;
1686 const char *encoding; size_t encoding_length;
1687 const char *version; size_t version_length;
1688
1689 /* Keep a coerced Ruby string alive for the query and any error message. */
1690 VALUE version_value;
1692
1693static void
1694string_query_arguments(int argc, VALUE *argv, string_query_arguments_t *args) {
1695 VALUE string, keywords;
1696 rb_scan_args(argc, argv, "1:", &string, &keywords);
1697 check_string(string);
1698
1699 args->version_value = Qundef;
1700 if (!NIL_P(keywords)) {
1701 ID keys[1] = { rb_id_option_version };
1702 rb_get_kwargs(keywords, keys, 0, 1, &args->version_value);
1703 }
1704
1705 args->version = string_query_version(&args->version_value, &args->version_length);
1706
1707 args->encoding = rb_enc_name(rb_enc_get(string));
1708 args->encoding_length = strlen(args->encoding);
1709
1710 args->source = (const uint8_t *) RSTRING_PTR(string);
1711 args->source_length = RSTRING_LEN(string);
1712}
1713
1715static VALUE
1716string_query_result(pm_string_query_t result, VALUE version) {
1717 switch (result) {
1719 rb_raise(rb_eArgError, "invalid version: %" PRIsVALUE, version);
1720 return Qfalse;
1722 rb_raise(rb_eArgError, "Invalid or non ascii-compatible encoding");
1723 return Qfalse;
1725 return Qfalse;
1727 return Qtrue;
1728 }
1729
1730 return Qfalse;
1731}
1732
1746static VALUE
1747string_query_local_p(int argc, VALUE *argv, VALUE self) {
1749 string_query_arguments(argc, argv, &args);
1750
1751 pm_string_query_t result = pm_string_query_local(args.source, args.source_length, args.encoding, args.encoding_length, args.version, args.version_length);
1752 return string_query_result(result, args.version_value);
1753}
1754
1768static VALUE
1769string_query_constant_p(int argc, VALUE *argv, VALUE self) {
1771 string_query_arguments(argc, argv, &args);
1772
1773 pm_string_query_t result = pm_string_query_constant(args.source, args.source_length, args.encoding, args.encoding_length, args.version, args.version_length);
1774 return string_query_result(result, args.version_value);
1775}
1776
1788static VALUE
1789string_query_method_name_p(int argc, VALUE *argv, VALUE self) {
1791 string_query_arguments(argc, argv, &args);
1792
1793 pm_string_query_t result = pm_string_query_method_name(args.source, args.source_length, args.encoding, args.encoding_length, args.version, args.version_length);
1794 return string_query_result(result, args.version_value);
1795}
1796
1809static VALUE
1810string_query_keyword_p(int argc, VALUE *argv, VALUE self) {
1812 string_query_arguments(argc, argv, &args);
1813
1814 pm_string_query_t result = pm_string_query_keyword(args.source, args.source_length, args.encoding, args.encoding_length, args.version, args.version_length);
1815 return string_query_result(result, args.version_value);
1816}
1817
1818/******************************************************************************/
1819/* Initialization of the extension */
1820/******************************************************************************/
1821
1825RUBY_FUNC_EXPORTED void
1826Init_prism(void) {
1827 /* Make sure that the prism library version matches the expected version.
1828 * Otherwise something was compiled incorrectly. */
1829 if (strcmp(pm_version(), EXPECTED_PRISM_VERSION) != 0) {
1830 rb_raise(
1832 "The prism library version (%s) does not match the expected version (%s)",
1833 pm_version(),
1834 EXPECTED_PRISM_VERSION
1835 );
1836 }
1837
1838#ifdef HAVE_RB_EXT_RACTOR_SAFE
1839 /* Mark this extension as Ractor-safe. */
1840 rb_ext_ractor_safe(true);
1841#endif
1842
1843 VALUE mRuby = rb_define_module("Ruby"); rb_cPrism = rb_define_module_under(mRuby, "Prism");
1844 rb_cPrismNode = rb_define_class_under(rb_cPrism, "Node", rb_cObject);
1845 rb_cPrismSource = rb_define_class_under(rb_cPrism, "Source", rb_cObject);
1846 rb_cPrismToken = rb_define_class_under(rb_cPrism, "Token", rb_cObject);
1847 rb_cPrismLocation = rb_define_class_under(rb_cPrism, "Location", rb_cObject);
1848 rb_cPrismComment = rb_define_class_under(rb_cPrism, "Comment", rb_cObject);
1849 rb_cPrismInlineComment = rb_define_class_under(rb_cPrism, "InlineComment", rb_cPrismComment);
1850 rb_cPrismEmbDocComment = rb_define_class_under(rb_cPrism, "EmbDocComment", rb_cPrismComment);
1851 rb_cPrismMagicComment = rb_define_class_under(rb_cPrism, "MagicComment", rb_cObject);
1852 rb_cPrismParseError = rb_define_class_under(rb_cPrism, "ParseError", rb_cObject);
1853 rb_cPrismParseWarning = rb_define_class_under(rb_cPrism, "ParseWarning", rb_cObject);
1854 rb_cPrismResult = rb_define_class_under(rb_cPrism, "Result", rb_cObject);
1855 rb_cPrismParseResult = rb_define_class_under(rb_cPrism, "ParseResult", rb_cPrismResult);
1856 rb_cPrismLexResult = rb_define_class_under(rb_cPrism, "LexResult", rb_cPrismResult);
1857 rb_cPrismParseLexResult = rb_define_class_under(rb_cPrism, "ParseLexResult", rb_cPrismResult);
1858 rb_cPrismStringQuery = rb_define_class_under(rb_cPrism, "StringQuery", rb_cObject);
1859 rb_cPrismScope = rb_define_class_under(rb_cPrism, "Scope", rb_cObject);
1860
1861 rb_cPrismCurrentVersionError = rb_const_get(rb_cPrism, rb_intern("CurrentVersionError"));
1862
1863 /* Intern all of the IDs eagerly that we support so that we do not have to
1864 * do it every time we parse. */
1865 rb_id_option_attach_comments = rb_intern_const("attach_comments");
1866 rb_id_option_command_line = rb_intern_const("command_line");
1867 rb_id_option_encoding = rb_intern_const("encoding");
1868 rb_id_option_filepath = rb_intern_const("filepath");
1869 rb_id_option_freeze = rb_intern_const("freeze");
1870 rb_id_option_frozen_string_literal = rb_intern_const("frozen_string_literal");
1871 rb_id_option_line = rb_intern_const("line");
1872 rb_id_option_main_script = rb_intern_const("main_script");
1873 rb_id_option_partial_script = rb_intern_const("partial_script");
1874 rb_id_option_raise_error = rb_intern_const("raise_error");
1875 rb_id_option_scopes = rb_intern_const("scopes");
1876 rb_id_option_version = rb_intern_const("version");
1877
1878 rb_id_source_for = rb_intern("for");
1879
1880 rb_id_forwarding_positionals = rb_intern("*");
1881 rb_id_forwarding_keywords = rb_intern("**");
1882 rb_id_forwarding_block = rb_intern("&");
1883 rb_id_forwarding_all = rb_intern("...");
1884
1885 rb_id_raise_error_plain = rb_intern_const("plain");
1886 rb_id_raise_error_style = rb_intern_const("style");
1887 rb_id_raise_error_color = rb_intern_const("color");
1888
1892 rb_define_const(rb_cPrism, "VERSION", rb_str_freeze(rb_str_new_cstr(EXPECTED_PRISM_VERSION)));
1893
1894 rb_define_singleton_method(rb_cPrism, "lex", lex, -1);
1895 rb_define_singleton_method(rb_cPrism, "lex_file", lex_file, -1);
1896 rb_define_singleton_method(rb_cPrism, "parse", parse, -1);
1897 rb_define_singleton_method(rb_cPrism, "parse_file", parse_file, -1);
1898 rb_define_singleton_method(rb_cPrism, "profile", profile, -1);
1899 rb_define_singleton_method(rb_cPrism, "profile_file", profile_file, -1);
1900 rb_define_singleton_method(rb_cPrism, "parse_stream", parse_stream, -1);
1901 rb_define_singleton_method(rb_cPrism, "parse_comments", parse_comments, -1);
1902 rb_define_singleton_method(rb_cPrism, "parse_file_comments", parse_file_comments, -1);
1903 rb_define_singleton_method(rb_cPrism, "parse_lex", parse_lex, -1);
1904 rb_define_singleton_method(rb_cPrism, "parse_lex_file", parse_lex_file, -1);
1905 rb_define_singleton_method(rb_cPrism, "parse_success?", parse_success_p, -1);
1906 rb_define_singleton_method(rb_cPrism, "parse_failure?", parse_failure_p, -1);
1907 rb_define_singleton_method(rb_cPrism, "parse_file_success?", parse_file_success_p, -1);
1908 rb_define_singleton_method(rb_cPrism, "parse_file_failure?", parse_file_failure_p, -1);
1909
1910#ifndef PRISM_EXCLUDE_SERIALIZATION
1911 rb_define_singleton_method(rb_cPrism, "dump", dump, -1);
1912 rb_define_singleton_method(rb_cPrism, "dump_file", dump_file, -1);
1913#endif
1914
1915 rb_define_singleton_method(rb_cPrismStringQuery, "local?", string_query_local_p, -1);
1916 rb_define_singleton_method(rb_cPrismStringQuery, "constant?", string_query_constant_p, -1);
1917 rb_define_singleton_method(rb_cPrismStringQuery, "method_name?", string_query_method_name_p, -1);
1918 rb_define_singleton_method(rb_cPrismStringQuery, "keyword?", string_query_keyword_p, -1);
1919
1920 Init_prism_api_node();
1921}
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_arena_t * pm_arena_new(void)
Returns a newly allocated and initialized arena.
Definition arena.c:146
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:205
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:213
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:221
#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:51
@ PM_WARNING_LEVEL_DEFAULT
For warnings which should be emitted if $VERBOSE != nil.
Definition diagnostic.h:53
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
Definition diagnostic.h:56
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_ENCODING_COMPATIBILITY
For errors that should raise an encoding compatibility error.
Definition diagnostic.h:45
@ 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:3394
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:3183
#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:679
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_eEncCompatError
Encoding::CompatibilityError exception.
Definition error.c:1464
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:1638
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
Definition encoding.c:1690
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:1199
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_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1582
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2067
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3441
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:3085
#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:3552
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:2185
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:1665
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3765
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:300
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:292
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:308
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
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:19
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:23196
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:23218
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:23389
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:28
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:51
#define RARRAY_AREF(a, i)
References an object in an array.
Definition rarray.h:398
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition rstring.h:76
#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.
This struct gets stored in the parser and passed in to the lex callback any time a new token is found...
Definition extension.c:882
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
The source, encoding name, and version for a string query.
Definition extension.c:1684
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