1#include "prism/extension.h"
16VALUE rb_cPrismLocation;
18VALUE rb_cPrismComment;
19VALUE rb_cPrismInlineComment;
20VALUE rb_cPrismEmbDocComment;
21VALUE rb_cPrismMagicComment;
22VALUE rb_cPrismParseError;
23VALUE rb_cPrismParseWarning;
25VALUE rb_cPrismParseResult;
26VALUE rb_cPrismLexResult;
27VALUE rb_cPrismParseLexResult;
28VALUE rb_cPrismStringQuery;
30VALUE rb_cPrismCurrentVersionError;
32VALUE rb_cPrismDebugEncoding;
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;
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;
49ID rb_id_forwarding_positionals;
50ID rb_id_forwarding_keywords;
51ID rb_id_forwarding_block;
52ID rb_id_forwarding_all;
54ID rb_id_raise_error_plain;
55ID rb_id_raise_error_style;
56ID rb_id_raise_error_color;
71result_ok(
VALUE value) {
72 return (
result_t) { .type = RESULT_OK, .value = value };
76result_err(
VALUE value) {
77 return (
result_t) { .type = RESULT_ERR, .value = value };
82 if (result.type == RESULT_OK) {
98check_string(
VALUE value) {
105 return RSTRING_PTR(value);
124 if (!pm_options_scopes_init(options, scopes_count)) {
129 for (
size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
151 for (
size_t name_index = 0; name_index < names_count; name_index++) {
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) {
170 rb_raise(rb_eArgError,
"invalid forwarding value: %" PRIsVALUE, name);
180 pm_options_scope_init(options_scope, locals_count);
183 for (
size_t local_index = 0; local_index < locals_count; local_index++) {
193 pm_string_t *scope_local = pm_options_scope_local_mut(options_scope, local_index);
195 pm_string_init(scope_local, (
const uint8_t *) RSTRING_PTR(name), RSTRING_LEN(name));
199 pm_options_scope_forwarding_set(options_scope, forwarding);
208ruby_version_before_lowest_p(
void) {
218 if (!pm_options_version_set(options,
ruby_version, 3)) {
220 if (ruby_version_before_lowest_p()) {
221 pm_options_version_set_lowest(options);
224 pm_options_version_set_highest(options);
244 if (key_id == rb_id_option_filepath) {
248 if (
NIL_P(data->filepath)) data->filepath = value;
250 }
else if (key_id == rb_id_option_encoding) {
253 pm_options_encoding_locked_set(options,
true);
255 const char *name = rb_enc_name(rb_to_encoding(value));
256 pm_options_encoding_set(options, name, strlen(name));
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) {
265 const char *version = check_string(value);
267 if (RSTRING_LEN(value) == 7 && strncmp(version,
"current", 7) == 0) {
268 if (!pm_options_version_set(options,
ruby_version, 3)) {
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);
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) {
281 const char *flags = check_string(value);
282 long length = RSTRING_LEN(value);
283 uint8_t command_line = 0;
285 for (
long index = 0; index < length; index++) {
286 switch (flags[index]) {
293 default: rb_raise(rb_eArgError,
"invalid command line flag: '%c'", flags[index]);
break;
297 pm_options_command_line_set(options, command_line);
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) {
309 if (value ==
Qtrue) {
319 const char *no_color = getenv(
"NO_COLOR");
320 if (no_color == NULL || no_color[0] ==
'\0') {
334 if (value_id == rb_id_raise_error_plain) {
336 }
else if (value_id == rb_id_raise_error_style) {
338 }
else if (value_id == rb_id_raise_error_color) {
341 rb_raise(rb_eArgError,
"invalid raise_error value: %" PRIsVALUE, value);
346 rb_raise(rb_eArgError,
"unknown keyword: %" PRIsVALUE, key);
356build_options(
VALUE argument) {
366 pm_options_filepath_set(data->options, RSTRING_PTR(filepath), RSTRING_LEN(filepath));
375 pm_options_line_set(options, 1);
376 options_version_set_nearest(options);
380 struct build_options_data data = { .options = options, .keywords = keywords, .filepath = filepath };
383 VALUE snapshot = rb_protect(build_options, (
VALUE) &data, &state);
385 pm_options_free(options);
402 pm_options_free(options);
406 *filepath = extract_options(options,
Qnil, keywords);
420 filepath = rb_protect(rb_get_path, filepath, &state);
422 pm_options_free(options);
426 *encoded_filepath = extract_options(options, rb_str_encode_ospath(filepath), keywords);
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);
436 pm_options_free(options);
439 int e = rb_w32_map_errno(GetLastError());
448 pm_options_free(options);
452 pm_options_free(options);
453 rb_raise(
rb_eRuntimeError,
"Unknown error (%d) initializing file: %s", result, source);
467 if (pm_parser_errors_size(parser) == 0)
return result_ok(
Qnil);
469 uint8_t raise_error = pm_options_raise_error(options);
470 if (raise_error == 0)
return result_ok(
Qnil);
473 if (buffer == NULL) {
483 VALUE message = rb_enc_str_new(pm_buffer_value(buffer), (
long) pm_buffer_length(buffer), message_encoding);
484 pm_buffer_free(buffer);
487 switch (error_level) {
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);
514 return result_err(error);
517#ifndef PRISM_EXCLUDE_SERIALIZATION
527dump_input(
const uint8_t *input,
size_t input_length,
const pm_options_t *options, rb_encoding *path_encoding) {
532 result_t result = check_raise_error_option(parser, options, path_encoding);
533 if (result.type == RESULT_OK) {
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);
547 pm_arena_free(arena);
564 VALUE string = string_options(argc, argv, options, &filepath);
566 const uint8_t *source = (
const uint8_t *) RSTRING_PTR(
string);
567 size_t length = RSTRING_LEN(
string);
569#ifdef PRISM_BUILD_DEBUG
571 memcpy(dup, source, length);
572 source = (
const uint8_t *) dup;
575 result_t result = dump_input(source, length, options, NULL);
577#ifdef PRISM_BUILD_DEBUG
579 xfree_sized(dup, length);
585 pm_options_free(options);
587 return result_get(result);
602 VALUE encoded_filepath;
603 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
605 result_t result = dump_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
607 pm_options_free(options);
610 return result_get(result);
624rb_class_new_instance_freeze(
int argc,
const VALUE *argv,
VALUE klass,
bool freeze) {
634parser_location(
VALUE source,
bool freeze, uint32_t start, uint32_t length) {
636 return rb_class_new_instance_freeze(3, argv, rb_cPrismLocation, freeze);
642#define PARSER_LOCATION(source, freeze, location) \
643 parser_location(source, freeze, location.start, location.length)
652 return rb_class_new_instance_freeze(1, argv,
type, freeze);
665 rb_ary_store(comments, index++, parser_comment(source, freeze, comment));
679 VALUE key_loc = parser_location(source, freeze, key.start, key.length);
680 VALUE value_loc = parser_location(source, freeze, value.
start, value.
length);
682 VALUE argv[] = { key_loc, value_loc };
683 return rb_class_new_instance_freeze(2, argv, rb_cPrismMagicComment, freeze);
690parser_magic_comments(
const pm_parser_t *parser,
VALUE source,
bool freeze) {
698 return magic_comments;
709 if (data_loc->
length == 0) {
712 return parser_location(source, freeze, data_loc->
start, data_loc->
length);
720parser_error(
const pm_diagnostic_t *diagnostic, rb_encoding *encoding,
VALUE source,
bool freeze) {
723 VALUE location = PARSER_LOCATION(source, freeze, pm_diagnostic_location(diagnostic));
728 switch (error_level) {
730 level =
ID2SYM(rb_intern(
"syntax"));
733 level =
ID2SYM(rb_intern(
"argument"));
736 level =
ID2SYM(rb_intern(
"load"));
739 level =
ID2SYM(rb_intern(
"stack"));
742 level =
ID2SYM(rb_intern(
"encoding_compatibility"));
748 VALUE argv[] = {
type, message, location, level };
749 return rb_class_new_instance_freeze(4, argv, rb_cPrismParseError, freeze);
756parser_errors(
const pm_parser_t *parser, rb_encoding *encoding,
VALUE source,
bool freeze) {
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));
771parser_warning(
const pm_diagnostic_t *diagnostic, rb_encoding *encoding,
VALUE source,
bool freeze) {
774 VALUE location = PARSER_LOCATION(source, freeze, pm_diagnostic_location(diagnostic));
779 switch (warning_level) {
781 level =
ID2SYM(rb_intern(
"default"));
784 level =
ID2SYM(rb_intern(
"verbose"));
790 VALUE argv[] = {
type, message, location, level };
791 return rb_class_new_instance_freeze(4, argv, rb_cPrismParseWarning, freeze);
798parser_warnings(
const pm_parser_t *parser, rb_encoding *encoding,
VALUE source,
bool freeze) {
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));
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);
830 uint32_t ranges_size = pm_parser_comment_ranges_size(parser);
832 if (ranges_size > 0) {
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)));
839 for (uint32_t node_id = 0; node_id < ranges_size; node_id++) {
842 if (range.length > 0) {
860 VALUE result_argv[] = {
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),
870 return rb_class_new_instance_freeze(7, result_argv,
class, freeze);
885 rb_encoding *encoding;
898 VALUE value = pm_token_new(parser, token, parse_lex_data->encoding, parse_lex_data->source, parse_lex_data->freeze);
908parse_lex_encoding_changed_callback(
pm_parser_t *parser) {
910 parse_lex_data->encoding = rb_enc_find(pm_parser_encoding_name(parser));
916 VALUE tokens = parse_lex_data->tokens;
919 for (
long index = 0; index <
RARRAY_LEN(tokens); index++) {
924 rb_enc_associate(next_value, parse_lex_data->encoding);
927 VALUE next_token_argv[] = {
928 parse_lex_data->source,
937 if (parse_lex_data->freeze) {
952parse_lex_input(
const uint8_t *input,
size_t input_length,
const pm_options_t *options, rb_encoding *path_encoding,
bool return_nodes) {
955 pm_parser_encoding_changed_callback_set(parser, parse_lex_encoding_changed_callback);
962 VALUE source =
rb_funcall(rb_cPrismSource, rb_id_source_for, 6, source_string,
LONG2NUM(pm_parser_start_line(parser)), offsets, comments, attachments, ranges);
967 .encoding = rb_enc_find(pm_parser_encoding_name(parser)),
968 .freeze = pm_options_freeze(options),
972 pm_parser_lex_callback_set(parser, parse_lex_token, data);
976 result_t result = check_raise_error_option(parser, options, path_encoding);
977 if (result.type == RESULT_OK) {
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));
986 for (
size_t index = 0; index < line_offsets->
size; index++) {
990 rb_funcall(source, rb_intern(
"replace_checksum"), 1,
ULL2NUM(pm_parser_checksum(parser)));
992 if (pm_options_freeze(options)) {
1001 uint32_t ranges_size = pm_parser_comment_ranges_size(parser);
1002 rb_ary_resize(comments, (
long) pm_parser_comments_size(parser));
1004 if (ranges_size > 0) {
1005 rb_ary_resize(attachments, (
long) pm_parser_comments_size(parser));
1009 parser_comments(parser, comments, source, pm_options_freeze(options));
1010 parser_attachments(parser, comments, attachments, ranges, pm_options_freeze(options));
1014 rb_ary_push(value, pm_ast_new(parser, node, parse_lex_data.encoding, source, pm_options_freeze(options)));
1017 result = result_ok(parse_result_create(rb_cPrismParseLexResult, parser, value, parse_lex_data.encoding, source, pm_options_freeze(options)));
1019 result = result_ok(parse_result_create(rb_cPrismLexResult, parser, parse_lex_data.tokens, parse_lex_data.encoding, source, pm_options_freeze(options)));
1024 pm_arena_free(arena);
1041 VALUE string = string_options(argc, argv, options, &filepath);
1043 result_t result = parse_lex_input((
const uint8_t *) RSTRING_PTR(
string), RSTRING_LEN(
string), options, NULL,
false);
1044 pm_options_free(options);
1047 return result_get(result);
1062 VALUE encoded_filepath;
1063 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
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);
1070 return result_get(result);
1081parse_input(
const uint8_t *input,
size_t input_length,
const pm_options_t *options, rb_encoding *path_encoding) {
1085 pm_node_t *node =
pm_parse(parser);
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));
1091 bool freeze = pm_options_freeze(options);
1093 VALUE attachments, ranges;
1094 parser_attachments_new(parser, &attachments, &ranges);
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);
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));
1105 pm_arena_free(arena);
1178 VALUE string = string_options(argc, argv, options, &filepath);
1180 const uint8_t *source = (
const uint8_t *) RSTRING_PTR(
string);
1181 size_t length = RSTRING_LEN(
string);
1183#ifdef PRISM_BUILD_DEBUG
1185 memcpy(dup, source, length);
1186 source = (
const uint8_t *) dup;
1189 result_t result = parse_input(source, length, options, NULL);
1191#ifdef PRISM_BUILD_DEBUG
1193 xfree_sized(dup, length);
1199 pm_options_free(options);
1201 return result_get(result);
1213parse_file(
int argc,
VALUE *argv,
VALUE self) {
1216 VALUE encoded_filepath;
1217 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
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);
1224 return result_get(result);
1231profile_input(
const uint8_t *input,
size_t input_length,
const pm_options_t *options, rb_encoding *path_encoding) {
1237 result_t result = check_raise_error_option(parser, options, path_encoding);
1239 pm_arena_free(arena);
1257 VALUE string = string_options(argc, argv, options, &filepath);
1259 result_t result = profile_input((
const uint8_t *) RSTRING_PTR(
string), RSTRING_LEN(
string), options, NULL);
1260 pm_options_free(options);
1277profile_file(
int argc,
VALUE *argv,
VALUE self) {
1280 VALUE encoded_filepath;
1281 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
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);
1293parse_stream_eof(
void *stream) {
1312#define MAX_ENC_LEN 6
1320parse_stream_fgets(
char *
string,
int size,
void *stream) {
1339 const char *cstr = RSTRING_PTR(line);
1340 size_t length = (size_t) RSTRING_LEN(line);
1347 if (length > (
size_t) (size - 1)) {
1348 length = (size_t) (size - 1);
1351 memcpy(
string, cstr, length);
1352 string[length] =
'\0';
1368parse_stream(
int argc,
VALUE *argv,
VALUE self) {
1374 VALUE filepath = extract_options(options,
Qnil, keywords);
1376 pm_source_t *src = pm_source_stream_new((
void *) stream, parse_stream_fgets, parse_stream_eof);
1380 pm_node_t *node = pm_parse_stream(&parser, arena, src, options);
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));
1387 VALUE attachments, ranges;
1388 parser_attachments_new(parser, &attachments, &ranges);
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));
1394 VALUE value = pm_ast_new(parser, node, encoding, source, pm_options_freeze(options));
1396 result = result_ok(parse_result_create(rb_cPrismParseResult, parser, value, encoding, source, pm_options_freeze(options)));
1399 pm_source_free(src);
1401 pm_arena_free(arena);
1402 pm_options_free(options);
1405 return result_get(result);
1412parse_input_comments(
const uint8_t *input,
size_t input_length,
const pm_options_t *options, rb_encoding *path_encoding) {
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));
1423 VALUE attachments, ranges;
1424 parser_attachments_new(parser, &attachments, &ranges);
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));
1430 result = result_ok(comments);
1434 pm_arena_free(arena);
1448parse_comments(
int argc,
VALUE *argv,
VALUE self) {
1451 VALUE string = string_options(argc, argv, options, &filepath);
1453 result_t result = parse_input_comments((
const uint8_t *) RSTRING_PTR(
string), RSTRING_LEN(
string), options, NULL);
1454 pm_options_free(options);
1457 return result_get(result);
1469parse_file_comments(
int argc,
VALUE *argv,
VALUE self) {
1472 VALUE encoded_filepath;
1473 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
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);
1480 return result_get(result);
1499parse_lex(
int argc,
VALUE *argv,
VALUE self) {
1502 VALUE string = string_options(argc, argv, options, &filepath);
1504 result_t result = parse_lex_input((
const uint8_t *) RSTRING_PTR(
string), RSTRING_LEN(
string), options, NULL,
true);
1505 pm_options_free(options);
1508 return result_get(result);
1527parse_lex_file(
int argc,
VALUE *argv,
VALUE self) {
1530 VALUE encoded_filepath;
1531 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
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);
1538 return result_get(result);
1545parse_input_success_p(
const uint8_t *input,
size_t input_length,
const pm_options_t *options, rb_encoding *path_encoding) {
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);
1556 pm_arena_free(arena);
1570parse_success_p(
int argc,
VALUE *argv,
VALUE self) {
1573 VALUE string = string_options(argc, argv, options, &filepath);
1575 result_t result = parse_input_success_p((
const uint8_t *) RSTRING_PTR(
string), RSTRING_LEN(
string), options, NULL);
1576 pm_options_free(options);
1579 return result_get(result);
1591parse_failure_p(
int argc,
VALUE *argv,
VALUE self) {
1604parse_file_success_p(
int argc,
VALUE *argv,
VALUE self) {
1607 VALUE encoded_filepath;
1608 pm_source_t *src = file_options(argc, argv, options, &encoded_filepath);
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);
1615 return result_get(result);
1627parse_file_failure_p(
int argc,
VALUE *argv,
VALUE self) {
1641string_query_nearest_version(
size_t *length) {
1645 }
else if (ruby_version_before_lowest_p()) {
1659string_query_version(
VALUE *value,
size_t *length) {
1661 return string_query_nearest_version(length);
1665 *length = RSTRING_LEN(*value);
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)) {
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;
1690 VALUE version_value;
1695 VALUE string, keywords;
1697 check_string(
string);
1699 args->version_value =
Qundef;
1700 if (!
NIL_P(keywords)) {
1701 ID keys[1] = { rb_id_option_version };
1705 args->version = string_query_version(&args->version_value, &args->version_length);
1707 args->encoding = rb_enc_name(rb_enc_get(
string));
1708 args->encoding_length = strlen(args->encoding);
1710 args->source = (
const uint8_t *) RSTRING_PTR(
string);
1711 args->source_length = RSTRING_LEN(
string);
1719 rb_raise(rb_eArgError,
"invalid version: %" PRIsVALUE, version);
1722 rb_raise(rb_eArgError,
"Invalid or non ascii-compatible encoding");
1747string_query_local_p(
int argc,
VALUE *argv,
VALUE self) {
1749 string_query_arguments(argc, argv, &args);
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);
1769string_query_constant_p(
int argc,
VALUE *argv,
VALUE self) {
1771 string_query_arguments(argc, argv, &args);
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);
1789string_query_method_name_p(
int argc,
VALUE *argv,
VALUE self) {
1791 string_query_arguments(argc, argv, &args);
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);
1810string_query_keyword_p(
int argc,
VALUE *argv,
VALUE self) {
1812 string_query_arguments(argc, argv, &args);
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);
1825RUBY_FUNC_EXPORTED
void
1829 if (strcmp(
pm_version(), EXPECTED_PRISM_VERSION) != 0) {
1832 "The prism library version (%s) does not match the expected version (%s)",
1834 EXPECTED_PRISM_VERSION
1838#ifdef HAVE_RB_EXT_RACTOR_SAFE
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);
1861 rb_cPrismCurrentVersionError =
rb_const_get(rb_cPrism, rb_intern(
"CurrentVersionError"));
1870 rb_id_option_frozen_string_literal =
rb_intern_const(
"frozen_string_literal");
1878 rb_id_source_for = rb_intern(
"for");
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(
"...");
1910#ifndef PRISM_EXCLUDE_SERIALIZATION
1920 Init_prism_api_node();
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_arena_t * pm_arena_new(void)
Returns a newly allocated and initialized arena.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
pm_warning_level_t
The levels of warnings generated during parsing.
@ PM_WARNING_LEVEL_DEFAULT
For warnings which should be emitted if $VERBOSE != nil.
@ PM_WARNING_LEVEL_VERBOSE
For warnings which should be emitted if $VERBOSE == true.
pm_error_level_t
The levels of errors generated during parsing.
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
@ PM_ERROR_LEVEL_STACK
For errors that should raise a system stack error.
@ PM_ERROR_LEVEL_ENCODING_COMPATIBILITY
For errors that should raise an encoding compatibility error.
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
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.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define SYM2ID
Old name of RB_SYM2ID.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
VALUE rb_eNoMemError
NoMemoryError exception.
VALUE rb_eLoadError
LoadError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
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.
VALUE rb_eSysStackError
SystemStackError exception.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_cObject
Object class.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_stderr
STDERR constant.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
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.
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
rb_encoding * rb_locale_encoding(void)
Queries the encoding that represents the current locale.
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.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
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.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#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.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
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.
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.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
const char ruby_version[]
Stringised version.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
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.
static const uint8_t PM_OPTIONS_COMMAND_LINE_L
A bit representing whether or not the command line -l option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_A
A bit representing whether or not the command line -a option was set.
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE
The default value for parameters.
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL
When the scope is forwarding with the ... parameter.
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS
When the scope is forwarding with the * parameter.
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS
When the scope is forwarding with the ** parameter.
static const uint8_t PM_OPTIONS_COMMAND_LINE_N
A bit representing whether or not the command line -n option was set.
static const uint8_t PM_OPTIONS_COMMAND_LINE_X
A bit representing whether or not the command line -x option was set.
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK
When the scope is forwarding with the & parameter.
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_options_t * pm_options_new(void)
Allocate a new options struct.
static const uint8_t PM_OPTIONS_COMMAND_LINE_P
A bit representing whether or not the command line -p option was set.
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.
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.
PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser) PRISM_NONNULL(1)
Initiate the parser with the given parser.
comment_range comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The comments found while parsing, in source order.
diagnostic_range errors(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The errors found while parsing.
diagnostic_range warnings(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept
The warnings found while parsing.
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_buffer_t * pm_buffer_new(void)
Allocate and initialize a new buffer.
PRISM_EXPORTED_FUNCTION const char * pm_version(void)
The prism version and the serialization format.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
#define errno
Ractor-aware version of errno.
pm_source_init_result_t
Represents the result of initializing a source from a file.
@ PM_SOURCE_INIT_ERROR_GENERIC
Indicates a generic error from a source init function, where the type of error should be read from er...
@ PM_SOURCE_INIT_SUCCESS
Indicates that the source was successfully initialized.
@ PM_SOURCE_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
#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...
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.
uint32_t start
The offset of the location from the start of the source.
uint32_t length
The length of the location.
A view of a sequence of bytes.
This struct represents a token in the Ruby source.
The source, encoding name, and version for a string query.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.