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.



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

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


6938
6939
6940
# File 'lib/prism/node.rb', line 6938

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


6947
6948
6949
# File 'lib/prism/node.rb', line 6947

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


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

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


6965
6966
6967
# File 'lib/prism/node.rb', line 6965

def right
  @right
end

Class Method Details

.typeObject

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



7038
7039
7040
# File 'lib/prism/node.rb', line 7038

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.



7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
# File 'lib/prism/node.rb', line 7044

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



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

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



6902
6903
6904
# File 'lib/prism/node.rb', line 6902

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

#closingObject

def closing: () -> String?



7023
7024
7025
# File 'lib/prism/node.rb', line 7023

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


6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
# File 'lib/prism/node.rb', line 6999

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]



6917
6918
6919
# File 'lib/prism/node.rb', line 6917

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



6907
6908
6909
6910
6911
6912
6913
6914
# File 'lib/prism/node.rb', line 6907

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



6922
6923
6924
# File 'lib/prism/node.rb', line 6922

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? }



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

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



7028
7029
7030
# File 'lib/prism/node.rb', line 7028

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



7018
7019
7020
# File 'lib/prism/node.rb', line 7018

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


6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
# File 'lib/prism/node.rb', line 6974

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.



7013
7014
7015
# File 'lib/prism/node.rb', line 7013

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.



6988
6989
6990
# File 'lib/prism/node.rb', line 6988

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



7033
7034
7035
# File 'lib/prism/node.rb', line 7033

def type
  :find_pattern_node
end