Skip to content

Commit

Permalink
* right assignment: use existing AST node types (whitequark#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Sep 7, 2020
1 parent 79561aa commit e58459d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
3 changes: 2 additions & 1 deletion doc/AST_FORMAT.md
Original file line number Diff line number Diff line change
Expand Up @@ -735,9 +735,10 @@ Format:
Format:

~~~
(rasgn (int 1) (lvasgn :a))
(lvasgn :a (int 1))
"1 => a"
~~~~~~ expression
~ name
~~ operator
~~~

Expand Down
3 changes: 0 additions & 3 deletions lib/parser/ast/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/source/comment/associator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
28 changes: 14 additions & 14 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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
Expand Down

0 comments on commit e58459d

Please sign in to comment.