From 71f16d498d30b3528774f519a0939eae59ef7602 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 31 Jan 2024 14:25:27 -0500 Subject: [PATCH] Raise errors for dumping prism parse tree --- ruby.c | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/ruby.c b/ruby.c index b3f9652955..b0bce7216b 100644 --- a/ruby.c +++ b/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))) {