Skip to content

Commit

Permalink
Move SCRIPT_LINES__ away from parse.y
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Aug 25, 2023
1 parent d9cba2f commit 6aa16f9
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 63 deletions.
23 changes: 16 additions & 7 deletions ast.c
Expand Up @@ -97,7 +97,7 @@ rb_ast_parse_str(VALUE str, VALUE keep_script_lines, VALUE error_tolerant, VALUE

StringValue(str);
VALUE vparser = ast_parse_new();
if (RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser, Qtrue);
if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
ast = rb_parser_compile_string_path(vparser, Qnil, str, 1);
Expand All @@ -121,7 +121,7 @@ rb_ast_parse_file(VALUE path, VALUE keep_script_lines, VALUE error_tolerant, VAL
f = rb_file_open_str(path, "r");
rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
VALUE vparser = ast_parse_new();
if (RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser, Qtrue);
if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
ast = rb_parser_compile_file_path(vparser, Qnil, f, 1);
Expand Down Expand Up @@ -149,7 +149,7 @@ rb_ast_parse_array(VALUE array, VALUE keep_script_lines, VALUE error_tolerant, V

array = rb_check_array_type(array);
VALUE vparser = ast_parse_new();
if (RTEST(keep_script_lines)) rb_parser_keep_script_lines(vparser);
if (RTEST(keep_script_lines)) rb_parser_set_script_lines(vparser, Qtrue);
if (RTEST(error_tolerant)) rb_parser_error_tolerant(vparser);
if (RTEST(keep_tokens)) rb_parser_keep_tokens(vparser);
ast = rb_parser_compile_generic(vparser, lex_array, Qnil, array, 1);
Expand Down Expand Up @@ -184,19 +184,28 @@ node_find(VALUE self, const int node_id)

extern VALUE rb_e_script;

static VALUE
script_lines(VALUE path)
VALUE
rb_script_lines_for(VALUE path, bool add)
{
VALUE hash, lines;
ID script_lines;
CONST_ID(script_lines, "SCRIPT_LINES__");
if (!rb_const_defined_at(rb_cObject, script_lines)) return Qnil;
hash = rb_const_get_at(rb_cObject, script_lines);
if (!RB_TYPE_P(hash, T_HASH)) return Qnil;
lines = rb_hash_lookup(hash, path);
if (!RB_TYPE_P(lines, T_ARRAY)) return Qnil;
if (add) {
rb_hash_aset(hash, path, lines = rb_ary_new());
}
else if (!RB_TYPE_P((lines = rb_hash_lookup(hash, path)), T_ARRAY)) {
return Qnil;
}
return lines;
}
static VALUE
script_lines(VALUE path)
{
return rb_script_lines_for(path, false);
}

static VALUE
node_id_for_backtrace_location(rb_execution_context_t *ec, VALUE module, VALUE location)
Expand Down
14 changes: 7 additions & 7 deletions ast.rb
Expand Up @@ -20,7 +20,7 @@
module RubyVM::AbstractSyntaxTree

# call-seq:
# RubyVM::AbstractSyntaxTree.parse(string, keep_script_lines: false, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
# RubyVM::AbstractSyntaxTree.parse(string, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
#
# Parses the given _string_ into an abstract syntax tree,
# returning the root node of that tree.
Expand Down Expand Up @@ -55,12 +55,12 @@ module RubyVM::AbstractSyntaxTree
#
# Note that parsing continues even after the errored expression.
#
def self.parse string, keep_script_lines: false, error_tolerant: false, keep_tokens: false
def self.parse string, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false
Primitive.ast_s_parse string, keep_script_lines, error_tolerant, keep_tokens
end

# call-seq:
# RubyVM::AbstractSyntaxTree.parse_file(pathname, keep_script_lines: false, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
# RubyVM::AbstractSyntaxTree.parse_file(pathname, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
#
# Reads the file from _pathname_, then parses it like ::parse,
# returning the root node of the abstract syntax tree.
Expand All @@ -72,13 +72,13 @@ def self.parse string, keep_script_lines: false, error_tolerant: false, keep_tok
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-31:3>
#
# See ::parse for explanation of keyword argument meaning and usage.
def self.parse_file pathname, keep_script_lines: false, error_tolerant: false, keep_tokens: false
def self.parse_file pathname, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false
Primitive.ast_s_parse_file pathname, keep_script_lines, error_tolerant, keep_tokens
end

# call-seq:
# RubyVM::AbstractSyntaxTree.of(proc, keep_script_lines: false, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
# RubyVM::AbstractSyntaxTree.of(method, keep_script_lines: false, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
# RubyVM::AbstractSyntaxTree.of(proc, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
# RubyVM::AbstractSyntaxTree.of(method, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false) -> RubyVM::AbstractSyntaxTree::Node
#
# Returns AST nodes of the given _proc_ or _method_.
#
Expand All @@ -93,7 +93,7 @@ def self.parse_file pathname, keep_script_lines: false, error_tolerant: false, k
# # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-3:3>
#
# See ::parse for explanation of keyword argument meaning and usage.
def self.of body, keep_script_lines: false, error_tolerant: false, keep_tokens: false
def self.of body, keep_script_lines: RubyVM.keep_script_lines, error_tolerant: false, keep_tokens: false
Primitive.ast_s_of body, keep_script_lines, error_tolerant, keep_tokens
end

Expand Down
2 changes: 1 addition & 1 deletion internal/parse.h
Expand Up @@ -78,7 +78,7 @@ size_t rb_ruby_parser_memsize(const void *ptr);

void rb_ruby_parser_set_options(rb_parser_t *p, int print, int loop, int chomp, int split);
rb_parser_t *rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main);
void rb_ruby_parser_keep_script_lines(rb_parser_t *p);
void rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines_array);
void rb_ruby_parser_error_tolerant(rb_parser_t *p);
rb_ast_t* rb_ruby_parser_compile_file_path(rb_parser_t *p, VALUE fname, VALUE file, int start);
void rb_ruby_parser_keep_tokens(rb_parser_t *p);
Expand Down
2 changes: 1 addition & 1 deletion internal/ruby_parser.h
Expand Up @@ -19,7 +19,7 @@ VALUE rb_parser_encoding(VALUE);
VALUE rb_parser_set_yydebug(VALUE, VALUE);
void rb_parser_set_options(VALUE, int, int, int, int);
void *rb_parser_load_file(VALUE parser, VALUE name);
void rb_parser_keep_script_lines(VALUE vparser);
void rb_parser_set_script_lines(VALUE vparser, VALUE lines_array);
void rb_parser_error_tolerant(VALUE vparser);
void rb_parser_keep_tokens(VALUE vparser);

Expand Down
1 change: 1 addition & 0 deletions iseq.c
Expand Up @@ -1122,6 +1122,7 @@ rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, V
const rb_iseq_t *outer_scope = rb_iseq_new(NULL, name, name, Qnil, 0, ISEQ_TYPE_TOP);
VALUE outer_scope_v = (VALUE)outer_scope;
rb_parser_set_context(parser, outer_scope, FALSE);
rb_parser_set_script_lines(parser, RBOOL(ruby_vm_keep_script_lines));
RB_GC_GUARD(outer_scope_v);
ast = (*parse)(parser, file, src, ln);
}
Expand Down
60 changes: 15 additions & 45 deletions parse.y
Expand Up @@ -151,26 +151,6 @@ literal_hash(VALUE a)
return rb_iseq_cdhash_hash(a);
}

static ID
script_lines(void)
{
ID script_lines;
CONST_ID(script_lines, "SCRIPT_LINES__");
return script_lines;
}

static int
script_lines_defined(void)
{
return rb_const_defined_at(rb_cObject, script_lines());
}

static VALUE
script_lines_get(void)
{
return rb_const_get_at(rb_cObject, script_lines());
}

static VALUE
syntax_error_new(void)
{
Expand Down Expand Up @@ -550,7 +530,6 @@ struct parser_params {
unsigned int do_loop: 1;
unsigned int do_chomp: 1;
unsigned int do_split: 1;
unsigned int keep_script_lines: 1;
unsigned int error_tolerant: 1;
unsigned int keep_tokens: 1;

Expand Down Expand Up @@ -6844,20 +6823,6 @@ static void parser_prepare(struct parser_params *p);
#ifndef RIPPER
static NODE *parser_append_options(struct parser_params *p, NODE *node);

static VALUE
debug_lines(struct parser_params *p, VALUE fname)
{
if (script_lines_defined()) {
VALUE hash = script_lines_get();
if (RB_TYPE_P(hash, T_HASH)) {
VALUE lines = rb_ary_new();
rb_hash_aset(hash, fname, lines);
return lines;
}
}
return 0;
}

static int
e_option_supplied(struct parser_params *p)
{
Expand All @@ -6873,7 +6838,6 @@ yycompile0(VALUE arg)
int cov = FALSE;

if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
p->debug_lines = debug_lines(p, p->ruby_sourcefile_string);
if (p->debug_lines && p->ruby_sourceline > 0) {
VALUE str = rb_default_rs;
n = p->ruby_sourceline;
Expand All @@ -6887,11 +6851,7 @@ yycompile0(VALUE arg)
}
}

if (p->keep_script_lines || ruby_vm_keep_script_lines) {
if (!p->debug_lines) {
p->debug_lines = rb_ary_new();
}

if (p->debug_lines) {
RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
}

Expand Down Expand Up @@ -13983,9 +13943,19 @@ rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, in
}

void
rb_ruby_parser_keep_script_lines(rb_parser_t *p)
rb_ruby_parser_set_script_lines(rb_parser_t *p, VALUE lines)
{
p->keep_script_lines = 1;
if (!RTEST(lines)) {
lines = Qfalse;
}
else if (lines == Qtrue) {
lines = rb_ary_new();
}
else {
Check_Type(lines, T_ARRAY);
rb_ary_modify(lines);
}
p->debug_lines = lines;
}

void
Expand Down Expand Up @@ -14073,12 +14043,12 @@ rb_parser_error_tolerant(VALUE vparser)
}

void
rb_parser_keep_script_lines(VALUE vparser)
rb_parser_set_script_lines(VALUE vparser, VALUE lines)
{
struct parser_params *p;

TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
rb_ruby_parser_keep_script_lines(p);
rb_ruby_parser_set_script_lines(p, lines);
}

void
Expand Down
8 changes: 8 additions & 0 deletions ruby.c
Expand Up @@ -2397,6 +2397,8 @@ struct load_file_arg {
VALUE f;
};

VALUE rb_script_lines_for(VALUE path, bool add);

static VALUE
load_file_internal(VALUE argp_v)
{
Expand Down Expand Up @@ -2499,6 +2501,12 @@ load_file_internal(VALUE argp_v)
}
rb_parser_set_options(parser, opt->do_print, opt->do_loop,
opt->do_line, opt->do_split);

VALUE lines = rb_script_lines_for(orig_fname, true);
if (!NIL_P(lines)) {
rb_parser_set_script_lines(parser, lines);
}

if (NIL_P(f)) {
f = rb_str_new(0, 0);
rb_enc_associate(f, enc);
Expand Down
4 changes: 2 additions & 2 deletions ruby_parser.c
Expand Up @@ -853,12 +853,12 @@ rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main
}

void
rb_parser_keep_script_lines(VALUE vparser)
rb_parser_set_script_lines(VALUE vparser, VALUE lines)
{
struct ruby_parser *parser;

TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
rb_ruby_parser_keep_script_lines(parser->parser_params);
rb_ruby_parser_set_script_lines(parser->parser_params, lines);
}

void
Expand Down
1 change: 1 addition & 0 deletions vm_eval.c
Expand Up @@ -1681,6 +1681,7 @@ eval_make_iseq(VALUE src, VALUE fname, int line, const rb_binding_t *bind,
}

rb_parser_set_context(parser, parent, FALSE);
rb_parser_set_script_lines(parser, RBOOL(ruby_vm_keep_script_lines));
ast = rb_parser_compile_string_path(parser, fname, src, line);
if (ast->body.root) {
ast->body.coverage_enabled = coverage_enabled;
Expand Down

0 comments on commit 6aa16f9

Please sign in to comment.