ruby-changes:53437
From: nobu <ko1@a...>
Date: Sat, 10 Nov 2018 20:40:38 +0900 (JST)
Subject: [ruby-changes:53437] nobu:r65653 (trunk): Parse the source in SCRIPT_LINES__ as array
nobu 2018-11-10 20:40:33 +0900 (Sat, 10 Nov 2018) New Revision: 65653 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65653 Log: Parse the source in SCRIPT_LINES__ as array Modified files: trunk/ast.c trunk/node.h trunk/parse.y Index: parse.y =================================================================== --- parse.y (revision 65652) +++ parse.y (revision 65653) @@ -201,7 +201,10 @@ struct parser_params { https://github.com/ruby/ruby/blob/trunk/parse.y#L201 const char *pcur; const char *pend; const char *ptok; - long gets_ptr; + union { + long ptr; + VALUE (*call)(VALUE, int); + } gets_; enum lex_state_e state; /* track the nest level of any parens "()[]{}" */ int paren_nest; @@ -4971,14 +4974,14 @@ lex_get_str(struct parser_params *p, VAL https://github.com/ruby/ruby/blob/trunk/parse.y#L4974 beg = RSTRING_PTR(s); len = RSTRING_LEN(s); start = beg; - if (p->lex.gets_ptr) { - if (len == p->lex.gets_ptr) return Qnil; - beg += p->lex.gets_ptr; - len -= p->lex.gets_ptr; + if (p->lex.gets_.ptr) { + if (len == p->lex.gets_.ptr) return Qnil; + beg += p->lex.gets_.ptr; + len -= p->lex.gets_.ptr; } end = memchr(beg, '\n', len); if (end) len = ++end - beg; - p->lex.gets_ptr += len; + p->lex.gets_.ptr += len; return rb_str_subseq(s, beg - start, len); } @@ -5009,7 +5012,7 @@ parser_compile_string(VALUE vparser, VAL https://github.com/ruby/ruby/blob/trunk/parse.y#L5012 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); p->lex.gets = lex_get_str; - p->lex.gets_ptr = 0; + p->lex.gets_.ptr = 0; p->lex.input = rb_str_new_frozen(s); p->lex.pbeg = p->lex.pcur = p->lex.pend = 0; @@ -5084,6 +5087,27 @@ rb_parser_compile_file_path(VALUE vparse https://github.com/ruby/ruby/blob/trunk/parse.y#L5087 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0; return yycompile(vparser, p, fname, start); +} + +static VALUE +lex_generic_gets(struct parser_params *p, VALUE input) +{ + return (*p->lex.gets_.call)(input, p->line_count); +} + +rb_ast_t* +rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start) +{ + struct parser_params *p; + + TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); + + p->lex.gets = lex_generic_gets; + p->lex.gets_.call = lex_gets; + p->lex.input = input; + p->lex.pbeg = p->lex.pcur = p->lex.pend = 0; + + return yycompile(vparser, p, fname, start); } #endif /* !RIPPER */ Index: node.h =================================================================== --- node.h (revision 65652) +++ node.h (revision 65653) @@ -402,6 +402,7 @@ rb_ast_t *rb_parser_compile_string(VALUE https://github.com/ruby/ruby/blob/trunk/node.h#L402 rb_ast_t *rb_parser_compile_file(VALUE, const char*, VALUE, int); rb_ast_t *rb_parser_compile_string_path(VALUE vparser, VALUE fname, VALUE src, int line); rb_ast_t *rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE input, int line); +rb_ast_t *rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int line); rb_ast_t *rb_compile_cstr(const char*, const char*, int, int); rb_ast_t *rb_compile_string(const char*, VALUE, int); Index: ast.c =================================================================== --- ast.c (revision 65652) +++ ast.c (revision 65653) @@ -54,6 +54,7 @@ ast_new_internal(rb_ast_t *ast, NODE *no https://github.com/ruby/ruby/blob/trunk/ast.c#L54 VALUE rb_ast_parse_str(VALUE str); VALUE rb_ast_parse_file(VALUE path); +VALUE rb_ast_parse_array(VALUE array); static VALUE ast_parse_new(void) @@ -134,6 +135,29 @@ rb_ast_parse_file(VALUE path) https://github.com/ruby/ruby/blob/trunk/ast.c#L135 return ast_parse_done(ast); } +VALUE +lex_array(VALUE array, int index) +{ + VALUE str = rb_ary_entry(array, index); + if (!NIL_P(str)) { + StringValue(str); + if (!rb_enc_asciicompat(rb_enc_get(str))) { + rb_raise(rb_eArgError, "invalid source encoding"); + } + } + return str; +} + +VALUE +rb_ast_parse_array(VALUE array) +{ + rb_ast_t *ast = 0; + + array = rb_check_array_type(array); + ast = rb_parser_compile_generic(ast_parse_new(), lex_array, Qnil, array, 1); + return ast_parse_done(ast); +} + static VALUE node_children(rb_ast_t*, NODE*); static VALUE @@ -197,7 +221,7 @@ rb_ast_s_of(VALUE module, VALUE body) https://github.com/ruby/ruby/blob/trunk/ast.c#L221 path = rb_iseq_path(iseq); node_id = iseq->body->location.node_id; if (!NIL_P(lines = script_lines(path))) { - node = rb_ast_parse_str(rb_ary_join(lines, Qnil)); + node = rb_ast_parse_array(lines); } else { node = rb_ast_parse_file(path); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/