Project

General

Profile

Actions

Feature #12686

closed

Allowing a postposed rescue in a method argument

Added by sonots (Naotoshi Seo) about 8 years ago. Updated about 8 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:76958]

Description

On defining my own DSL, I wanted to write a code like:

foo (Integer(ENV['FOO']) rescue nil)

where foo is a method which receives one argument , but I got following error

SyntaxError: (irb):1: syntax error, unexpected modifier_rescue, expecting ')'

In contrast, following codes were valid:

foo ((Integer(ENV['FOO']) rescue nil))

or

foo (begin; Integer(ENV['FOO']); rescue; nil; end)

This is a feature request to allow such a syntax, a postposed rescue in a method argument.

Updated by nobu (Nobuyoshi Nakada) about 8 years ago

It sounds nothing wrong with it, to me.

diff --git i/parse.y w/parse.y
index 17f0f9c..7a14941 100644
--- i/parse.y
+++ w/parse.y
@@ -2659,7 +2659,7 @@ primary		: literal
 			$<val>1 = cmdarg_stack;
 			CMDARG_SET(0);
 		    }
-		  expr {SET_LEX_STATE(EXPR_ENDARG);} rparen
+		  stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
 		    {
 			CMDARG_SET($<val>1);
 		    /*%%%*/
diff --git i/test/ruby/test_syntax.rb w/test/ruby/test_syntax.rb
index ce01358..9b5b9f1 100644
--- i/test/ruby/test_syntax.rb
+++ w/test/ruby/test_syntax.rb
@@ -827,6 +827,10 @@
     end
   end
 
+  def test_stmt_lparen_arg
+    assert_valid_syntax("foo (bar rescue nil)")
+  end
+
   private
 
   def not_label(x) @result = x; @not_label ||= nil end

Updated by nobu (Nobuyoshi Nakada) about 8 years ago

tLPAREN_ARG has been introduced at 1.7, I think we can clear up spaces and parentheses rules now.

Actions #4

Updated by nobu (Nobuyoshi Nakada) about 8 years ago

  • Status changed from Open to Closed

Applied in changeset r55965.


parse.y: lparen_arg statement

  • parse.y (primary): allow parenthesised statement as a method
    argument. [Feature #12686]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0