Class: Yadriggy::Rescue

Inherits:
ASTnode show all
Includes:
AstHelper
Defined in:
lib/yadriggy/ast.rb

Overview

begin-rescue-ensure.

raise is parsed as a method call (or a command).

Instance Attribute Summary collapse

Attributes inherited from ASTnode

#parent, #usertype

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AstHelper

#has_tag?, #to_node, #to_nodes

Methods inherited from ASTnode

#add_child, #add_children, #const_value, #const_value_in_class, #get_context_class, #get_receiver_object, #is_proc?, #pretty_print, #root, #source_location, #source_location_string, #value, #value_in_class

Constructor Details

#initialize(rescue_expr, else_expr = nil, ensure_expr = nil) ⇒ Rescue

Returns a new instance of Rescue.



1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
# File 'lib/yadriggy/ast.rb', line 1465

def initialize(rescue_expr, else_expr=nil, ensure_expr=nil)
  if rescue_expr.nil?
    @types = []
    @parameter = nil
    @body = nil
    @nested_rescue = nil
  else
    expr = has_tag?(rescue_expr, :rescue)
    rescue_clause = expr[1]
    if rescue_clause.nil?
      @types = []
    elsif rescue_clause[0] == :mrhs_new_from_args
      types1 = to_nodes(rescue_clause[1])
      @types = types1 << to_node(rescue_clause[2])
    else
      @types = to_nodes(rescue_clause)
    end

    if expr[2].nil?
      @parameter = nil
    else
      @parameter = to_node(has_tag?(has_tag?(expr[2], :var_field)[1],
                                    :@ident))
    end

    @body = Exprs.make(expr[3])
    @nested_rescue = to_node(expr[4])
    add_children(@types)
    add_child(@parameter)
    add_child(@body)
    add_child(@nested_rescue)
  end

  if else_expr.nil?
    @else = nil
  else
    if !else_expr.nil? && else_expr[0].is_a?(Array)
      else_body = else_expr
    else
      # Ruby 2.4 or earlier
      elsexpr = has_tag?(else_expr, :else)
      else_body = else_expr[1]
    end
    @else = Exprs.make(else_body)
    add_child(@else)
  end

  if ensure_expr.nil?
    @ensure = nil
  else
    ensexpr = has_tag?(ensure_expr, :ensure)
    @ensure = Exprs.make(ensexpr[1])
    add_child(@ensure)
  end
end

Instance Attribute Details

#bodyASTnode|nil (readonly)

Returns the body of the rescue clause.

Returns:

  • (ASTnode|nil)

    the body of the rescue clause.



1446
1447
1448
# File 'lib/yadriggy/ast.rb', line 1446

def body
  @body
end

#elseASTnode|nil (readonly)

Returns the else clause.

Returns:

  • (ASTnode|nil)

    the else clause.



1451
1452
1453
# File 'lib/yadriggy/ast.rb', line 1451

def else
  @else
end

#ensureASTnode|nil (readonly)

Returns the ensure clause.

Returns:

  • (ASTnode|nil)

    the ensure clause.



1453
1454
1455
# File 'lib/yadriggy/ast.rb', line 1453

def ensure
  @ensure
end

#nested_rescueRescue|nil (readonly)

Returns the rest of rescue clauses. The returned object's #else and #ensure are nil.

Returns:

  • (Rescue|nil)

    the rest of rescue clauses. The returned object's #else and #ensure are nil.



1449
1450
1451
# File 'lib/yadriggy/ast.rb', line 1449

def nested_rescue
  @nested_rescue
end

#parameterASTnode|nil (readonly)

Returns the rescue parameter.

Returns:

  • (ASTnode|nil)

    the rescue parameter.



1444
1445
1446
# File 'lib/yadriggy/ast.rb', line 1444

def parameter
  @parameter
end

#typesArray<ASTnode> (readonly)

Returns the exception types. It may be empty.

Returns:

  • (Array<ASTnode>)

    the exception types. It may be empty.



1442
1443
1444
# File 'lib/yadriggy/ast.rb', line 1442

def types
  @types
end

Class Method Details

.make(rescue_expr, else_expr, ensure_expr) ⇒ Object



1457
1458
1459
1460
1461
1462
1463
# File 'lib/yadriggy/ast.rb', line 1457

def self.make(rescue_expr, else_expr, ensure_expr)
  if rescue_expr.nil? && else_expr.nil? && ensure_expr.nil?
    nil
  else
    Rescue.new(rescue_expr, else_expr, ensure_expr)
  end
end

.tagObject



1455
# File 'lib/yadriggy/ast.rb', line 1455

def self.tag() :rescue end

Instance Method Details

#accept(evaluator) ⇒ void

This method returns an undefined value.

A method for Visitor pattern.

Parameters:

  • evaluator (Eval)

    the visitor of Visitor pattern.



1524
1525
1526
# File 'lib/yadriggy/ast.rb', line 1524

def accept(evaluator)
  evaluator.rescue_end(self)
end

#has_else?Boolean

Returns true if the else clause is included.

Returns:

  • (Boolean)

    true if the else clause is included.



1534
1535
1536
# File 'lib/yadriggy/ast.rb', line 1534

def has_else?
  !@else.nil?
end

#has_ensure?Boolean

Returns true if the ensure clause is included.

Returns:

  • (Boolean)

    true if the ensure clause is included.



1539
1540
1541
# File 'lib/yadriggy/ast.rb', line 1539

def has_ensure?
  !@ensure.nil?
end

#has_rescue?Boolean

Returns true if the rescue clause is included.

Returns:

  • (Boolean)

    true if the rescue clause is included.



1529
1530
1531
# File 'lib/yadriggy/ast.rb', line 1529

def has_rescue?
  !@body.nil?
end