Class: Prism::MatchRequiredNode

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

Overview

Represents the use of the ‘=>` operator.

foo => bar
^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, value, pattern, operator_loc) ⇒ MatchRequiredNode

Initialize a new MatchRequiredNode node.



13527
13528
13529
13530
13531
13532
13533
13534
13535
# File 'lib/prism/node.rb', line 13527

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

Instance Attribute Details

#patternObject (readonly)

Represents the right-hand side of the operator. The type of the node depends on the expression.

Anything that looks like a local variable name (including _) will result in a LocalVariableTargetNode.

foo => a # This is equivalent to writing `a = foo`
       ^

Using an explicit Array or combining expressions with ‘,` will result in a ArrayPatternNode. This can be preceded by a constant.

foo => [a]
       ^^^

foo => a, b
       ^^^^

foo => Bar[a, b]
       ^^^^^^^^^

If the array pattern contains at least two wildcard matches, a FindPatternNode is created instead.

foo => *, 1, *a
       ^^^^^

Using an explicit Hash or a constant with square brackets and hash keys in the square brackets will result in a HashPatternNode.

foo => { a: 1, b: }

foo => Bar[a: 1, b:]

foo => Bar[**]

To use any variable that needs run time evaluation, pinning is required. This results in a PinnedVariableNode

foo => ^a
       ^^

Similar, any expression can be used with pinning. This results in a PinnedExpressionNode.

foo => ^(a + 1)

Anything else will result in the regular node for that expression, for example a ConstantReadNode.

foo => CONST


13627
13628
13629
# File 'lib/prism/node.rb', line 13627

def pattern
  @pattern
end

#valueObject (readonly)

Represents the left-hand side of the operator.

foo => bar
^^^


13582
13583
13584
# File 'lib/prism/node.rb', line 13582

def value
  @value
end

Class Method Details

.typeObject

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



13661
13662
13663
# File 'lib/prism/node.rb', line 13661

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



13667
13668
13669
13670
13671
13672
# File 'lib/prism/node.rb', line 13667

def ===(other)
  other.is_a?(MatchRequiredNode) &&
    (value === other.value) &&
    (pattern === other.pattern) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



13538
13539
13540
# File 'lib/prism/node.rb', line 13538

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



13543
13544
13545
# File 'lib/prism/node.rb', line 13543

def child_nodes
  [value, pattern]
end

#comment_targetsObject

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



13561
13562
13563
# File 'lib/prism/node.rb', line 13561

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



13556
13557
13558
# File 'lib/prism/node.rb', line 13556

def compact_child_nodes
  [value, pattern]
end

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

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?value: Prism::node, ?pattern: Prism::node, ?operator_loc: Location) -> MatchRequiredNode



13566
13567
13568
# File 'lib/prism/node.rb', line 13566

def copy(node_id: self.node_id, location: self.location, flags: self.flags, value: self.value, pattern: self.pattern, operator_loc: self.operator_loc)
  MatchRequiredNode.new(source, node_id, location, flags, value, pattern, operator_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, value: Prism::node, pattern: Prism::node, operator_loc: Location }



13574
13575
13576
# File 'lib/prism/node.rb', line 13574

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

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

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

Yields:



13548
13549
13550
13551
13552
13553
# File 'lib/prism/node.rb', line 13548

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield value
  yield pattern
end

#inspectObject

def inspect -> String



13651
13652
13653
# File 'lib/prism/node.rb', line 13651

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



13646
13647
13648
# File 'lib/prism/node.rb', line 13646

def operator
  operator_loc.slice
end

#operator_locObject

The location of the operator.

foo => bar
    ^^


13633
13634
13635
13636
13637
# File 'lib/prism/node.rb', line 13633

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.



13641
13642
13643
# File 'lib/prism/node.rb', line 13641

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

#typeObject

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



13656
13657
13658
# File 'lib/prism/node.rb', line 13656

def type
  :match_required_node
end