From d7058852861d9d4cf762492057254a8b9e966b36 Mon Sep 17 00:00:00 2001 From: Jemma Issroff Date: Fri, 13 Oct 2023 12:24:40 -0700 Subject: [PATCH] [PRISM] Add --dump=prism mode (#8643) --- common.mk | 21 +++++++++++++++++++++ ruby.c | 27 +++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/common.mk b/common.mk index ab151d0f8c..fa583a8faa 100644 --- a/common.mk +++ b/common.mk @@ -15327,6 +15327,25 @@ ruby.$(OBJEXT): $(top_srcdir)/internal/thread.h ruby.$(OBJEXT): $(top_srcdir)/internal/variable.h ruby.$(OBJEXT): $(top_srcdir)/internal/vm.h ruby.$(OBJEXT): $(top_srcdir)/internal/warnings.h +ruby.$(OBJEXT): $(top_srcdir)/prism/defines.h +ruby.$(OBJEXT): $(top_srcdir)/prism/diagnostic.h +ruby.$(OBJEXT): $(top_srcdir)/prism/enc/pm_encoding.h +ruby.$(OBJEXT): $(top_srcdir)/prism/node.h +ruby.$(OBJEXT): $(top_srcdir)/prism/pack.h +ruby.$(OBJEXT): $(top_srcdir)/prism/parser.h +ruby.$(OBJEXT): $(top_srcdir)/prism/prism.h +ruby.$(OBJEXT): $(top_srcdir)/prism/regexp.h +ruby.$(OBJEXT): $(top_srcdir)/prism/unescape.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_buffer.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_char.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_constant_pool.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_list.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_memchr.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_newline_list.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_state_stack.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_string.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_string_list.h +ruby.$(OBJEXT): $(top_srcdir)/prism/util/pm_strpbrk.h ruby.$(OBJEXT): {$(VPATH)}assert.h ruby.$(OBJEXT): {$(VPATH)}atomic.h ruby.$(OBJEXT): {$(VPATH)}backward/2/assume.h @@ -15504,6 +15523,8 @@ ruby.$(OBJEXT): {$(VPATH)}missing.h ruby.$(OBJEXT): {$(VPATH)}node.h ruby.$(OBJEXT): {$(VPATH)}onigmo.h ruby.$(OBJEXT): {$(VPATH)}oniguruma.h +ruby.$(OBJEXT): {$(VPATH)}prism/ast.h +ruby.$(OBJEXT): {$(VPATH)}prism/version.h ruby.$(OBJEXT): {$(VPATH)}rjit.h ruby.$(OBJEXT): {$(VPATH)}ruby.c ruby.$(OBJEXT): {$(VPATH)}ruby_assert.h diff --git a/ruby.c b/ruby.c index 89a546920f..4f29a5b98a 100644 --- a/ruby.c +++ b/ruby.c @@ -56,6 +56,7 @@ #include "internal/thread.h" #include "internal/ruby_parser.h" #include "internal/variable.h" +#include "prism/prism.h" #include "ruby/encoding.h" #include "ruby/thread.h" #include "ruby/util.h" @@ -153,6 +154,8 @@ enum feature_flag_bits { SEP \ X(parsetree_with_comment) \ SEP \ + X(prism) \ + SEP \ X(insns) \ SEP \ X(insns_without_opt) \ @@ -166,7 +169,7 @@ enum dump_flag_bits { DUMP_BIT(parsetree_with_comment)), dump_exit_bits = (DUMP_BIT(yydebug) | DUMP_BIT(syntax) | DUMP_BIT(parsetree) | DUMP_BIT(parsetree_with_comment) | - DUMP_BIT(insns) | DUMP_BIT(insns_without_opt)) + DUMP_BIT(prism) | DUMP_BIT(insns) | DUMP_BIT(insns_without_opt)) }; static inline void @@ -353,7 +356,7 @@ usage(const char *name, int help, int highlight, int columns) static const struct ruby_opt_message help_msg[] = { M("--copyright", "", "print the copyright"), - M("--dump={insns|parsetree|...}[,...]", "", + M("--dump={insns|parsetree|prism|...}[,...]", "", "dump debug information. see below for available dump list"), M("--enable={jit|rubyopt|...}[,...]", ", --disable={jit|rubyopt|...}[,...]", "enable or disable features. see below for available features"), @@ -372,6 +375,7 @@ usage(const char *name, int help, int highlight, int columns) M("yydebug(+error-tolerant)", "", "yydebug of yacc parser generator"), M("parsetree(+error-tolerant)","", "AST"), M("parsetree_with_comment(+error-tolerant)", "", "AST with comments"), + M("prism", "", "Prism AST with comments"), }; static const struct ruby_opt_message features[] = { M("gems", "", "rubygems (only for debugging, default: "DEFAULT_RUBYGEMS_ENABLED")"), @@ -2333,6 +2337,25 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_define_global_function("chomp", rb_f_chomp, -1); } + if (dump & (DUMP_BIT(prism))) { + pm_parser_t parser; + if (opt->e_script) { + size_t len = RSTRING_LEN(opt->e_script); + pm_parser_init(&parser, (const uint8_t *) RSTRING_PTR(opt->e_script), len, "-e"); + } + else { + pm_string_t input; + char *filepath = RSTRING_PTR(opt->script_name); + pm_string_mapped_init(&input, filepath); + pm_parser_init(&parser, pm_string_source(&input), pm_string_length(&input), filepath); + } + + pm_node_t *node = pm_parse(&parser); + pm_print_node(&parser, node); + pm_node_destroy(&parser, node); + pm_parser_free(&parser); + } + if (dump & (DUMP_BIT(parsetree)|DUMP_BIT(parsetree_with_comment))) { rb_io_write(rb_stdout, rb_parser_dump_tree(ast->body.root, dump & DUMP_BIT(parsetree_with_comment))); rb_io_flush(rb_stdout);