Class: SyntaxTree::WhileMod

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



11506
11507
11508
11509
11510
11511
# File 'lib/syntax_tree/node.rb', line 11506

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



11504
11505
11506
# File 'lib/syntax_tree/node.rb', line 11504

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



11501
11502
11503
# File 'lib/syntax_tree/node.rb', line 11501

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



11498
11499
11500
# File 'lib/syntax_tree/node.rb', line 11498

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



11495
11496
11497
# File 'lib/syntax_tree/node.rb', line 11495

def statement
  @statement
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



11513
11514
11515
# File 'lib/syntax_tree/node.rb', line 11513

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(keys) ⇒ Object



11519
11520
11521
11522
11523
11524
11525
11526
# File 'lib/syntax_tree/node.rb', line 11519

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

#format(q) ⇒ Object



11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
# File 'lib/syntax_tree/node.rb', line 11528

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



11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
# File 'lib/syntax_tree/node.rb', line 11552

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



11566
11567
11568
11569
11570
11571
11572
11573
11574
# File 'lib/syntax_tree/node.rb', line 11566

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