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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of For.



5383
5384
5385
5386
5387
5388
5389
# File 'lib/syntax_tree/node.rb', line 5383

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

Instance Attribute Details

#collectionObject (readonly)

untyped

the object being enumerated in the loop



5372
5373
5374
# File 'lib/syntax_tree/node.rb', line 5372

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5381
5382
5383
# File 'lib/syntax_tree/node.rb', line 5381

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



5369
5370
5371
# File 'lib/syntax_tree/node.rb', line 5369

def index
  @index
end

#locationObject (readonly)

Location

the location of this node



5378
5379
5380
# File 'lib/syntax_tree/node.rb', line 5378

def location
  @location
end

#statementsObject (readonly)

Statements

the statements to be executed



5375
5376
5377
# File 'lib/syntax_tree/node.rb', line 5375

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



5391
5392
5393
# File 'lib/syntax_tree/node.rb', line 5391

def child_nodes
  [index, collection, statements]
end

#deconstruct_keys(keys) ⇒ Object



5397
5398
5399
5400
5401
5402
5403
5404
5405
# File 'lib/syntax_tree/node.rb', line 5397

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

#format(q) ⇒ Object



5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
# File 'lib/syntax_tree/node.rb', line 5407

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: true)
        q.format(statements)
      end
    end

    q.breakable(force: true)
    q.text("end")
  end
end

#pretty_print(q) ⇒ Object



5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
# File 'lib/syntax_tree/node.rb', line 5426

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("for")

    q.breakable
    q.pp(index)

    q.breakable
    q.pp(collection)

    q.breakable
    q.pp(statements)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
# File 'lib/syntax_tree/node.rb', line 5443

def to_json(*opts)
  {
    type: :for,
    index: index,
    collection: collection,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end