mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* parse.y (parser_here_document): dispatch delayed heredoc
contents. based on a patch from Andy Keep in [ruby-core:24855]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25402 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
80eda69d26
commit
b907ee320d
5 changed files with 74 additions and 26 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Tue Oct 20 14:50:51 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* parse.y (parser_here_document): dispatch delayed heredoc
|
||||||
|
contents. based on a patch from Andy Keep in [ruby-core:24855].
|
||||||
|
|
||||||
Mon Oct 19 15:17:29 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Mon Oct 19 15:17:29 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* .gdbinit (rb_method_entry): search method entry by class and id.
|
* .gdbinit (rb_method_entry): search method entry by class and id.
|
||||||
|
|
69
parse.y
69
parse.y
|
@ -4726,21 +4726,42 @@ ripper_yylval_id(ID x)
|
||||||
# define yylval_id() yylval.id
|
# define yylval_id() yylval.id
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef RIPPER
|
#ifndef RIPPER
|
||||||
|
#define ripper_flush(p) (void)(p)
|
||||||
|
#else
|
||||||
#define ripper_flush(p) (p->tokp = p->parser_lex_p)
|
#define ripper_flush(p) (p->tokp = p->parser_lex_p)
|
||||||
|
|
||||||
#define yylval_rval *(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val)
|
#define yylval_rval *(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val)
|
||||||
|
|
||||||
|
static int
|
||||||
|
ripper_has_scan_event(struct parser_params *parser)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp");
|
||||||
|
return lex_p > parser->tokp;
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
ripper_scan_event_val(struct parser_params *parser, int t)
|
||||||
|
{
|
||||||
|
VALUE str = STR_NEW(parser->tokp, lex_p - parser->tokp);
|
||||||
|
VALUE rval = ripper_dispatch1(parser, ripper_token2eventid(t), str);
|
||||||
|
ripper_flush(parser);
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ripper_dispatch_scan_event(struct parser_params *parser, int t)
|
ripper_dispatch_scan_event(struct parser_params *parser, int t)
|
||||||
{
|
{
|
||||||
VALUE str;
|
if (!ripper_has_scan_event(parser)) return;
|
||||||
|
yylval_rval = ripper_scan_event_val(parser, t);
|
||||||
|
}
|
||||||
|
|
||||||
if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp");
|
static void
|
||||||
if (lex_p == parser->tokp) return;
|
ripper_dispatch_ignored_scan_event(struct parser_params *parser, int t)
|
||||||
str = STR_NEW(parser->tokp, lex_p - parser->tokp);
|
{
|
||||||
yylval_rval = ripper_dispatch1(parser, ripper_token2eventid(t), str);
|
if (!ripper_has_scan_event(parser)) return;
|
||||||
ripper_flush(parser);
|
(void)ripper_scan_event_val(parser, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -5227,9 +5248,7 @@ parser_nextc(struct parser_params *parser)
|
||||||
parser->line_count++;
|
parser->line_count++;
|
||||||
lex_pbeg = lex_p = RSTRING_PTR(v);
|
lex_pbeg = lex_p = RSTRING_PTR(v);
|
||||||
lex_pend = lex_p + RSTRING_LEN(v);
|
lex_pend = lex_p + RSTRING_LEN(v);
|
||||||
#ifdef RIPPER
|
|
||||||
ripper_flush(parser);
|
ripper_flush(parser);
|
||||||
#endif
|
|
||||||
lex_lastline = v;
|
lex_lastline = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5885,9 +5904,7 @@ parser_heredoc_identifier(struct parser_params *parser)
|
||||||
len, /* nd_nth */
|
len, /* nd_nth */
|
||||||
lex_lastline); /* nd_orig */
|
lex_lastline); /* nd_orig */
|
||||||
nd_set_line(lex_strterm, ruby_sourceline);
|
nd_set_line(lex_strterm, ruby_sourceline);
|
||||||
#ifdef RIPPER
|
|
||||||
ripper_flush(parser);
|
ripper_flush(parser);
|
||||||
#endif
|
|
||||||
return term == '`' ? tXSTRING_BEG : tSTRING_BEG;
|
return term == '`' ? tXSTRING_BEG : tSTRING_BEG;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5896,12 +5913,6 @@ parser_heredoc_restore(struct parser_params *parser, NODE *here)
|
||||||
{
|
{
|
||||||
VALUE line;
|
VALUE line;
|
||||||
|
|
||||||
#ifdef RIPPER
|
|
||||||
if (!NIL_P(parser->delayed))
|
|
||||||
ripper_dispatch_delayed_token(parser, tSTRING_CONTENT);
|
|
||||||
lex_goto_eol(parser);
|
|
||||||
ripper_dispatch_scan_event(parser, tHEREDOC_END);
|
|
||||||
#endif
|
|
||||||
line = here->nd_orig;
|
line = here->nd_orig;
|
||||||
lex_lastline = line;
|
lex_lastline = line;
|
||||||
lex_pbeg = RSTRING_PTR(line);
|
lex_pbeg = RSTRING_PTR(line);
|
||||||
|
@ -5911,9 +5922,7 @@ parser_heredoc_restore(struct parser_params *parser, NODE *here)
|
||||||
ruby_sourceline = nd_line(here);
|
ruby_sourceline = nd_line(here);
|
||||||
dispose_string(here->nd_lit);
|
dispose_string(here->nd_lit);
|
||||||
rb_gc_force_recycle((VALUE)here);
|
rb_gc_force_recycle((VALUE)here);
|
||||||
#ifdef RIPPER
|
|
||||||
ripper_flush(parser);
|
ripper_flush(parser);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -5938,6 +5947,7 @@ parser_here_document(struct parser_params *parser, NODE *here)
|
||||||
const char *eos, *p, *pend;
|
const char *eos, *p, *pend;
|
||||||
long len;
|
long len;
|
||||||
VALUE str = 0;
|
VALUE str = 0;
|
||||||
|
rb_encoding *enc = parser->enc;
|
||||||
|
|
||||||
eos = RSTRING_PTR(here->nd_lit);
|
eos = RSTRING_PTR(here->nd_lit);
|
||||||
len = RSTRING_LEN(here->nd_lit) - 1;
|
len = RSTRING_LEN(here->nd_lit) - 1;
|
||||||
|
@ -5946,6 +5956,20 @@ parser_here_document(struct parser_params *parser, NODE *here)
|
||||||
if ((c = nextc()) == -1) {
|
if ((c = nextc()) == -1) {
|
||||||
error:
|
error:
|
||||||
compile_error(PARSER_ARG "can't find string \"%s\" anywhere before EOF", eos);
|
compile_error(PARSER_ARG "can't find string \"%s\" anywhere before EOF", eos);
|
||||||
|
#ifdef RIPPER
|
||||||
|
if (NIL_P(parser->delayed)) {
|
||||||
|
ripper_dispatch_scan_event(parser, tSTRING_CONTENT);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (str ||
|
||||||
|
((len = lex_p - parser->tokp) > 0 &&
|
||||||
|
(str = STR_NEW3(parser->tokp, len, enc, func), 1))) {
|
||||||
|
rb_str_append(parser->delayed, str);
|
||||||
|
}
|
||||||
|
ripper_dispatch_delayed_token(parser, tSTRING_CONTENT);
|
||||||
|
}
|
||||||
|
lex_goto_eol(parser);
|
||||||
|
#endif
|
||||||
restore:
|
restore:
|
||||||
heredoc_restore(lex_strterm);
|
heredoc_restore(lex_strterm);
|
||||||
lex_strterm = 0;
|
lex_strterm = 0;
|
||||||
|
@ -5985,7 +6009,6 @@ parser_here_document(struct parser_params *parser, NODE *here)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
|
/* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
|
||||||
rb_encoding *enc = parser->enc;
|
|
||||||
newtok();
|
newtok();
|
||||||
if (c == '#') {
|
if (c == '#') {
|
||||||
switch (c = nextc()) {
|
switch (c = nextc()) {
|
||||||
|
@ -6014,6 +6037,12 @@ parser_here_document(struct parser_params *parser, NODE *here)
|
||||||
} while (!whole_match_p(eos, len, indent));
|
} while (!whole_match_p(eos, len, indent));
|
||||||
str = STR_NEW3(tok(), toklen(), enc, func);
|
str = STR_NEW3(tok(), toklen(), enc, func);
|
||||||
}
|
}
|
||||||
|
#ifdef RIPPER
|
||||||
|
if (!NIL_P(parser->delayed))
|
||||||
|
ripper_dispatch_delayed_token(parser, tSTRING_CONTENT);
|
||||||
|
lex_goto_eol(parser);
|
||||||
|
ripper_dispatch_ignored_scan_event(parser, tHEREDOC_END);
|
||||||
|
#endif
|
||||||
heredoc_restore(lex_strterm);
|
heredoc_restore(lex_strterm);
|
||||||
lex_strterm = NEW_STRTERM(-1, 0, 0);
|
lex_strterm = NEW_STRTERM(-1, 0, 0);
|
||||||
set_yylval_str(str);
|
set_yylval_str(str);
|
||||||
|
|
|
@ -51,7 +51,7 @@ class DummyParser < Ripper
|
||||||
class << self; self; end.class_eval do
|
class << self; self; end.class_eval do
|
||||||
define_method(name) do |*a, &b|
|
define_method(name) do |*a, &b|
|
||||||
result = super(*a, &b)
|
result = super(*a, &b)
|
||||||
yield
|
yield(*a)
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -202,6 +202,20 @@ class TestRipper_ParserEvents < Test::Unit::TestCase
|
||||||
assert_equal true, thru_bodystmt
|
assert_equal true, thru_bodystmt
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_heredoc
|
||||||
|
bug1921 = '[ruby-core:24855]'
|
||||||
|
thru_heredoc_beg = false
|
||||||
|
tree = parse("<<EOS\nheredoc\nEOS\n", :on_heredoc_beg) {thru_heredoc_beg = true}
|
||||||
|
assert_equal true, thru_heredoc_beg
|
||||||
|
assert_match(/string_content\(\),heredoc\n/, tree, bug1921)
|
||||||
|
heredoc = nil
|
||||||
|
parse("<<EOS\nheredoc1\nheredoc2\nEOS\n", :on_string_add) {|n, s| heredoc = s}
|
||||||
|
assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921)
|
||||||
|
heredoc = nil
|
||||||
|
parse("<<-EOS\nheredoc1\nheredoc2\n\tEOS\n", :on_string_add) {|n, s| heredoc = s}
|
||||||
|
assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921)
|
||||||
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
def test_brace_block
|
def test_brace_block
|
||||||
assert_equal true, $thru__brace_block
|
assert_equal true, $thru__brace_block
|
||||||
|
|
|
@ -628,9 +628,9 @@ class TestRipper_ScannerEvents < Test::Unit::TestCase
|
||||||
scan('tstring_content', "<<EOS\nheredoc\nEOS")
|
scan('tstring_content', "<<EOS\nheredoc\nEOS")
|
||||||
assert_equal ["heredoc\n"],
|
assert_equal ["heredoc\n"],
|
||||||
scan('tstring_content', "<<EOS\nheredoc\nEOS\n")
|
scan('tstring_content', "<<EOS\nheredoc\nEOS\n")
|
||||||
assert_equal ["heredoc \n"],
|
assert_equal ["here\ndoc \nEOS \n"],
|
||||||
scan('tstring_content', "<<EOS\nheredoc \nEOS \n")
|
scan('tstring_content', "<<EOS\nhere\ndoc \nEOS \n")
|
||||||
assert_equal ["heredoc\n"],
|
assert_equal ["heredoc\n\tEOS \n"],
|
||||||
scan('tstring_content', "<<-EOS\nheredoc\n\tEOS \n")
|
scan('tstring_content', "<<-EOS\nheredoc\n\tEOS \n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -641,9 +641,9 @@ class TestRipper_ScannerEvents < Test::Unit::TestCase
|
||||||
scan('heredoc_end', "<<EOS\nheredoc\nEOS")
|
scan('heredoc_end', "<<EOS\nheredoc\nEOS")
|
||||||
assert_equal ["EOS\n"],
|
assert_equal ["EOS\n"],
|
||||||
scan('heredoc_end', "<<EOS\nheredoc\nEOS\n")
|
scan('heredoc_end', "<<EOS\nheredoc\nEOS\n")
|
||||||
assert_equal ["EOS \n"],
|
assert_equal [],
|
||||||
scan('heredoc_end', "<<EOS\nheredoc\nEOS \n")
|
scan('heredoc_end', "<<EOS\nheredoc\nEOS \n")
|
||||||
assert_equal ["\tEOS \n"],
|
assert_equal [],
|
||||||
scan('heredoc_end', "<<-EOS\nheredoc\n\tEOS \n")
|
scan('heredoc_end', "<<-EOS\nheredoc\n\tEOS \n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue