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



10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
# File 'lib/syntax_tree.rb', line 10178

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



10176
10177
10178
# File 'lib/syntax_tree.rb', line 10176

def comments
  @comments
end

#consequentObject (readonly)

nil | Rescue

the optional next clause in the chain



10170
10171
10172
# File 'lib/syntax_tree.rb', line 10170

def consequent
  @consequent
end

#exceptionObject (readonly)

RescueEx

the exceptions being rescued



10164
10165
10166
# File 'lib/syntax_tree.rb', line 10164

def exception
  @exception
end

#locationObject (readonly)

Location

the location of this node



10173
10174
10175
# File 'lib/syntax_tree.rb', line 10173

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to evaluate when an error is rescued



10167
10168
10169
# File 'lib/syntax_tree.rb', line 10167

def statements
  @statements
end

Instance Method Details

#bind_end(end_char) ⇒ Object



10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
# File 'lib/syntax_tree.rb', line 10192

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



10209
10210
10211
# File 'lib/syntax_tree.rb', line 10209

def child_nodes
  [exception, statements, consequent]
end

#format(q) ⇒ Object



10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
# File 'lib/syntax_tree.rb', line 10213

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



10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
# File 'lib/syntax_tree.rb', line 10237

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



10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
# File 'lib/syntax_tree.rb', line 10258

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