diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-05-30 22:03:33 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-05-30 22:03:33 +0900 |
commit | 92ecf58b1e8ed15f5ec2bf69e3871de4343f71e0 (patch) | |
tree | 79ae69f7500e843157c573437ebeb212d5b51ca1 | |
parent | b0e2b7a5ff0df14b3c8062c31ebb526a73a03763 (diff) | |
download | ruby-92ecf58b1e8ed15f5ec2bf69e3871de4343f71e0.tar.gz |
parse.y: adjust here-doc error token
* parse.y (here_document): adjust token to the here-doc identifier
in compile_error when a here-document misses the closing
identifier.
-rw-r--r-- | parse.y | 11 | ||||
-rw-r--r-- | test/ripper/test_lexer.rb | 10 |
2 files changed, 14 insertions, 7 deletions
@@ -7005,9 +7005,9 @@ heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here) p->lex.pbeg = RSTRING_PTR(line); p->lex.pend = p->lex.pbeg + RSTRING_LEN(line); p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote; + p->lex.ptok = p->lex.pbeg + here->offset - here->quote; p->heredoc_end = p->ruby_sourceline; p->ruby_sourceline = (int)here->sourceline; - token_flush(p); if (p->eofp) p->lex.nextline = Qnil; p->eofp = 0; } @@ -7238,8 +7238,6 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) if ((c = nextc(p)) == -1) { error: - compile_error(p, "can't find string \"%.*s\" anywhere before EOF", - (int)len, eos); #ifdef RIPPER if (!has_delayed_token(p)) { dispatch_scan_event(p, tSTRING_CONTENT); @@ -7264,8 +7262,10 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) } lex_goto_eol(p); #endif - restore: heredoc_restore(p, &p->lex.strterm->u.heredoc); + compile_error(p, "can't find string \"%.*s\" anywhere before EOF", + (int)len, eos); + token_flush(p); p->lex.strterm = 0; SET_LEX_STATE(EXPR_END); return tSTRING_END; @@ -7283,7 +7283,9 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) } else if (whole_match_p(p, eos, len, indent)) { dispatch_heredoc_end(p); + restore: heredoc_restore(p, &p->lex.strterm->u.heredoc); + token_flush(p); p->lex.strterm = 0; SET_LEX_STATE(EXPR_END); return tSTRING_END; @@ -7379,6 +7381,7 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here) yylval.val, str); #endif heredoc_restore(p, &p->lex.strterm->u.heredoc); + token_flush(p); p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0); set_yylval_str(str); #ifndef RIPPER diff --git a/test/ripper/test_lexer.rb b/test/ripper/test_lexer.rb index 4b70915851..c8cc512a33 100644 --- a/test/ripper/test_lexer.rb +++ b/test/ripper/test_lexer.rb @@ -120,17 +120,21 @@ class TestRipper::Lexer < Test::Unit::TestCase message = proc {result.pretty_inspect} expected = [ [[1, 0], :on_heredoc_beg, "<<A", state(:EXPR_BEG)], + [[1, 2], :compile_error, "A", state(:EXPR_BEG), "can't find string \"A\" anywhere before EOF"], [[1, 3], :on_period, ".", state(:EXPR_DOT)], [[1, 4], :on_ident, "upcase", state(:EXPR_ARG)], [[1, 10], :on_nl, "\n", state(:EXPR_BEG)], - [[1, 11], :compile_error, "", state(:EXPR_BEG), "can't find string \"A\" anywhere before EOF"], ] pos = 0 expected.each_with_index do |ex, i| s = result[i] assert_equal ex, s.to_a, message - assert_equal pos, s.pos[1], message - pos += s.tok.bytesize + if pos > s.pos[1] + assert_equal pos, s.pos[1] + s.tok.bytesize, message + else + assert_equal pos, s.pos[1], message + pos += s.tok.bytesize + end end assert_equal pos, code.bytesize assert_equal expected.size, result.size |