Class: SyntaxTree::WhileMod

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

Overview

WhileMod represents the modifier form of a while loop.

expression while predicate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statement:, predicate:, location:, comments: []) ⇒ WhileMod

Returns a new instance of WhileMod.



13444
13445
13446
13447
13448
13449
# File 'lib/syntax_tree.rb', line 13444

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



13442
13443
13444
# File 'lib/syntax_tree.rb', line 13442

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13439
13440
13441
# File 'lib/syntax_tree.rb', line 13439

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



13436
13437
13438
# File 'lib/syntax_tree.rb', line 13436

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



13433
13434
13435
# File 'lib/syntax_tree.rb', line 13433

def statement
  @statement
end

Instance Method Details

#child_nodesObject



13451
13452
13453
# File 'lib/syntax_tree.rb', line 13451

def child_nodes
  [statement, predicate]
end

#format(q) ⇒ Object



13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
# File 'lib/syntax_tree.rb', line 13455

def format(q)
  # If we're in the modifier form and we're modifying a `begin`, then this
  # is a special case where we need to explicitly use the modifier form
  # because otherwise the semantic meaning changes. This looks like:
  #
  #     begin
  #       foo
  #     end while bar
  #
  # Also, if the statement of the modifier includes an assignment, then we
  # can't know for certain that it won't impact the predicate, so we need to
  # force it to stay as it is. This looks like:
  #
  #     foo = bar while foo
  #
  if statement.is_a?(Begin) || ContainsAssignment.call(statement)
    q.format(statement)
    q.text(" while ")
    q.format(predicate)
  else
    LoopFormatter.new("while", self, statement).format(q)
  end
end

#pretty_print(q) ⇒ Object



13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
# File 'lib/syntax_tree.rb', line 13479

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

    q.breakable
    q.pp(statement)

    q.breakable
    q.pp(predicate)

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

#to_json(*opts) ⇒ Object



13493
13494
13495
13496
13497
13498
13499
13500
13501
# File 'lib/syntax_tree.rb', line 13493

def to_json(*opts)
  {
    type: :while_mod,
    stmt: statement,
    pred: predicate,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end