ruby-changes:66191
From: Yusuke <ko1@a...>
Date: Thu, 13 May 2021 00:17:31 +0900 (JST)
Subject: [ruby-changes:66191] 31794d2e73 (master): parse.y: Allow "command" syntax in endless method definition
https://git.ruby-lang.org/ruby.git/commit/?id=31794d2e73 From 31794d2e733e081e4e221f27aff6380393981681 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh <mame@r...> Date: Thu, 13 May 2021 00:14:50 +0900 Subject: parse.y: Allow "command" syntax in endless method definition This change allows `def hello = puts "Hello"` without parentheses. Note that `private def hello = puts "Hello"` does not parse for technical reason. [Feature #17398] --- parse.y | 46 ++++++++++++++++++++++++++++++++++++++++++++++ test/ruby/test_syntax.rb | 25 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/parse.y b/parse.y index 6909e9c..fc1fbd7 100644 --- a/parse.y +++ b/parse.y @@ -1629,6 +1629,52 @@ command_asgn : lhs '=' lex_ctxt command_rhs https://github.com/ruby/ruby/blob/trunk/parse.y#L1629 /*% %*/ /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/ } + | defn_head f_opt_paren_args '=' command + { + endless_method_name(p, $<node>1, &@1); + restore_defun(p, $<node>1->nd_defn); + /*%%%*/ + $$ = set_defun_body(p, $1, $2, $4, &@$); + /*% %*/ + /*% ripper: def!(get_value($1), $2, $4) %*/ + local_pop(p); + } + | defn_head f_opt_paren_args '=' command modifier_rescue arg + { + endless_method_name(p, $<node>1, &@1); + restore_defun(p, $<node>1->nd_defn); + /*%%%*/ + $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6); + $$ = set_defun_body(p, $1, $2, $4, &@$); + /*% %*/ + /*% ripper: def!(get_value($1), $2, rescue_mod!($4, $6)) %*/ + local_pop(p); + } + | defs_head f_opt_paren_args '=' command + { + endless_method_name(p, $<node>1, &@1); + restore_defun(p, $<node>1->nd_defn); + /*%%%*/ + $$ = set_defun_body(p, $1, $2, $4, &@$); + /*% + $1 = get_value($1); + %*/ + /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/ + local_pop(p); + } + | defs_head f_opt_paren_args '=' command modifier_rescue arg + { + endless_method_name(p, $<node>1, &@1); + restore_defun(p, $<node>1->nd_defn); + /*%%%*/ + $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6); + $$ = set_defun_body(p, $1, $2, $4, &@$); + /*% + $1 = get_value($1); + %*/ + /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, rescue_mod!($4, $6)) %*/ + local_pop(p); + } | backref tOP_ASGN lex_ctxt command_rhs { /*%%%*/ diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index a0cdb5b..31db132 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1462,6 +1462,31 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L1462 assert_syntax_error('def obj.foo=() = 42 rescue nil', error) end + def test_methoddef_endless_command + assert_valid_syntax('def foo = puts "Hello"') + assert_valid_syntax('def foo() = puts "Hello"') + assert_valid_syntax('def foo(x) = puts x') + assert_valid_syntax('def obj.foo = puts "Hello"') + assert_valid_syntax('def obj.foo() = puts "Hello"') + assert_valid_syntax('def obj.foo(x) = puts x') + k = Class.new do + class_eval('def rescued(x) = raise "to be caught" rescue "instance #{x}"') + class_eval('def self.rescued(x) = raise "to be caught" rescue "class #{x}"') + end + assert_equal("class ok", k.rescued("ok")) + assert_equal("instance ok", k.new.rescued("ok")) + + # Current technical limitation: cannot prepend "private" or something for command endless def + error = /syntax error, unexpected string literal/ + error2 = /syntax error, unexpected local variable or method/ + assert_syntax_error('private def foo = puts "Hello"', error) + assert_syntax_error('private def foo() = puts "Hello"', error) + assert_syntax_error('private def foo(x) = puts x', error2) + assert_syntax_error('private def obj.foo = puts "Hello"', error) + assert_syntax_error('private def obj.foo() = puts "Hello"', error) + assert_syntax_error('private def obj.foo(x) = puts x', error2) + end + def test_methoddef_in_cond assert_valid_syntax('while def foo; tap do end; end; break; end') assert_valid_syntax('while def foo a = tap do end; end; break; end') -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/