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.



7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
# File 'lib/prism/node.rb', line 7278

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


7343
7344
7345
# File 'lib/prism/node.rb', line 7343

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


7352
7353
7354
# File 'lib/prism/node.rb', line 7352

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


7361
7362
7363
# File 'lib/prism/node.rb', line 7361

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


7370
7371
7372
# File 'lib/prism/node.rb', line 7370

def right
  @right
end

Class Method Details

.typeObject

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



7443
7444
7445
# File 'lib/prism/node.rb', line 7443

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.



7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
# File 'lib/prism/node.rb', line 7449

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



7292
7293
7294
# File 'lib/prism/node.rb', line 7292

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



7297
7298
7299
# File 'lib/prism/node.rb', line 7297

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

#closingObject

def closing: () -> String?



7428
7429
7430
# File 'lib/prism/node.rb', line 7428

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


7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
# File 'lib/prism/node.rb', line 7404

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]



7322
7323
7324
# File 'lib/prism/node.rb', line 7322

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



7312
7313
7314
7315
7316
7317
7318
7319
# File 'lib/prism/node.rb', line 7312

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



7327
7328
7329
# File 'lib/prism/node.rb', line 7327

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



7335
7336
7337
# File 'lib/prism/node.rb', line 7335

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

#each_child_node {|constant| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



7302
7303
7304
7305
7306
7307
7308
7309
# File 'lib/prism/node.rb', line 7302

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield constant if constant
  yield left
  requireds.each { |node| yield node }
  yield right
end

#inspectObject

def inspect -> String



7433
7434
7435
# File 'lib/prism/node.rb', line 7433

def inspect
  InspectVisitor.compose(self)
end

#openingObject

def opening: () -> String?



7423
7424
7425
# File 'lib/prism/node.rb', line 7423

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


7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
# File 'lib/prism/node.rb', line 7379

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.



7418
7419
7420
# File 'lib/prism/node.rb', line 7418

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.



7393
7394
7395
# File 'lib/prism/node.rb', line 7393

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



7438
7439
7440
# File 'lib/prism/node.rb', line 7438

def type
  :find_pattern_node
end