summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--parse.y9
-rw-r--r--test/ripper/test_parser_events.rb3
-rw-r--r--test/ruby/test_parse.rb19
4 files changed, 34 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 0a2afc63fa..86f2817460 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed May 13 11:13:40 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * parse.y (parse_gvar): separate message for gvar without
+ non-space characters from message for invalid identitirs.
+
Tue May 12 22:18:27 2015 Masaki Matsushita <glass.saga@gmail.com>
* enum.c (enum_to_a): fix incompatibility introduced in r50457.
diff --git a/parse.y b/parse.y
index 63c8d45480..54850e7e63 100644
--- a/parse.y
+++ b/parse.y
@@ -7675,8 +7675,13 @@ parse_gvar(struct parser_params *parser, const enum lex_state_e last_state)
default:
if (!parser_is_identchar()) {
- pushback(c);
- compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c);
+ if (c == -1 || ISSPACE(c)) {
+ compile_error(PARSER_ARG "`$' without identifiers is not allowed as a global variable name");
+ }
+ else {
+ pushback(c);
+ compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c);
+ }
return 0;
}
case '0':
diff --git a/test/ripper/test_parser_events.rb b/test/ripper/test_parser_events.rb
index 08296a241e..6f1fc92238 100644
--- a/test/ripper/test_parser_events.rb
+++ b/test/ripper/test_parser_events.rb
@@ -1219,14 +1219,17 @@ class TestRipper::ParserEvents < Test::Unit::TestCase
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('@%'))
+ assert_equal("`@' without identifiers 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('@@%'))
+ assert_equal("`@@' without identifiers 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('$%'))
+ assert_equal("`$' without identifiers is not allowed as a global variable name", compile_error('$'))
end
end if ripper_test
diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb
index 2d51ff3453..216cdc083b 100644
--- a/test/ruby/test_parse.rb
+++ b/test/ruby/test_parse.rb
@@ -375,6 +375,25 @@ class TestParse < Test::Unit::TestCase
assert_nothing_raised { eval(':""') }
end
+ def assert_disallowed_variable(type, noname, *invalid)
+ assert_syntax_error(noname, "`#{noname}' without identifiers is not allowed as #{type} variable name")
+ invalid.each do |name|
+ assert_syntax_error(name, "`#{name}' is not allowed as #{type} variable name")
+ end
+ end
+
+ def test_disallowed_instance_variable
+ assert_disallowed_variable("an instance", *%w[@ @1 @.])
+ end
+
+ def test_disallowed_class_variable
+ assert_disallowed_variable("a class", *%w[@@ @@1 @@.])
+ end
+
+ def test_disallowed_gloal_variable
+ assert_disallowed_variable("a global", *%w[$ $%])
+ end
+
def test_arg2
o = Object.new