Skip to content

Commit 3e1021b

Browse files
committed
Make default parser enum and define getter/setter
1 parent 4e219d8 commit 3e1021b

File tree

10 files changed

+35
-23
lines changed

10 files changed

+35
-23
lines changed

Diff for: common.mk

+1
Original file line numberDiff line numberDiff line change
@@ -19522,6 +19522,7 @@ version.$(OBJEXT): $(top_srcdir)/internal/cmdlineopt.h
1952219522
version.$(OBJEXT): $(top_srcdir)/internal/compilers.h
1952319523
version.$(OBJEXT): $(top_srcdir)/internal/gc.h
1952419524
version.$(OBJEXT): $(top_srcdir)/internal/imemo.h
19525+
version.$(OBJEXT): $(top_srcdir)/internal/parse.h
1952519526
version.$(OBJEXT): $(top_srcdir)/internal/sanitizers.h
1952619527
version.$(OBJEXT): $(top_srcdir)/internal/serial.h
1952719528
version.$(OBJEXT): $(top_srcdir)/internal/static_assert.h

Diff for: internal/parse.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
#include "internal/static_assert.h"
1414

1515
// The default parser to use for Ruby code.
16-
// 0: parse.y
17-
// 1: Prism
18-
#ifndef RB_DEFAULT_PARSER
19-
#define RB_DEFAULT_PARSER 1
20-
#endif
16+
typedef enum {
17+
RB_DEFAULT_PARSER_PARSE_Y,
18+
RB_DEFAULT_PARSER_PRISM,
19+
} ruby_default_parser_enum;
20+
21+
ruby_default_parser_enum rb_ruby_default_parser(void);
22+
void rb_ruby_default_parser_set(ruby_default_parser_enum parser);
23+
24+
#define rb_ruby_prism_p() (rb_ruby_default_parser() == RB_DEFAULT_PARSER_PRISM)
2125

2226
#ifdef UNIVERSAL_PARSER
2327
#define rb_encoding const void

Diff for: iseq.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ iseqw_s_compile_parser(int argc, VALUE *argv, VALUE self, bool prism)
15641564
static VALUE
15651565
iseqw_s_compile(int argc, VALUE *argv, VALUE self)
15661566
{
1567-
return iseqw_s_compile_parser(argc, argv, self, *rb_ruby_prism_ptr());
1567+
return iseqw_s_compile_parser(argc, argv, self, rb_ruby_prism_p());
15681568
}
15691569

15701570
/*

Diff for: load.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ load_iseq_eval(rb_execution_context_t *ec, VALUE fname)
743743
rb_thread_t *th = rb_ec_thread_ptr(ec);
744744
VALUE realpath_map = get_loaded_features_realpath_map(th->vm);
745745

746-
if (*rb_ruby_prism_ptr()) {
746+
if (rb_ruby_prism_p()) {
747747
pm_parse_result_t result = { 0 };
748748
result.options.line = 1;
749749
result.node.coverage_enabled = 1;

Diff for: mini_builtin.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *ta
5858
.debug_level = 0,
5959
};
6060

61-
if (*rb_ruby_prism_ptr()) {
61+
if (rb_ruby_prism_p()) {
6262
pm_parse_result_t result = { 0 };
6363
pm_prelude_load(&result, name_str, code, start_line);
6464

Diff for: prism_compile.h

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ typedef struct pm_scope_node {
6565

6666
void pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_t *previous);
6767
void pm_scope_node_destroy(pm_scope_node_t *scope_node);
68-
bool *rb_ruby_prism_ptr(void);
6968

7069
typedef struct {
7170
/** The parser that will do the actual parsing. */

Diff for: ruby.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1430,10 +1430,10 @@ proc_long_options(ruby_cmdline_options_t *opt, const char *s, long argc, char **
14301430
}
14311431
else if (is_option_with_arg("parser", Qfalse, Qtrue)) {
14321432
if (strcmp("prism", s) == 0) {
1433-
*rb_ruby_prism_ptr() = true;
1433+
rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PRISM);
14341434
}
14351435
else if (strcmp("parse.y", s) == 0) {
1436-
*rb_ruby_prism_ptr() = false;
1436+
rb_ruby_default_parser_set(RB_DEFAULT_PARSER_PARSE_Y);
14371437
}
14381438
else {
14391439
rb_raise(rb_eRuntimeError, "unknown parser %s", s);
@@ -2522,7 +2522,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
25222522
rb_enc_associate(opt->e_script, eenc);
25232523
}
25242524

2525-
if (!(*rb_ruby_prism_ptr())) {
2525+
if (!rb_ruby_prism_p()) {
25262526
ast_value = process_script(opt);
25272527
if (!(result.ast = rb_ruby_ast_data_get(ast_value))) return Qfalse;
25282528
}

Diff for: version.c

+18-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
**********************************************************************/
1111

1212
#include "internal/cmdlineopt.h"
13+
#include "internal/parse.h"
1314
#include "ruby/ruby.h"
1415
#include "version.h"
1516
#include "vm_core.h"
@@ -141,7 +142,22 @@ Init_version(void)
141142

142143
int ruby_mn_threads_enabled;
143144

144-
bool * rb_ruby_prism_ptr(void);
145+
#ifndef RB_DEFAULT_PARSER
146+
#define RB_DEFAULT_PARSER RB_DEFAULT_PARSER_PRISM
147+
#endif
148+
static ruby_default_parser_enum default_parser = RB_DEFAULT_PARSER;
149+
150+
ruby_default_parser_enum
151+
rb_ruby_default_parser(void)
152+
{
153+
return default_parser;
154+
}
155+
156+
void
157+
rb_ruby_default_parser_set(ruby_default_parser_enum parser)
158+
{
159+
default_parser = parser;
160+
}
145161

146162
static void
147163
define_ruby_description(const char *const jit_opt)
@@ -158,7 +174,7 @@ define_ruby_description(const char *const jit_opt)
158174
# define append(s) (n += (int)strlcpy(desc + n, s, sizeof(desc) - n), assert(n < sizeof(desc)))
159175
if (*jit_opt) append(jit_opt);
160176
if (ruby_mn_threads_enabled) append(" +MN");
161-
if (*rb_ruby_prism_ptr()) append(" +PRISM");
177+
if (rb_ruby_prism_p()) append(" +PRISM");
162178
append(ruby_description + ruby_description_opt_point);
163179
# undef append
164180

Diff for: vm.c

-8
Original file line numberDiff line numberDiff line change
@@ -4456,14 +4456,6 @@ rb_ruby_verbose_ptr(void)
44564456
return &cr->verbose;
44574457
}
44584458

4459-
static bool prism = (RB_DEFAULT_PARSER == 1);
4460-
4461-
bool *
4462-
rb_ruby_prism_ptr(void)
4463-
{
4464-
return &prism;
4465-
}
4466-
44674459
VALUE *
44684460
rb_ruby_debug_ptr(void)
44694461
{

Diff for: vm_eval.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ static const rb_iseq_t *
17871787
eval_make_iseq(VALUE src, VALUE fname, int line,
17881788
const struct rb_block *base_block)
17891789
{
1790-
if (*rb_ruby_prism_ptr()) {
1790+
if (rb_ruby_prism_p()) {
17911791
return pm_eval_make_iseq(src, fname, line, base_block);
17921792
}
17931793
const VALUE parser = rb_parser_new();

0 commit comments

Comments
 (0)