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.



6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
# File 'lib/prism/node.rb', line 6818

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: ConstantReadNode | ConstantPathNode | nil



6870
6871
6872
# File 'lib/prism/node.rb', line 6870

def constant
  @constant
end

#leftObject (readonly)

attr_reader left: SplatNode



6873
6874
6875
# File 'lib/prism/node.rb', line 6873

def left
  @left
end

#requiredsObject (readonly)

attr_reader requireds: Array



6876
6877
6878
# File 'lib/prism/node.rb', line 6876

def requireds
  @requireds
end

#rightObject (readonly)

attr_reader right: SplatNode | MissingNode



6879
6880
6881
# File 'lib/prism/node.rb', line 6879

def right
  @right
end

Class Method Details

.typeObject

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



6940
6941
6942
# File 'lib/prism/node.rb', line 6940

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.



6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
# File 'lib/prism/node.rb', line 6946

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



6832
6833
6834
# File 'lib/prism/node.rb', line 6832

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

#child_nodesObject Also known as: deconstruct

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



6837
6838
6839
# File 'lib/prism/node.rb', line 6837

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

#closingObject

def closing: () -> String?



6925
6926
6927
# File 'lib/prism/node.rb', line 6925

def closing
  closing_loc&.slice
end

#closing_locObject

attr_reader closing_loc: Location?



6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
# File 'lib/prism/node.rb', line 6901

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]



6852
6853
6854
# File 'lib/prism/node.rb', line 6852

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



6842
6843
6844
6845
6846
6847
6848
6849
# File 'lib/prism/node.rb', line 6842

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: ConstantReadNode | ConstantPathNode | nil, ?left: SplatNode, ?requireds: Array, ?right: SplatNode | MissingNode, ?opening_loc: Location?, ?closing_loc: Location?) -> FindPatternNode



6857
6858
6859
# File 'lib/prism/node.rb', line 6857

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: ConstantReadNode | ConstantPathNode | nil, left: SplatNode, requireds: Array, right: SplatNode | MissingNode, opening_loc: Location?, closing_loc: Location? }



6865
6866
6867
# File 'lib/prism/node.rb', line 6865

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



6930
6931
6932
# File 'lib/prism/node.rb', line 6930

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



6920
6921
6922
# File 'lib/prism/node.rb', line 6920

def opening
  opening_loc&.slice
end

#opening_locObject

attr_reader opening_loc: Location?



6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
# File 'lib/prism/node.rb', line 6882

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

#save_closing_loc(repository) ⇒ Object

Save the closing_loc location using the given saved source so that it can be retrieved later.



6915
6916
6917
# File 'lib/prism/node.rb', line 6915

def save_closing_loc(repository)
  repository.enter(node_id, :closing_loc) unless @closing_loc.nil?
end

#save_opening_loc(repository) ⇒ Object

Save the opening_loc location using the given saved source so that it can be retrieved later.



6896
6897
6898
# File 'lib/prism/node.rb', line 6896

def save_opening_loc(repository)
  repository.enter(node_id, :opening_loc) unless @opening_loc.nil?
end

#typeObject

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



6935
6936
6937
# File 'lib/prism/node.rb', line 6935

def type
  :find_pattern_node
end