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

ruby-changes:21967

From: shugo <ko1@a...>
Date: Mon, 12 Dec 2011 22:44:28 +0900 (JST)
Subject: [ruby-changes:21967] shugo:r34016 (trunk): * parse.y (stmt_or_begin): changed the error message for BEGIN not

shugo	2011-12-12 22:43:56 +0900 (Mon, 12 Dec 2011)

  New Revision: 34016

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

  Log:
    * parse.y (stmt_or_begin): changed the error message for BEGIN not
      at toplevel.  [ruby-dev:44963] [Bug #5738]

  Modified files:
    trunk/ChangeLog
    trunk/parse.y
    trunk/test/ruby/test_beginendblock.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 34015)
+++ ChangeLog	(revision 34016)
@@ -1,3 +1,8 @@
+Mon Dec 12 22:35:39 2011  Shugo Maeda  <shugo@r...>
+
+	* parse.y (stmt_or_begin): changed the error message for BEGIN not
+	  at toplevel.  [ruby-dev:44963] [Bug #5738]
+
 Mon Dec 12 17:29:01 2011  NARUSE, Yui  <naruse@r...>
 
 	* README: Fixed SupportedPlatforms URL in the README.
Index: parse.y
===================================================================
--- parse.y	(revision 34015)
+++ parse.y	(revision 34016)
@@ -686,7 +686,7 @@
 %type <node> words qwords word_list qword_list word
 %type <node> literal numeric dsym cpath
 %type <node> top_compstmt top_stmts top_stmt
-%type <node> bodystmt compstmt stmts stmt expr arg primary command command_call method_call
+%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
 %type <node> expr_value arg_value primary_value
 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
 %type <node> args call_args opt_call_args
@@ -855,9 +855,6 @@
 top_stmt	: stmt
 		| keyword_BEGIN
 		    {
-			if (in_def || in_single) {
-			    yyerror("BEGIN in method");
-			}
 		    /*%%%*/
 			/* local_push(0); */
 		    /*%
@@ -930,7 +927,7 @@
 						  dispatch0(void_stmt));
 		    %*/
 		    }
-		| stmt
+		| stmt_or_begin
 		    {
 		    /*%%%*/
 			$$ = newline_node($1);
@@ -938,7 +935,7 @@
 			$$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
 		    %*/
 		    }
-		| stmts terms stmt
+		| stmts terms stmt_or_begin
 		    {
 		    /*%%%*/
 			$$ = block_append($1, newline_node($3));
@@ -952,6 +949,31 @@
 		    }
 		;
 
+stmt_or_begin	: stmt
+                    {
+			$$ = $1;
+		    }
+                | keyword_BEGIN
+		    {
+			yyerror("BEGIN is permitted only at toplevel");
+		    /*%%%*/
+			/* local_push(0); */
+		    /*%
+		    %*/
+		    }
+		  '{' top_compstmt '}'
+		    {
+		    /*%%%*/
+			ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
+							    $4);
+			/* NEW_PREEXE($4)); */
+			/* local_pop(); */
+			$$ = NEW_BEGIN(0);
+		    /*%
+			$$ = dispatch1(BEGIN, $4);
+		    %*/
+		    }
+
 stmt		: keyword_alias fitem {lex_state = EXPR_FNAME;} fitem
 		    {
 		    /*%%%*/
Index: test/ruby/test_beginendblock.rb
===================================================================
--- test/ruby/test_beginendblock.rb	(revision 34015)
+++ test/ruby/test_beginendblock.rb	(revision 34016)
@@ -33,19 +33,22 @@
   end
 
   def test_begininmethod
-    assert_raise(SyntaxError) do
+    e = assert_raise(SyntaxError) do
       eval("def foo; BEGIN {}; end")
     end
+    assert_match(/BEGIN is permitted only at toplevel/, e.message)
 
-    assert_raise(SyntaxError) do
+    e = assert_raise(SyntaxError) do
       eval('eval("def foo; BEGIN {}; end")')
     end
+    assert_match(/BEGIN is permitted only at toplevel/, e.message)
   end
 
   def test_begininclass
-    assert_raise(SyntaxError) do
+    e = assert_raise(SyntaxError) do
       eval("class TestBeginEndBlock; BEGIN {}; end")
     end
+    assert_match(/BEGIN is permitted only at toplevel/, e.message)
   end
 
   def test_endblockwarn

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

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