Class: Prism::MultiWriteNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents a write to a multi-target expression.

a, b, c = 1, 2, 3
^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, lefts, rest, rights, lparen_loc, rparen_loc, operator_loc, value) ⇒ MultiWriteNode

Initialize a new MultiWriteNode node.



11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
# File 'lib/prism/node.rb', line 11832

def initialize(source, node_id, location, flags, lefts, rest, rights, lparen_loc, rparen_loc, operator_loc, value)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @lefts = lefts
  @rest = rest
  @rights = rights
  @lparen_loc = lparen_loc
  @rparen_loc = rparen_loc
  @operator_loc = operator_loc
  @value = value
end

Instance Attribute Details

#leftsObject (readonly)

Represents the targets expressions before a splat node.

a, b, * = 1, 2, 3, 4, 5
^^^^

The splat node can be absent, in that case all target expressions are in the left field.

a, b, c = 1, 2, 3, 4, 5
^^^^^^^


11893
11894
11895
# File 'lib/prism/node.rb', line 11893

def lefts
  @lefts
end

#restObject (readonly)

Represents a splat node in the target expression.

a, b, *c = 1, 2, 3, 4
      ^^

The variable can be empty, this results in a ‘SplatNode` with a `nil` expression field.

a, b, * = 1, 2, 3, 4
      ^

If the ‘*` is omitted, the field will containt an `ImplicitRestNode`

a, b, = 1, 2, 3, 4
    ^


11909
11910
11911
# File 'lib/prism/node.rb', line 11909

def rest
  @rest
end

#rightsObject (readonly)

Represents the targets expressions after a splat node.

a, *, b, c = 1, 2, 3, 4, 5
      ^^^^


11915
11916
11917
# File 'lib/prism/node.rb', line 11915

def rights
  @rights
end

#valueObject (readonly)

The value to write to the targets. It can be any [non-void expression](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression).

a, b, c = 1, 2, 3
          ^^^^^^^


11963
11964
11965
# File 'lib/prism/node.rb', line 11963

def value
  @value
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



11991
11992
11993
# File 'lib/prism/node.rb', line 11991

def self.type
  :multi_write_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
# File 'lib/prism/node.rb', line 11997

def ===(other)
  other.is_a?(MultiWriteNode) &&
    (lefts.length == other.lefts.length) &&
    lefts.zip(other.lefts).all? { |left, right| left === right } &&
    (rest === other.rest) &&
    (rights.length == other.rights.length) &&
    rights.zip(other.rights).all? { |left, right| left === right } &&
    (lparen_loc.nil? == other.lparen_loc.nil?) &&
    (rparen_loc.nil? == other.rparen_loc.nil?) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (value === other.value)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



11847
11848
11849
# File 'lib/prism/node.rb', line 11847

def accept(visitor)
  visitor.visit_multi_write_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



11852
11853
11854
# File 'lib/prism/node.rb', line 11852

def child_nodes
  [*lefts, rest, *rights, value]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



11867
11868
11869
# File 'lib/prism/node.rb', line 11867

def comment_targets
  [*lefts, *rest, *rights, *lparen_loc, *rparen_loc, operator_loc, value] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



11857
11858
11859
11860
11861
11862
11863
11864
# File 'lib/prism/node.rb', line 11857

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact.concat(lefts)
  compact << rest if rest
  compact.concat(rights)
  compact << value
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, lefts: self.lefts, rest: self.rest, rights: self.rights, lparen_loc: self.lparen_loc, rparen_loc: self.rparen_loc, operator_loc: self.operator_loc, value: self.value) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?lefts: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode], ?rest: ImplicitRestNode | SplatNode | nil, ?rights: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode], ?lparen_loc: Location?, ?rparen_loc: Location?, ?operator_loc: Location, ?value: Prism::node) -> MultiWriteNode



11872
11873
11874
# File 'lib/prism/node.rb', line 11872

def copy(node_id: self.node_id, location: self.location, flags: self.flags, lefts: self.lefts, rest: self.rest, rights: self.rights, lparen_loc: self.lparen_loc, rparen_loc: self.rparen_loc, operator_loc: self.operator_loc, value: self.value)
  MultiWriteNode.new(source, node_id, location, flags, lefts, rest, rights, lparen_loc, rparen_loc, operator_loc, value)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, lefts: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode], rest: ImplicitRestNode | SplatNode | nil, rights: Array[LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode], lparen_loc: Location?, rparen_loc: Location?, operator_loc: Location, value: Prism::node }



11880
11881
11882
# File 'lib/prism/node.rb', line 11880

def deconstruct_keys(keys)
  { node_id: node_id, location: location, lefts: lefts, rest: rest, rights: rights, lparen_loc: lparen_loc, rparen_loc: rparen_loc, operator_loc: operator_loc, value: value }
end

#inspectObject

def inspect -> String



11981
11982
11983
# File 'lib/prism/node.rb', line 11981

def inspect
  InspectVisitor.compose(self)
end

#lparenObject

def lparen: () -> String?



11966
11967
11968
# File 'lib/prism/node.rb', line 11966

def lparen
  lparen_loc&.slice
end

#lparen_locObject

The location of the opening parenthesis.

(a, b, c) = 1, 2, 3
^


11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
# File 'lib/prism/node.rb', line 11921

def lparen_loc
  location = @lparen_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @lparen_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#operatorObject

def operator: () -> String



11976
11977
11978
# File 'lib/prism/node.rb', line 11976

def operator
  operator_loc.slice
end

#operator_locObject

The location of the operator.

a, b, c = 1, 2, 3
        ^


11953
11954
11955
11956
11957
# File 'lib/prism/node.rb', line 11953

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#rparenObject

def rparen: () -> String?



11971
11972
11973
# File 'lib/prism/node.rb', line 11971

def rparen
  rparen_loc&.slice
end

#rparen_locObject

The location of the closing parenthesis.

(a, b, c) = 1, 2, 3
        ^


11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
# File 'lib/prism/node.rb', line 11937

def rparen_loc
  location = @rparen_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @rparen_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



11986
11987
11988
# File 'lib/prism/node.rb', line 11986

def type
  :multi_write_node
end