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

ruby-changes:10333

From: mame <ko1@a...>
Date: Fri, 30 Jan 2009 01:51:38 +0900 (JST)
Subject: [ruby-changes:10333] Ruby:r21876 (ruby_1_9_1): * parse.y (top_compstmt, top_stmts, top_stmt): prohibit BEGIN {} in

mame	2009-01-30 01:51:19 +0900 (Fri, 30 Jan 2009)

  New Revision: 21876

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

  Log:
    * parse.y (top_compstmt, top_stmts, top_stmt): prohibit BEGIN {} in
      non-toplevel scope.  [ruby-core:21657]
    * test/ruby/test_beginendblock.rb (test_begininclass): add a test for
      above.

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

Index: ruby_1_9_1/ChangeLog
===================================================================
--- ruby_1_9_1/ChangeLog	(revision 21875)
+++ ruby_1_9_1/ChangeLog	(revision 21876)
@@ -1,3 +1,11 @@
+Fri Jan 30 01:39:27 2009  Yusuke Endoh  <mame@t...>
+
+	* parse.y (top_compstmt, top_stmts, top_stmt): prohibit BEGIN {} in
+	  non-toplevel scope.  [ruby-core:21657]
+
+	* test/ruby/test_beginendblock.rb (test_begininclass): add a test for
+	  above.
+
 Wed Jan 28 22:51:12 2009  NAKAMURA Usaku  <usa@r...>
 
 	* ext/zlib/zlib.c (zstream_run): desperately guard the variable.
Index: ruby_1_9_1/parse.y
===================================================================
--- ruby_1_9_1/parse.y	(revision 21875)
+++ ruby_1_9_1/parse.y	(revision 21876)
@@ -667,6 +667,7 @@
 %type <node> string_contents xstring_contents string_content
 %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> expr_value arg_value primary_value
 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
@@ -768,7 +769,7 @@
 		    /*%
 		    %*/
 		    }
-		  compstmt
+		  top_compstmt
 		    {
 		    /*%%%*/
 			if ($2 && !compile_for_eval) {
@@ -791,6 +792,73 @@
 		    }
 		;
 
+top_compstmt	: top_stmts opt_terms
+		    {
+		    /*%%%*/
+			void_stmts($1);
+			fixup_nodes(&deferred_nodes);
+		    /*%
+		    %*/
+			$$ = $1;
+		    }
+		;
+
+top_stmts	: none
+                    {
+		    /*%%%*/
+			$$ = NEW_BEGIN(0);
+		    /*%
+			$$ = dispatch2(stmts_add, dispatch0(stmts_new),
+						  dispatch0(void_stmt));
+		    %*/
+		    }
+		| top_stmt
+		    {
+		    /*%%%*/
+			$$ = newline_node($1);
+		    /*%
+			$$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
+		    %*/
+		    }
+		| top_stmts terms top_stmt
+		    {
+		    /*%%%*/
+			$$ = block_append($1, newline_node($3));
+		    /*%
+			$$ = dispatch2(stmts_add, $1, $3);
+		    %*/
+		    }
+		| error top_stmt
+		    {
+			$$ = remove_begin($2);
+		    }
+		;
+
+top_stmt	: stmt
+		| keyword_BEGIN
+		    {
+			if (in_def || in_single) {
+			    yyerror("BEGIN in method");
+			}
+		    /*%%%*/
+			/* 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);
+		    %*/
+		    }
+		;
+
 bodystmt	: compstmt
 		  opt_rescue
 		  opt_else
@@ -964,28 +1032,6 @@
 			$$ = dispatch2(rescue_mod, $3, $1);
 		    %*/
 		    }
-		| keyword_BEGIN
-		    {
-			if (in_def || in_single) {
-			    yyerror("BEGIN in method");
-			}
-		    /*%%%*/
-			/* local_push(0); */
-		    /*%
-		    %*/
-		    }
-		  '{' compstmt '}'
-		    {
-		    /*%%%*/
-			ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
-							    $4);
-			/* NEW_PREEXE($4)); */
-			/* local_pop(); */
-			$$ = NEW_BEGIN(0);
-		    /*%
-			$$ = dispatch1(BEGIN, $4);
-		    %*/
-		    }
 		| keyword_END '{' compstmt '}'
 		    {
 			if (in_def || in_single) {
Index: ruby_1_9_1/test/ruby/test_beginendblock.rb
===================================================================
--- ruby_1_9_1/test/ruby/test_beginendblock.rb	(revision 21875)
+++ ruby_1_9_1/test/ruby/test_beginendblock.rb	(revision 21876)
@@ -41,6 +41,12 @@
     end
   end
 
+  def test_begininclass
+    assert_raise(SyntaxError) do
+      eval("class TestBeginEndBlock; BEGIN {}; end")
+    end
+  end
+
   def test_endblockwarn
     ruby = EnvUtil.rubybin
     # Use Tempfile to create temporary file path.

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

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