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

ruby-changes:28555

From: nobu <ko1@a...>
Date: Wed, 8 May 2013 13:07:55 +0900 (JST)
Subject: [ruby-changes:28555] nobu:r40607 (trunk): parse.y: fail if invalid name

nobu	2013-05-08 13:07:22 +0900 (Wed, 08 May 2013)

  New Revision: 40607

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40607

  Log:
    parse.y: fail if invalid name
    
    * parse.y (parser_yylex): fail if $, @, @@ are not followed by a valid
      name character.  [ruby-core:54846] [Bug #8375].

  Modified files:
    trunk/ChangeLog
    trunk/parse.y
    trunk/test/ripper/test_parser_events.rb
    trunk/test/ripper/test_scanner_events.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40606)
+++ ChangeLog	(revision 40607)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed May  8 13:07:17 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (parser_yylex): fail if $, @, @@ are not followed by a valid
+	  name character.  [ruby-core:54846] [Bug #8375].
+
 Wed May  8 13:06:31 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* include/ruby/ruby.h (ISGRAPH): add missing macro.
Index: parse.y
===================================================================
--- parse.y	(revision 40606)
+++ parse.y	(revision 40607)
@@ -7876,7 +7876,8 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L7876
 	  default:
 	    if (!parser_is_identchar()) {
 		pushback(c);
-		return '$';
+		compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c);
+		return 0;
 	    }
 	  case '0':
 	    tokadd('$');
@@ -7891,7 +7892,8 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L7892
 	    tokadd('@');
 	    c = nextc();
 	}
-	if (c != -1 && ISDIGIT(c)) {
+	if (c != -1 && (ISDIGIT(c) || !parser_is_identchar())) {
+	    pushback(c);
 	    if (tokidx == 1) {
 		compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c);
 	    }
@@ -7900,10 +7902,6 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L7902
 	    }
 	    return 0;
 	}
-	if (!parser_is_identchar()) {
-	    pushback(c);
-	    return '@';
-	}
 	break;
 
       case '_':
Index: test/ripper/test_parser_events.rb
===================================================================
--- test/ripper/test_parser_events.rb	(revision 40606)
+++ test/ripper/test_parser_events.rb	(revision 40607)
@@ -24,6 +24,10 @@ class TestRipper::ParserEvents < Test::U https://github.com/ruby/ruby/blob/trunk/test/ripper/test_parser_events.rb#L24
     dp.parse.to_s
   end
 
+  def compile_error(str)
+    parse(str, :compile_error) {|e, msg| return msg}
+  end
+
   def test_program
     thru_program = false
     assert_equal '[void()]', parse('', :on_program) {thru_program = true}
@@ -1167,8 +1171,20 @@ class TestRipper::ParserEvents < Test::U https://github.com/ruby/ruby/blob/trunk/test/ripper/test_parser_events.rb#L1171
   end
 
   def test_unterminated_regexp
-    compile_error = false
-    parse('/', :compile_error) {|e, msg| compile_error = msg}
-    assert_equal("unterminated regexp meets end of file", compile_error)
+    assert_equal("unterminated regexp meets end of file", compile_error('/'))
+  end
+
+  def test_invalid_instance_variable_name
+    assert_equal("`@1' is not allowed as an instance variable name", compile_error('@1'))
+    assert_equal("`@%' is not allowed as an instance variable name", compile_error('@%'))
+  end
+
+  def test_invalid_class_variable_name
+    assert_equal("`@@1' is not allowed as a class variable name", compile_error('@@1'))
+    assert_equal("`@@%' is not allowed as a class variable name", compile_error('@@%'))
+  end
+
+  def test_invalid_global_variable_name
+    assert_equal("`$%' is not allowed as a global variable name", compile_error('$%'))
   end
 end if ripper_test
Index: test/ripper/test_scanner_events.rb
===================================================================
--- test/ripper/test_scanner_events.rb	(revision 40606)
+++ test/ripper/test_scanner_events.rb	(revision 40607)
@@ -92,7 +92,7 @@ class TestRipper::ScannerEvents < Test:: https://github.com/ruby/ruby/blob/trunk/test/ripper/test_scanner_events.rb#L92
   def test_location
     assert_location ""
     assert_location " "
-    assert_location "@"
+    assert_location ":"
     assert_location "\n"
     assert_location "\r\n"
     assert_location "\n\n\n\n\n\r\n\n\n"
@@ -842,8 +842,8 @@ class TestRipper::ScannerEvents < Test:: https://github.com/ruby/ruby/blob/trunk/test/ripper/test_scanner_events.rb#L842
   def test_CHAR
     assert_equal [],
                  scan('CHAR', "")
-    assert_equal ["@"],
-                 scan('CHAR', "@")
+    assert_equal ["?a"],
+                 scan('CHAR', "?a")
     assert_equal [],
                  scan('CHAR', "@ivar")
   end

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

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