Class: Prism::ForNode

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

Overview

Represents the use of the ‘for` keyword.

for i in a end
^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc) ⇒ ForNode

Initialize a new ForNode node.



7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
# File 'lib/prism/node.rb', line 7242

def initialize(source, node_id, location, flags, index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @index = index
  @collection = collection
  @statements = statements
  @for_keyword_loc = for_keyword_loc
  @in_keyword_loc = in_keyword_loc
  @do_keyword_loc = do_keyword_loc
  @end_keyword_loc = end_keyword_loc
end

Instance Attribute Details

#collectionObject (readonly)

The collection to iterate over.

for i in a end
         ^


7303
7304
7305
# File 'lib/prism/node.rb', line 7303

def collection
  @collection
end

#indexObject (readonly)

The index expression for ‘for` loops.

for i in a end
    ^


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

def index
  @index
end

#statementsObject (readonly)

Represents the body of statements to execute for each iteration of the loop.

for i in a
  foo(i)
  ^^^^^^
end


7311
7312
7313
# File 'lib/prism/node.rb', line 7311

def statements
  @statements
end

Class Method Details

.typeObject

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



7414
7415
7416
# File 'lib/prism/node.rb', line 7414

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



7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
# File 'lib/prism/node.rb', line 7420

def ===(other)
  other.is_a?(ForNode) &&
    (index === other.index) &&
    (collection === other.collection) &&
    (statements === other.statements) &&
    (for_keyword_loc.nil? == other.for_keyword_loc.nil?) &&
    (in_keyword_loc.nil? == other.in_keyword_loc.nil?) &&
    (do_keyword_loc.nil? == other.do_keyword_loc.nil?) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



7257
7258
7259
# File 'lib/prism/node.rb', line 7257

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



7262
7263
7264
# File 'lib/prism/node.rb', line 7262

def child_nodes
  [index, collection, statements]
end

#comment_targetsObject

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



7276
7277
7278
# File 'lib/prism/node.rb', line 7276

def comment_targets
  [index, collection, *statements, for_keyword_loc, in_keyword_loc, *do_keyword_loc, end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



7267
7268
7269
7270
7271
7272
7273
# File 'lib/prism/node.rb', line 7267

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << index
  compact << collection
  compact << statements if statements
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, index: self.index, collection: self.collection, statements: self.statements, for_keyword_loc: self.for_keyword_loc, in_keyword_loc: self.in_keyword_loc, do_keyword_loc: self.do_keyword_loc, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?index: LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | BackReferenceReadNode | NumberedReferenceReadNode | MissingNode, ?collection: Prism::node, ?statements: StatementsNode?, ?for_keyword_loc: Location, ?in_keyword_loc: Location, ?do_keyword_loc: Location?, ?end_keyword_loc: Location) -> ForNode



7281
7282
7283
# File 'lib/prism/node.rb', line 7281

def copy(node_id: self.node_id, location: self.location, flags: self.flags, index: self.index, collection: self.collection, statements: self.statements, for_keyword_loc: self.for_keyword_loc, in_keyword_loc: self.in_keyword_loc, do_keyword_loc: self.do_keyword_loc, end_keyword_loc: self.end_keyword_loc)
  ForNode.new(source, node_id, location, flags, index, collection, statements, for_keyword_loc, in_keyword_loc, do_keyword_loc, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, index: LocalVariableTargetNode | InstanceVariableTargetNode | ClassVariableTargetNode | GlobalVariableTargetNode | ConstantTargetNode | ConstantPathTargetNode | CallTargetNode | IndexTargetNode | MultiTargetNode | BackReferenceReadNode | NumberedReferenceReadNode | MissingNode, collection: Prism::node, statements: StatementsNode?, for_keyword_loc: Location, in_keyword_loc: Location, do_keyword_loc: Location?, end_keyword_loc: Location }



7289
7290
7291
# File 'lib/prism/node.rb', line 7289

def deconstruct_keys(keys)
  { node_id: node_id, location: location, index: index, collection: collection, statements: statements, for_keyword_loc: for_keyword_loc, in_keyword_loc: in_keyword_loc, do_keyword_loc: do_keyword_loc, end_keyword_loc: end_keyword_loc }
end

#do_keywordObject

def do_keyword: () -> String?



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

def do_keyword
  do_keyword_loc&.slice
end

#do_keyword_locObject

The location of the ‘do` keyword, if present.

for i in a do end
           ^^


7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
# File 'lib/prism/node.rb', line 7349

def do_keyword_loc
  location = @do_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @do_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#end_keywordObject

def end_keyword: () -> String



7399
7400
7401
# File 'lib/prism/node.rb', line 7399

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

The location of the ‘end` keyword.

for i in a end
           ^^^


7371
7372
7373
7374
7375
# File 'lib/prism/node.rb', line 7371

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

#for_keywordObject

def for_keyword: () -> String



7384
7385
7386
# File 'lib/prism/node.rb', line 7384

def for_keyword
  for_keyword_loc.slice
end

#for_keyword_locObject

The location of the ‘for` keyword.

for i in a end
^^^


7317
7318
7319
7320
7321
# File 'lib/prism/node.rb', line 7317

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

#in_keywordObject

def in_keyword: () -> String



7389
7390
7391
# File 'lib/prism/node.rb', line 7389

def in_keyword
  in_keyword_loc.slice
end

#in_keyword_locObject

The location of the ‘in` keyword.

for i in a end
      ^^


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

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

#inspectObject

def inspect -> String



7404
7405
7406
# File 'lib/prism/node.rb', line 7404

def inspect
  InspectVisitor.compose(self)
end

#save_do_keyword_loc(repository) ⇒ Object

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



7363
7364
7365
# File 'lib/prism/node.rb', line 7363

def save_do_keyword_loc(repository)
  repository.enter(node_id, :do_keyword_loc) unless @do_keyword_loc.nil?
end

#save_end_keyword_loc(repository) ⇒ Object

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



7379
7380
7381
# File 'lib/prism/node.rb', line 7379

def save_end_keyword_loc(repository)
  repository.enter(node_id, :end_keyword_loc)
end

#save_for_keyword_loc(repository) ⇒ Object

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



7325
7326
7327
# File 'lib/prism/node.rb', line 7325

def save_for_keyword_loc(repository)
  repository.enter(node_id, :for_keyword_loc)
end

#save_in_keyword_loc(repository) ⇒ Object

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



7341
7342
7343
# File 'lib/prism/node.rb', line 7341

def save_in_keyword_loc(repository)
  repository.enter(node_id, :in_keyword_loc)
end

#typeObject

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



7409
7410
7411
# File 'lib/prism/node.rb', line 7409

def type
  :for_node
end