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:
parent
907ae13cf6
commit
6e610f5ea7
26
ast.c
26
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_str(VALUE str);
|
||||||
VALUE rb_ast_parse_file(VALUE path);
|
VALUE rb_ast_parse_file(VALUE path);
|
||||||
|
VALUE rb_ast_parse_array(VALUE array);
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
ast_parse_new(void)
|
ast_parse_new(void)
|
||||||
@ -134,6 +135,29 @@ rb_ast_parse_file(VALUE path)
|
|||||||
return ast_parse_done(ast);
|
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 node_children(rb_ast_t*, NODE*);
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
@ -197,7 +221,7 @@ rb_ast_s_of(VALUE module, VALUE body)
|
|||||||
path = rb_iseq_path(iseq);
|
path = rb_iseq_path(iseq);
|
||||||
node_id = iseq->body->location.node_id;
|
node_id = iseq->body->location.node_id;
|
||||||
if (!NIL_P(lines = script_lines(path))) {
|
if (!NIL_P(lines = script_lines(path))) {
|
||||||
node = rb_ast_parse_str(rb_ary_join(lines, Qnil));
|
node = rb_ast_parse_array(lines);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
node = rb_ast_parse_file(path);
|
node = rb_ast_parse_file(path);
|
||||||
|
1
node.h
1
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_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_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_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_cstr(const char*, const char*, int, int);
|
||||||
rb_ast_t *rb_compile_string(const char*, VALUE, int);
|
rb_ast_t *rb_compile_string(const char*, VALUE, int);
|
||||||
|
38
parse.y
38
parse.y
@ -201,7 +201,10 @@ struct parser_params {
|
|||||||
const char *pcur;
|
const char *pcur;
|
||||||
const char *pend;
|
const char *pend;
|
||||||
const char *ptok;
|
const char *ptok;
|
||||||
long gets_ptr;
|
union {
|
||||||
|
long ptr;
|
||||||
|
VALUE (*call)(VALUE, int);
|
||||||
|
} gets_;
|
||||||
enum lex_state_e state;
|
enum lex_state_e state;
|
||||||
/* track the nest level of any parens "()[]{}" */
|
/* track the nest level of any parens "()[]{}" */
|
||||||
int paren_nest;
|
int paren_nest;
|
||||||
@ -4971,14 +4974,14 @@ lex_get_str(struct parser_params *p, VALUE s)
|
|||||||
beg = RSTRING_PTR(s);
|
beg = RSTRING_PTR(s);
|
||||||
len = RSTRING_LEN(s);
|
len = RSTRING_LEN(s);
|
||||||
start = beg;
|
start = beg;
|
||||||
if (p->lex.gets_ptr) {
|
if (p->lex.gets_.ptr) {
|
||||||
if (len == p->lex.gets_ptr) return Qnil;
|
if (len == p->lex.gets_.ptr) return Qnil;
|
||||||
beg += p->lex.gets_ptr;
|
beg += p->lex.gets_.ptr;
|
||||||
len -= p->lex.gets_ptr;
|
len -= p->lex.gets_.ptr;
|
||||||
}
|
}
|
||||||
end = memchr(beg, '\n', len);
|
end = memchr(beg, '\n', len);
|
||||||
if (end) len = ++end - beg;
|
if (end) len = ++end - beg;
|
||||||
p->lex.gets_ptr += len;
|
p->lex.gets_.ptr += len;
|
||||||
return rb_str_subseq(s, beg - start, 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);
|
TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
|
||||||
|
|
||||||
p->lex.gets = lex_get_str;
|
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.input = rb_str_new_frozen(s);
|
||||||
p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
|
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);
|
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 */
|
#endif /* !RIPPER */
|
||||||
|
|
||||||
#define STR_FUNC_ESCAPE 0x01
|
#define STR_FUNC_ESCAPE 0x01
|
||||||
|
Loading…
x
Reference in New Issue
Block a user