summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-10-21 09:34:09 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-10-21 09:34:09 +0000
commita76fdea9fc923583461ee9149120a677fe6a8bae (patch)
treee32da41d359126f5ccc7bff7f5431fa76276b8a4
parent5fe691934438438e33cde5890bc3b6696537e538 (diff)
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52208 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--parse.y25
-rw-r--r--test/ruby/test_literal.rb28
3 files changed, 50 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 97f00ae..af59f93 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Oct 21 18:34:06 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@marc-andre.ca>
* lib/prime.rb: Add basic argument checking to Prime.prime?
diff --git a/parse.y b/parse.y
index edb9fb2..21d6131 100644
--- a/parse.y
+++ b/parse.y
@@ -6978,6 +6978,7 @@ magic_comment_marker(const char *str, long len)
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_params *parser, const char *str, long len)
: (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_params *parser, const char *str, long len)
}
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_params *parser, const char *str, long len)
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);
diff --git a/test/ruby/test_literal.rb b/test/ruby/test_literal.rb
index 0d65eba..e523f11 100644
--- a/test/ruby/test_literal.rb
+++ b/test/ruby/test_literal.rb
@@ -123,14 +123,38 @@ class TestRubyLiteral < Test::Unit::TestCase
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