From acae5f363dfaedd9c2873cee68c9498da3c072f5 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 17 Jun 2021 23:43:08 +0900 Subject: ast.rb: RubyVM::AST.parse and .of accepts `save_script_lines: true` This option makes the parser keep the original source as an array of the original code lines. This feature exploits the mechanism of `SCRIPT_LINES__` but records only the specified code that is passed to RubyVM::AST.of or .parse, instead of recording all parsed program texts. --- parse.y | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'parse.y') diff --git a/parse.y b/parse.y index 6b42b6b31b..47b63e810d 100644 --- a/parse.y +++ b/parse.y @@ -337,6 +337,7 @@ struct parser_params { unsigned int do_loop: 1; unsigned int do_chomp: 1; unsigned int do_split: 1; + unsigned int save_script_lines: 1; NODE *eval_tree_begin; NODE *eval_tree; @@ -6241,6 +6242,13 @@ yycompile0(VALUE arg) cov = Qtrue; } } + if (p->save_script_lines) { + if (!p->debug_lines) { + p->debug_lines = rb_ary_new(); + } + + RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines); + } parser_prepare(p); #define RUBY_DTRACE_PARSE_HOOK(name) \ @@ -13186,6 +13194,15 @@ rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main p->parent_iseq = base; return vparser; } + +void +rb_parser_save_script_lines(VALUE vparser) +{ + struct parser_params *p; + + TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); + p->save_script_lines = 1; +} #endif #ifdef RIPPER -- cgit v1.2.3