| 1 | #include "prism/extension.h" |
| 2 | |
| 3 | #ifdef _WIN32 |
| 4 | #include <ruby/win32.h> |
| 5 | #endif |
| 6 | |
| 7 | // NOTE: this file should contain only bindings. All non-trivial logic should be |
| 8 | // in libprism so it can be shared its the various callers. |
| 9 | |
| 10 | VALUE rb_cPrism; |
| 11 | VALUE rb_cPrismNode; |
| 12 | VALUE rb_cPrismSource; |
| 13 | VALUE rb_cPrismToken; |
| 14 | VALUE rb_cPrismLocation; |
| 15 | |
| 16 | VALUE rb_cPrismComment; |
| 17 | VALUE rb_cPrismInlineComment; |
| 18 | VALUE rb_cPrismEmbDocComment; |
| 19 | VALUE rb_cPrismMagicComment; |
| 20 | VALUE rb_cPrismParseError; |
| 21 | VALUE rb_cPrismParseWarning; |
| 22 | VALUE rb_cPrismResult; |
| 23 | VALUE rb_cPrismParseResult; |
| 24 | VALUE rb_cPrismLexResult; |
| 25 | VALUE rb_cPrismParseLexResult; |
| 26 | VALUE rb_cPrismStringQuery; |
| 27 | VALUE rb_cPrismScope; |
| 28 | VALUE rb_cPrismCurrentVersionError; |
| 29 | |
| 30 | VALUE rb_cPrismDebugEncoding; |
| 31 | |
| 32 | ID rb_id_option_command_line; |
| 33 | ID rb_id_option_encoding; |
| 34 | ID rb_id_option_filepath; |
| 35 | ID rb_id_option_freeze; |
| 36 | ID rb_id_option_frozen_string_literal; |
| 37 | ID rb_id_option_line; |
| 38 | ID rb_id_option_main_script; |
| 39 | ID rb_id_option_partial_script; |
| 40 | ID rb_id_option_scopes; |
| 41 | ID rb_id_option_version; |
| 42 | ID rb_id_source_for; |
| 43 | ID rb_id_forwarding_positionals; |
| 44 | ID rb_id_forwarding_keywords; |
| 45 | ID rb_id_forwarding_block; |
| 46 | ID rb_id_forwarding_all; |
| 47 | |
| 48 | /******************************************************************************/ |
| 49 | /* IO of Ruby code */ |
| 50 | /******************************************************************************/ |
| 51 | |
| 52 | /** |
| 53 | * Check if the given VALUE is a string. If it's not a string, then raise a |
| 54 | * TypeError. Otherwise return the VALUE as a C string. |
| 55 | */ |
| 56 | static const char * |
| 57 | check_string(VALUE value) { |
| 58 | // Check if the value is a string. If it's not, then raise a type error. |
| 59 | if (!RB_TYPE_P(value, T_STRING)) { |
| 60 | rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(value)); |
| 61 | } |
| 62 | |
| 63 | // Otherwise, return the value as a C string. |
| 64 | return RSTRING_PTR(value); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Load the contents and size of the given string into the given pm_string_t. |
| 69 | */ |
| 70 | static void |
| 71 | input_load_string(pm_string_t *input, VALUE string) { |
| 72 | // Check if the string is a string. If it's not, then raise a type error. |
| 73 | if (!RB_TYPE_P(string, T_STRING)) { |
| 74 | rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(string)); |
| 75 | } |
| 76 | |
| 77 | pm_string_constant_init(input, RSTRING_PTR(string), RSTRING_LEN(string)); |
| 78 | } |
| 79 | |
| 80 | /******************************************************************************/ |
| 81 | /* Building C options from Ruby options */ |
| 82 | /******************************************************************************/ |
| 83 | |
| 84 | /** |
| 85 | * Build the scopes associated with the provided Ruby keyword value. |
| 86 | */ |
| 87 | static void |
| 88 | build_options_scopes(pm_options_t *options, VALUE scopes) { |
| 89 | // Check if the value is an array. If it's not, then raise a type error. |
| 90 | if (!RB_TYPE_P(scopes, T_ARRAY)) { |
| 91 | rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(scopes)); |
| 92 | } |
| 93 | |
| 94 | // Initialize the scopes array. |
| 95 | size_t scopes_count = RARRAY_LEN(scopes); |
| 96 | if (!pm_options_scopes_init(options, scopes_count)) { |
| 97 | rb_raise(rb_eNoMemError, "failed to allocate memory"); |
| 98 | } |
| 99 | |
| 100 | // Iterate over the scopes and add them to the options. |
| 101 | for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) { |
| 102 | VALUE scope = rb_ary_entry(scopes, scope_index); |
| 103 | |
| 104 | // The scope can be either an array or it can be a Prism::Scope object. |
| 105 | // Parse out the correct values here from either. |
| 106 | VALUE locals; |
| 107 | uint8_t forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE; |
| 108 | |
| 109 | if (RB_TYPE_P(scope, T_ARRAY)) { |
| 110 | locals = scope; |
| 111 | } else if (rb_obj_is_kind_of(scope, rb_cPrismScope)) { |
| 112 | locals = rb_ivar_get(scope, rb_intern("@locals")); |
| 113 | if (!RB_TYPE_P(locals, T_ARRAY)) { |
| 114 | rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(locals)); |
| 115 | } |
| 116 | |
| 117 | VALUE names = rb_ivar_get(scope, rb_intern("@forwarding")); |
| 118 | if (!RB_TYPE_P(names, T_ARRAY)) { |
| 119 | rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(names)); |
| 120 | } |
| 121 | |
| 122 | size_t names_count = RARRAY_LEN(names); |
| 123 | for (size_t name_index = 0; name_index < names_count; name_index++) { |
| 124 | VALUE name = rb_ary_entry(names, name_index); |
| 125 | |
| 126 | // Check that the name is a symbol. If it's not, then raise |
| 127 | // a type error. |
| 128 | if (!RB_TYPE_P(name, T_SYMBOL)) { |
| 129 | rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Symbol)", rb_obj_class(name)); |
| 130 | } |
| 131 | |
| 132 | ID id = SYM2ID(name); |
| 133 | if (id == rb_id_forwarding_positionals) { |
| 134 | forwarding |= PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS; |
| 135 | } else if (id == rb_id_forwarding_keywords) { |
| 136 | forwarding |= PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS; |
| 137 | } else if (id == rb_id_forwarding_block) { |
| 138 | forwarding |= PM_OPTIONS_SCOPE_FORWARDING_BLOCK; |
| 139 | } else if (id == rb_id_forwarding_all) { |
| 140 | forwarding |= PM_OPTIONS_SCOPE_FORWARDING_ALL; |
| 141 | } else { |
| 142 | rb_raise(rb_eArgError, "invalid forwarding value: %" PRIsVALUE, name); |
| 143 | } |
| 144 | } |
| 145 | } else { |
| 146 | rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array or Prism::Scope)", rb_obj_class(scope)); |
| 147 | } |
| 148 | |
| 149 | // Initialize the scope array. |
| 150 | size_t locals_count = RARRAY_LEN(locals); |
| 151 | pm_options_scope_t *options_scope = &options->scopes[scope_index]; |
| 152 | if (!pm_options_scope_init(options_scope, locals_count)) { |
| 153 | rb_raise(rb_eNoMemError, "failed to allocate memory"); |
| 154 | } |
| 155 | |
| 156 | // Iterate over the locals and add them to the scope. |
| 157 | for (size_t local_index = 0; local_index < locals_count; local_index++) { |
| 158 | VALUE local = rb_ary_entry(locals, local_index); |
| 159 | |
| 160 | // Check that the local is a symbol. If it's not, then raise a |
| 161 | // type error. |
| 162 | if (!RB_TYPE_P(local, T_SYMBOL)) { |
| 163 | rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Symbol)", rb_obj_class(local)); |
| 164 | } |
| 165 | |
| 166 | // Add the local to the scope. |
| 167 | pm_string_t *scope_local = &options_scope->locals[local_index]; |
| 168 | const char *name = rb_id2name(SYM2ID(local)); |
| 169 | pm_string_constant_init(scope_local, name, strlen(name)); |
| 170 | } |
| 171 | |
| 172 | // Now set the forwarding options. |
| 173 | pm_options_scope_forwarding_set(options_scope, forwarding); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * An iterator function that is called for each key-value in the keywords hash. |
| 179 | */ |
| 180 | static int |
| 181 | build_options_i(VALUE key, VALUE value, VALUE argument) { |
| 182 | pm_options_t *options = (pm_options_t *) argument; |
| 183 | ID key_id = SYM2ID(key); |
| 184 | |
| 185 | if (key_id == rb_id_option_filepath) { |
| 186 | if (!NIL_P(value)) pm_options_filepath_set(options, check_string(value)); |
| 187 | } else if (key_id == rb_id_option_encoding) { |
| 188 | if (!NIL_P(value)) { |
| 189 | if (value == Qfalse) { |
| 190 | pm_options_encoding_locked_set(options, true); |
| 191 | } else { |
| 192 | pm_options_encoding_set(options, rb_enc_name(rb_to_encoding(value))); |
| 193 | } |
| 194 | } |
| 195 | } else if (key_id == rb_id_option_line) { |
| 196 | if (!NIL_P(value)) pm_options_line_set(options, NUM2INT(value)); |
| 197 | } else if (key_id == rb_id_option_frozen_string_literal) { |
| 198 | if (!NIL_P(value)) pm_options_frozen_string_literal_set(options, RTEST(value)); |
| 199 | } else if (key_id == rb_id_option_version) { |
| 200 | if (!NIL_P(value)) { |
| 201 | const char *version = check_string(value); |
| 202 | |
| 203 | if (RSTRING_LEN(value) == 7 && strncmp(version, "current", 7) == 0) { |
| 204 | const char *current_version = RSTRING_PTR(rb_const_get(rb_cObject, rb_intern("RUBY_VERSION"))); |
| 205 | if (!pm_options_version_set(options, current_version, 3)) { |
| 206 | rb_exc_raise(rb_exc_new_cstr(rb_cPrismCurrentVersionError, current_version)); |
| 207 | } |
| 208 | } else if (!pm_options_version_set(options, version, RSTRING_LEN(value))) { |
| 209 | rb_raise(rb_eArgError, "invalid version: %" PRIsVALUE, value); |
| 210 | } |
| 211 | } |
| 212 | } else if (key_id == rb_id_option_scopes) { |
| 213 | if (!NIL_P(value)) build_options_scopes(options, value); |
| 214 | } else if (key_id == rb_id_option_command_line) { |
| 215 | if (!NIL_P(value)) { |
| 216 | const char *string = check_string(value); |
| 217 | uint8_t command_line = 0; |
| 218 | |
| 219 | for (size_t index = 0; index < strlen(string); index++) { |
| 220 | switch (string[index]) { |
| 221 | case 'a': command_line |= PM_OPTIONS_COMMAND_LINE_A; break; |
| 222 | case 'e': command_line |= PM_OPTIONS_COMMAND_LINE_E; break; |
| 223 | case 'l': command_line |= PM_OPTIONS_COMMAND_LINE_L; break; |
| 224 | case 'n': command_line |= PM_OPTIONS_COMMAND_LINE_N; break; |
| 225 | case 'p': command_line |= PM_OPTIONS_COMMAND_LINE_P; break; |
| 226 | case 'x': command_line |= PM_OPTIONS_COMMAND_LINE_X; break; |
| 227 | default: rb_raise(rb_eArgError, "invalid command line flag: '%c'", string[index]); break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | pm_options_command_line_set(options, command_line); |
| 232 | } |
| 233 | } else if (key_id == rb_id_option_main_script) { |
| 234 | if (!NIL_P(value)) pm_options_main_script_set(options, RTEST(value)); |
| 235 | } else if (key_id == rb_id_option_partial_script) { |
| 236 | if (!NIL_P(value)) pm_options_partial_script_set(options, RTEST(value)); |
| 237 | } else if (key_id == rb_id_option_freeze) { |
| 238 | if (!NIL_P(value)) pm_options_freeze_set(options, RTEST(value)); |
| 239 | } else { |
| 240 | rb_raise(rb_eArgError, "unknown keyword: %" PRIsVALUE, key); |
| 241 | } |
| 242 | |
| 243 | return ST_CONTINUE; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * We need a struct here to pass through rb_protect and it has to be a single |
| 248 | * value. Because the sizeof(VALUE) == sizeof(void *), we're going to pass this |
| 249 | * through as an opaque pointer and cast it on both sides. |
| 250 | */ |
| 251 | struct build_options_data { |
| 252 | pm_options_t *options; |
| 253 | VALUE keywords; |
| 254 | }; |
| 255 | |
| 256 | /** |
| 257 | * Build the set of options from the given keywords. Note that this can raise a |
| 258 | * Ruby error if the options are not valid. |
| 259 | */ |
| 260 | static VALUE |
| 261 | build_options(VALUE argument) { |
| 262 | struct build_options_data *data = (struct build_options_data *) argument; |
| 263 | rb_hash_foreach(data->keywords, build_options_i, (VALUE) data->options); |
| 264 | return Qnil; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Extract the options from the given keyword arguments. |
| 269 | */ |
| 270 | static void |
| 271 | extract_options(pm_options_t *options, VALUE filepath, VALUE keywords) { |
| 272 | options->line = 1; // default |
| 273 | |
| 274 | if (!NIL_P(keywords)) { |
| 275 | struct build_options_data data = { .options = options, .keywords = keywords }; |
| 276 | struct build_options_data *argument = &data; |
| 277 | |
| 278 | int state = 0; |
| 279 | rb_protect(build_options, (VALUE) argument, &state); |
| 280 | |
| 281 | if (state != 0) { |
| 282 | pm_options_free(options); |
| 283 | rb_jump_tag(state); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if (!NIL_P(filepath)) { |
| 288 | if (!RB_TYPE_P(filepath, T_STRING)) { |
| 289 | pm_options_free(options); |
| 290 | rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected String)", rb_obj_class(filepath)); |
| 291 | } |
| 292 | |
| 293 | pm_options_filepath_set(options, RSTRING_PTR(filepath)); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Read options for methods that look like (source, **options). |
| 299 | */ |
| 300 | static void |
| 301 | string_options(int argc, VALUE *argv, pm_string_t *input, pm_options_t *options) { |
| 302 | VALUE string; |
| 303 | VALUE keywords; |
| 304 | rb_scan_args(argc, argv, "1:", &string, &keywords); |
| 305 | |
| 306 | extract_options(options, Qnil, keywords); |
| 307 | input_load_string(input, string); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Read options for methods that look like (filepath, **options). |
| 312 | */ |
| 313 | static void |
| 314 | file_options(int argc, VALUE *argv, pm_string_t *input, pm_options_t *options, VALUE *encoded_filepath) { |
| 315 | VALUE filepath; |
| 316 | VALUE keywords; |
| 317 | rb_scan_args(argc, argv, "1:", &filepath, &keywords); |
| 318 | |
| 319 | Check_Type(filepath, T_STRING); |
| 320 | *encoded_filepath = rb_str_encode_ospath(filepath); |
| 321 | extract_options(options, *encoded_filepath, keywords); |
| 322 | |
| 323 | const char *source = (const char *) pm_string_source(&options->filepath); |
| 324 | pm_string_init_result_t result; |
| 325 | |
| 326 | switch (result = pm_string_file_init(input, source)) { |
| 327 | case PM_STRING_INIT_SUCCESS: |
| 328 | break; |
| 329 | case PM_STRING_INIT_ERROR_GENERIC: { |
| 330 | pm_options_free(options); |
| 331 | |
| 332 | #ifdef _WIN32 |
| 333 | int e = rb_w32_map_errno(GetLastError()); |
| 334 | #else |
| 335 | int e = errno; |
| 336 | #endif |
| 337 | |
| 338 | rb_syserr_fail(e, source); |
| 339 | break; |
| 340 | } |
| 341 | case PM_STRING_INIT_ERROR_DIRECTORY: |
| 342 | pm_options_free(options); |
| 343 | rb_syserr_fail(EISDIR, source); |
| 344 | break; |
| 345 | default: |
| 346 | pm_options_free(options); |
| 347 | rb_raise(rb_eRuntimeError, "Unknown error (%d) initializing file: %s", result, source); |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | #ifndef PRISM_EXCLUDE_SERIALIZATION |
| 353 | |
| 354 | /******************************************************************************/ |
| 355 | /* Serializing the AST */ |
| 356 | /******************************************************************************/ |
| 357 | |
| 358 | /** |
| 359 | * Dump the AST corresponding to the given input to a string. |
| 360 | */ |
| 361 | static VALUE |
| 362 | dump_input(pm_string_t *input, const pm_options_t *options) { |
| 363 | pm_buffer_t buffer; |
| 364 | if (!pm_buffer_init(&buffer)) { |
| 365 | rb_raise(rb_eNoMemError, "failed to allocate memory"); |
| 366 | } |
| 367 | |
| 368 | pm_parser_t parser; |
| 369 | pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options); |
| 370 | |
| 371 | pm_node_t *node = pm_parse(&parser); |
| 372 | pm_serialize(&parser, node, &buffer); |
| 373 | |
| 374 | VALUE result = rb_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer)); |
| 375 | pm_node_destroy(&parser, node); |
| 376 | pm_buffer_free(&buffer); |
| 377 | pm_parser_free(&parser); |
| 378 | |
| 379 | return result; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * call-seq: |
| 384 | * Prism::dump(source, **options) -> String |
| 385 | * |
| 386 | * Dump the AST corresponding to the given string to a string. For supported |
| 387 | * options, see Prism::parse. |
| 388 | */ |
| 389 | static VALUE |
| 390 | dump(int argc, VALUE *argv, VALUE self) { |
| 391 | pm_string_t input; |
| 392 | pm_options_t options = { 0 }; |
| 393 | string_options(argc, argv, &input, &options); |
| 394 | |
| 395 | #ifdef PRISM_BUILD_DEBUG |
| 396 | size_t length = pm_string_length(&input); |
| 397 | char* dup = xmalloc(length); |
| 398 | memcpy(dup, pm_string_source(&input), length); |
| 399 | pm_string_constant_init(&input, dup, length); |
| 400 | #endif |
| 401 | |
| 402 | VALUE value = dump_input(&input, &options); |
| 403 | if (options.freeze) rb_obj_freeze(value); |
| 404 | |
| 405 | #ifdef PRISM_BUILD_DEBUG |
| 406 | xfree(dup); |
| 407 | #endif |
| 408 | |
| 409 | pm_string_free(&input); |
| 410 | pm_options_free(&options); |
| 411 | |
| 412 | return value; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * call-seq: |
| 417 | * Prism::dump_file(filepath, **options) -> String |
| 418 | * |
| 419 | * Dump the AST corresponding to the given file to a string. For supported |
| 420 | * options, see Prism::parse. |
| 421 | */ |
| 422 | static VALUE |
| 423 | dump_file(int argc, VALUE *argv, VALUE self) { |
| 424 | pm_string_t input; |
| 425 | pm_options_t options = { 0 }; |
| 426 | |
| 427 | VALUE encoded_filepath; |
| 428 | file_options(argc, argv, &input, &options, &encoded_filepath); |
| 429 | |
| 430 | VALUE value = dump_input(&input, &options); |
| 431 | pm_string_free(&input); |
| 432 | pm_options_free(&options); |
| 433 | |
| 434 | return value; |
| 435 | } |
| 436 | |
| 437 | #endif |
| 438 | |
| 439 | /******************************************************************************/ |
| 440 | /* Extracting values for the parse result */ |
| 441 | /******************************************************************************/ |
| 442 | |
| 443 | /** |
| 444 | * The same as rb_class_new_instance, but accepts an additional boolean to |
| 445 | * indicate whether or not the resulting class instance should be frozen. |
| 446 | */ |
| 447 | static inline VALUE |
| 448 | rb_class_new_instance_freeze(int argc, const VALUE *argv, VALUE klass, bool freeze) { |
| 449 | VALUE value = rb_class_new_instance(argc, argv, klass); |
| 450 | if (freeze) rb_obj_freeze(value); |
| 451 | return value; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Create a new Location instance from the given parser and bounds. |
| 456 | */ |
| 457 | static inline VALUE |
| 458 | parser_location(const pm_parser_t *parser, VALUE source, bool freeze, const uint8_t *start, size_t length) { |
| 459 | VALUE argv[] = { source, LONG2FIX(start - parser->start), LONG2FIX(length) }; |
| 460 | return rb_class_new_instance_freeze(3, argv, rb_cPrismLocation, freeze); |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Create a new Location instance from the given parser and location. |
| 465 | */ |
| 466 | #define PARSER_LOCATION_LOC(parser, source, freeze, loc) \ |
| 467 | parser_location(parser, source, freeze, loc.start, (size_t) (loc.end - loc.start)) |
| 468 | |
| 469 | /** |
| 470 | * Build a new Comment instance from the given parser and comment. |
| 471 | */ |
| 472 | static inline VALUE |
| 473 | parser_comment(const pm_parser_t *parser, VALUE source, bool freeze, const pm_comment_t *comment) { |
| 474 | VALUE argv[] = { PARSER_LOCATION_LOC(parser, source, freeze, comment->location) }; |
| 475 | VALUE type = (comment->type == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment; |
| 476 | return rb_class_new_instance_freeze(1, argv, type, freeze); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Extract the comments out of the parser into an array. |
| 481 | */ |
| 482 | static VALUE |
| 483 | parser_comments(const pm_parser_t *parser, VALUE source, bool freeze) { |
| 484 | VALUE comments = rb_ary_new_capa(parser->comment_list.size); |
| 485 | |
| 486 | for ( |
| 487 | const pm_comment_t *comment = (const pm_comment_t *) parser->comment_list.head; |
| 488 | comment != NULL; |
| 489 | comment = (const pm_comment_t *) comment->node.next |
| 490 | ) { |
| 491 | VALUE value = parser_comment(parser, source, freeze, comment); |
| 492 | rb_ary_push(comments, value); |
| 493 | } |
| 494 | |
| 495 | if (freeze) rb_obj_freeze(comments); |
| 496 | return comments; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Build a new MagicComment instance from the given parser and magic comment. |
| 501 | */ |
| 502 | static inline VALUE |
| 503 | parser_magic_comment(const pm_parser_t *parser, VALUE source, bool freeze, const pm_magic_comment_t *magic_comment) { |
| 504 | VALUE key_loc = parser_location(parser, source, freeze, magic_comment->key_start, magic_comment->key_length); |
| 505 | VALUE value_loc = parser_location(parser, source, freeze, magic_comment->value_start, magic_comment->value_length); |
| 506 | VALUE argv[] = { key_loc, value_loc }; |
| 507 | return rb_class_new_instance_freeze(2, argv, rb_cPrismMagicComment, freeze); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Extract the magic comments out of the parser into an array. |
| 512 | */ |
| 513 | static VALUE |
| 514 | parser_magic_comments(const pm_parser_t *parser, VALUE source, bool freeze) { |
| 515 | VALUE magic_comments = rb_ary_new_capa(parser->magic_comment_list.size); |
| 516 | |
| 517 | for ( |
| 518 | const pm_magic_comment_t *magic_comment = (const pm_magic_comment_t *) parser->magic_comment_list.head; |
| 519 | magic_comment != NULL; |
| 520 | magic_comment = (const pm_magic_comment_t *) magic_comment->node.next |
| 521 | ) { |
| 522 | VALUE value = parser_magic_comment(parser, source, freeze, magic_comment); |
| 523 | rb_ary_push(magic_comments, value); |
| 524 | } |
| 525 | |
| 526 | if (freeze) rb_obj_freeze(magic_comments); |
| 527 | return magic_comments; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Extract out the data location from the parser into a Location instance if one |
| 532 | * exists. |
| 533 | */ |
| 534 | static VALUE |
| 535 | parser_data_loc(const pm_parser_t *parser, VALUE source, bool freeze) { |
| 536 | if (parser->data_loc.end == NULL) { |
| 537 | return Qnil; |
| 538 | } else { |
| 539 | return PARSER_LOCATION_LOC(parser, source, freeze, parser->data_loc); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Extract the errors out of the parser into an array. |
| 545 | */ |
| 546 | static VALUE |
| 547 | parser_errors(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) { |
| 548 | VALUE errors = rb_ary_new_capa(parser->error_list.size); |
| 549 | |
| 550 | for ( |
| 551 | const pm_diagnostic_t *error = (const pm_diagnostic_t *) parser->error_list.head; |
| 552 | error != NULL; |
| 553 | error = (const pm_diagnostic_t *) error->node.next |
| 554 | ) { |
| 555 | VALUE type = ID2SYM(rb_intern(pm_diagnostic_id_human(error->diag_id))); |
| 556 | VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(error->message, encoding)); |
| 557 | VALUE location = PARSER_LOCATION_LOC(parser, source, freeze, error->location); |
| 558 | |
| 559 | VALUE level = Qnil; |
| 560 | switch (error->level) { |
| 561 | case PM_ERROR_LEVEL_SYNTAX: |
| 562 | level = ID2SYM(rb_intern("syntax")); |
| 563 | break; |
| 564 | case PM_ERROR_LEVEL_ARGUMENT: |
| 565 | level = ID2SYM(rb_intern("argument")); |
| 566 | break; |
| 567 | case PM_ERROR_LEVEL_LOAD: |
| 568 | level = ID2SYM(rb_intern("load")); |
| 569 | break; |
| 570 | default: |
| 571 | rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, error->level); |
| 572 | } |
| 573 | |
| 574 | VALUE argv[] = { type, message, location, level }; |
| 575 | VALUE value = rb_class_new_instance_freeze(4, argv, rb_cPrismParseError, freeze); |
| 576 | rb_ary_push(errors, value); |
| 577 | } |
| 578 | |
| 579 | if (freeze) rb_obj_freeze(errors); |
| 580 | return errors; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Extract the warnings out of the parser into an array. |
| 585 | */ |
| 586 | static VALUE |
| 587 | parser_warnings(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) { |
| 588 | VALUE warnings = rb_ary_new_capa(parser->warning_list.size); |
| 589 | |
| 590 | for ( |
| 591 | const pm_diagnostic_t *warning = (const pm_diagnostic_t *) parser->warning_list.head; |
| 592 | warning != NULL; |
| 593 | warning = (const pm_diagnostic_t *) warning->node.next |
| 594 | ) { |
| 595 | VALUE type = ID2SYM(rb_intern(pm_diagnostic_id_human(warning->diag_id))); |
| 596 | VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(warning->message, encoding)); |
| 597 | VALUE location = PARSER_LOCATION_LOC(parser, source, freeze, warning->location); |
| 598 | |
| 599 | VALUE level = Qnil; |
| 600 | switch (warning->level) { |
| 601 | case PM_WARNING_LEVEL_DEFAULT: |
| 602 | level = ID2SYM(rb_intern("default")); |
| 603 | break; |
| 604 | case PM_WARNING_LEVEL_VERBOSE: |
| 605 | level = ID2SYM(rb_intern("verbose")); |
| 606 | break; |
| 607 | default: |
| 608 | rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, warning->level); |
| 609 | } |
| 610 | |
| 611 | VALUE argv[] = { type, message, location, level }; |
| 612 | VALUE value = rb_class_new_instance_freeze(4, argv, rb_cPrismParseWarning, freeze); |
| 613 | rb_ary_push(warnings, value); |
| 614 | } |
| 615 | |
| 616 | if (freeze) rb_obj_freeze(warnings); |
| 617 | return warnings; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Create a new parse result from the given parser, value, encoding, and source. |
| 622 | */ |
| 623 | static VALUE |
| 624 | parse_result_create(VALUE class, const pm_parser_t *parser, VALUE value, rb_encoding *encoding, VALUE source, bool freeze) { |
| 625 | VALUE result_argv[] = { |
| 626 | value, |
| 627 | parser_comments(parser, source, freeze), |
| 628 | parser_magic_comments(parser, source, freeze), |
| 629 | parser_data_loc(parser, source, freeze), |
| 630 | parser_errors(parser, encoding, source, freeze), |
| 631 | parser_warnings(parser, encoding, source, freeze), |
| 632 | source |
| 633 | }; |
| 634 | |
| 635 | return rb_class_new_instance_freeze(7, result_argv, class, freeze); |
| 636 | } |
| 637 | |
| 638 | /******************************************************************************/ |
| 639 | /* Lexing Ruby code */ |
| 640 | /******************************************************************************/ |
| 641 | |
| 642 | /** |
| 643 | * This struct gets stored in the parser and passed in to the lex callback any |
| 644 | * time a new token is found. We use it to store the necessary information to |
| 645 | * initialize a Token instance. |
| 646 | */ |
| 647 | typedef struct { |
| 648 | VALUE source; |
| 649 | VALUE tokens; |
| 650 | rb_encoding *encoding; |
| 651 | bool freeze; |
| 652 | } parse_lex_data_t; |
| 653 | |
| 654 | /** |
| 655 | * This is passed as a callback to the parser. It gets called every time a new |
| 656 | * token is found. Once found, we initialize a new instance of Token and push it |
| 657 | * onto the tokens array. |
| 658 | */ |
| 659 | static void |
| 660 | parse_lex_token(void *data, pm_parser_t *parser, pm_token_t *token) { |
| 661 | parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) parser->lex_callback->data; |
| 662 | |
| 663 | VALUE value = pm_token_new(parser, token, parse_lex_data->encoding, parse_lex_data->source, parse_lex_data->freeze); |
| 664 | VALUE yields = rb_assoc_new(value, INT2FIX(parser->lex_state)); |
| 665 | |
| 666 | if (parse_lex_data->freeze) { |
| 667 | rb_obj_freeze(value); |
| 668 | rb_obj_freeze(yields); |
| 669 | } |
| 670 | |
| 671 | rb_ary_push(parse_lex_data->tokens, yields); |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * This is called whenever the encoding changes based on the magic comment at |
| 676 | * the top of the file. We use it to update the encoding that we are using to |
| 677 | * create tokens. |
| 678 | */ |
| 679 | static void |
| 680 | parse_lex_encoding_changed_callback(pm_parser_t *parser) { |
| 681 | parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) parser->lex_callback->data; |
| 682 | parse_lex_data->encoding = rb_enc_find(parser->encoding->name); |
| 683 | |
| 684 | // Since the encoding changed, we need to go back and change the encoding of |
| 685 | // the tokens that were already lexed. This is only going to end up being |
| 686 | // one or two tokens, since the encoding can only change at the top of the |
| 687 | // file. |
| 688 | VALUE tokens = parse_lex_data->tokens; |
| 689 | VALUE next_tokens = rb_ary_new(); |
| 690 | |
| 691 | for (long index = 0; index < RARRAY_LEN(tokens); index++) { |
| 692 | VALUE yields = rb_ary_entry(tokens, index); |
| 693 | VALUE token = rb_ary_entry(yields, 0); |
| 694 | |
| 695 | VALUE value = rb_ivar_get(token, rb_intern("@value")); |
| 696 | VALUE next_value = rb_str_dup(value); |
| 697 | |
| 698 | rb_enc_associate(next_value, parse_lex_data->encoding); |
| 699 | if (parse_lex_data->freeze) rb_obj_freeze(next_value); |
| 700 | |
| 701 | VALUE next_token_argv[] = { |
| 702 | parse_lex_data->source, |
| 703 | rb_ivar_get(token, rb_intern("@type")), |
| 704 | next_value, |
| 705 | rb_ivar_get(token, rb_intern("@location")) |
| 706 | }; |
| 707 | |
| 708 | VALUE next_token = rb_class_new_instance(4, next_token_argv, rb_cPrismToken); |
| 709 | VALUE next_yields = rb_assoc_new(next_token, rb_ary_entry(yields, 1)); |
| 710 | |
| 711 | if (parse_lex_data->freeze) { |
| 712 | rb_obj_freeze(next_token); |
| 713 | rb_obj_freeze(next_yields); |
| 714 | } |
| 715 | |
| 716 | rb_ary_push(next_tokens, next_yields); |
| 717 | } |
| 718 | |
| 719 | rb_ary_replace(parse_lex_data->tokens, next_tokens); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Parse the given input and return a ParseResult containing just the tokens or |
| 724 | * the nodes and tokens. |
| 725 | */ |
| 726 | static VALUE |
| 727 | parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nodes) { |
| 728 | pm_parser_t parser; |
| 729 | pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options); |
| 730 | pm_parser_register_encoding_changed_callback(&parser, parse_lex_encoding_changed_callback); |
| 731 | |
| 732 | VALUE source_string = rb_str_new((const char *) pm_string_source(input), pm_string_length(input)); |
| 733 | VALUE offsets = rb_ary_new_capa(parser.newline_list.size); |
| 734 | VALUE source = rb_funcall(rb_cPrismSource, rb_id_source_for, 3, source_string, LONG2NUM(parser.start_line), offsets); |
| 735 | |
| 736 | parse_lex_data_t parse_lex_data = { |
| 737 | .source = source, |
| 738 | .tokens = rb_ary_new(), |
| 739 | .encoding = rb_utf8_encoding(), |
| 740 | .freeze = options->freeze, |
| 741 | }; |
| 742 | |
| 743 | parse_lex_data_t *data = &parse_lex_data; |
| 744 | pm_lex_callback_t lex_callback = (pm_lex_callback_t) { |
| 745 | .data = (void *) data, |
| 746 | .callback = parse_lex_token, |
| 747 | }; |
| 748 | |
| 749 | parser.lex_callback = &lex_callback; |
| 750 | pm_node_t *node = pm_parse(&parser); |
| 751 | |
| 752 | // Here we need to update the Source object to have the correct |
| 753 | // encoding for the source string and the correct newline offsets. |
| 754 | // We do it here because we've already created the Source object and given |
| 755 | // it over to all of the tokens, and both of these are only set after pm_parse(). |
| 756 | rb_encoding *encoding = rb_enc_find(parser.encoding->name); |
| 757 | rb_enc_associate(source_string, encoding); |
| 758 | |
| 759 | for (size_t index = 0; index < parser.newline_list.size; index++) { |
| 760 | rb_ary_push(offsets, ULONG2NUM(parser.newline_list.offsets[index])); |
| 761 | } |
| 762 | |
| 763 | if (options->freeze) { |
| 764 | rb_obj_freeze(source_string); |
| 765 | rb_obj_freeze(offsets); |
| 766 | rb_obj_freeze(source); |
| 767 | rb_obj_freeze(parse_lex_data.tokens); |
| 768 | } |
| 769 | |
| 770 | VALUE result; |
| 771 | if (return_nodes) { |
| 772 | VALUE value = rb_ary_new_capa(2); |
| 773 | rb_ary_push(value, pm_ast_new(&parser, node, parse_lex_data.encoding, source, options->freeze)); |
| 774 | rb_ary_push(value, parse_lex_data.tokens); |
| 775 | if (options->freeze) rb_obj_freeze(value); |
| 776 | result = parse_result_create(rb_cPrismParseLexResult, &parser, value, parse_lex_data.encoding, source, options->freeze); |
| 777 | } else { |
| 778 | result = parse_result_create(rb_cPrismLexResult, &parser, parse_lex_data.tokens, parse_lex_data.encoding, source, options->freeze); |
| 779 | } |
| 780 | |
| 781 | pm_node_destroy(&parser, node); |
| 782 | pm_parser_free(&parser); |
| 783 | |
| 784 | return result; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * call-seq: |
| 789 | * Prism::lex(source, **options) -> LexResult |
| 790 | * |
| 791 | * Return a LexResult instance that contains an array of Token instances |
| 792 | * corresponding to the given string. For supported options, see Prism::parse. |
| 793 | */ |
| 794 | static VALUE |
| 795 | lex(int argc, VALUE *argv, VALUE self) { |
| 796 | pm_string_t input; |
| 797 | pm_options_t options = { 0 }; |
| 798 | string_options(argc, argv, &input, &options); |
| 799 | |
| 800 | VALUE result = parse_lex_input(&input, &options, false); |
| 801 | pm_string_free(&input); |
| 802 | pm_options_free(&options); |
| 803 | |
| 804 | return result; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * call-seq: |
| 809 | * Prism::lex_file(filepath, **options) -> LexResult |
| 810 | * |
| 811 | * Return a LexResult instance that contains an array of Token instances |
| 812 | * corresponding to the given file. For supported options, see Prism::parse. |
| 813 | */ |
| 814 | static VALUE |
| 815 | lex_file(int argc, VALUE *argv, VALUE self) { |
| 816 | pm_string_t input; |
| 817 | pm_options_t options = { 0 }; |
| 818 | |
| 819 | VALUE encoded_filepath; |
| 820 | file_options(argc, argv, &input, &options, &encoded_filepath); |
| 821 | |
| 822 | VALUE value = parse_lex_input(&input, &options, false); |
| 823 | pm_string_free(&input); |
| 824 | pm_options_free(&options); |
| 825 | |
| 826 | return value; |
| 827 | } |
| 828 | |
| 829 | /******************************************************************************/ |
| 830 | /* Parsing Ruby code */ |
| 831 | /******************************************************************************/ |
| 832 | |
| 833 | /** |
| 834 | * Parse the given input and return a ParseResult instance. |
| 835 | */ |
| 836 | static VALUE |
| 837 | parse_input(pm_string_t *input, const pm_options_t *options) { |
| 838 | pm_parser_t parser; |
| 839 | pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options); |
| 840 | |
| 841 | pm_node_t *node = pm_parse(&parser); |
| 842 | rb_encoding *encoding = rb_enc_find(parser.encoding->name); |
| 843 | |
| 844 | VALUE source = pm_source_new(&parser, encoding, options->freeze); |
| 845 | VALUE value = pm_ast_new(&parser, node, encoding, source, options->freeze); |
| 846 | VALUE result = parse_result_create(rb_cPrismParseResult, &parser, value, encoding, source, options->freeze); |
| 847 | |
| 848 | if (options->freeze) { |
| 849 | rb_obj_freeze(source); |
| 850 | } |
| 851 | |
| 852 | pm_node_destroy(&parser, node); |
| 853 | pm_parser_free(&parser); |
| 854 | |
| 855 | return result; |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * call-seq: |
| 860 | * Prism::parse(source, **options) -> ParseResult |
| 861 | * |
| 862 | * Parse the given string and return a ParseResult instance. The options that |
| 863 | * are supported are: |
| 864 | * |
| 865 | * * `command_line` - either nil or a string of the various options that were |
| 866 | * set on the command line. Valid values are combinations of "a", "l", |
| 867 | * "n", "p", and "x". |
| 868 | * * `encoding` - the encoding of the source being parsed. This should be an |
| 869 | * encoding or nil. |
| 870 | * * `filepath` - the filepath of the source being parsed. This should be a |
| 871 | * string or nil. |
| 872 | * * `freeze` - whether or not to deeply freeze the AST. This should be a |
| 873 | * boolean or nil. |
| 874 | * * `frozen_string_literal` - whether or not the frozen string literal pragma |
| 875 | * has been set. This should be a boolean or nil. |
| 876 | * * `line` - the line number that the parse starts on. This should be an |
| 877 | * integer or nil. Note that this is 1-indexed. |
| 878 | * * `main_script` - a boolean indicating whether or not the source being parsed |
| 879 | * is the main script being run by the interpreter. This controls whether |
| 880 | * or not shebangs are parsed for additional flags and whether or not the |
| 881 | * parser will attempt to find a matching shebang if the first one does |
| 882 | * not contain the word "ruby". |
| 883 | * * `partial_script` - when the file being parsed is considered a "partial" |
| 884 | * script, jumps will not be marked as errors if they are not contained |
| 885 | * within loops/blocks. This is used in the case that you're parsing a |
| 886 | * script that you know will be embedded inside another script later, but |
| 887 | * you do not have that context yet. For example, when parsing an ERB |
| 888 | * template that will be evaluated inside another script. |
| 889 | * * `scopes` - the locals that are in scope surrounding the code that is being |
| 890 | * parsed. This should be an array of arrays of symbols or nil. Scopes are |
| 891 | * ordered from the outermost scope to the innermost one. |
| 892 | * * `version` - the version of Ruby syntax that prism should used to parse Ruby |
| 893 | * code. By default prism assumes you want to parse with the latest |
| 894 | * version of Ruby syntax (which you can trigger with `nil` or |
| 895 | * `"latest"`). You may also restrict the syntax to a specific version of |
| 896 | * Ruby, e.g., with `"3.3.0"`. To parse with the same syntax version that |
| 897 | * the current Ruby is running use `version: "current"`. Raises |
| 898 | * ArgumentError if the version is not currently supported by Prism. |
| 899 | */ |
| 900 | static VALUE |
| 901 | parse(int argc, VALUE *argv, VALUE self) { |
| 902 | pm_string_t input; |
| 903 | pm_options_t options = { 0 }; |
| 904 | string_options(argc, argv, &input, &options); |
| 905 | |
| 906 | #ifdef PRISM_BUILD_DEBUG |
| 907 | size_t length = pm_string_length(&input); |
| 908 | char* dup = xmalloc(length); |
| 909 | memcpy(dup, pm_string_source(&input), length); |
| 910 | pm_string_constant_init(&input, dup, length); |
| 911 | #endif |
| 912 | |
| 913 | VALUE value = parse_input(&input, &options); |
| 914 | |
| 915 | #ifdef PRISM_BUILD_DEBUG |
| 916 | xfree(dup); |
| 917 | #endif |
| 918 | |
| 919 | pm_string_free(&input); |
| 920 | pm_options_free(&options); |
| 921 | return value; |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * call-seq: |
| 926 | * Prism::parse_file(filepath, **options) -> ParseResult |
| 927 | * |
| 928 | * Parse the given file and return a ParseResult instance. For supported |
| 929 | * options, see Prism::parse. |
| 930 | */ |
| 931 | static VALUE |
| 932 | parse_file(int argc, VALUE *argv, VALUE self) { |
| 933 | pm_string_t input; |
| 934 | pm_options_t options = { 0 }; |
| 935 | |
| 936 | VALUE encoded_filepath; |
| 937 | file_options(argc, argv, &input, &options, &encoded_filepath); |
| 938 | |
| 939 | VALUE value = parse_input(&input, &options); |
| 940 | pm_string_free(&input); |
| 941 | pm_options_free(&options); |
| 942 | |
| 943 | return value; |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * Parse the given input and return nothing. |
| 948 | */ |
| 949 | static void |
| 950 | profile_input(pm_string_t *input, const pm_options_t *options) { |
| 951 | pm_parser_t parser; |
| 952 | pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options); |
| 953 | |
| 954 | pm_node_t *node = pm_parse(&parser); |
| 955 | pm_node_destroy(&parser, node); |
| 956 | pm_parser_free(&parser); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * call-seq: |
| 961 | * Prism::profile(source, **options) -> nil |
| 962 | * |
| 963 | * Parse the given string and return nothing. This method is meant to allow |
| 964 | * profilers to avoid the overhead of reifying the AST to Ruby. For supported |
| 965 | * options, see Prism::parse. |
| 966 | */ |
| 967 | static VALUE |
| 968 | profile(int argc, VALUE *argv, VALUE self) { |
| 969 | pm_string_t input; |
| 970 | pm_options_t options = { 0 }; |
| 971 | |
| 972 | string_options(argc, argv, &input, &options); |
| 973 | profile_input(&input, &options); |
| 974 | pm_string_free(&input); |
| 975 | pm_options_free(&options); |
| 976 | |
| 977 | return Qnil; |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * call-seq: |
| 982 | * Prism::profile_file(filepath, **options) -> nil |
| 983 | * |
| 984 | * Parse the given file and return nothing. This method is meant to allow |
| 985 | * profilers to avoid the overhead of reifying the AST to Ruby. For supported |
| 986 | * options, see Prism::parse. |
| 987 | */ |
| 988 | static VALUE |
| 989 | profile_file(int argc, VALUE *argv, VALUE self) { |
| 990 | pm_string_t input; |
| 991 | pm_options_t options = { 0 }; |
| 992 | |
| 993 | VALUE encoded_filepath; |
| 994 | file_options(argc, argv, &input, &options, &encoded_filepath); |
| 995 | |
| 996 | profile_input(&input, &options); |
| 997 | pm_string_free(&input); |
| 998 | pm_options_free(&options); |
| 999 | |
| 1000 | return Qnil; |
| 1001 | } |
| 1002 | |
| 1003 | static int |
| 1004 | parse_stream_eof(void *stream) { |
| 1005 | if (rb_funcall((VALUE) stream, rb_intern("eof?"), 0)) { |
| 1006 | return 1; |
| 1007 | } |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * An implementation of fgets that is suitable for use with Ruby IO objects. |
| 1013 | */ |
| 1014 | static char * |
| 1015 | parse_stream_fgets(char *string, int size, void *stream) { |
| 1016 | RUBY_ASSERT(size > 0); |
| 1017 | |
| 1018 | VALUE line = rb_funcall((VALUE) stream, rb_intern("gets"), 1, INT2FIX(size - 1)); |
| 1019 | if (NIL_P(line)) { |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | |
| 1023 | const char *cstr = RSTRING_PTR(line); |
| 1024 | long length = RSTRING_LEN(line); |
| 1025 | |
| 1026 | memcpy(string, cstr, length); |
| 1027 | string[length] = '\0'; |
| 1028 | |
| 1029 | return string; |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * call-seq: |
| 1034 | * Prism::parse_stream(stream, **options) -> ParseResult |
| 1035 | * |
| 1036 | * Parse the given object that responds to `gets` and return a ParseResult |
| 1037 | * instance. The options that are supported are the same as Prism::parse. |
| 1038 | */ |
| 1039 | static VALUE |
| 1040 | parse_stream(int argc, VALUE *argv, VALUE self) { |
| 1041 | VALUE stream; |
| 1042 | VALUE keywords; |
| 1043 | rb_scan_args(argc, argv, "1:", &stream, &keywords); |
| 1044 | |
| 1045 | pm_options_t options = { 0 }; |
| 1046 | extract_options(&options, Qnil, keywords); |
| 1047 | |
| 1048 | pm_parser_t parser; |
| 1049 | pm_buffer_t buffer; |
| 1050 | |
| 1051 | pm_node_t *node = pm_parse_stream(&parser, &buffer, (void *) stream, parse_stream_fgets, parse_stream_eof, &options); |
| 1052 | rb_encoding *encoding = rb_enc_find(parser.encoding->name); |
| 1053 | |
| 1054 | VALUE source = pm_source_new(&parser, encoding, options.freeze); |
| 1055 | VALUE value = pm_ast_new(&parser, node, encoding, source, options.freeze); |
| 1056 | VALUE result = parse_result_create(rb_cPrismParseResult, &parser, value, encoding, source, options.freeze); |
| 1057 | |
| 1058 | pm_node_destroy(&parser, node); |
| 1059 | pm_buffer_free(&buffer); |
| 1060 | pm_parser_free(&parser); |
| 1061 | |
| 1062 | return result; |
| 1063 | } |
| 1064 | |
| 1065 | /** |
| 1066 | * Parse the given input and return an array of Comment objects. |
| 1067 | */ |
| 1068 | static VALUE |
| 1069 | parse_input_comments(pm_string_t *input, const pm_options_t *options) { |
| 1070 | pm_parser_t parser; |
| 1071 | pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options); |
| 1072 | |
| 1073 | pm_node_t *node = pm_parse(&parser); |
| 1074 | rb_encoding *encoding = rb_enc_find(parser.encoding->name); |
| 1075 | |
| 1076 | VALUE source = pm_source_new(&parser, encoding, options->freeze); |
| 1077 | VALUE comments = parser_comments(&parser, source, options->freeze); |
| 1078 | |
| 1079 | pm_node_destroy(&parser, node); |
| 1080 | pm_parser_free(&parser); |
| 1081 | |
| 1082 | return comments; |
| 1083 | } |
| 1084 | |
| 1085 | /** |
| 1086 | * call-seq: |
| 1087 | * Prism::parse_comments(source, **options) -> Array |
| 1088 | * |
| 1089 | * Parse the given string and return an array of Comment objects. For supported |
| 1090 | * options, see Prism::parse. |
| 1091 | */ |
| 1092 | static VALUE |
| 1093 | parse_comments(int argc, VALUE *argv, VALUE self) { |
| 1094 | pm_string_t input; |
| 1095 | pm_options_t options = { 0 }; |
| 1096 | string_options(argc, argv, &input, &options); |
| 1097 | |
| 1098 | VALUE result = parse_input_comments(&input, &options); |
| 1099 | pm_string_free(&input); |
| 1100 | pm_options_free(&options); |
| 1101 | |
| 1102 | return result; |
| 1103 | } |
| 1104 | |
| 1105 | /** |
| 1106 | * call-seq: |
| 1107 | * Prism::parse_file_comments(filepath, **options) -> Array |
| 1108 | * |
| 1109 | * Parse the given file and return an array of Comment objects. For supported |
| 1110 | * options, see Prism::parse. |
| 1111 | */ |
| 1112 | static VALUE |
| 1113 | parse_file_comments(int argc, VALUE *argv, VALUE self) { |
| 1114 | pm_string_t input; |
| 1115 | pm_options_t options = { 0 }; |
| 1116 | |
| 1117 | VALUE encoded_filepath; |
| 1118 | file_options(argc, argv, &input, &options, &encoded_filepath); |
| 1119 | |
| 1120 | VALUE value = parse_input_comments(&input, &options); |
| 1121 | pm_string_free(&input); |
| 1122 | pm_options_free(&options); |
| 1123 | |
| 1124 | return value; |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * call-seq: |
| 1129 | * Prism::parse_lex(source, **options) -> ParseLexResult |
| 1130 | * |
| 1131 | * Parse the given string and return a ParseLexResult instance that contains a |
| 1132 | * 2-element array, where the first element is the AST and the second element is |
| 1133 | * an array of Token instances. |
| 1134 | * |
| 1135 | * This API is only meant to be used in the case where you need both the AST and |
| 1136 | * the tokens. If you only need one or the other, use either Prism::parse or |
| 1137 | * Prism::lex. |
| 1138 | * |
| 1139 | * For supported options, see Prism::parse. |
| 1140 | */ |
| 1141 | static VALUE |
| 1142 | parse_lex(int argc, VALUE *argv, VALUE self) { |
| 1143 | pm_string_t input; |
| 1144 | pm_options_t options = { 0 }; |
| 1145 | string_options(argc, argv, &input, &options); |
| 1146 | |
| 1147 | VALUE value = parse_lex_input(&input, &options, true); |
| 1148 | pm_string_free(&input); |
| 1149 | pm_options_free(&options); |
| 1150 | |
| 1151 | return value; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * call-seq: |
| 1156 | * Prism::parse_lex_file(filepath, **options) -> ParseLexResult |
| 1157 | * |
| 1158 | * Parse the given file and return a ParseLexResult instance that contains a |
| 1159 | * 2-element array, where the first element is the AST and the second element is |
| 1160 | * an array of Token instances. |
| 1161 | * |
| 1162 | * This API is only meant to be used in the case where you need both the AST and |
| 1163 | * the tokens. If you only need one or the other, use either Prism::parse_file |
| 1164 | * or Prism::lex_file. |
| 1165 | * |
| 1166 | * For supported options, see Prism::parse. |
| 1167 | */ |
| 1168 | static VALUE |
| 1169 | parse_lex_file(int argc, VALUE *argv, VALUE self) { |
| 1170 | pm_string_t input; |
| 1171 | pm_options_t options = { 0 }; |
| 1172 | |
| 1173 | VALUE encoded_filepath; |
| 1174 | file_options(argc, argv, &input, &options, &encoded_filepath); |
| 1175 | |
| 1176 | VALUE value = parse_lex_input(&input, &options, true); |
| 1177 | pm_string_free(&input); |
| 1178 | pm_options_free(&options); |
| 1179 | |
| 1180 | return value; |
| 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * Parse the given input and return true if it parses without errors. |
| 1185 | */ |
| 1186 | static VALUE |
| 1187 | parse_input_success_p(pm_string_t *input, const pm_options_t *options) { |
| 1188 | pm_parser_t parser; |
| 1189 | pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options); |
| 1190 | |
| 1191 | pm_node_t *node = pm_parse(&parser); |
| 1192 | pm_node_destroy(&parser, node); |
| 1193 | |
| 1194 | VALUE result = parser.error_list.size == 0 ? Qtrue : Qfalse; |
| 1195 | pm_parser_free(&parser); |
| 1196 | |
| 1197 | return result; |
| 1198 | } |
| 1199 | |
| 1200 | /** |
| 1201 | * call-seq: |
| 1202 | * Prism::parse_success?(source, **options) -> bool |
| 1203 | * |
| 1204 | * Parse the given string and return true if it parses without errors. For |
| 1205 | * supported options, see Prism::parse. |
| 1206 | */ |
| 1207 | static VALUE |
| 1208 | parse_success_p(int argc, VALUE *argv, VALUE self) { |
| 1209 | pm_string_t input; |
| 1210 | pm_options_t options = { 0 }; |
| 1211 | string_options(argc, argv, &input, &options); |
| 1212 | |
| 1213 | VALUE result = parse_input_success_p(&input, &options); |
| 1214 | pm_string_free(&input); |
| 1215 | pm_options_free(&options); |
| 1216 | |
| 1217 | return result; |
| 1218 | } |
| 1219 | |
| 1220 | /** |
| 1221 | * call-seq: |
| 1222 | * Prism::parse_failure?(source, **options) -> bool |
| 1223 | * |
| 1224 | * Parse the given string and return true if it parses with errors. For |
| 1225 | * supported options, see Prism::parse. |
| 1226 | */ |
| 1227 | static VALUE |
| 1228 | parse_failure_p(int argc, VALUE *argv, VALUE self) { |
| 1229 | return RTEST(parse_success_p(argc, argv, self)) ? Qfalse : Qtrue; |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * call-seq: |
| 1234 | * Prism::parse_file_success?(filepath, **options) -> bool |
| 1235 | * |
| 1236 | * Parse the given file and return true if it parses without errors. For |
| 1237 | * supported options, see Prism::parse. |
| 1238 | */ |
| 1239 | static VALUE |
| 1240 | parse_file_success_p(int argc, VALUE *argv, VALUE self) { |
| 1241 | pm_string_t input; |
| 1242 | pm_options_t options = { 0 }; |
| 1243 | |
| 1244 | VALUE encoded_filepath; |
| 1245 | file_options(argc, argv, &input, &options, &encoded_filepath); |
| 1246 | |
| 1247 | VALUE result = parse_input_success_p(&input, &options); |
| 1248 | pm_string_free(&input); |
| 1249 | pm_options_free(&options); |
| 1250 | |
| 1251 | return result; |
| 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * call-seq: |
| 1256 | * Prism::parse_file_failure?(filepath, **options) -> bool |
| 1257 | * |
| 1258 | * Parse the given file and return true if it parses with errors. For |
| 1259 | * supported options, see Prism::parse. |
| 1260 | */ |
| 1261 | static VALUE |
| 1262 | parse_file_failure_p(int argc, VALUE *argv, VALUE self) { |
| 1263 | return RTEST(parse_file_success_p(argc, argv, self)) ? Qfalse : Qtrue; |
| 1264 | } |
| 1265 | |
| 1266 | /******************************************************************************/ |
| 1267 | /* String query methods */ |
| 1268 | /******************************************************************************/ |
| 1269 | |
| 1270 | /** |
| 1271 | * Process the result of a call to a string query method and return an |
| 1272 | * appropriate value. |
| 1273 | */ |
| 1274 | static VALUE |
| 1275 | string_query(pm_string_query_t result) { |
| 1276 | switch (result) { |
| 1277 | case PM_STRING_QUERY_ERROR: |
| 1278 | rb_raise(rb_eArgError, "Invalid or non ascii-compatible encoding"); |
| 1279 | return Qfalse; |
| 1280 | case PM_STRING_QUERY_FALSE: |
| 1281 | return Qfalse; |
| 1282 | case PM_STRING_QUERY_TRUE: |
| 1283 | return Qtrue; |
| 1284 | } |
| 1285 | return Qfalse; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * call-seq: |
| 1290 | * Prism::StringQuery::local?(string) -> bool |
| 1291 | * |
| 1292 | * Returns true if the string constitutes a valid local variable name. Note that |
| 1293 | * this means the names that can be set through Binding#local_variable_set, not |
| 1294 | * necessarily the ones that can be set through a local variable assignment. |
| 1295 | */ |
| 1296 | static VALUE |
| 1297 | string_query_local_p(VALUE self, VALUE string) { |
| 1298 | const uint8_t *source = (const uint8_t *) check_string(string); |
| 1299 | return string_query(pm_string_query_local(source, RSTRING_LEN(string), rb_enc_get(string)->name)); |
| 1300 | } |
| 1301 | |
| 1302 | /** |
| 1303 | * call-seq: |
| 1304 | * Prism::StringQuery::constant?(string) -> bool |
| 1305 | * |
| 1306 | * Returns true if the string constitutes a valid constant name. Note that this |
| 1307 | * means the names that can be set through Module#const_set, not necessarily the |
| 1308 | * ones that can be set through a constant assignment. |
| 1309 | */ |
| 1310 | static VALUE |
| 1311 | string_query_constant_p(VALUE self, VALUE string) { |
| 1312 | const uint8_t *source = (const uint8_t *) check_string(string); |
| 1313 | return string_query(pm_string_query_constant(source, RSTRING_LEN(string), rb_enc_get(string)->name)); |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * call-seq: |
| 1318 | * Prism::StringQuery::method_name?(string) -> bool |
| 1319 | * |
| 1320 | * Returns true if the string constitutes a valid method name. |
| 1321 | */ |
| 1322 | static VALUE |
| 1323 | string_query_method_name_p(VALUE self, VALUE string) { |
| 1324 | const uint8_t *source = (const uint8_t *) check_string(string); |
| 1325 | return string_query(pm_string_query_method_name(source, RSTRING_LEN(string), rb_enc_get(string)->name)); |
| 1326 | } |
| 1327 | |
| 1328 | /******************************************************************************/ |
| 1329 | /* Initialization of the extension */ |
| 1330 | /******************************************************************************/ |
| 1331 | |
| 1332 | /** |
| 1333 | * The init function that Ruby calls when loading this extension. |
| 1334 | */ |
| 1335 | RUBY_FUNC_EXPORTED void |
| 1336 | Init_prism(void) { |
| 1337 | // Make sure that the prism library version matches the expected version. |
| 1338 | // Otherwise something was compiled incorrectly. |
| 1339 | if (strcmp(pm_version(), EXPECTED_PRISM_VERSION) != 0) { |
| 1340 | rb_raise( |
| 1341 | rb_eRuntimeError, |
| 1342 | "The prism library version (%s) does not match the expected version (%s)", |
| 1343 | pm_version(), |
| 1344 | EXPECTED_PRISM_VERSION |
| 1345 | ); |
| 1346 | } |
| 1347 | |
| 1348 | #ifdef HAVE_RB_EXT_RACTOR_SAFE |
| 1349 | // Mark this extension as Ractor-safe. |
| 1350 | rb_ext_ractor_safe(true); |
| 1351 | #endif |
| 1352 | |
| 1353 | // Grab up references to all of the constants that we're going to need to |
| 1354 | // reference throughout this extension. |
| 1355 | rb_cPrism = rb_define_module("Prism"); |
| 1356 | rb_cPrismNode = rb_define_class_under(rb_cPrism, "Node", rb_cObject); |
| 1357 | rb_cPrismSource = rb_define_class_under(rb_cPrism, "Source", rb_cObject); |
| 1358 | rb_cPrismToken = rb_define_class_under(rb_cPrism, "Token", rb_cObject); |
| 1359 | rb_cPrismLocation = rb_define_class_under(rb_cPrism, "Location", rb_cObject); |
| 1360 | rb_cPrismComment = rb_define_class_under(rb_cPrism, "Comment", rb_cObject); |
| 1361 | rb_cPrismInlineComment = rb_define_class_under(rb_cPrism, "InlineComment", rb_cPrismComment); |
| 1362 | rb_cPrismEmbDocComment = rb_define_class_under(rb_cPrism, "EmbDocComment", rb_cPrismComment); |
| 1363 | rb_cPrismMagicComment = rb_define_class_under(rb_cPrism, "MagicComment", rb_cObject); |
| 1364 | rb_cPrismParseError = rb_define_class_under(rb_cPrism, "ParseError", rb_cObject); |
| 1365 | rb_cPrismParseWarning = rb_define_class_under(rb_cPrism, "ParseWarning", rb_cObject); |
| 1366 | rb_cPrismResult = rb_define_class_under(rb_cPrism, "Result", rb_cObject); |
| 1367 | rb_cPrismParseResult = rb_define_class_under(rb_cPrism, "ParseResult", rb_cPrismResult); |
| 1368 | rb_cPrismLexResult = rb_define_class_under(rb_cPrism, "LexResult", rb_cPrismResult); |
| 1369 | rb_cPrismParseLexResult = rb_define_class_under(rb_cPrism, "ParseLexResult", rb_cPrismResult); |
| 1370 | rb_cPrismStringQuery = rb_define_class_under(rb_cPrism, "StringQuery", rb_cObject); |
| 1371 | rb_cPrismScope = rb_define_class_under(rb_cPrism, "Scope", rb_cObject); |
| 1372 | |
| 1373 | rb_cPrismCurrentVersionError = rb_const_get(rb_cPrism, rb_intern("CurrentVersionError")); |
| 1374 | |
| 1375 | // Intern all of the IDs eagerly that we support so that we don't have to do |
| 1376 | // it every time we parse. |
| 1377 | rb_id_option_command_line = rb_intern_const("command_line"); |
| 1378 | rb_id_option_encoding = rb_intern_const("encoding"); |
| 1379 | rb_id_option_filepath = rb_intern_const("filepath"); |
| 1380 | rb_id_option_freeze = rb_intern_const("freeze"); |
| 1381 | rb_id_option_frozen_string_literal = rb_intern_const("frozen_string_literal"); |
| 1382 | rb_id_option_line = rb_intern_const("line"); |
| 1383 | rb_id_option_main_script = rb_intern_const("main_script"); |
| 1384 | rb_id_option_partial_script = rb_intern_const("partial_script"); |
| 1385 | rb_id_option_scopes = rb_intern_const("scopes"); |
| 1386 | rb_id_option_version = rb_intern_const("version"); |
| 1387 | rb_id_source_for = rb_intern("for"); |
| 1388 | rb_id_forwarding_positionals = rb_intern("*"); |
| 1389 | rb_id_forwarding_keywords = rb_intern("**"); |
| 1390 | rb_id_forwarding_block = rb_intern("&"); |
| 1391 | rb_id_forwarding_all = rb_intern("..."); |
| 1392 | |
| 1393 | /** |
| 1394 | * The version of the prism library. |
| 1395 | */ |
| 1396 | rb_define_const(rb_cPrism, "VERSION", rb_str_freeze(rb_str_new_cstr(EXPECTED_PRISM_VERSION))); |
| 1397 | |
| 1398 | // First, the functions that have to do with lexing and parsing. |
| 1399 | rb_define_singleton_method(rb_cPrism, "lex", lex, -1); |
| 1400 | rb_define_singleton_method(rb_cPrism, "lex_file", lex_file, -1); |
| 1401 | rb_define_singleton_method(rb_cPrism, "parse", parse, -1); |
| 1402 | rb_define_singleton_method(rb_cPrism, "parse_file", parse_file, -1); |
| 1403 | rb_define_singleton_method(rb_cPrism, "profile", profile, -1); |
| 1404 | rb_define_singleton_method(rb_cPrism, "profile_file", profile_file, -1); |
| 1405 | rb_define_singleton_method(rb_cPrism, "parse_stream", parse_stream, -1); |
| 1406 | rb_define_singleton_method(rb_cPrism, "parse_comments", parse_comments, -1); |
| 1407 | rb_define_singleton_method(rb_cPrism, "parse_file_comments", parse_file_comments, -1); |
| 1408 | rb_define_singleton_method(rb_cPrism, "parse_lex", parse_lex, -1); |
| 1409 | rb_define_singleton_method(rb_cPrism, "parse_lex_file", parse_lex_file, -1); |
| 1410 | rb_define_singleton_method(rb_cPrism, "parse_success?", parse_success_p, -1); |
| 1411 | rb_define_singleton_method(rb_cPrism, "parse_failure?", parse_failure_p, -1); |
| 1412 | rb_define_singleton_method(rb_cPrism, "parse_file_success?", parse_file_success_p, -1); |
| 1413 | rb_define_singleton_method(rb_cPrism, "parse_file_failure?", parse_file_failure_p, -1); |
| 1414 | |
| 1415 | #ifndef PRISM_EXCLUDE_SERIALIZATION |
| 1416 | rb_define_singleton_method(rb_cPrism, "dump", dump, -1); |
| 1417 | rb_define_singleton_method(rb_cPrism, "dump_file", dump_file, -1); |
| 1418 | #endif |
| 1419 | |
| 1420 | rb_define_singleton_method(rb_cPrismStringQuery, "local?", string_query_local_p, 1); |
| 1421 | rb_define_singleton_method(rb_cPrismStringQuery, "constant?", string_query_constant_p, 1); |
| 1422 | rb_define_singleton_method(rb_cPrismStringQuery, "method_name?", string_query_method_name_p, 1); |
| 1423 | |
| 1424 | // Next, initialize the other APIs. |
| 1425 | Init_prism_api_node(); |
| 1426 | Init_prism_pack(); |
| 1427 | } |