summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2021-05-13 00:14:50 +0900
committerYusuke Endoh <mame@ruby-lang.org>2021-05-13 00:14:50 +0900
commit31794d2e733e081e4e221f27aff6380393981681 (patch)
tree50bfed072e069e12cad801b33c1e9bb2399c73a5
parent81513c9dab75fa26e02e16945c8886eb6bb9413c (diff)
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]
-rw-r--r--parse.y46
-rw-r--r--test/ruby/test_syntax.rb25
2 files changed, 71 insertions, 0 deletions
diff --git a/parse.y b/parse.y
index 6909e9c777e..fc1fbd7fd92 100644
--- a/parse.y
+++ b/parse.y
@@ -1629,6 +1629,52 @@ command_asgn : lhs '=' lex_ctxt command_rhs
/*% %*/
/*% 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 a0cdb5b6fd2..31db1320fc7 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -1462,6 +1462,31 @@ eom
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')