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.



6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
# File 'lib/syntax_tree.rb', line 6501

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



6499
6500
6501
# File 'lib/syntax_tree.rb', line 6499

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



6493
6494
6495
# File 'lib/syntax_tree.rb', line 6493

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



6496
6497
6498
# File 'lib/syntax_tree.rb', line 6496

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



6487
6488
6489
# File 'lib/syntax_tree.rb', line 6487

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



6490
6491
6492
# File 'lib/syntax_tree.rb', line 6490

def statements
  @statements
end

Instance Method Details

#child_nodesObject



6515
6516
6517
# File 'lib/syntax_tree.rb', line 6515

def child_nodes
  [predicate, statements, consequent]
end

#format(q) ⇒ Object



6519
6520
6521
# File 'lib/syntax_tree.rb', line 6519

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

#pretty_print(q) ⇒ Object



6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
# File 'lib/syntax_tree.rb', line 6523

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



6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
# File 'lib/syntax_tree.rb', line 6542

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