Raise errors for dumping prism parse tree

This commit is contained in:
Kevin Newton 2024-01-31 14:25:27 -05:00
Родитель b4880af0e2
Коммит 71f16d498d
1 изменённых файлов: 19 добавлений и 21 удалений

40
ruby.c
Просмотреть файл

@ -2341,8 +2341,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
}
if (dump & (DUMP_BIT(prism_parsetree))) {
pm_string_t input;
pm_options_t options = { 0 };
pm_parse_result_t result = { 0 };
VALUE error;
if (strcmp(opt->script, "-") == 0) {
int xflag = opt->xflag;
@ -2350,34 +2350,32 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
opt->xflag = xflag != 0;
rb_warn("Prism support for streaming code from stdin is not currently supported");
pm_string_constant_init(&input, RSTRING_PTR(rb_source), RSTRING_LEN(rb_source));
pm_options_filepath_set(&options, RSTRING_PTR(opt->script_name));
error = pm_parse_string(&result, rb_source, opt->script_name);
}
else if (opt->e_script) {
pm_string_constant_init(&input, RSTRING_PTR(opt->e_script), RSTRING_LEN(opt->e_script));
pm_options_filepath_set(&options, "-e");
error = pm_parse_string(&result, opt->e_script, rb_str_new2("-e"));
}
else {
pm_string_mapped_init(&input, RSTRING_PTR(opt->script_name));
pm_options_filepath_set(&options, RSTRING_PTR(opt->script_name));
error = pm_parse_file(&result, opt->script_name);
}
pm_parser_t parser;
pm_parser_init(&parser, pm_string_source(&input), pm_string_length(&input), &options);
if (error == Qnil) {
pm_buffer_t output_buffer = { 0 };
pm_node_t *node = pm_parse(&parser);
pm_buffer_t output_buffer = { 0 };
pm_prettyprint(&output_buffer, &result.parser, result.node.ast_node);
rb_io_write(rb_stdout, rb_str_new((const char *) output_buffer.value, output_buffer.length));
rb_io_flush(rb_stdout);
pm_prettyprint(&output_buffer, &parser, node);
rb_io_write(rb_stdout, rb_str_new((const char *) output_buffer.value, output_buffer.length));
rb_io_flush(rb_stdout);
pm_buffer_free(&output_buffer);
pm_parse_result_free(&result);
}
else {
pm_parse_result_free(&result);
rb_exc_raise(error);
}
pm_buffer_free(&output_buffer);
pm_node_destroy(&parser, node);
pm_parser_free(&parser);
pm_string_free(&input);
pm_options_free(&options);
dump &= ~DUMP_BIT(prism_parsetree);
if (!dump) return Qtrue;
}
if (dump & (DUMP_BIT(parsetree)|DUMP_BIT(parsetree_with_comment))) {