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.



6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
# File 'lib/syntax_tree.rb', line 6851

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



6849
6850
6851
# File 'lib/syntax_tree.rb', line 6849

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



6843
6844
6845
# File 'lib/syntax_tree.rb', line 6843

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



6846
6847
6848
# File 'lib/syntax_tree.rb', line 6846

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



6837
6838
6839
# File 'lib/syntax_tree.rb', line 6837

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



6840
6841
6842
# File 'lib/syntax_tree.rb', line 6840

def statements
  @statements
end

Instance Method Details

#child_nodesObject



6865
6866
6867
# File 'lib/syntax_tree.rb', line 6865

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



6869
6870
6871
# File 'lib/syntax_tree.rb', line 6869

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

#pretty_print(q) ⇒ Object



6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
# File 'lib/syntax_tree.rb', line 6873

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



6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
# File 'lib/syntax_tree.rb', line 6892

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