1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Parse the source in SCRIPT_LINES__ as array

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65653 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-11-10 11:40:33 +00:00
parent 907ae13cf6
commit 6e610f5ea7
3 changed files with 57 additions and 8 deletions

26
ast.c
View file

@ -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)
@ -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
@ -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);

1
node.h
View file

@ -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);

38
parse.y
View file

@ -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;
@ -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);
}
@ -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;
@ -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