Class: SyntaxTree::UntilMod

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



10881
10882
10883
10884
10885
10886
# File 'lib/syntax_tree/node.rb', line 10881

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



10879
10880
10881
# File 'lib/syntax_tree/node.rb', line 10879

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



10876
10877
10878
# File 'lib/syntax_tree/node.rb', line 10876

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



10873
10874
10875
# File 'lib/syntax_tree/node.rb', line 10873

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



10870
10871
10872
# File 'lib/syntax_tree/node.rb', line 10870

def statement
  @statement
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



10888
10889
10890
# File 'lib/syntax_tree/node.rb', line 10888

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(keys) ⇒ Object



10894
10895
10896
10897
10898
10899
10900
10901
# File 'lib/syntax_tree/node.rb', line 10894

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

#format(q) ⇒ Object



10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
# File 'lib/syntax_tree/node.rb', line 10903

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



10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
# File 'lib/syntax_tree/node.rb', line 10927

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



10941
10942
10943
10944
10945
10946
10947
10948
10949
# File 'lib/syntax_tree/node.rb', line 10941

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