Class: Prism::PinnedVariableNode

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

Overview

Represents the use of the ‘^` operator for pinning a variable in a pattern matching expression.

foo in ^bar
       ^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, variable, operator_loc) ⇒ PinnedVariableNode

Initialize a new PinnedVariableNode node.



15596
15597
15598
15599
15600
15601
15602
15603
# File 'lib/prism/node.rb', line 15596

def initialize(source, node_id, location, flags, variable, operator_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @variable = variable
  @operator_loc = operator_loc
end

Instance Attribute Details

#variableObject (readonly)

The variable used in the pinned expression

foo in ^bar
        ^^^


15649
15650
15651
# File 'lib/prism/node.rb', line 15649

def variable
  @variable
end

Class Method Details

.typeObject

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



15683
15684
15685
# File 'lib/prism/node.rb', line 15683

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



15689
15690
15691
15692
15693
# File 'lib/prism/node.rb', line 15689

def ===(other)
  other.is_a?(PinnedVariableNode) &&
    (variable === other.variable) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



15606
15607
15608
# File 'lib/prism/node.rb', line 15606

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



15611
15612
15613
# File 'lib/prism/node.rb', line 15611

def child_nodes
  [variable]
end

#comment_targetsObject

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



15628
15629
15630
# File 'lib/prism/node.rb', line 15628

def comment_targets
  [variable, operator_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



15623
15624
15625
# File 'lib/prism/node.rb', line 15623

def compact_child_nodes
  [variable]
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, variable: self.variable, operator_loc: self.operator_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?variable: LocalVariableReadNode | InstanceVariableReadNode | ClassVariableReadNode | GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode | ItLocalVariableReadNode | MissingNode, ?operator_loc: Location) -> PinnedVariableNode



15633
15634
15635
# File 'lib/prism/node.rb', line 15633

def copy(node_id: self.node_id, location: self.location, flags: self.flags, variable: self.variable, operator_loc: self.operator_loc)
  PinnedVariableNode.new(source, node_id, location, flags, variable, operator_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, variable: LocalVariableReadNode | InstanceVariableReadNode | ClassVariableReadNode | GlobalVariableReadNode | BackReferenceReadNode | NumberedReferenceReadNode | ItLocalVariableReadNode | MissingNode, operator_loc: Location }



15641
15642
15643
# File 'lib/prism/node.rb', line 15641

def deconstruct_keys(keys)
  { node_id: node_id, location: location, variable: variable, operator_loc: operator_loc }
end

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

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

Yields:



15616
15617
15618
15619
15620
# File 'lib/prism/node.rb', line 15616

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield variable
end

#inspectObject

def inspect -> String



15673
15674
15675
# File 'lib/prism/node.rb', line 15673

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



15668
15669
15670
# File 'lib/prism/node.rb', line 15668

def operator
  operator_loc.slice
end

#operator_locObject

The location of the ‘^` operator

foo in ^bar
       ^


15655
15656
15657
15658
15659
# File 'lib/prism/node.rb', line 15655

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#save_operator_loc(repository) ⇒ Object

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



15663
15664
15665
# File 'lib/prism/node.rb', line 15663

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#typeObject

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



15678
15679
15680
# File 'lib/prism/node.rb', line 15678

def type
  :pinned_variable_node
end