Class: SyntaxTree::UntilMod

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

Overview

UntilMod represents the modifier form of a until loop.

expression until predicate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of UntilMod.



12642
12643
12644
12645
12646
12647
# File 'lib/syntax_tree.rb', line 12642

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



12640
12641
12642
# File 'lib/syntax_tree.rb', line 12640

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



12637
12638
12639
# File 'lib/syntax_tree.rb', line 12637

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



12634
12635
12636
# File 'lib/syntax_tree.rb', line 12634

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



12631
12632
12633
# File 'lib/syntax_tree.rb', line 12631

def statement
  @statement
end

Instance Method Details

#child_nodesObject



12649
12650
12651
# File 'lib/syntax_tree.rb', line 12649

def child_nodes
  [statement, predicate]
end

#format(q) ⇒ Object



12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
# File 'lib/syntax_tree.rb', line 12653

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 until 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 until foo
  #
  if statement.is_a?(Begin) || ContainsAssignment.call(statement)
    q.format(statement)
    q.text(" until ")
    q.format(predicate)
  else
    LoopFormatter.new("until", self, statement).format(q)
  end
end

#pretty_print(q) ⇒ Object



12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
# File 'lib/syntax_tree.rb', line 12677

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

    q.breakable
    q.pp(statement)

    q.breakable
    q.pp(predicate)

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

#to_json(*opts) ⇒ Object



12691
12692
12693
12694
12695
12696
12697
12698
12699
# File 'lib/syntax_tree.rb', line 12691

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