ruby-changes:47131
From: nobu <ko1@a...>
Date: Sat, 1 Jul 2017 11:01:10 +0900 (JST)
Subject: [ruby-changes:47131] nobu:r59246 (trunk): parse.y: f_margs parser events
nobu 2017-07-01 11:01:05 +0900 (Sat, 01 Jul 2017) New Revision: 59246 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59246 Log: parse.y: f_margs parser events * parse.y (f_margs): implemented parser events for massign formal arguments. [ruby-core:81848] [Bug #13701] Modified files: trunk/ext/ripper/lib/ripper/sexp.rb trunk/parse.y trunk/test/ripper/test_parser_events.rb trunk/test/ripper/test_sexp.rb Index: parse.y =================================================================== --- parse.y (revision 59245) +++ parse.y (revision 59246) @@ -3027,7 +3027,6 @@ f_marg : f_norm_arg https://github.com/ruby/ruby/blob/trunk/parse.y#L3027 $$ = assignable($1, 0); /*%%%*/ /*% - $$ = dispatch1(mlhs_paren, $$); %*/ } | tLPAREN f_margs rparen @@ -3082,6 +3081,7 @@ f_margs : f_marg_list https://github.com/ruby/ruby/blob/trunk/parse.y#L3081 $$ = NEW_MASGN($1, NEW_POSTARG($$, $6)); /*% $$ = mlhs_add_star($1, $$); + $$ = mlhs_add_post($$, $6); %*/ } | f_marg_list ',' tSTAR @@ -3097,7 +3097,8 @@ f_margs : f_marg_list https://github.com/ruby/ruby/blob/trunk/parse.y#L3097 /*%%%*/ $$ = NEW_MASGN($1, NEW_POSTARG(-1, $5)); /*% - $$ = mlhs_add_star($1, $5); + $$ = mlhs_add_star($1, Qnil); + $$ = mlhs_add_post($$, $5); %*/ } | tSTAR f_norm_arg @@ -3115,10 +3116,8 @@ f_margs : f_marg_list https://github.com/ruby/ruby/blob/trunk/parse.y#L3116 /*%%%*/ $$ = NEW_MASGN(0, NEW_POSTARG($$, $4)); /*% - #if 0 - TODO: Check me - #endif - $$ = mlhs_add_star($$, $4); + $$ = mlhs_add_star(mlhs_new(), $$); + $$ = mlhs_add_post($$, $4); %*/ } | tSTAR @@ -3135,6 +3134,7 @@ f_margs : f_marg_list https://github.com/ruby/ruby/blob/trunk/parse.y#L3134 $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3)); /*% $$ = mlhs_add_star(mlhs_new(), Qnil); + $$ = mlhs_add_post($$, $3); %*/ } ; Index: ext/ripper/lib/ripper/sexp.rb =================================================================== --- ext/ripper/lib/ripper/sexp.rb (revision 59245) +++ ext/ripper/lib/ripper/sexp.rb (revision 59246) @@ -134,6 +134,18 @@ class Ripper https://github.com/ruby/ruby/blob/trunk/ext/ripper/lib/ripper/sexp.rb#L134 list end + def on_mlhs_paren(list) + [:mlhs, *list] + end + + def on_mlhs_add_star(list, star) + list.push([:rest_param, star]) + end + + def on_mlhs_add_post(list, post) + list.concat(post) + end + PARSER_EVENT_TABLE.each do |event, arity| if /_new\z/ =~ event and arity == 0 alias_method "on_#{event}", :_dispatch_event_new Index: test/ripper/test_sexp.rb =================================================================== --- test/ripper/test_sexp.rb (revision 59245) +++ test/ripper/test_sexp.rb (revision 59246) @@ -60,6 +60,21 @@ eot https://github.com/ruby/ruby/blob/trunk/test/ripper/test_sexp.rb#L60 assert_equal clear_pos(sexp1), clear_pos(sexp2) end + def test_params_mlhs + sexp = Ripper.sexp("proc {|(w, *x, y), z|}") + _, ((mlhs, w, (rest, x), y), z) = search_sexp(:params, sexp) + assert_equal(:mlhs, mlhs) + assert_equal(:@ident, w[0]) + assert_equal("w", w[1]) + assert_equal(:rest_param, rest) + assert_equal(:@ident, x[0]) + assert_equal("x", x[1]) + assert_equal(:@ident, y[0]) + assert_equal("y", y[1]) + assert_equal(:@ident, z[0]) + assert_equal("z", z[1]) + end + def search_sexp(sym, sexp) return sexp if !sexp or sexp[0] == sym sexp.find do |e| Index: test/ripper/test_parser_events.rb =================================================================== --- test/ripper/test_parser_events.rb (revision 59245) +++ test/ripper/test_parser_events.rb (revision 59246) @@ -896,6 +896,84 @@ class TestRipper::ParserEvents < Test::U https://github.com/ruby/ruby/blob/trunk/test/ripper/test_parser_events.rb#L896 assert_equal [nil, nil, nil, nil, nil, "x", nil], arg end + def test_params_mlhs + thru_mlhs = false + tree = parse("proc {|(a, b)|}", :on_mlhs_paren) {thru_mlhs = true} + assert_equal true, thru_mlhs + assert_include(tree, "[mlhs([a,b])]") + end + + def test_params_mlhs_add + thru_mlhs_add = false + tree = parse("proc {|(a, b)|}", :on_mlhs_add) {thru_mlhs_add = true} + assert_equal true, thru_mlhs_add + assert_include(tree, "[mlhs([a,b])]") + end + + def test_params_mlhs_add_star + thru_mlhs_add_star = false + tree = parse("proc {|(a, *b)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true} + assert_equal true, thru_mlhs_add_star + assert_include(tree, "[mlhs([a,*b])]") + thru_mlhs_add_star = false + tree = parse("proc {|(a, *b, c)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true} + assert_equal true, thru_mlhs_add_star + assert_include(tree, "[mlhs([a,*b,c])]") + thru_mlhs_add_star = false + tree = parse("proc {|(a, *, c)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true} + assert_equal true, thru_mlhs_add_star + assert_include(tree, "[mlhs([a,*,c])]") + thru_mlhs_add_star = false + tree = parse("proc {|(*b, c)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true} + assert_equal true, thru_mlhs_add_star + assert_include(tree, "[mlhs([*b,c])]") + thru_mlhs_add_star = false + tree = parse("proc {|(*b)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true} + assert_equal true, thru_mlhs_add_star + assert_include(tree, "[mlhs([*b])]") + end + + def test_params_mlhs_add_post + thru_mlhs_add_post = false + tree = parse("proc {|(a, *b)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true} + assert_equal false, thru_mlhs_add_post + assert_include(tree, "mlhs([a,*b])") + thru_mlhs_add_post = false + tree = parse("proc {|(a, *b, c)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true} + assert_equal true, thru_mlhs_add_post + assert_include(tree, "mlhs([a,*b,c])") + thru_mlhs_add_post = false + tree = parse("proc {|(a, *, c)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true} + assert_equal true, thru_mlhs_add_post + assert_include(tree, "mlhs([a,*,c])") + thru_mlhs_add_post = false + tree = parse("proc {|(*b, c)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true} + assert_equal true, thru_mlhs_add_post + assert_include(tree, "mlhs([*b,c])") + thru_mlhs_add_post = false + tree = parse("proc {|(*, c)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true} + assert_equal true, thru_mlhs_add_post + assert_include(tree, "mlhs([*,c])") + end + + def test_params_mlhs_new + thru_mlhs_new = false + tree = parse("proc {|(a, b)|}", :on_mlhs_new) {thru_mlhs_new = true} + assert_equal true, thru_mlhs_new + assert_include(tree, "[mlhs([a,b])]") + end + + def test_params_mlhs_paren + thru_mlhs_paren = 0 + tree = parse("proc {|(a, b)|}", :on_mlhs_paren) {thru_mlhs_paren += 1} + assert_equal 1, thru_mlhs_paren + assert_include(tree, "[mlhs([a,b])]") + thru_mlhs_paren = 0 + tree = parse("proc {|((a, b))|}", :on_mlhs_paren) {thru_mlhs_paren += 1} + assert_equal 2, thru_mlhs_paren + assert_include(tree, "[mlhs([a,b])]") + end + def test_paren thru_paren = false parse('()', :on_paren) {thru_paren = true} -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/