From c25585d8d91f3470d9cdfd6f3b6a553ad9a809fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lafortune?= Date: Mon, 7 Sep 2020 14:04:23 -0400 Subject: [PATCH] * right assignment: use existing AST node types (#738) (#739) --- doc/AST_FORMAT.md | 5 +++-- lib/parser/ast/processor.rb | 3 --- lib/parser/builders/default.rb | 5 ++--- lib/parser/meta.rb | 2 +- lib/parser/source/comment/associator.rb | 2 +- test/test_parser.rb | 28 ++++++++++++------------- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/doc/AST_FORMAT.md b/doc/AST_FORMAT.md index 26b8ab6ee..a86585fa3 100644 --- a/doc/AST_FORMAT.md +++ b/doc/AST_FORMAT.md @@ -735,9 +735,10 @@ Format: Format: ~~~ -(rasgn (int 1) (lvasgn :a)) +(lvasgn :a (int 1)) "1 => a" ~~~~~~ expression + ~ name ~~ operator ~~~ @@ -746,7 +747,7 @@ Format: Format: ~~~ -(mrasgn (send (int 13) :divmod (int 5)) (mlhs (lvasgn :a) (lvasgn :b))) +(masgn (mlhs (lvasgn :a) (lvasgn :b)) (send (int 13) :divmod (int 5))) "13.divmod(5) => a,b" ~~~~~~~~~~~~~~~~~~~ expression ^^ operator diff --git a/lib/parser/ast/processor.rb b/lib/parser/ast/processor.rb index 7617c1acc..aa08f3010 100644 --- a/lib/parser/ast/processor.rb +++ b/lib/parser/ast/processor.rb @@ -75,9 +75,6 @@ def on_op_asgn(node) alias on_mlhs process_regular_node alias on_masgn process_regular_node - alias on_rasgn process_regular_node - alias on_mrasgn process_regular_node - def on_const(node) scope_node, name = *node diff --git a/lib/parser/builders/default.rb b/lib/parser/builders/default.rb index 26412ca08..78c7b2347 100644 --- a/lib/parser/builders/default.rb +++ b/lib/parser/builders/default.rb @@ -656,12 +656,11 @@ def multi_assign(lhs, eql_t, rhs) end def rassign(lhs, assoc_t, rhs) - n(:rasgn, [lhs, rhs], binary_op_map(lhs, assoc_t, rhs)) + assign(rhs, assoc_t, lhs) end def multi_rassign(lhs, assoc_t, rhs) - n(:mrasgn, [ lhs, rhs ], - binary_op_map(lhs, assoc_t, rhs)) + multi_assign(rhs, assoc_t, lhs) end # diff --git a/lib/parser/meta.rb b/lib/parser/meta.rb index 1b36a7b93..7671564a1 100644 --- a/lib/parser/meta.rb +++ b/lib/parser/meta.rb @@ -12,7 +12,7 @@ module Meta sym dsym xstr regopt regexp array splat pair kwsplat hash irange erange self lvar ivar cvar gvar const defined? lvasgn - ivasgn cvasgn gvasgn casgn mlhs masgn rasgn mrasgn + ivasgn cvasgn gvasgn casgn mlhs masgn op_asgn and_asgn ensure rescue arg_expr or_asgn back_ref nth_ref match_with_lvasgn match_current_line diff --git a/lib/parser/source/comment/associator.rb b/lib/parser/source/comment/associator.rb index 05fa76fd8..8138d6290 100644 --- a/lib/parser/source/comment/associator.rb +++ b/lib/parser/source/comment/associator.rb @@ -107,7 +107,7 @@ def associate_locations private - POSTFIX_TYPES = Set[:if, :while, :while_post, :until, :until_post].freeze + POSTFIX_TYPES = Set[:if, :while, :while_post, :until, :until_post, :masgn].freeze def children_in_source_order(node) if POSTFIX_TYPES.include?(node.type) # All these types have either nodes with expressions, or `nil` diff --git a/test/test_parser.rb b/test/test_parser.rb index a176fe1f4..bd55b05ba 100644 --- a/test/test_parser.rb +++ b/test/test_parser.rb @@ -9688,17 +9688,16 @@ def test_endless_method_with_rescue_mod def test_rasgn assert_parses( - s(:rasgn, - s(:int, 1), s(:lvasgn, :a)), + s(:lvasgn, :a, + s(:int, 1)), %q{1 => a}, %q{~~~~~~ expression | ^^ operator}, SINCE_3_0) assert_parses( - s(:rasgn, - s(:send, s(:int, 1), :+, s(:int, 2)), - s(:gvasgn, :$a)), + s(:gvasgn, :$a, + s(:send, s(:int, 1), :+, s(:int, 2))), %q{1 + 2 => $a}, %q{~~~~~~~~~~~ expression | ^^ operator}, @@ -9707,22 +9706,23 @@ def test_rasgn def test_mrasgn assert_parses( - s(:mrasgn, - s(:send, s(:int, 13), :divmod, s(:int, 5)), - s(:mlhs, s(:lvasgn, :a), s(:lvasgn, :b))), + s(:masgn, + s(:mlhs, s(:lvasgn, :a), s(:lvasgn, :b)), + s(:send, s(:int, 13), :divmod, s(:int, 5))), %q{13.divmod(5) => a,b}, %q{~~~~~~~~~~~~~~~~~~~ expression | ^^ operator}, SINCE_3_0) assert_parses( - s(:mrasgn, - s(:mrasgn, - s(:send, s(:int, 13), :divmod, s(:int, 5)), - s(:mlhs, s(:lvasgn, :a), s(:lvasgn, :b))), - s(:mlhs, s(:lvasgn, :c), s(:lvasgn, :d))), + s(:masgn, + s(:mlhs, s(:lvasgn, :c), s(:lvasgn, :d)), + s(:masgn, + s(:mlhs, s(:lvasgn, :a), s(:lvasgn, :b)), + s(:send, s(:int, 13), :divmod, s(:int, 5))), + ), %q{13.divmod(5) => a,b => c, d}, - %q{~~~~~~~~~~~~~~~~~~~ expression (mrasgn) + %q{~~~~~~~~~~~~~~~~~~~ expression (masgn) |~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression}, SINCE_3_0) end