Class: SyntaxTree::If

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

Overview

If represents the first clause in an if chain.

if predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ If

Returns a new instance of If.



6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
# File 'lib/syntax_tree.rb', line 6911

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6909
6910
6911
# File 'lib/syntax_tree.rb', line 6909

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



6903
6904
6905
# File 'lib/syntax_tree.rb', line 6903

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



6906
6907
6908
# File 'lib/syntax_tree.rb', line 6906

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



6897
6898
6899
# File 'lib/syntax_tree.rb', line 6897

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



6900
6901
6902
# File 'lib/syntax_tree.rb', line 6900

def statements
  @statements
end

Instance Method Details

#child_nodesObject



6925
6926
6927
# File 'lib/syntax_tree.rb', line 6925

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



6929
6930
6931
# File 'lib/syntax_tree.rb', line 6929

def format(q)
  ConditionalFormatter.new("if", self).format(q)
end

#pretty_print(q) ⇒ Object



6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
# File 'lib/syntax_tree.rb', line 6933

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

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(statements)

    if consequent
      q.breakable
      q.pp(consequent)
    end

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

#to_json(*opts) ⇒ Object



6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
# File 'lib/syntax_tree.rb', line 6952

def to_json(*opts)
  {
    type: :if,
    pred: predicate,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end