@@ -153,8 +153,6 @@ enum feature_flag_bits {
153
153
SEP \
154
154
X(parsetree_with_comment) \
155
155
SEP \
156
- X(prism_parsetree) \
157
- SEP \
158
156
X(insns) \
159
157
SEP \
160
158
X(insns_without_opt) \
@@ -168,7 +166,7 @@ enum dump_flag_bits {
168
166
DUMP_BIT (parsetree_with_comment )),
169
167
dump_exit_bits = (DUMP_BIT (yydebug ) | DUMP_BIT (syntax ) |
170
168
DUMP_BIT (parsetree ) | DUMP_BIT (parsetree_with_comment ) |
171
- DUMP_BIT (prism_parsetree ) | DUMP_BIT ( insns ) | DUMP_BIT (insns_without_opt ))
169
+ DUMP_BIT (insns ) | DUMP_BIT (insns_without_opt ))
172
170
};
173
171
174
172
static inline void
@@ -355,7 +353,7 @@ usage(const char *name, int help, int highlight, int columns)
355
353
356
354
static const struct ruby_opt_message help_msg [] = {
357
355
M ("--copyright" , "" , "print the copyright" ),
358
- M ("--dump={insns|parsetree|prism_parsetree| ...}[,...]" , "" ,
356
+ M ("--dump={insns|parsetree|...}[,...]" , "" ,
359
357
"dump debug information. see below for available dump list" ),
360
358
M ("--enable={jit|rubyopt|...}[,...]" , ", --disable={jit|rubyopt|...}[,...]" ,
361
359
"enable or disable features. see below for available features" ),
@@ -1986,12 +1984,96 @@ env_var_truthy(const char *name)
1986
1984
1987
1985
rb_pid_t rb_fork_ruby (int * status );
1988
1986
1987
+ static rb_ast_t *
1988
+ process_script (ruby_cmdline_options_t * opt )
1989
+ {
1990
+ rb_ast_t * ast ;
1991
+ VALUE parser = rb_parser_new ();
1992
+
1993
+ if (opt -> dump & DUMP_BIT (yydebug )) {
1994
+ rb_parser_set_yydebug (parser , Qtrue );
1995
+ }
1996
+
1997
+ if (opt -> dump & DUMP_BIT (error_tolerant )) {
1998
+ rb_parser_error_tolerant (parser );
1999
+ }
2000
+
2001
+ if (opt -> e_script ) {
2002
+ VALUE progname = rb_progname ;
2003
+ rb_parser_set_context (parser , 0 , TRUE);
2004
+
2005
+ ruby_opt_init (opt );
2006
+ ruby_set_script_name (progname );
2007
+ rb_parser_set_options (parser , opt -> do_print , opt -> do_loop ,
2008
+ opt -> do_line , opt -> do_split );
2009
+ ast = rb_parser_compile_string (parser , opt -> script , opt -> e_script , 1 );
2010
+ }
2011
+ else {
2012
+ VALUE f ;
2013
+ int xflag = opt -> xflag ;
2014
+ f = open_load_file (opt -> script_name , & xflag );
2015
+ opt -> xflag = xflag != 0 ;
2016
+ rb_parser_set_context (parser , 0 , f == rb_stdin );
2017
+ ast = load_file (parser , opt -> script_name , f , 1 , opt );
2018
+ }
2019
+ if (!ast -> body .root ) {
2020
+ rb_ast_dispose (ast );
2021
+ return NULL ;
2022
+ }
2023
+ return ast ;
2024
+ }
2025
+
2026
+ static void
2027
+ prism_script (ruby_cmdline_options_t * opt , pm_string_t * input , pm_options_t * options )
2028
+ {
2029
+ ruby_opt_init (opt );
2030
+
2031
+ if (strcmp (opt -> script , "-" ) == 0 ) {
2032
+ rb_warn ("Prism support for streaming code from stdin is not currently supported" );
2033
+ pm_string_constant_init (input , "" , 0 );
2034
+ pm_options_filepath_set (options , "-e" );
2035
+ }
2036
+ else if (opt -> e_script ) {
2037
+ pm_string_constant_init (input , RSTRING_PTR (opt -> e_script ), RSTRING_LEN (opt -> e_script ));
2038
+ pm_options_filepath_set (options , "-e" );
2039
+ }
2040
+ else {
2041
+ pm_string_mapped_init (input , RSTRING_PTR (opt -> script_name ));
2042
+ pm_options_filepath_set (options , RSTRING_PTR (opt -> script_name ));
2043
+ }
2044
+ }
2045
+
2046
+ static VALUE
2047
+ prism_dump_tree (pm_string_t * input , pm_options_t * options )
2048
+ {
2049
+ pm_parser_t parser ;
2050
+ pm_parser_init (& parser , pm_string_source (input ), pm_string_length (input ), options );
2051
+
2052
+ pm_node_t * node = pm_parse (& parser );
2053
+
2054
+ pm_buffer_t output_buffer = { 0 };
2055
+
2056
+ pm_prettyprint (& output_buffer , & parser , node );
2057
+
2058
+ VALUE tree = rb_str_new (output_buffer .value , output_buffer .length );
2059
+
2060
+ pm_buffer_free (& output_buffer );
2061
+ pm_node_destroy (& parser , node );
2062
+ pm_parser_free (& parser );
2063
+
2064
+ return tree ;
2065
+ }
2066
+
1989
2067
static VALUE
1990
2068
process_options (int argc , char * * argv , ruby_cmdline_options_t * opt )
1991
2069
{
1992
- rb_ast_t * ast = 0 ;
1993
- VALUE parser ;
1994
- VALUE script_name ;
2070
+ rb_ast_t * ast = NULL ;
2071
+ pm_string_t pm_input = { 0 };
2072
+ pm_options_t pm_options = { 0 };
2073
+
2074
+ #define dispose_result () \
2075
+ (ast ? rb_ast_dispose(ast) : (pm_string_free(&pm_input), pm_options_free(&pm_options)))
2076
+
1995
2077
const rb_iseq_t * iseq ;
1996
2078
rb_encoding * enc , * lenc ;
1997
2079
#if UTF8_PATH
@@ -2162,13 +2244,6 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
2162
2244
lenc = rb_locale_encoding ();
2163
2245
rb_enc_associate (rb_progname , lenc );
2164
2246
rb_obj_freeze (rb_progname );
2165
- parser = rb_parser_new ();
2166
- if (opt -> dump & DUMP_BIT (yydebug )) {
2167
- rb_parser_set_yydebug (parser , Qtrue );
2168
- }
2169
- if (opt -> dump & DUMP_BIT (error_tolerant )) {
2170
- rb_parser_error_tolerant (parser );
2171
- }
2172
2247
if (opt -> ext .enc .name != 0 ) {
2173
2248
opt -> ext .enc .index = opt_enc_index (opt -> ext .enc .name );
2174
2249
}
@@ -2194,7 +2269,6 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
2194
2269
ienc = enc ;
2195
2270
#endif
2196
2271
}
2197
- script_name = opt -> script_name ;
2198
2272
rb_enc_associate (opt -> script_name , IF_UTF8_PATH (uenc , lenc ));
2199
2273
#if UTF8_PATH
2200
2274
if (uenc != lenc ) {
@@ -2261,46 +2335,35 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
2261
2335
ruby_set_argv (argc , argv );
2262
2336
opt -> sflag = process_sflag (opt -> sflag );
2263
2337
2264
- if (!(* rb_ruby_prism_ptr ())) {
2265
- if (opt -> e_script ) {
2266
- VALUE progname = rb_progname ;
2267
- rb_encoding * eenc ;
2268
- rb_parser_set_context (parser , 0 , TRUE);
2269
-
2270
- if (opt -> src .enc .index >= 0 ) {
2271
- eenc = rb_enc_from_index (opt -> src .enc .index );
2272
- }
2273
- else {
2274
- eenc = lenc ;
2275
- #if UTF8_PATH
2276
- if (ienc ) eenc = ienc ;
2277
- #endif
2278
- }
2338
+ if (opt -> e_script ) {
2339
+ rb_encoding * eenc ;
2340
+ if (opt -> src .enc .index >= 0 ) {
2341
+ eenc = rb_enc_from_index (opt -> src .enc .index );
2342
+ }
2343
+ else {
2344
+ eenc = lenc ;
2279
2345
#if UTF8_PATH
2280
- if (eenc != uenc ) {
2281
- opt -> e_script = str_conv_enc (opt -> e_script , uenc , eenc );
2282
- }
2346
+ if (ienc ) eenc = ienc ;
2283
2347
#endif
2284
- rb_enc_associate (opt -> e_script , eenc );
2285
- ruby_opt_init (opt );
2286
- ruby_set_script_name (progname );
2287
- rb_parser_set_options (parser , opt -> do_print , opt -> do_loop ,
2288
- opt -> do_line , opt -> do_split );
2289
- ast = rb_parser_compile_string (parser , opt -> script , opt -> e_script , 1 );
2290
2348
}
2291
- else {
2292
- VALUE f ;
2293
- int xflag = opt -> xflag ;
2294
- f = open_load_file (script_name , & xflag );
2295
- opt -> xflag = xflag != 0 ;
2296
- rb_parser_set_context (parser , 0 , f == rb_stdin );
2297
- ast = load_file (parser , opt -> script_name , f , 1 , opt );
2349
+ #if UTF8_PATH
2350
+ if (eenc != uenc ) {
2351
+ opt -> e_script = str_conv_enc (opt -> e_script , uenc , eenc );
2298
2352
}
2353
+ #endif
2354
+ rb_enc_associate (opt -> e_script , eenc );
2355
+ }
2356
+
2357
+ if (!(* rb_ruby_prism_ptr ())) {
2358
+ if (!(ast = process_script (opt ))) return Qfalse ;
2359
+ }
2360
+ else {
2361
+ prism_script (opt , & pm_input , & pm_options );
2299
2362
}
2300
2363
ruby_set_script_name (opt -> script_name );
2301
- if (dump & DUMP_BIT (yydebug )) {
2302
- dump &= ~ DUMP_BIT ( yydebug );
2303
- if (! dump ) return Qtrue ;
2364
+ if (( dump & DUMP_BIT (yydebug )) && !( dump &= ~ DUMP_BIT ( yydebug ) )) {
2365
+ dispose_result ( );
2366
+ return Qtrue ;
2304
2367
}
2305
2368
2306
2369
if (opt -> ext .enc .index >= 0 ) {
@@ -2320,11 +2383,6 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
2320
2383
rb_enc_set_default_internal (Qnil );
2321
2384
rb_stdio_set_default_encoding ();
2322
2385
2323
- if (!(* rb_ruby_prism_ptr ()) && !ast -> body .root ) {
2324
- rb_ast_dispose (ast );
2325
- return Qfalse ;
2326
- }
2327
-
2328
2386
opt -> sflag = process_sflag (opt -> sflag );
2329
2387
opt -> xflag = 0 ;
2330
2388
@@ -2341,60 +2399,28 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
2341
2399
rb_define_global_function ("chomp" , rb_f_chomp , -1 );
2342
2400
}
2343
2401
2344
- if (dump & (DUMP_BIT (prism_parsetree ))) {
2345
- pm_string_t input ;
2346
- pm_options_t options = { 0 };
2347
-
2348
- if (strcmp (opt -> script , "-" ) == 0 ) {
2349
- int xflag = opt -> xflag ;
2350
- VALUE rb_source = open_load_file (opt -> script_name , & xflag );
2351
- opt -> xflag = xflag != 0 ;
2352
-
2353
- rb_warn ("Prism support for streaming code from stdin is not currently supported" );
2354
- pm_string_constant_init (& input , RSTRING_PTR (rb_source ), RSTRING_LEN (rb_source ));
2355
- pm_options_filepath_set (& options , RSTRING_PTR (opt -> script_name ));
2356
- }
2357
- else if (opt -> e_script ) {
2358
- pm_string_constant_init (& input , RSTRING_PTR (opt -> e_script ), RSTRING_LEN (opt -> e_script ));
2359
- pm_options_filepath_set (& options , "-e" );
2402
+ if (dump & (DUMP_BIT (parsetree )|DUMP_BIT (parsetree_with_comment ))) {
2403
+ VALUE tree ;
2404
+ if (ast ) {
2405
+ int comment = dump & DUMP_BIT (parsetree_with_comment );
2406
+ tree = rb_parser_dump_tree (ast -> body .root , comment );
2360
2407
}
2361
2408
else {
2362
- pm_string_mapped_init (& input , RSTRING_PTR (opt -> script_name ));
2363
- pm_options_filepath_set (& options , RSTRING_PTR (opt -> script_name ));
2409
+ tree = prism_dump_tree (& pm_input , & pm_options );
2364
2410
}
2365
-
2366
- pm_parser_t parser ;
2367
- pm_parser_init (& parser , pm_string_source (& input ), pm_string_length (& input ), & options );
2368
-
2369
- pm_node_t * node = pm_parse (& parser );
2370
- pm_buffer_t output_buffer = { 0 };
2371
-
2372
- pm_prettyprint (& output_buffer , & parser , node );
2373
- rb_io_write (rb_stdout , rb_str_new ((const char * ) output_buffer .value , output_buffer .length ));
2374
- rb_io_flush (rb_stdout );
2375
-
2376
- pm_buffer_free (& output_buffer );
2377
- pm_node_destroy (& parser , node );
2378
- pm_parser_free (& parser );
2379
-
2380
- pm_string_free (& input );
2381
- pm_options_free (& options );
2382
- }
2383
-
2384
- if (dump & (DUMP_BIT (parsetree )|DUMP_BIT (parsetree_with_comment ))) {
2385
- rb_io_write (rb_stdout , rb_parser_dump_tree (ast -> body .root , dump & DUMP_BIT (parsetree_with_comment )));
2411
+ rb_io_write (rb_stdout , tree );
2386
2412
rb_io_flush (rb_stdout );
2387
2413
dump &= ~DUMP_BIT (parsetree )& ~DUMP_BIT (parsetree_with_comment );
2388
2414
if (!dump ) {
2389
- rb_ast_dispose ( ast );
2415
+ dispose_result ( );
2390
2416
return Qtrue ;
2391
2417
}
2392
2418
}
2393
2419
2394
2420
{
2395
2421
VALUE path = Qnil ;
2396
2422
if (!opt -> e_script && strcmp (opt -> script , "-" )) {
2397
- path = rb_realpath_internal (Qnil , script_name , 1 );
2423
+ path = rb_realpath_internal (Qnil , opt -> script_name , 1 );
2398
2424
#if UTF8_PATH
2399
2425
if (uenc != lenc ) {
2400
2426
path = str_conv_enc (path , uenc , lenc );
@@ -2405,41 +2431,17 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
2405
2431
}
2406
2432
}
2407
2433
2434
+ bool optimize = !(dump & DUMP_BIT (insns_without_opt ));
2408
2435
2409
- if ((* rb_ruby_prism_ptr ())) {
2410
- pm_string_t input ;
2411
- pm_options_t options = { 0 };
2412
-
2413
- if (strcmp (opt -> script , "-" ) == 0 ) {
2414
- int xflag = opt -> xflag ;
2415
- VALUE rb_source = open_load_file (opt -> script_name , & xflag );
2416
- opt -> xflag = xflag != 0 ;
2417
-
2418
- rb_warn ("Prism support for streaming code from stdin is not currently supported" );
2419
- pm_string_constant_init (& input , RSTRING_PTR (rb_source ), RSTRING_LEN (rb_source ));
2420
- pm_options_filepath_set (& options , RSTRING_PTR (opt -> script_name ));
2421
- }
2422
- else if (opt -> e_script ) {
2423
- pm_string_constant_init (& input , RSTRING_PTR (opt -> e_script ), RSTRING_LEN (opt -> e_script ));
2424
- pm_options_filepath_set (& options , "-e" );
2425
- }
2426
- else {
2427
- pm_string_mapped_init (& input , RSTRING_PTR (opt -> script_name ));
2428
- pm_options_filepath_set (& options , RSTRING_PTR (opt -> script_name ));
2429
- }
2430
-
2431
- iseq = rb_iseq_new_main_prism (& input , & options , path );
2432
- ruby_opt_init (opt );
2433
-
2434
- pm_string_free (& input );
2435
- pm_options_free (& options );
2436
+ if (!ast ) {
2437
+ iseq = rb_iseq_new_main_prism (& pm_input , & pm_options , path );
2436
2438
}
2437
2439
else {
2438
2440
rb_binding_t * toplevel_binding ;
2439
2441
GetBindingPtr (rb_const_get (rb_cObject , rb_intern ("TOPLEVEL_BINDING" )),
2440
2442
toplevel_binding );
2441
2443
const struct rb_block * base_block = toplevel_context (toplevel_binding );
2442
- iseq = rb_iseq_new_main (& ast -> body , opt -> script_name , path , vm_block_iseq (base_block ), !( dump & DUMP_BIT ( insns_without_opt )) );
2444
+ iseq = rb_iseq_new_main (& ast -> body , opt -> script_name , path , vm_block_iseq (base_block ), optimize );
2443
2445
rb_ast_dispose (ast );
2444
2446
}
2445
2447
}
0 commit comments