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)
       ^^^^^^^^^^^^^^^^^^^^

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.



6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
# File 'lib/prism/node.rb', line 6851

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)

Represents the optional constant preceding the pattern

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


6906
6907
6908
# File 'lib/prism/node.rb', line 6906

def constant
  @constant
end

#leftObject (readonly)

Represents the first wildcard node in the pattern.

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

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


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

def left
  @left
end

#requiredsObject (readonly)

Represents the nodes in between the wildcards.

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

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


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

def requireds
  @requireds
end

#rightObject (readonly)

Represents the second wildcard node in the pattern.

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

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


6933
6934
6935
# File 'lib/prism/node.rb', line 6933

def right
  @right
end

Class Method Details

.typeObject

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



7006
7007
7008
# File 'lib/prism/node.rb', line 7006

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.



7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
# File 'lib/prism/node.rb', line 7012

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



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

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



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

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

#closingObject

def closing: () -> String?



6991
6992
6993
# File 'lib/prism/node.rb', line 6991

def closing
  closing_loc&.slice
end

#closing_locObject

The location of the closing brace.

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

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


6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
# File 'lib/prism/node.rb', line 6967

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]



6885
6886
6887
# File 'lib/prism/node.rb', line 6885

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



6875
6876
6877
6878
6879
6880
6881
6882
# File 'lib/prism/node.rb', line 6875

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



6890
6891
6892
# File 'lib/prism/node.rb', line 6890

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



6898
6899
6900
# File 'lib/prism/node.rb', line 6898

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



6996
6997
6998
# File 'lib/prism/node.rb', line 6996

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



6986
6987
6988
# File 'lib/prism/node.rb', line 6986

def opening
  opening_loc&.slice
end

#opening_locObject

The location of the opening brace.

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

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


6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
# File 'lib/prism/node.rb', line 6942

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.



6981
6982
6983
# File 'lib/prism/node.rb', line 6981

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.



6956
6957
6958
# File 'lib/prism/node.rb', line 6956

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`.



7001
7002
7003
# File 'lib/prism/node.rb', line 7001

def type
  :find_pattern_node
end