Skip to content

Commit

Permalink
Parse the source in SCRIPT_LINES__ as array
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65653 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Nov 10, 2018
1 parent 907ae13 commit 6e610f5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
26 changes: 25 additions & 1 deletion ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ast_new_internal(rb_ast_t *ast, NODE *node)

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)
Expand Down Expand Up @@ -134,6 +135,29 @@ rb_ast_parse_file(VALUE path)
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
Expand Down Expand Up @@ -197,7 +221,7 @@ rb_ast_s_of(VALUE module, VALUE body)
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);
Expand Down
1 change: 1 addition & 0 deletions node.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ rb_ast_t *rb_parser_compile_string(VALUE, const char*, VALUE, int);
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);
Expand Down
38 changes: 31 additions & 7 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ struct parser_params {
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;
Expand Down Expand Up @@ -4971,14 +4974,14 @@ lex_get_str(struct parser_params *p, VALUE s)
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);
}

Expand Down Expand Up @@ -5009,7 +5012,7 @@ parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
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;

Expand Down Expand Up @@ -5085,6 +5088,27 @@ rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)

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 */

#define STR_FUNC_ESCAPE 0x01
Expand Down

0 comments on commit 6e610f5

Please sign in to comment.