Skip to content

Commit

Permalink
Raise errors for dumping prism parse tree
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 31, 2024
1 parent b4880af commit 71f16d4
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions ruby.c
Expand Up @@ -2341,43 +2341,41 @@ 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;
VALUE rb_source = open_load_file(opt->script_name, &xflag);
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_node_destroy(&parser, node);
pm_parser_free(&parser);
pm_buffer_free(&output_buffer);
pm_parse_result_free(&result);
}
else {
pm_parse_result_free(&result);
rb_exc_raise(error);
}

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))) {
Expand Down

0 comments on commit 71f16d4

Please sign in to comment.