Skip to content

Commit

Permalink
dispatch delayed heredoc contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 committed Jan 19, 2012
1 parent 22fa49e commit 74c29d7
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions parse.y
Expand Up @@ -4822,21 +4822,42 @@ ripper_yylval_id(ID x)
# define yylval_id() yylval.id
#endif

#ifdef RIPPER
#ifndef RIPPER
#define ripper_flush(p) (void)(p)
#else
#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)

static void
ripper_dispatch_scan_event(struct parser_params *parser, int t)
static int
ripper_has_scan_event(struct parser_params *parser)
{
VALUE str;

if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp");
if (lex_p == parser->tokp) return;
str = STR_NEW(parser->tokp, lex_p - parser->tokp);
yylval_rval = ripper_dispatch1(parser, ripper_token2eventid(t), str);
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
ripper_dispatch_scan_event(struct parser_params *parser, int t)
{
if (!ripper_has_scan_event(parser)) return;
yylval_rval = ripper_scan_event_val(parser, t);
}

static void
ripper_dispatch_ignored_scan_event(struct parser_params *parser, int t)
{
if (!ripper_has_scan_event(parser)) return;
(void)ripper_scan_event_val(parser, t);
}

static void
Expand Down Expand Up @@ -5223,9 +5244,7 @@ parser_nextc(struct parser_params *parser)
parser->line_count++;
lex_pbeg = lex_p = RSTRING_PTR(v);
lex_pend = lex_p + RSTRING_LEN(v);
#ifdef RIPPER
ripper_flush(parser);
#endif
GC_WB(&lex_lastline, v);
}
}
Expand Down Expand Up @@ -5914,9 +5933,7 @@ parser_heredoc_identifier(struct parser_params *parser)
len, /* nd_nth */
lex_lastline)); /* nd_orig */
nd_set_line(lex_strterm, ruby_sourceline);
#ifdef RIPPER
ripper_flush(parser);
#endif
return term == '`' ? tXSTRING_BEG : tSTRING_BEG;
}

Expand All @@ -5925,12 +5942,6 @@ parser_heredoc_restore(struct parser_params *parser, NODE *here)
{
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;
GC_WB(&lex_lastline, line);
lex_pbeg = RSTRING_PTR(line);
Expand All @@ -5940,9 +5951,7 @@ parser_heredoc_restore(struct parser_params *parser, NODE *here)
ruby_sourceline = nd_line(here);
dispose_string(here->nd_lit);
rb_gc_force_recycle((VALUE)here);
#ifdef RIPPER
ripper_flush(parser);
#endif
}

static int
Expand All @@ -5967,6 +5976,7 @@ parser_here_document(struct parser_params *parser, NODE *here)
const char *eos, *p, *pend;
long len;
VALUE str = 0;
rb_encoding *enc = parser->enc;

eos = RSTRING_PTR(here->nd_lit);
len = RSTRING_LEN(here->nd_lit) - 1;
Expand All @@ -5975,6 +5985,20 @@ parser_here_document(struct parser_params *parser, NODE *here)
if ((c = nextc()) == -1) {
error:
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:
heredoc_restore(lex_strterm);
lex_strterm = 0;
Expand Down Expand Up @@ -6014,7 +6038,6 @@ parser_here_document(struct parser_params *parser, NODE *here)
}
else {
/* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
rb_encoding *enc = parser->enc;
newtok();
if (c == '#') {
switch (c = nextc()) {
Expand Down Expand Up @@ -6043,6 +6066,12 @@ parser_here_document(struct parser_params *parser, NODE *here)
} while (!whole_match_p(eos, len, indent));
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);
GC_WB(&lex_strterm, NEW_STRTERM(-1, 0, 0));
set_yylval_str(str);
Expand Down

0 comments on commit 74c29d7

Please sign in to comment.