ruby-changes:55044
From: nobu <ko1@a...>
Date: Thu, 14 Mar 2019 14:57:47 +0900 (JST)
Subject: [ruby-changes:55044] nobu:r67251 (trunk): parse.y: show invalid global variable line
nobu 2019-03-14 14:57:42 +0900 (Thu, 14 Mar 2019) New Revision: 67251 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=67251 Log: parse.y: show invalid global variable line * parse.y (parse_gvar): show the source line erred by invalid global variable, and indicate the variable including the wrong punctuation. Modified files: trunk/parse.y trunk/test/ruby/test_parse.rb Index: parse.y =================================================================== --- parse.y (revision 67250) +++ parse.y (revision 67251) @@ -7480,9 +7480,11 @@ parse_numvar(struct parser_params *p) https://github.com/ruby/ruby/blob/trunk/parse.y#L7480 static enum yytokentype parse_gvar(struct parser_params *p, const enum lex_state_e last_state) { + const char *ptr = p->lex.pcur; register int c; SET_LEX_STATE(EXPR_END); + p->lex.ptok = ptr - 1; /* from '$' */ newtok(p); c = nextc(p); switch (c) { @@ -7560,6 +7562,7 @@ parse_gvar(struct parser_params *p, cons https://github.com/ruby/ruby/blob/trunk/parse.y#L7562 default: if (!parser_is_identchar(p)) { + YYLTYPE loc = RUBY_INIT_YYLLOC(); if (c == -1 || ISSPACE(c)) { compile_error(p, "`$' without identifiers is not allowed as a global variable name"); } @@ -7567,6 +7570,7 @@ parse_gvar(struct parser_params *p, cons https://github.com/ruby/ruby/blob/trunk/parse.y#L7570 pushback(p, c); compile_error(p, "`$%c' is not allowed as a global variable name", c); } + parser_show_error_line(p, &loc); return 0; } case '0': Index: test/ruby/test_parse.rb =================================================================== --- test/ruby/test_parse.rb (revision 67250) +++ test/ruby/test_parse.rb (revision 67251) @@ -377,9 +377,9 @@ class TestParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L377 end def assert_disallowed_variable(type, noname, *invalid) - assert_syntax_error(noname, "`#{noname}' without identifiers is not allowed as #{type} variable name") + assert_syntax_error("a = #{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") + assert_syntax_error("a = #{name}", "`#{name}' is not allowed as #{type} variable name") end end @@ -712,7 +712,9 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L712 $test_parse_foobarbazqux = nil assert_equal(nil, $&) assert_equal(nil, eval('alias $& $preserve_last_match')) - assert_raise(SyntaxError) { eval('$#') } + assert_raise_with_message(SyntaxError, /as a global variable name\na = \$\#\n \^~$/) do + eval('a = $#') + end end def test_invalid_instance_variable -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/