Class: SyntaxTree::Rescue

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

Overview

Rescue represents the use of the rescue keyword inside of a BodyStmt node.

begin
rescue
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception:, statements:, consequent:, location:, comments: []) ⇒ Rescue

Returns a new instance of Rescue.



9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
# File 'lib/syntax_tree.rb', line 9724

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9722
9723
9724
# File 'lib/syntax_tree.rb', line 9722

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



9716
9717
9718
# File 'lib/syntax_tree.rb', line 9716

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



9710
9711
9712
# File 'lib/syntax_tree.rb', line 9710

def exception
  @exception
end

#locationObject (readonly)

Location

the location of this node



9719
9720
9721
# File 'lib/syntax_tree.rb', line 9719

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



9713
9714
9715
# File 'lib/syntax_tree.rb', line 9713

def statements
  @statements
end

Instance Method Details

#bind_end(end_char) ⇒ Object



9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
# File 'lib/syntax_tree.rb', line 9738

def bind_end(end_char)
  @location =
    Location.new(
      start_line: location.start_line,
      start_char: location.start_char,
      end_line: location.end_line,
      end_char: end_char
    )

  if consequent
    consequent.bind_end(end_char)
    statements.bind_end(consequent.location.start_char)
  else
    statements.bind_end(end_char)
  end
end

#child_nodesObject



9755
9756
9757
# File 'lib/syntax_tree.rb', line 9755

def child_nodes
  [exception, statements, consequent]
end

#format(q) ⇒ Object



9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
# File 'lib/syntax_tree.rb', line 9759

def format(q)
  q.group do
    q.text("rescue")

    if exception
      q.nest("rescue ".length) { q.format(exception) }
    else
      q.text(" StandardError")
    end

    unless statements.empty?
      q.indent do
        q.breakable(force: true)
        q.format(statements)
      end
    end

    if consequent
      q.breakable(force: true)
      q.format(consequent)
    end
  end
end

#pretty_print(q) ⇒ Object



9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
# File 'lib/syntax_tree.rb', line 9783

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

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

    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



9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
# File 'lib/syntax_tree.rb', line 9804

def to_json(*opts)
  {
    type: :rescue,
    extn: exception,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end