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

ruby-changes:40127

From: nobu <ko1@a...>
Date: Wed, 21 Oct 2015 18:34:23 +0900 (JST)
Subject: [ruby-changes:40127] nobu:r52208 (trunk): parse.y: magic comment w/o indicators

nobu	2015-10-21 18:34:09 +0900 (Wed, 21 Oct 2015)

  New Revision: 52208

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

  Log:
    parse.y: magic comment w/o indicators
    
    * parse.y (parser_magic_comment): allow a sole magic comment without
      indicators, neither other non-space comments.  [Feature #8976]

  Modified files:
    trunk/ChangeLog
    trunk/parse.y
    trunk/test/ruby/test_literal.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52207)
+++ ChangeLog	(revision 52208)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Oct 21 18:34:06 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (parser_magic_comment): allow a sole magic comment without
+	  indicators, neither other non-space comments.  [Feature #8976]
+
 Tue Oct 20 12:17:56 2015  Marc-Andre Lafortune  <ruby-core@m...>
 
 	* lib/prime.rb: Add basic argument checking to Prime.prime?
Index: parse.y
===================================================================
--- parse.y	(revision 52207)
+++ parse.y	(revision 52208)
@@ -6978,6 +6978,7 @@ magic_comment_marker(const char *str, lo https://github.com/ruby/ruby/blob/trunk/parse.y#L6978
 static int
 parser_magic_comment(struct parser_params *parser, const char *str, long len)
 {
+    int indicator = 0;
     VALUE name = 0, val = 0;
     const char *beg, *end, *vbeg, *vend;
 #define str_copy(_s, _p, _n) ((_s) \
@@ -6986,10 +6987,13 @@ parser_magic_comment(struct parser_param https://github.com/ruby/ruby/blob/trunk/parse.y#L6987
 	: (void)((_s) = STR_NEW((_p), (_n))))
 
     if (len <= 7) return FALSE;
-    if (!(beg = magic_comment_marker(str, len))) return FALSE;
-    if (!(end = magic_comment_marker(beg, str + len - beg))) return FALSE;
-    str = beg;
-    len = end - beg - 3;
+    if (!!(beg = magic_comment_marker(str, len))) {
+	if (!(end = magic_comment_marker(beg, str + len - beg)))
+	    return FALSE;
+	indicator = TRUE;
+	str = beg;
+	len = end - beg - 3;
+    }
 
     /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
     while (len > 0) {
@@ -7017,7 +7021,10 @@ parser_magic_comment(struct parser_param https://github.com/ruby/ruby/blob/trunk/parse.y#L7021
 	}
 	for (end = str; len > 0 && ISSPACE(*str); str++, --len);
 	if (!len) break;
-	if (*str != ':') continue;
+	if (*str != ':') {
+	    if (!indicator) return FALSE;
+	    continue;
+	}
 
 	do str++; while (--len > 0 && ISSPACE(*str));
 	if (!len) break;
@@ -7038,7 +7045,13 @@ parser_magic_comment(struct parser_param https://github.com/ruby/ruby/blob/trunk/parse.y#L7045
 	    for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
 	    vend = str;
 	}
-	while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
+	if (indicator) {
+	    while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
+	}
+	else {
+	    while (len > 0 && (ISSPACE(*str))) --len, str++;
+	    if (len) return FALSE;
+	}
 
 	n = end - beg;
 	str_copy(name, beg, n);
Index: test/ruby/test_literal.rb
===================================================================
--- test/ruby/test_literal.rb	(revision 52207)
+++ test/ruby/test_literal.rb	(revision 52208)
@@ -123,14 +123,38 @@ class TestRubyLiteral < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/ruby/test_literal.rb#L123
 
   def test_frozen_string
     all_assertions do |a|
-      a.for("false") do
+      a.for("false with indicator") do
         str = eval("# -*- frozen-string-literal: false -*-\n""'foo'")
         assert_not_predicate(str, :frozen?)
       end
-      a.for("true") do
+      a.for("true with indicator") do
         str = eval("# -*- frozen-string-literal: true -*-\n""'foo'")
         assert_predicate(str, :frozen?)
       end
+      a.for("false without indicator") do
+        str = eval("# frozen-string-literal: false\n""'foo'")
+        assert_not_predicate(str, :frozen?)
+      end
+      a.for("true without indicator") do
+        str = eval("# frozen-string-literal: true\n""'foo'")
+        assert_predicate(str, :frozen?)
+      end
+      a.for("false with preceding garbage") do
+        str = eval("# x frozen-string-literal: false\n""'foo'")
+        assert_not_predicate(str, :frozen?)
+      end
+      a.for("true with preceding garbage") do
+        str = eval("# x frozen-string-literal: true\n""'foo'")
+        assert_not_predicate(str, :frozen?)
+      end
+      a.for("false with succeeding garbage") do
+        str = eval("# frozen-string-literal: false x\n""'foo'")
+        assert_not_predicate(str, :frozen?)
+      end
+      a.for("true with succeeding garbage") do
+        str = eval("# frozen-string-literal: true x\n""'foo'")
+        assert_not_predicate(str, :frozen?)
+      end
     end
   end
 

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

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