Class: Prism::MatchRequiredNode
- Inherits:
-
PrismNode
- Object
- PrismNode
- Prism::MatchRequiredNode
- Defined in:
- lib/prism/node.rb,
ext/prism/api_node.c
Overview
Represents the use of the ‘=>` operator.
foo => bar
^^^^^^^^^^
Instance Attribute Summary collapse
-
#pattern ⇒ Object
readonly
Represents the right-hand side of the operator.
-
#value ⇒ Object
readonly
Represents the left-hand side of the operator.
Class Method Summary collapse
-
.type ⇒ Object
Return a symbol representation of this node type.
Instance Method Summary collapse
-
#===(other) ⇒ Object
Implements case-equality for the node.
-
#accept(visitor) ⇒ Object
def accept: (Visitor visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
def child_nodes: () -> Array.
-
#comment_targets ⇒ Object
def comment_targets: () -> Array[Node | Location].
-
#compact_child_nodes ⇒ Object
def compact_child_nodes: () -> Array.
-
#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.
-
#deconstruct_keys(keys) ⇒ Object
def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, value: Prism::node, pattern: Prism::node, operator_loc: Location }.
-
#each_child_node {|value| ... } ⇒ Object
def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator.
-
#initialize(source, node_id, location, flags, value, pattern, operator_loc) ⇒ MatchRequiredNode
constructor
Initialize a new MatchRequiredNode node.
-
#inspect ⇒ Object
def inspect -> String.
-
#operator ⇒ Object
def operator: () -> String.
-
#operator_loc ⇒ Object
The location of the operator.
-
#save_operator_loc(repository) ⇒ Object
Save the operator_loc location using the given saved source so that it can be retrieved later.
-
#type ⇒ Object
Return a symbol representation of this node type.
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
#pattern ⇒ Object (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 |
#value ⇒ Object (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
.type ⇒ Object
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_nodes ⇒ Object 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_targets ⇒ Object
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_nodes ⇒ Object
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
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 |
#inspect ⇒ Object
def inspect -> String
13651 13652 13653 |
# File 'lib/prism/node.rb', line 13651 def inspect InspectVisitor.compose(self) end |
#operator ⇒ Object
def operator: () -> String
13646 13647 13648 |
# File 'lib/prism/node.rb', line 13646 def operator operator_loc.slice end |
#operator_loc ⇒ Object
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 |
#type ⇒ Object
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 |