[前][次][番号順一覧][スレッド一覧]

ruby-changes:47226

From: nobu <ko1@a...>
Date: Sat, 15 Jul 2017 17:29:09 +0900 (JST)
Subject: [ruby-changes:47226] nobu:r59341 (trunk): parse.y: refine error messages

nobu	2017-07-15 17:29:03 +0900 (Sat, 15 Jul 2017)

  New Revision: 59341

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59341

  Log:
    parse.y: refine error messages
    
    * parse.y (parser_read_escape, parser_tok_hex): refine error
      messages.  point from the backslash up to the invalid char.

  Modified files:
    trunk/parse.y
    trunk/test/ruby/test_parse.rb
Index: parse.y
===================================================================
--- parse.y	(revision 59340)
+++ parse.y	(revision 59341)
@@ -5636,6 +5636,7 @@ parser_tok_hex(struct parser_params *par https://github.com/ruby/ruby/blob/trunk/parse.y#L5636
 
     c = scan_hex(lex_p, 2, numlen);
     if (!*numlen) {
+	parser->tokp = lex_p;
 	yyerror("invalid hex escape");
 	return 0;
     }
@@ -5780,7 +5781,6 @@ parser_read_escape(struct parser_params https://github.com/ruby/ruby/blob/trunk/parse.y#L5781
       case 'M':
 	if (flags & ESCAPE_META) goto eof;
 	if ((c = nextc()) != '-') {
-	    pushback(c);
 	    goto eof;
 	}
 	if ((c = nextc()) == '\\') {
@@ -5794,7 +5794,6 @@ parser_read_escape(struct parser_params https://github.com/ruby/ruby/blob/trunk/parse.y#L5794
 
       case 'C':
 	if ((c = nextc()) != '-') {
-	    pushback(c);
 	    goto eof;
 	}
       case 'c':
@@ -5811,6 +5810,7 @@ parser_read_escape(struct parser_params https://github.com/ruby/ruby/blob/trunk/parse.y#L5810
       eof:
       case -1:
         yyerror("Invalid escape character syntax");
+	pushback(c);
 	return '\0';
 
       default:
@@ -6045,6 +6045,9 @@ parser_tokadd_string(struct parser_param https://github.com/ruby/ruby/blob/trunk/parse.y#L6045
 	}
 	else if (c == '\\') {
 	    const char *beg = lex_p - 1;
+#ifndef RIPPER
+	    parser->tokp = beg;
+#endif
 	    c = nextc();
 	    switch (c) {
 	      case '\n':
Index: test/ruby/test_parse.rb
===================================================================
--- test/ruby/test_parse.rb	(revision 59340)
+++ test/ruby/test_parse.rb	(revision 59341)
@@ -484,21 +484,27 @@ class TestParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L484
   end
 
   def test_string
-    assert_raise(SyntaxError) do
+    mesg = 'from the backslash through the invalid char'
+
+    e = assert_raise_with_message(SyntaxError, /hex escape/) do
       eval '"\xg1"'
     end
+    assert_equal('   ^', e.message.lines.last, mesg)
 
-    assert_raise(SyntaxError) do
+    e = assert_raise(SyntaxError) do
       eval '"\u{1234"'
     end
+    assert_match(' ^~~~~~~', e.message.lines.last, mesg)
 
-    assert_raise(SyntaxError) do
+    e = assert_raise_with_message(SyntaxError, /escape character syntax/) do
       eval '"\M1"'
     end
+    assert_equal(' ^~~', e.message.lines.last, mesg)
 
-    assert_raise(SyntaxError) do
+    e = assert_raise_with_message(SyntaxError, /escape character syntax/) do
       eval '"\C1"'
     end
+    assert_equal(' ^~~', e.message.lines.last, mesg)
 
     assert_equal("\x81", eval('"\C-\M-a"'))
     assert_equal("\177", eval('"\c?"'))

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]