Class: SyntaxTree::For

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

For represents using a for loop.

for value in list do
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(index:, collection:, statements:, location:) ⇒ For

Returns a new instance of For.



5422
5423
5424
5425
5426
5427
5428
# File 'lib/syntax_tree/node.rb', line 5422

def initialize(index:, collection:, statements:, location:)
  @index = index
  @collection = collection
  @statements = statements
  @location = location
  @comments = []
end

Instance Attribute Details

#collectionObject (readonly)

untyped

the object being enumerated in the loop



5414
5415
5416
# File 'lib/syntax_tree/node.rb', line 5414

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5420
5421
5422
# File 'lib/syntax_tree/node.rb', line 5420

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



5411
5412
5413
# File 'lib/syntax_tree/node.rb', line 5411

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



5417
5418
5419
# File 'lib/syntax_tree/node.rb', line 5417

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5482
5483
5484
5485
# File 'lib/syntax_tree/node.rb', line 5482

def ===(other)
  other.is_a?(For) && index === other.index &&
    collection === other.collection && statements === other.statements
end

#accept(visitor) ⇒ Object



5430
5431
5432
# File 'lib/syntax_tree/node.rb', line 5430

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

#child_nodesObject Also known as: deconstruct



5434
5435
5436
# File 'lib/syntax_tree/node.rb', line 5434

def child_nodes
  [index, collection, statements]
end

#copy(index: nil, collection: nil, statements: nil, location: nil) ⇒ Object



5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
# File 'lib/syntax_tree/node.rb', line 5438

def copy(index: nil, collection: nil, statements: nil, location: nil)
  node =
    For.new(
      index: index || self.index,
      collection: collection || self.collection,
      statements: statements || self.statements,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



5453
5454
5455
5456
5457
5458
5459
5460
5461
# File 'lib/syntax_tree/node.rb', line 5453

def deconstruct_keys(_keys)
  {
    index: index,
    collection: collection,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
# File 'lib/syntax_tree/node.rb', line 5463

def format(q)
  q.group do
    q.text("for ")
    q.group { q.format(index) }
    q.text(" in ")
    q.format(collection)

    unless statements.empty?
      q.indent do
        q.breakable_force
        q.format(statements)
      end
    end

    q.breakable_force
    q.text("end")
  end
end