From 02b39daef834890981689f1f697c7c25cad55dd6 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 26 May 2019 11:33:40 +0900 Subject: Fix scanner event at invalid syntax * parse.y (parser_yyerror, parser_compile_error): revert r67224 (e5d10cda07b23682e5e4e64d1324e4d3247d4785) "Flush erred token". --- parse.y | 2 -- test/irb/test_color.rb | 2 +- test/ripper/test_scanner_events.rb | 16 ++++++++++++++-- test/ruby/test_iseq.rb | 9 ++++++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/parse.y b/parse.y index 2b34e8ac2f..fcd495969c 100644 --- a/parse.y +++ b/parse.y @@ -5558,7 +5558,6 @@ parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg) { dispatch1(parse_error, STR_NEW2(msg)); ripper_error(p); - token_flush(p); return 0; } @@ -12260,7 +12259,6 @@ parser_compile_error(struct parser_params *p, const char *fmt, ...) rb_long2int(p->lex.pcur - p->lex.pbeg), p->enc, fmt, ap); va_end(ap); - token_flush(p); } static size_t diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb index 3ba0c4c1ac..c25996e976 100644 --- a/test/irb/test_color.rb +++ b/test/irb/test_color.rb @@ -35,7 +35,7 @@ module TestIRB '"foo#{a} #{b}"' => "#{RED}\"#{CLEAR}#{RED}foo#{CLEAR}#{RED}\#{#{CLEAR}a#{RED}}#{CLEAR}#{RED} #{CLEAR}#{RED}\#{#{CLEAR}b#{RED}}#{CLEAR}#{RED}\"#{CLEAR}", '/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e#{RED}}#{CLEAR}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}", "'a\nb'" => "#{RED}'#{CLEAR}#{RED}a#{CLEAR}\n#{RED}b#{CLEAR}#{RED}'#{CLEAR}", - "4.5.6" => "4.5.6", + "4.5.6" => "#{MAGENTA}#{BOLD}4.5.6#{CLEAR}", "[1]]]" => "[1]]]", "\e[0m\n" => "^[[#{BLUE}#{BOLD}0#{CLEAR}m\n", "%w[a b]" => "#{RED}%w[#{CLEAR}#{RED}a#{CLEAR} #{RED}b#{CLEAR}#{RED}]#{CLEAR}", diff --git a/test/ripper/test_scanner_events.rb b/test/ripper/test_scanner_events.rb index bb98e5d3d9..5fab5649ee 100644 --- a/test/ripper/test_scanner_events.rb +++ b/test/ripper/test_scanner_events.rb @@ -19,9 +19,16 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase end end - def scan(target, str) + def scan(target, str, &error) sym = "on_#{target}".intern - Ripper.lex(str).select {|_1,type,_2| type == sym }.map {|_1,_2,tok| tok } + lexer = Ripper::Lexer.new(str) + if error + lexer.singleton_class.class_eval do + define_method(:compile_error, error) + define_method(:on_parse_error, error) + end + end + lexer.lex.select {|_1,type,_2| type == sym }.map {|_1,_2,tok| tok } end def test_tokenize @@ -927,6 +934,11 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase scan('CHAR', "?a") assert_equal [], scan('CHAR', "@ivar") + + assert_equal ["?\\M-H"], scan('CHAR', '?\\M-H') + err = nil + assert_equal ["?\\M"], scan('CHAR', '?\\M ') {|e| err = [__callee__, e]} + assert_equal([:on_parse_error, "Invalid escape character syntax"], err) end def test_label diff --git a/test/ruby/test_iseq.rb b/test/ruby/test_iseq.rb index f2d11cdb7e..dda70ac7dd 100644 --- a/test/ruby/test_iseq.rb +++ b/test/ruby/test_iseq.rb @@ -246,9 +246,12 @@ class TestISeq < Test::Unit::TestCase end end assert_equal([m1, e1.message], [m2, e2.message], feature11951) - e1, *, e2 = e1.message.lines - assert_send([e1, :start_with?, __FILE__]) - assert_send([e2, :start_with?, __FILE__]) + message = e1.message.each_line + message.with_index(1) do |line, i| + next if /^ / =~ line + assert_send([line, :start_with?, __FILE__], + proc {message.map {|l, j| (i == j ? ">" : " ") + l}.join("")}) + end end def test_compile_file_error -- cgit v1.2.3