Class: Prism::OrNode

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

Overview

Represents the use of the ‘||` operator or the `or` keyword.

left or right
^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, left, right, operator_loc) ⇒ OrNode

Initialize a new OrNode node.



14225
14226
14227
14228
14229
14230
14231
14232
14233
# File 'lib/prism/node.rb', line 14225

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

Instance Attribute Details

#leftObject (readonly)

Represents the left side of the expression. It can be any [non-void expression](github.com/ruby/prism/blob/main/docs/parsing_rules.md#non-void-expression).

left or right
^^^^

1 || 2
^


14275
14276
14277
# File 'lib/prism/node.rb', line 14275

def left
  @left
end

#rightObject (readonly)

Represents the right side of the expression.

left || right
        ^^^^^

1 or 2
     ^


14284
14285
14286
# File 'lib/prism/node.rb', line 14284

def right
  @right
end

Class Method Details

.typeObject

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



14318
14319
14320
# File 'lib/prism/node.rb', line 14318

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



14324
14325
14326
14327
14328
14329
# File 'lib/prism/node.rb', line 14324

def ===(other)
  other.is_a?(OrNode) &&
    (left === other.left) &&
    (right === other.right) &&
    (operator_loc.nil? == other.operator_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



14236
14237
14238
# File 'lib/prism/node.rb', line 14236

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



14241
14242
14243
# File 'lib/prism/node.rb', line 14241

def child_nodes
  [left, right]
end

#comment_targetsObject

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



14251
14252
14253
# File 'lib/prism/node.rb', line 14251

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

#compact_child_nodesObject

def compact_child_nodes: () -> Array



14246
14247
14248
# File 'lib/prism/node.rb', line 14246

def compact_child_nodes
  [left, right]
end

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

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?left: Prism::node, ?right: Prism::node, ?operator_loc: Location) -> OrNode



14256
14257
14258
# File 'lib/prism/node.rb', line 14256

def copy(node_id: self.node_id, location: self.location, flags: self.flags, left: self.left, right: self.right, operator_loc: self.operator_loc)
  OrNode.new(source, node_id, location, flags, left, right, operator_loc)
end

#deconstruct_keys(keys) ⇒ Object

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



14264
14265
14266
# File 'lib/prism/node.rb', line 14264

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

#inspectObject

def inspect -> String



14308
14309
14310
# File 'lib/prism/node.rb', line 14308

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



14303
14304
14305
# File 'lib/prism/node.rb', line 14303

def operator
  operator_loc.slice
end

#operator_locObject

The location of the ‘or` keyword or the `||` operator.

left or right
     ^^


14290
14291
14292
14293
14294
# File 'lib/prism/node.rb', line 14290

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.



14298
14299
14300
# File 'lib/prism/node.rb', line 14298

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

#typeObject

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



14313
14314
14315
# File 'lib/prism/node.rb', line 14313

def type
  :or_node
end