Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

A block after the do-block without parenthesis #102

Closed
wants to merge 3 commits into from

3 participants

@pasberth

$ ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]

I think seemingly this source code is no problem:

$ cat unexpected.rb

langs = ["Ruby"]
langs.each_with_object [] do |lang, the_langs|
  the_langs << "The #{lang}"
end.each do |the_lang|
  puts the_lang
end

But syntax error:

$ ruby unexpected.rb
unexpected.rb:4: syntax error, unexpected keyword_do
end.each do |the_lang|
^
unexpected.rb:5: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('

I want to this code behaves like:

$ cat expected.rb

langs = ["Ruby"]
(langs.each_with_object [] do |lang, the_langs|
  the_langs << "The #{lang}"
end).each do |the_lang|
  puts the_lang
end

$ ruby expected.rb
The Ruby

I changed the block_call in the parse.y to a primary-expression.

$ ./ruby -I./lib -I. unexpected.rb
The Ruby

英語に自信がないので日本語でも失礼します。

上の unexpected.rb はぼくには一見なんの問題もなさそうなコードに見えます。
しかしそれは構文エラーです。
ぼくはこのブロックを一次式として扱うべきだと思いました。

@nobu
Collaborator

It makes too many conflicts: 5 shift/reduce, 240 reduce/reduce, and not acceptable straightforwardly.
コンフリクトが多すぎるので、そのままでは受け入れられません。

@nobu nobu closed this
@pasberth

Pardon me. I'm thoughtless.
I did think twice about this, and I thought to syntax about a block after the do-block without parenthesis need not be primary-expression.

thanks!

@szuecs szuecs referenced this pull request from a commit in szuecs/ruby
@nobu nobu * parse.y (block_call): rules for block_call after block_call.
  based on a patch by pasberth ruby#102
  [ruby-dev:45308][Bug #6115]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
c46bdad
@tenderlove tenderlove referenced this pull request from a commit in tenderlove/ruby
@nobu nobu * parse.y (block_call): rules for block_call after block_call.
  based on a patch by pasberth ruby#102
  [ruby-dev:45308][Bug #6115]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
873f7f6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 56 additions and 0 deletions.
  1. +24 −0 parse.y
  2. +32 −0 test/ruby/test_syntax.rb
View
24 parse.y
@@ -3874,6 +3874,30 @@ block_call : command do_block
$$ = method_optarg($$, $4);
%*/
}
+ | block_call '.' operation2 opt_paren_args brace_block
+ {
+ /*%%%*/
+ block_dup_check($4,$5);
+ $5->nd_iter = NEW_CALL($1, $3, $4);
+ $$ = $5;
+ fixpos($$, $1);
+ /*%
+ $$ = dispatch4(command_call, $1, ripper_id2sym('.'), $3, $4);
+ $$ = method_add_block($$, $5);
+ %*/
+ }
+ | block_call '.' operation2 command_args do_block
+ {
+ /*%%%*/
+ block_dup_check($4,$5);
+ $5->nd_iter = NEW_CALL($1, $3, $4);
+ $$ = $5;
+ fixpos($$, $1);
+ /*%
+ $$ = dispatch4(command_call, $1, ripper_id2sym('.'), $3, $4);
+ $$ = method_add_block($$, $5);
+ %*/
+ }
;
method_call : operation
View
32 test/ruby/test_syntax.rb
@@ -61,6 +61,38 @@ def test_newline_in_block_parameters
end
end
end
+
+ def test_do_block_after_do_block_without_parenthesis
+ assert_nothing_raised(SyntaxError) { eval(<<-CODE) }
+ ["elem"].each_with_object [] do
+ end.map do
+ end
+ CODE
+ end
+
+ def test_do_block_after_do_block_without_parenthesis2
+ assert_nothing_raised(SyntaxError) { eval(<<-CODE) }
+ ["elem"].each_with_object [] do
+ end.each_with_object [] do
+ end
+ CODE
+ end
+
+ def test_brace_block_after_do_block_without_parenthesis
+ assert_nothing_raised(SyntaxError) { eval(<<-CODE) }
+ ["elem"].each_with_object [] do
+ end.map {
+ }
+ CODE
+ end
+
+ def test_brace_block_after_do_block_without_parenthesis2
+ assert_nothing_raised(SyntaxError) { eval(<<-CODE) }
+ ["elem"].each_with_object [] do
+ end.each_with_object([]) {
+ }
+ CODE
+ end
private
Something went wrong with that request. Please try again.