summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-10 11:40:33 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-10 11:40:33 +0000
commit6e610f5ea717c824b232fc5f3052dd9d5c1a25e3 (patch)
tree4cb0cbda17385e07d2fb4745af9f0fafa2e5aedd
parent907ae13cf6c8b65039e19a86d3c8fd51a3cd868f (diff)
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
-rw-r--r--ast.c26
-rw-r--r--node.h1
-rw-r--r--parse.y38
3 files changed, 57 insertions, 8 deletions
diff --git a/ast.c b/ast.c
index be2ebd8362..7a037d173b 100644
--- a/ast.c
+++ b/ast.c
@@ -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);
diff --git a/node.h b/node.h
index ad2798b48c..0f5a2a923a 100644
--- a/node.h
+++ b/node.h
@@ -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);
diff --git a/parse.y b/parse.y
index 71392ea00c..b12f3a4429 100644
--- a/parse.y
+++ b/parse.y
@@ -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