From 6e86e290e4bd029e3ef53be6c4ac1d52e20538af Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 14 Apr 2020 23:47:40 +0900 Subject: Rightward-assign by ASSOC. [ruby-bugs:15921] --- mrbgems/mruby-compiler/core/parse.y | 23 ++++++++++++++++++++++- test/t/syntax.rb | 9 +++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mrbgems/mruby-compiler/core/parse.y b/mrbgems/mruby-compiler/core/parse.y index c7c3d2a2f..73e928b20 100644 --- a/mrbgems/mruby-compiler/core/parse.y +++ b/mrbgems/mruby-compiler/core/parse.y @@ -1416,7 +1416,7 @@ heredoc_end(parser_state *p) %type singleton string string_fragment string_rep string_interp xstring regexp %type literal numeric cpath symbol -%type top_compstmt top_stmts top_stmt +%type top_compstmt top_stmts top_stmt rassign %type bodystmt compstmt stmts stmt expr arg primary command command_call method_call %type expr_value arg_rhs primary_value %type if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure @@ -1658,9 +1658,30 @@ stmt : keyword_alias fsym {p->lstate = EXPR_FNAME;} fsym { $$ = new_masgn(p, $1, new_array(p, $3)); } + | rassign | expr ; +rassign : arg tASSOC lhs + { + void_expr_error(p, $1); + $$ = new_asgn(p, $3, $1); + } + | arg tASSOC mlhs + { + void_expr_error(p, $1); + $$ = new_masgn(p, $3, $1); + } + | rassign tASSOC lhs + { + $$ = new_asgn(p, $3, $1); + } + | rassign tASSOC mlhs + { + $$ = new_masgn(p, $3, $1); + } + ; + command_asgn : lhs '=' command_rhs { $$ = new_asgn(p, $1, $3); diff --git a/test/t/syntax.rb b/test/t/syntax.rb index 436c06601..a52e0dcea 100644 --- a/test/t/syntax.rb +++ b/test/t/syntax.rb @@ -345,6 +345,15 @@ assert('splat object in assignment') do assert_equal [2], (a = *o) end +assert('right-ward assignment') do + 1 => a + assert_equal(1, a) + 13.divmod(5) => a,b + assert_equal([2,3], [a, b]) + 13.divmod(5) => a,b => c, d + assert_equal([2,3,2,3], [a, b, c, d]) +end + assert('splat object in case statement') do o = Object.new def o.to_a -- cgit v1.2.3