ruby-changes:50727
From: nobu <ko1@a...>
Date: Fri, 23 Mar 2018 10:10:03 +0900 (JST)
Subject: [ruby-changes:50727] nobu:r62902 (trunk): parse.y: k_else in bodystmt
nobu 2018-03-23 10:09:57 +0900 (Fri, 23 Mar 2018) New Revision: 62902 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62902 Log: parse.y: k_else in bodystmt * parse.y (bodystmt): expand opt_else to show the error message at the right place. Modified files: trunk/parse.y trunk/test/ruby/test_parse.rb Index: test/ruby/test_parse.rb =================================================================== --- test/ruby/test_parse.rb (revision 62901) +++ test/ruby/test_parse.rb (revision 62902) @@ -14,7 +14,7 @@ class TestParse < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L14 end def test_else_without_rescue - assert_syntax_error(<<-END, /else without rescue/) + assert_syntax_error(<<-END, %r":#{__LINE__+2}: else without rescue"o, [__FILE__, __LINE__+1]) begin else 42 Index: parse.y =================================================================== --- parse.y (revision 62901) +++ parse.y (revision 62902) @@ -408,6 +408,7 @@ static NODE *new_op_assign(struct parser https://github.com/ruby/ruby/blob/trunk/parse.y#L408 static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc); static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc); static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc); +static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc); static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc); @@ -991,24 +992,23 @@ begin_block : '{' top_compstmt '}' https://github.com/ruby/ruby/blob/trunk/parse.y#L992 bodystmt : compstmt opt_rescue - opt_else + k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}} + compstmt opt_ensure { /*%%%*/ - $$ = $1; - if ($2) { - $$ = NEW_RESCUE($1, $2, $3, &@$); - } - else if ($3) { - compile_error(p, "else without rescue is useless"); - $$ = block_append(p, $$, $3); - } - if ($4) { - $$ = NEW_ENSURE($$, $4, &@$); - } - fixpos($$, $1); + $$ = new_bodystmt(p, $1, $2, $5, $6, &@$); /*% %*/ - /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($3), escape_Qundef($4)) %*/ + /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/ + } + | compstmt + opt_rescue + opt_ensure + { + /*%%%*/ + $$ = new_bodystmt(p, $1, $2, 0, $3, &@$); + /*% %*/ + /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/ } ; @@ -10201,6 +10201,25 @@ var_field(struct parser_params *p, VALUE https://github.com/ruby/ruby/blob/trunk/parse.y#L10201 } #endif +#ifndef RIPPER +static NODE * +new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc) +{ + NODE *result = head; + if (rescue) { + result = NEW_RESCUE(head, rescue, rescue_else, loc); + } + else if (rescue_else) { + result = block_append(p, result, rescue_else); + } + if (ensure) { + result = NEW_ENSURE(result, ensure, loc); + } + fixpos(result, head); + return result; +} +#endif + static void warn_unused_var(struct parser_params *p, struct local_vars *local) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/