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

ruby-changes:26327

From: nobu <ko1@a...>
Date: Fri, 14 Dec 2012 17:11:10 +0900 (JST)
Subject: [ruby-changes:26327] nobu:r38378 (trunk): parse.y: fix line number

nobu	2012-12-14 17:11:00 +0900 (Fri, 14 Dec 2012)

  New Revision: 38378

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

  Log:
    parse.y: fix line number
    
    * parse.y (parser_params): parser_tokline to track the line number at
      which token started.  [ruby-dev:46737] [Bug #7559]
    
    * parse.y (fcall): operation with starting line number.
    
    * parse.y (command, primary, method_call): point method name line.
    
    * parse.y (gettable_gen): return token line for __LINE__.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38377)
+++ ChangeLog	(revision 38378)
@@ -1,3 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Dec 14 17:10:19 2012  Nobuyoshi Nakada  <nobu@r...>
+
+	* parse.y (parser_params): parser_tokline to track the line number at
+	  which token started.  [ruby-dev:46737] [Bug #7559]
+
+	* parse.y (fcall): operation with starting line number.
+
+	* parse.y (command, primary, method_call): point method name line.
+
+	* parse.y (gettable_gen): return token line for __LINE__.
+
 Fri Dec 14 16:56:59 2012  Shugo Maeda  <shugo@r...>
 
 	* vm_insnhelper.c (vm_call_super_method): remove volatile introduced
Index: parse.y
===================================================================
--- parse.y	(revision 38377)
+++ parse.y	(revision 38378)
@@ -249,6 +249,7 @@ struct parser_params { https://github.com/ruby/ruby/blob/trunk/parse.y#L249
     char *parser_tokenbuf;
     int parser_tokidx;
     int parser_toksiz;
+    int parser_tokline;
     VALUE parser_lex_input;
     VALUE parser_lex_lastline;
     VALUE parser_lex_nextline;
@@ -323,6 +324,7 @@ static int parser_yyerror(struct parser_ https://github.com/ruby/ruby/blob/trunk/parse.y#L324
 #define tokenbuf		(parser->parser_tokenbuf)
 #define tokidx			(parser->parser_tokidx)
 #define toksiz			(parser->parser_toksiz)
+#define tokline			(parser->parser_tokline)
 #define lex_input		(parser->parser_lex_input)
 #define lex_lastline		(parser->parser_lex_lastline)
 #define lex_nextline		(parser->parser_lex_nextline)
@@ -753,7 +755,7 @@ static void token_info_pop(struct parser https://github.com/ruby/ruby/blob/trunk/parse.y#L755
 %type <node> literal numeric dsym cpath
 %type <node> top_compstmt top_stmts top_stmt
 %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> expr_value arg_value primary_value fcall
 %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
 %type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
@@ -1358,22 +1360,33 @@ cmd_brace_block	: tLBRACE_ARG https://github.com/ruby/ruby/blob/trunk/parse.y#L1360
 		    }
 		;
 
-command		: operation command_args       %prec tLOWEST
+fcall		: operation
 		    {
 		    /*%%%*/
-			$$ = NEW_FCALL($1, $2);
-			fixpos($$, $2);
+			$$ = NEW_FCALL($1, 0);
+			nd_set_line($$, tokline);
+		    /*%
+		    %*/
+		    }
+		;
+
+command		: fcall command_args       %prec tLOWEST
+		    {
+		    /*%%%*/
+			$$ = $1;
+			$$->nd_args = $2;
 		    /*%
 			$$ = dispatch2(command, $1, $2);
 		    %*/
 		    }
-		| operation command_args cmd_brace_block
+		| fcall command_args cmd_brace_block
 		    {
 		    /*%%%*/
 			block_dup_check($2,$3);
-		        $3->nd_iter = NEW_FCALL($1, $2);
+			$1->nd_args = $2;
+		        $3->nd_iter = $1;
 			$$ = $3;
-			fixpos($$, $2);
+			fixpos($$, $1);
 		    /*%
 			$$ = dispatch2(command, $1, $2);
 			$$ = method_add_block($$, $3);
@@ -2715,10 +2728,10 @@ primary		: literal https://github.com/ruby/ruby/blob/trunk/parse.y#L2728
 			$$ = dispatch2(unary, ripper_intern("not"), Qnil);
 		    %*/
 		    }
-		| operation brace_block
+		| fcall brace_block
 		    {
 		    /*%%%*/
-			$2->nd_iter = NEW_FCALL($1, 0);
+			$2->nd_iter = $1;
 			$$ = $2;
 		    /*%
 			$$ = method_arg(dispatch1(fcall, $1), arg_new());
@@ -3559,19 +3572,13 @@ block_call	: command do_block https://github.com/ruby/ruby/blob/trunk/parse.y#L3572
 		    }
 		;
 
-method_call	: operation
-		    {
-		    /*%%%*/
-			$<num>$ = ruby_sourceline;
-		    /*% %*/
-		    }
-		  paren_args
+method_call	: fcall paren_args
 		    {
 		    /*%%%*/
-			$$ = NEW_FCALL($1, $3);
-			nd_set_line($$, $<num>2);
+			$$ = $1;
+			$$->nd_args = $2;
 		    /*%
-			$$ = method_arg(dispatch1(fcall, $1), $3);
+			$$ = method_arg(dispatch1(fcall, $1), $2);
 		    %*/
 		    }
 		| primary_value '.' operation2
@@ -5601,6 +5608,7 @@ static char* https://github.com/ruby/ruby/blob/trunk/parse.y#L5608
 parser_newtok(struct parser_params *parser)
 {
     tokidx = 0;
+    tokline = ruby_sourceline;
     if (!tokenbuf) {
 	toksiz = 60;
 	tokenbuf = ALLOC_N(char, 60);
@@ -8401,7 +8409,7 @@ gettable_gen(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8409
 	return NEW_STR(rb_external_str_new_with_enc(ruby_sourcefile, strlen(ruby_sourcefile),
 						    rb_filesystem_encoding()));
       case keyword__LINE__:
-	return NEW_LIT(INT2FIX(ruby_sourceline));
+	return NEW_LIT(INT2FIX(tokline));
       case keyword__ENCODING__:
 	return NEW_LIT(rb_enc_from_encoding(current_enc));
     }
Index: test/ruby/test_syntax.rb
===================================================================
--- test/ruby/test_syntax.rb	(revision 38377)
+++ test/ruby/test_syntax.rb	(revision 38378)
@@ -194,6 +194,36 @@ class TestSyntax < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L194
     end
   end
 
+  Bug7559 = '[ruby-dev:46737]'
+
+  def test_lineno_command_call_quote
+    expected = __LINE__ + 1
+    actual = caller_lineno "a
+b
+c
+d
+e"
+    assert_equal(expected, actual, "#{Bug7559}: ")
+  end
+
+  def test_lineno_after_heredoc
+    bug7559 = '[ruby-dev:46737]'
+    expected, _, actual = __LINE__, <<eom, __LINE__
+    a
+    b
+    c
+    d
+eom
+    assert_equal(expected, actual, bug7559)
+  end
+
+  def test_lineno_operation_brace_block
+    expected = __LINE__ + 1
+    actual = caller_lineno\
+    {}
+    assert_equal(expected, actual)
+  end
+
   private
 
   def not_label(x) @result = x; @not_label ||= nil end
@@ -227,4 +257,8 @@ class TestSyntax < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L257
       const_set(:SCRIPT_LINES__, script_lines) if script_lines
     end
   end
+
+  def caller_lineno(*)
+    caller_locations(1, 1)[0].lineno
+  end
 end

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

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