Class: Prism::FindPatternNode

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

Overview

Represents a find pattern in pattern matching.

foo in *bar, baz, *qux
       ^^^^^^^^^^^^^^^

foo in [*bar, baz, *qux]
       ^^^^^^^^^^^^^^^^^

foo in Foo(*bar, baz, *qux)
       ^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, constant, left, requireds, right, opening_loc, closing_loc) ⇒ FindPatternNode

Initialize a new FindPatternNode node.



5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
# File 'lib/prism/node.rb', line 5930

def initialize(source, node_id, location, flags, constant, left, requireds, right, opening_loc, closing_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @constant = constant
  @left = left
  @requireds = requireds
  @right = right
  @opening_loc = opening_loc
  @closing_loc = closing_loc
end

Instance Attribute Details

#constantObject (readonly)

attr_reader constant: Prism::node?



5982
5983
5984
# File 'lib/prism/node.rb', line 5982

def constant
  @constant
end

#leftObject (readonly)

attr_reader left: Prism::node



5985
5986
5987
# File 'lib/prism/node.rb', line 5985

def left
  @left
end

#requiredsObject (readonly)

attr_reader requireds: Array



5988
5989
5990
# File 'lib/prism/node.rb', line 5988

def requireds
  @requireds
end

#rightObject (readonly)

attr_reader right: Prism::node



5991
5992
5993
# File 'lib/prism/node.rb', line 5991

def right
  @right
end

Class Method Details

.typeObject

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



6040
6041
6042
# File 'lib/prism/node.rb', line 6040

def self.type
  :find_pattern_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.



6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
# File 'lib/prism/node.rb', line 6046

def ===(other)
  other.is_a?(FindPatternNode) &&
    (constant === other.constant) &&
    (left === other.left) &&
    (requireds.length == other.requireds.length) &&
    requireds.zip(other.requireds).all? { |left, right| left === right } &&
    (right === other.right) &&
    (opening_loc.nil? == other.opening_loc.nil?) &&
    (closing_loc.nil? == other.closing_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



5944
5945
5946
# File 'lib/prism/node.rb', line 5944

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

#child_nodesObject Also known as: deconstruct

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



5949
5950
5951
# File 'lib/prism/node.rb', line 5949

def child_nodes
  [constant, left, *requireds, right]
end

#closingObject

def closing: () -> String?



6025
6026
6027
# File 'lib/prism/node.rb', line 6025

def closing
  closing_loc&.slice
end

#closing_locObject

attr_reader closing_loc: Location?



6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
# File 'lib/prism/node.rb', line 6007

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

#comment_targetsObject

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



5964
5965
5966
# File 'lib/prism/node.rb', line 5964

def comment_targets
  [*constant, left, *requireds, right, *opening_loc, *closing_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



5954
5955
5956
5957
5958
5959
5960
5961
# File 'lib/prism/node.rb', line 5954

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << constant if constant
  compact << left
  compact.concat(requireds)
  compact << right
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, left: self.left, requireds: self.requireds, right: self.right, opening_loc: self.opening_loc, closing_loc: self.closing_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?constant: Prism::node?, ?left: Prism::node, ?requireds: Array, ?right: Prism::node, ?opening_loc: Location?, ?closing_loc: Location?) -> FindPatternNode



5969
5970
5971
# File 'lib/prism/node.rb', line 5969

def copy(node_id: self.node_id, location: self.location, flags: self.flags, constant: self.constant, left: self.left, requireds: self.requireds, right: self.right, opening_loc: self.opening_loc, closing_loc: self.closing_loc)
  FindPatternNode.new(source, node_id, location, flags, constant, left, requireds, right, opening_loc, closing_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, constant: Prism::node?, left: Prism::node, requireds: Array, right: Prism::node, opening_loc: Location?, closing_loc: Location? }



5977
5978
5979
# File 'lib/prism/node.rb', line 5977

def deconstruct_keys(keys)
  { node_id: node_id, location: location, constant: constant, left: left, requireds: requireds, right: right, opening_loc: opening_loc, closing_loc: closing_loc }
end

#inspectObject

def inspect -> String



6030
6031
6032
# File 'lib/prism/node.rb', line 6030

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



6020
6021
6022
# File 'lib/prism/node.rb', line 6020

def opening
  opening_loc&.slice
end

#opening_locObject

attr_reader opening_loc: Location?



5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
# File 'lib/prism/node.rb', line 5994

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

#typeObject

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



6035
6036
6037
# File 'lib/prism/node.rb', line 6035

def type
  :find_pattern_node
end