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.



12774
12775
12776
12777
12778
12779
# File 'lib/syntax_tree.rb', line 12774

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



12772
12773
12774
# File 'lib/syntax_tree.rb', line 12772

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



12769
12770
12771
# File 'lib/syntax_tree.rb', line 12769

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



12766
12767
12768
# File 'lib/syntax_tree.rb', line 12766

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



12763
12764
12765
# File 'lib/syntax_tree.rb', line 12763

def statement
  @statement
end

Instance Method Details

#child_nodesObject



12781
12782
12783
# File 'lib/syntax_tree.rb', line 12781

def child_nodes
  [statement, predicate]
end

#format(q) ⇒ Object



12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
# File 'lib/syntax_tree.rb', line 12785

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



12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
# File 'lib/syntax_tree.rb', line 12809

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



12823
12824
12825
12826
12827
12828
12829
12830
12831
# File 'lib/syntax_tree.rb', line 12823

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