Class: SyntaxTree::For

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



6283
6284
6285
6286
6287
6288
6289
# File 'lib/syntax_tree.rb', line 6283

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



6272
6273
6274
# File 'lib/syntax_tree.rb', line 6272

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6281
6282
6283
# File 'lib/syntax_tree.rb', line 6281

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



6269
6270
6271
# File 'lib/syntax_tree.rb', line 6269

def index
  @index
end

#locationObject (readonly)

Location

the location of this node



6278
6279
6280
# File 'lib/syntax_tree.rb', line 6278

def location
  @location
end

#statementsObject (readonly)

Statements

the statements to be executed



6275
6276
6277
# File 'lib/syntax_tree.rb', line 6275

def statements
  @statements
end

Instance Method Details

#child_nodesObject



6291
6292
6293
# File 'lib/syntax_tree.rb', line 6291

def child_nodes
  [index, collection, statements]
end

#format(q) ⇒ Object



6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
# File 'lib/syntax_tree.rb', line 6295

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



6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
# File 'lib/syntax_tree.rb', line 6314

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



6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
# File 'lib/syntax_tree.rb', line 6331

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