Class: SyntaxTree::While

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

Overview

While represents a while loop.

while predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate:, statements:, location:, comments: []) ⇒ While

Returns a new instance of While.



11427
11428
11429
11430
11431
11432
# File 'lib/syntax_tree/node.rb', line 11427

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11425
11426
11427
# File 'lib/syntax_tree/node.rb', line 11425

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



11422
11423
11424
# File 'lib/syntax_tree/node.rb', line 11422

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



11416
11417
11418
# File 'lib/syntax_tree/node.rb', line 11416

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



11419
11420
11421
# File 'lib/syntax_tree/node.rb', line 11419

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



11434
11435
11436
# File 'lib/syntax_tree/node.rb', line 11434

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(keys) ⇒ Object



11440
11441
11442
11443
11444
11445
11446
11447
# File 'lib/syntax_tree/node.rb', line 11440

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

#format(q) ⇒ Object



11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
# File 'lib/syntax_tree/node.rb', line 11449

def format(q)
  if statements.empty?
    keyword = "while "

    q.group do
      q.text(keyword)
      q.nest(keyword.length) { q.format(predicate) }
      q.breakable(force: true)
      q.text("end")
    end
  else
    LoopFormatter.new("while", self, statements).format(q)
  end
end

#pretty_print(q) ⇒ Object



11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
# File 'lib/syntax_tree/node.rb', line 11464

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

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



11478
11479
11480
11481
11482
11483
11484
11485
11486
# File 'lib/syntax_tree/node.rb', line 11478

def to_json(*opts)
  {
    type: :while,
    pred: predicate,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end